解释HLSL着色器编译器宏代码

时间:2017-04-06 09:18:27

标签: c++ directx-11 hlsl

资料:
WiN7 SP1教授

的DirectX11

开发IDE:VS 2015

程序语言:C#

应用类型:3D渲染

Directx11 Wrapper:SLimDX

在我的C#项目中,我有一个正在运行的3D渲染器。

我想整合第三方HLSL着色器代码。

我已经翻译了Directx11功能级别的代码,它使用fxc.exe进行编译,没有错误或警告。

着色器由我的C#应用​​程序加载,我可以访问所有着色器变量并设置它们的值。

问题:

当我尝试在C#代码中获取引用以访问Shader的第一个技术的第一遍时,我得到一个SlimDX运行时错误。

我可以引用着色器的第一种技术,没有运行时错误,但获得的对象有一个标志is_valid = false,表明该技术没有被正确引用。

澄清:我不打算进行平局调用,但在准备平局调用时,我必须得到相关传递的参考。

问题:

我无法理解关于这些技术的最后一个编译器指令是什么意思。

有人可以翻译哪个HLSL代码最终会从编译器指令生成吗?

我的意思是从:

开始

#define OBJECT_TEC(name,mmdpass)\

#include "../ray.conf"
#include "../ray_advanced.conf"
#include "../shader/math.fxsub"
#include "../shader/common.fxsub"
#include "../shader/gbuffer.fxsub"
#include "../shader/lighting.fxsub"

float3  LightDirection  : DIRECTION < string Object = "Light"; >;
float3  LightSpecular   : SPECULAR  < string Object = "Light"; >;

bool ExistRay : CONTROLOBJECT<string name = "ray.x";>;

texture DiffuseMap: MATERIALTEXTURE;
sampler DiffuseMapSamp = sampler_state
{
    texture = <DiffuseMap>;
    MINFILTER = ANISOTROPIC;
    MAGFILTER = ANISOTROPIC;
    MIPFILTER = POINT;
    MAXANISOTROPY = 16;
    ADDRESSU  = WRAP;
    ADDRESSV  = WRAP;
};

float4 GetTextureColor(float4 albedo, float2 uv)
{
    if (use_texture)
    {
        float4 TexColor = tex2D(DiffuseMapSamp, uv);
        TexColor.rgb = lerp(1, TexColor * TextureMulValue + TextureAddValue, TextureMulValue.a + TextureAddValue.a).rgb;
        albedo *= TexColor;
    }

    return srgb2linear(albedo);
}

void DrawObjectVS(
    in float4 Position : POSITION,
    in float3 Normal   : NORMAL,
    in float4 Texcoord : TEXCOORD0,
    out float4 oTexcoord  : TEXCOORD0,
    out float3 oNormal    : TEXCOORD1,
    out float3 oViewdir   : TEXCOORD2,
    out float4 oPosition  : SV_Position)
{
    oNormal = Normal;
    oTexcoord = Texcoord;
    oViewdir = CameraPosition - Position.xyz;
    oPosition = mul(Position, matWorldViewProject);
}

float4 DrawObjectPS(float4 texcoord : TEXCOORD0, float3 normal : TEXCOORD1, float3 viewdir : TEXCOORD2) : SV_Target
{
#if EXIST_RAY
    #if DISCARD_ALPHA_ENABLE
        float alpha = MaterialDiffuse.a;
    #if DISCARD_ALPHA_MAP_ENABLE
        if (use_texture) alpha *= tex2D(DiffuseMapSamp, texcoord.xy).a;
    #endif
        clip(alpha - DiscardAlphaThreshold);
    #endif
        return 0;
#else
    if (ExistRay)
    {
#if DISCARD_ALPHA_ENABLE
        float alpha = MaterialDiffuse.a;
#if DISCARD_ALPHA_MAP_ENABLE
        if (use_texture) alpha *= tex2D(DiffuseMapSamp, texcoord.xy).a;
#endif
        clip(alpha - DiscardAlphaThreshold);
#endif
        return 0;
    }
    else
    {
        float4 albedo = GetTextureColor(MaterialDiffuse, texcoord.xy);

        float3 L = normalize(-LightDirection);
        float3 V = normalize(viewdir);
        float3 N = normalize(normal);

        float MaterialRoughness = SmoothnessToRoughness(ShininessToSmoothness(MaterialPower));

        float4 lighting = albedo;
        lighting.rgb *= DiffuseBRDF(N, L, V, MaterialRoughness);
        lighting.rgb += SpecularBRDF_GGX(N, L, V, MaterialRoughness, 0.04, 1.0);
        lighting.rgb *= LightSpecular;

        return linear2srgb(lighting);
    }
#endif
}

#define OBJECT_TEC(name, mmdpass) \
    technique name < string MMDPass = mmdpass;\
    > { \
        pass DrawObject { \
            AlphaTestEnable = FALSE; AlphaBlendEnable = FALSE; \
            VertexShader = compile vs_3_0 DrawObjectVS(); \
            PixelShader  = compile ps_3_0 DrawObjectPS(); \
        } \
    }

OBJECT_TEC(MainTec0, "object")
OBJECT_TEC(MainTecBS0, "object_ss")

technique EdgeTec < string MMDPass = "edge"; > {}
technique ShadowTech < string MMDPass = "shadow";  > {}
technique ZplotTec < string MMDPass = "zplot"; > {}

0 个答案:

没有答案