Search code examples
c++parametersshaderdirect3d9

passing direct3d shader parameters dynamically


I want to make my brightness and contrast shader change by dynamically changing offset parameters, but I dont want to recompile my shader everytime I am changing shader buffer.

The current shader code is

char shaderCode[] = " \
sampler2D screen: register(s0); \
\
float offsetBrightness = 0.0f; \
float offsetContrast   = 0.9f; \
float4 PSBrightnessContrast(float2 inCoord : TEXCOORD0) : COLOR0\
{\
    return (tex2D(screen, inCoord.xy) + offsetBrightness) * (1.0 + offsetContrast);\
}\
\
technique brightness \
{ \
    pass p1 \
    { \
        PixelShader = compile ps_2_0 PSBrightnessContrast(); \
    } \
} \
";

How I can pass the offsetBrightness and offsetContrast dynamically?


Solution

  • In HLSL, all variables declared outside function can be changed between each draw call.

    To change their value you can use the following setter from ID3DXEffect :

    HRESULT SetFloat( [in] D3DXHANDLE hParameter, [in] FLOAT f );

    where hParameter can be either the name as char * or the handle queried using GetParameterHandle()

    So in your example :

    effet->SetFloat( "offsetBrightness", 0.5f );

    If you are inside a Begin()/End(), you need to call CommitChanges()to apply those values