XDGE-Render-HLSL组织结构

@author:白袍小道

image-20201010005110602

前言

1、由于重新梳理了下Render部分,也方便总结,所以有了这一篇博客。

2、不涉及到关键部分的会大致说明过程,也方便检查编码结构的合理性

3、XDRender仅限Studio

正文

一、结构说明

1、四层结构

目录 说明
API OpenGL,ES,DX,Vu等细分底层Handle的API
Core 公用库
CoreExtend 利用公用库,集合和嵌套的着色模式
Simple 基础案例(内置着色模型)
CustomSimple 自定义(这里后期会分开为CustomNode,和ShaderGraph)

2、部分文件说明

XDArt_Common.hlsl

最基础的公用文件(对API的嵌套),坐标系,精度,纹理(XDArt_Texture.hlsl会再分化一层)操作,

image-20201010010344795

#if defined(SHADER_API_XBOXONE)
#include "../API/XDArt_XBoxOne.hlsl"
#elif defined(SHADER_API_PSSL)
#include "../API/XDArt_PSSL.hlsl"
#elif defined(SHADER_API_D3D11)
#include "../API/XDArt_D3D11.hlsl"
#elif defined(SHADER_API_METAL)
#include "../API/XDArt_Metal.hlsl"
#elif defined(SHADER_API_VULKAN)
#include "../API/XDArt_Vulkan.hlsl"
#elif defined(SHADER_API_SWITCH)
#include "../API/XDArt_Switch.hlsl"
#elif defined(SHADER_API_GLCORE)
#include "../API/XDArt_GLCore.hlsl"
#elif defined(SHADER_API_GLES3)
#include "../API/XDArt_GLES3.hlsl"
#elif defined(SHADER_API_GLES)
#include "../API/XDArt_GLES2.hlsl"
#else
#error unsupported shader api
#endif

这里可以参考
Unreal:Platform.usf

Unity的SRPCore--Common.hlsl

uint GetMipCount(Texture2D tex)
{
#if defined(SHADER_API_D3D11) || defined(SHADER_API_D3D12) || defined(SHADER_API_D3D11_9X) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PSSL)
    #define MIP_COUNT_SUPPORTED 1
#endif
#if (defined(SHADER_API_OPENGL) || defined(SHADER_API_VULKAN)) && !defined(SHADER_STAGE_COMPUTE)
    // OpenGL only supports textureSize for width, height, depth
    // textureQueryLevels (GL_ARB_texture_query_levels) needs OpenGL 4.3 or above and doesn't compile in compute shaders
    // tex.GetDimensions converted to textureQueryLevels
    #define MIP_COUNT_SUPPORTED 1
#endif
    // Metal doesn't support high enough OpenGL version

#if defined(MIP_COUNT_SUPPORTED)
    uint mipLevel, width, height, mipCount;
    mipLevel = width = height = mipCount = 0;
    tex.GetDimensions(mipLevel, width, height, mipCount);
    return mipCount;
#else
    return 0;
#endif
}
XDArt_Macros.hlsl

常用宏定义:TEMPLATE_SWAP,REVERSED_Z等等

XDArt_Instancing.hlsl

实例化(共享)

XDArt_Packing.hlsl

编码: 如深度编码,R8G8B8编码到Float2等等

XDArt_SpaceTransforms.hlsl

空间变化

XDArt_RenderCoreInput.hlsl和XDArt_RenderCoreLib.hlsl

一些Uniform,和对Uniform的处理帮助

XDArt_RenderLightInput.hlsl和XDArt_RenderLightInputHelp.hlsl

光照数据相关,如灯光数据,材质属性,着色使用的结构(避免传递参数过多)

XDArt_RenderLightFunction.hlsl

光照模型涉及到函数,当积累过多后就会分化。由于一个着色算法经常会拆分为多个单项式,单项式所用的子着色函数会根据需要进行组合,所以也尽量单一放。

CoreExtends

部分Pass

按LightMode或者Tag中关键字来被包含使用

XDArt_RenderPass_DepthOnly.hlsl

用于early-z,深度获取等等

XDArt_RenderPass_Shadow.hlsl

包含正常ShaderMap, CSM, PanelShadow,等等。

XDArt_RenderLight_Forward.hlsl
XDArt_RenderLight_Deffer.hlsl
XDArt_ShaderMode_DefaultLit.hlsl
内置的Shader
XDArt_ShaderMode_DefaultLit.shader
Shader中自定义Surface的输入部分

这里作为一个使用案例, 并且兼容到Surface的输入部分可以自定义

