How to define a functionally-like macro with D3DCompile () from C ++?

Is it possible to define a functionally similar macro with a parameter const D3D_SHADER_MACRO *pDefines

D3DCompile()

?

Sadly MSDN is not very specific, but implies that they can be specified using the / d fxc.exe option.


I tried with this:

const D3D_SHADER_MACRO dx11Defines[] = {
    {"SIMPLE_MACRO", "42"},
    {"FUNCTION_MACRO(x)", "DoSometing((x).somefield)"},
    {nullptr, nullptr},
};

      

And usage in HLSL:

float test1 = SIMPLE_MACRO;
float test2 = FUNCTION_MACRO(data);

      

But the compilation failed:

Test.fx(XX,XX-XX): error X3004: undeclared identifier 'FUNCTION_MACRO'

      

Conclusion D3DPreprocess()

:

float test1 = 42 ;
float test2 = FUNCTION_MACRO ( data ) ;

      


[edit]

I tested the following command line with the same HLSL code:

fxc dtest.fx /Ptest_p.fx /D"SIMPLE_MACRO=42" /D"FUNCTION_MACRO(data)=24"

What's the conclusion:

float test1 = 42 ;
float test2 = FUNCTION_MACRO ( data ) ;

      


[edit]

I finally solved the problem using the ID3DInclude interface as suggested by @catflier. I am generating functional macros in a temporary buffer when requesting a specific header file name.

I came across a nice article by Adam Savitsky that helped me a lot.

+3


source to share


1 answer


Defined by special characters only if they are defined within hlsl.

so: FUNCTION_MACRO (x) will not be replaced by the preprocessor.

Alternatively, you can use Include handlers, so at the beginning of your file you add:

#include <myfunction.fxh>

      

then in the handler you enter (simple example):



#define FUNCTION_MACRO(data) sin(data)

      

to create a handler you can look at the link

It's pretty simple, you implement a public method, check if the include name is the same as yours, and paste the definition from there.

In the d3dcompile method, you send an instance of your include to call the open method.

If you want to keep it simple, you can also just add the definition manually at the beginning of the code before calling compilation, I just think inclusion will be more flexible in many cases.

+1


source







All Articles