Compiling and using shaders

Started by
22 comments, last by InfoGeek 9 years, 7 months ago

hi all.

i do not understand compiling and using shaders good enough. which i tried to ask here . but i guess it is a question for a new thread.

I tried to find a a tutorial to explain how switching of shaders and their compiling works, like what gets replaces when switching a vertex shader, what happens to global variables declared in one shader file when you switch to another and just how it all comes together.

i did no find such a tutorial. i tried the official msdn site Programming Guide for HLSL and Compiling Shaders or get general overviews from googling Introduction to DirectX Shader. nothing explains that.

can anyone please point to a resource or explain?

Advertisement

I am not sure what you mean? If just talking about the compiling and loading of a shader.

Pre-compiled shaders:

  1. Creating shaders in VS2013 by adding a new item, Visual C++ -> HLSL then picking the type of shader I want.
  2. These will compile to .cso files
  3. Loading the .cso file into memory
  4. Using ID3D11Device::CreateVertexShader etc. to turn that loaded cso file into one of the shader objects such as ID3D11VertexShader.

Runtime-compiled shaders:

  1. Get/load your shader code
  2. Use D3DCompile or D3DCompile2 to compile the shader to byte code (the same as the .cso file)
  3. Use ID3D11Device::CreateVertexShader etc. to create your shader object.

By global variables I guess you means constants (ID3D11Context::VSSetConstantBuffers etc.)? In which case the constants for each type of shader exist separately from whatever the current shader is. If you change shaders and have different constants, you need to set them again.

If you are using the effects system, use D3DX11CreateEffectFromMemory instead of the CreateVertexShader etc. I understand that ID3D11XEffectPass will call all the individual ID3D11Context:: set methods automatically, including putting the effects variables back as the GPU's active constants/variables/globals.

When a shader is compiled, it references texture slots, not textures. Same thing applies for constant buffers and samplers. The shader is compiled with code that references a slot. You then attach textures, buffers, and samplers to these slots. So changing a shader does nothing to change a texture.

Even though this is specifically a DirectX question I feel like I should mention OpenGL. It works a little differently. In OpenGL, any uniforms are bound to the actual program and change when you change a program but texture slots and constant buffers (aka uniform block) work the same.

My current game project Platform RPG

what happens if you have 2 shaders for normal rendering and 2 shaders for font rendering? like... what happens to global declarations which as HappyCoder said refer to slots which you bound actual resources to later?

right now i have 1 file with two shader(following a tutorial):


cbuffer CBPerObject
{
	float4x4 WVP;
};


Texture2D objTexture;
SamplerState objSamplerState;

struct VS_OUTPUT
{
	float4 position : SV_POSITION;
	float2 texturePosition : TEXPOS;
};

// Vertex Shader.
VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexPos : TEXPOS)
{
	VS_OUTPUT output;
	output.position = mul(inPos, WVP);
	output.texturePosition = inTexPos;
	return output;
}


// Pixel Shader.
float4 PS(VS_OUTPUT input) : SV_TARGET
{
	float4 textureColor = objTexture.Sample(objSamplerState, input.texturePosition);
	return textureColor;
}

if i split it into 3 files, 1 normal PS, 1 normal VS and 1 font PS. i would only change the PSes, where would i declare this:


Texture2D objTexture;
SamplerState objSamplerState;

struct VS_OUTPUT
{
	float4 position : SV_POSITION;
	float2 texturePosition : TEXPOS;
};

? and at which point do they get compiled and how... do they get compiled everytime i compile a shader... i don't even know?

You can place code used by multiple shaders into header files and then include them in each shader that uses it. So for example, you could place the VS_OUTPUT struct into some header file like "ShaderCommon.hlsli" and then add #include "ShaderCommon.hlsli" to each file that needs access to it. It will then be compiled into each shader.


You can place code used by multiple shaders into header files and then include them in each shader that uses it. So for example, you could place the VS_OUTPUT struct into some header file like "ShaderCommon.hlsli" and then add #include "ShaderCommon.hlsli" to each file that needs access to it. It will then be compiled into each shader.

is that how everything is done by standard/industry?

Edit: wait a minute... do shader files allow pre-processor directives? i'm compiling using D3DCompileFromFile.

Yes it's pretty common to use includes to share code between the shaders. You can also use it for any shared functions.

Pre-processor directives are allowed. See http://msdn.microsoft.com/en-us/library/windows/desktop/bb943993(v=vs.85).aspx

Defines can be set with the pDefines parameter of D3DCompileFromFile. See http://msdn.microsoft.com/en-us/library/windows/desktop/hh968107(v=vs.85).aspx for an example.

thanks guys. i think have to go practical now, to continue learning and understand this stuff.

wait... ultimately it's the same as simply copy pasting the shared code/declarations. so you should treat each shader as a separate program with copies of all declarations?

Yeah it will end up being compiled to the same code so it could just be copied instead of included, but then if you need to make a change to the copied code, you'll need to change every place that uses it. It kind of depends on the extent of the shared code and the likelihood of it to change.

This topic is closed to new replies.

Advertisement