处理的方式, 主要是利用包含的顺序关系和分离, 比如我先包含了XDArt_ShaderMode_DefaultLitInput.hlsl,这样能够方便我们定义SurfaceDataConfig(DefaultLit_Varyings input, inout SurfaceData outSurfaceData)

Pass
        {
            Name "Unlit"
            Tags {"LightMode" = "DefaultLit"}
            HLSLPROGRAM
            #include "Packages/com.xdgameengine.unity/EngineResource/ShaderLibrary//CoreExtends/XDArt_ShaderMode_DefaultLitInput.hlsl"
            
            
            TEXTURE2D(_DiffcuseMap);            
            SAMPLER(sampler_DiffcuseMap);
                        
            TEXTURE2D(_NormalMap);            
            SAMPLER(sampler_NormalMap);

            TEXTURE2D(_MetRouAOMap);            
            SAMPLER(sampler_MetRouAOMap);
            void SurfaceDataConfig(DefaultLit_Varyings input, inout SurfaceData outSurfaceData)
            {
            #ifdef  _DIFFCUSE_MAP
                half4 albedoAlpha = SAMPLE_TEXTURE2D(_DiffcuseMap,sampler_DiffcuseMap, input.baseUV);
            #else
                half4 albedoAlpha = _BaseColor;
            #endif
                outSurfaceData.alpha = albedoAlpha.a;
                outSurfaceData.albedo = albedoAlpha.rgb;
                
            #ifdef _NORMALMAP
                float3 normalTS = SampleNormal(input.baseUV, TEXTURE2D_ARGS(_NormalMap, sampler_NormalMap), 1.0);
                #if _REVERCE_NORMAL
                    normalTS.xy = -1 * normalTS.xy;
                #endif
                outSurfaceData.normalWS =  TransformTangentToWorld(normalTS,half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
            #else
                outSurfaceData.normalWS = input.normalWS;
            #endif

                outSurfaceData.specular = _SpecColor;
                outSurfaceData.metallic = _Metallic;
                outSurfaceData.smoothness = _Smoothness;
                outSurfaceData.occlusion = _OcclusionStrength;

            #ifdef _SAMPLE_MRAO_MAP
                float4 mrao = SAMPLE_TEXTURE2D(_MetRouAOMap,sampler_MetRouAOMap, input.baseUV);
                #if _SAMPLE_MET
                outSurfaceData.metallic = mrao.r;
                #endif
                #if _SAMPLE_ROUGH
                outSurfaceData.smoothness = 1 - mrao.g;
                #endif
                #if _SAMPLE_AO
                outSurfaceData.occlusion = mrao.b;
                #endif	
            #endif
                outSurfaceData.emission = _EmissionColor;
                outSurfaceData.clearCoat = _ClearCoat;
                outSurfaceData.clearCoatGloss = _ClearCoatGloss;
                outSurfaceData.surfaceColor = _SurfaceColor;
                outSurfaceData.customData  = _CustomData;
            }

            #pragma shader_feature_local _DIFFCUSE_MAP
            #pragma shader_feature_local _NORMALMAP
            #pragma shader_feature_local _REVERCE_NORMAL
            #pragma shader_feature_local _RECEIVE_SHADOWS_ON
            #pragma shader_feature_local _APPLY_DEPTHFOG_ON

            #pragma shader_feature_local _SAMPLE_MRAO_MAP
            #pragma shader_feature_local _SAMPLE_MET
            #pragma shader_feature_local _SAMPLE_ROUGH
            #pragma shader_feature_local _SAMPLE_AO
            #pragma shader_feature_local  _SHADERMODE_DEFAULTLIT
            #pragma vertex DefaultLit_Vertex
            #pragma fragment DefaultLit_Fragment
            //自定义的输入过程
            #define CustomSurface SurfaceDataConfig 
            #include "Packages/com.xdgameengine.unity/EngineResource/ShaderLibrary//CoreExtends/XDArt_ShaderMode_DefaultLit.hlsl"
            ENDHLSL
        }

后期使用:

颜色矫正:XDArt_ShaderMode_Post_ColorGrauding.hlsl

颜色映射等。

传统泛光,FAstBloom,等等 :XDArt_ShaderMode_Post_Bloom.hlsl

变形:DOF,鱼眼等等

未完待续

部分CustomShader,

自定义后期。

二、依赖关系

按层处理

备注


posted @ 2020-10-10 01:53  白袍小道  阅读(271)  评论(1编辑  收藏  举报

白袍小道 DaoZhang_XDZ@163.com - 创建于 8012

窥探道理