Hi i am currently following the '3d game programming with directx11'. and i have found out that this old book(from about 2011) is using effect file for hlsl code. and for my project, i am not using any effect file at all, just making hlsl file and code in that. so i have a number of hlsl file as much as i need.
so this question comes to my mind that is using effect file for shader coding useful??? If then, what is that reason???
This is sample codes for comparing between effect file and hlsl file (just example)
this is effect file that contains VS,PS all in one
cbuffer cbPerObject
{
float4x4 gWorldViewProj;
};
struct VertexIn
{
float3 Pos : POSITION;
float4 Color : COLOR;
};
struct VertexOut
{
float4 PosH : SV_POSITION;
float4 Color : COLOR;
};
VertexOut VS(VertexIn vin)
{
VertexOut vout;
vout.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);
vout.Color = vin.Color; return vout;
}
float4 PS(VertexOut pin) : SV_Target
{
return pin.Color;
}
technique11 ColorTech
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetPixelShader( CompileShader( ps_5_0, PS() ) );
}
}
this is my 2 hlsl project for VS,PS relatively
struct VS_INPUT
{
float3 pos : POSITION;
};
struct VS_OUTPUT
{
float4 pos : SV_POSITION;
};
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;
output.pos = float4(input.pos, 1);
return output;
}
struct PS_INPUT
{
float4 pos : SV_POSITION;
};
float4 main(PS_INPUT input) : SV_Target
{
return float4(1,1,1,1);
}
Here's a copy of an answer to a version of this question referenced in this thread.
Q: ...is it best to separate each shader (i.e. Vertex, Geometry, Tessellation, Pixel) in each file? I can’t seem to find a best practice around this. I see some examples that have all shaders in one
.hlsl
or.fx
file and some in each vs.hlsl, ps.hlsl or vs.fx, px.fx files. Thanks!A: Historically the extension '.fx' refers to a HLSL shader intended to be built using the legacy effects fx_4_0, fx_5_0, etc. profiles; while '.hlsl' refers to individual HLSL shaders. The reality is that there's no consistency here. You will see lots of '.fx' files without any technique/pass statements.
Unlike C/C++ source files, HLSL files can contain a lot of stuff that gets ignored in a given invocation of the compiler. If you have a big .fx file with a dozen shaders in it and a bunch of technique/passes, you can compile the same file many times with different entry-points and profiles to generate lots of shaders.
It's really all a matter of how you want to build your shader content. For example, DirectX Tool Kit uses a command-line script to build all the possible combinations of shaders: currrently it builds 161 shader blobs from 12
.fx
files--and actually it can build them twice: once with the Windows compiler and once with the Xbox One XDK compiler, but only one set of these shader blobs is included in a given build of the DirectXTK.lib anyhow. DirectXTK doesn't use legacy Effects 11 at all.If you use the Visual Studio 2012-2019 content pipeline to build your shaders, you do have to deal with a limitation of MSBuild which assumes each source file is built once for each result. So in that case, you'd want an individual
.hlsl
file for each shader combination (which for the DirectX Tool Kit example would mean 161.hlsl
files). One way to handle this would be to keep the shader source itself in a single file, and then have a bunch of .hlsl files that #include the 'source' file. Something like:
// BasicEffectVSBasicVertexLighting.hlsl
//
#include "BasicEffect.fx"
// BasicEffectPSBasicVertexLighting.hlsl
//
#include "BasicEffect.fx"
You then compile each of these .hlsl files once with the proper shader profile and entry-point. This approach works fine for smaller projects, but doesn't scale well to large complex shaders without using legacy fx profiles. In that case, other build solutions like DirectX Tool Kit's build solution is probably better.
If you are using legacy fx profiles, you of course only have to compile each .fx
once and it will generate all the shaders it specifies in technique/passes in one go. Keep in mind for Effects 11:
fx_5_0
profile support in the HLSL compiler is deprecated, and does not fully support DirectX 11.1 HLSL features such as minimum precision types. It is supported in the Windows 8.x/Windows 10 SDK version of the HLSL compiler (FXC.EXE
) and D3DCompile API (#46). It generates a deprecation warning with D3DCompile API (#47). It is not supported by the Shader Model 6 (DXC.EXE
) or later DXIL compiler.