简体   繁体   中英

HLSL splitting shader into multiple files

It might be stupid question, but I was looking for a long time for an answer and I can't find one for my DirectX 11 game engine. For example I have got two pixel shaders A and B and I don't want to repeat my code for gamma correction. So my idea was to move that to separate hlsl file and include them, but I don't know how to do that. When I was google that I was only able to find informations about .fx workflow, but I'm not using it. When I'm trying to make a new shader I always get an error, that my shaders need to have main function. How can I do this?

EDIT: As VTT suggested I will provide example. Let's say I have my uber_pixel_shader.hlsl like this:

#include "gamma_utils_shader.hlsl"

...

float4 main(PS_INPUT input) : SV_TARGET
{
    ...
    finalColor = gammaCorrect(finalColor);

    return float4(finalColor, Alpha);
}

And there is no method gammaCorrect in HLSL, so I want to include it from another file named gamma_utils_shader.hlsl . This file looks like this:

float3 gammaCorrect(float3 inputColor)
{
    return pow(inputColor, 1.0f / 2.2f);
}

When I'm trying to compile this, the comipler is throwing an error "Error 3501 'main': entrypoint not found" . And it's true, becase I don't have main method in this file, but I do not need one. How can I solve this in Visual Studio 2017?

Your project settings by default specify that an HLSL file should be compiled with the HLSL compiler. This means that during the build, VS queues all your HLSL files, including your include file, for compilation by the compiler with the default entrypoint of main . Obviously this is not desired - an include file can't be truly compiled.

To solve the issue, right click on your HLSL include file in the 'Solution Explorer', click 'Properties', and change the 'Item Type' field from "HLSL Compiler" to "Does not participate in build". This will prevent Visual Studio from compiling your include file.

In the future, provide the '.hlsli' extension to your HLSL include files. Visual Studio will open those files with the HLSL editor, but automatically identify them as not participating in the build procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM