Hi!
I have created a github pull request for a macro that implements a shader inline. You can find it here: Implementing a new macro to implement a shader inline. by KeithRare · Pull Request #13198 · EpicGames/UnrealEngine
Like in the PR, here is an example of its use
We currently create shader classes like this:
`class FTestShader
: public FGlobalShader
{
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
SHADER_PARAMETER_RDG_BUFFER_UAV(RWBuffer, OutBuffer)
END_SHADER_PARAMETER_STRUCT()
DECLARE_GLOBAL_SHADER(FTestShader)
SHADER_USE_PARAMETER_STRUCT(FTestShader, FGlobalShader)
};
IMPLEMENT_GLOBAL_SHADER(FTestShader, “/Plugin/MyTestPlugin/TestShader.usf”, “Main”, SF_Compute);`The main points here are that we have to use the DECLARE_GLOBAL_SHADER macro inside the class definition and then we must use the IMPLEMENT_GLOBAL_SHADER macro in a cpp file at namespace scope. This is kinda clunky.
My PR proposes a new macro:
`class FTestShader
: public FGlobalShader
{
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
SHADER_PARAMETER_RDG_BUFFER_UAV(RWBuffer, OutBuffer)
END_SHADER_PARAMETER_STRUCT()
IMPLEMENT_GLOBAL_SHADER_INLINE(
FTestShader,
TEXT(“/Plugin/MyTestPlugin/TestShader.usf”),
TEXT(“Main”),
SF_Compute)
SHADER_USE_PARAMETER_STRUCT(FTestShader, FGlobalShader)
};`IMPLEMENT_GLOBAL_SHADER_INLINE will do all of what the standard 2 macro approach does but just with a single macro.
I believe that this is a nicer way to implement the boilerplate of shaders for two reasons:
- It is 1 macro instead of 2. The fewer macros the better. It is easier to use because of this.
- Because the implementation is inline, exporting from modules is not a concern.
Of course this would not replace the old 2 macro approach in the case of wanting to explicitly control whether a shader is exported from a module or not.
I am not precious about the name or, indeed the implementation, but I do prefer this macro over using the standard 2 macro approach and I think it would be useful if it was accepted in Unreal Engine
Cheers,
Keith