Jump to content

  • Log In with Google      Sign In   
  • Create Account


Compiling and using shaders

  • You cannot reply to this topic
23 replies to this topic

#1 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 01 September 2014 - 03:58 PM

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?



Sponsor:

#2 SyncViews   Members   -  Reputation: 393

Like
3Likes
Like

Posted 01 September 2014 - 04:30 PM

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.


Edited by SyncViews, 01 September 2014 - 04:37 PM.


#3 HappyCoder   Members   -  Reputation: 2567

Like
0Likes
Like

Posted 01 September 2014 - 07:21 PM

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.



#4 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 02 September 2014 - 05:33 PM

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?



#5 megadan   GDNet+   -  Reputation: 500

Like
0Likes
Like

Posted 02 September 2014 - 07:08 PM

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.



#6 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 03 September 2014 - 05:01 AM


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.


Edited by InfoGeek, 03 September 2014 - 05:13 AM.


#7 megadan   GDNet+   -  Reputation: 500

Like
2Likes
Like

Posted 03 September 2014 - 10:04 AM

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.


Edited by megadan, 03 September 2014 - 10:06 AM.


#8 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 03 September 2014 - 10:10 AM

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


Edited by InfoGeek, 03 September 2014 - 10:10 AM.


#9 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 05 September 2014 - 10:41 AM

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?



#10 megadan   GDNet+   -  Reputation: 500

Like
2Likes
Like

Posted 05 September 2014 - 11:13 AM

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.



#11 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 05 September 2014 - 12:09 PM


Yeah it will end up being compiled to the same code so it could just be copied instead of included

 

no mechanism to have shared declaractions across shaders?



#12 megadan   GDNet+   -  Reputation: 500

Like
1Likes
Like

Posted 05 September 2014 - 05:36 PM

What did you have in mind? If you were to declare a cbuffer or something in a header and then include it in multiple shaders, while the shader compiler will ultimately copy the declaration to both shaders, the actual resource can be shared from C++ like this:
 

context->PSSetConstantBuffers( 0, 1, &buffer );
context->PSSetShader( shader1, NULL, 0 );
... do some drawing ...
context->PSSetShader( shader2, NULL, 0 );
... do some more drawing ...

Here the buffer is bound to slot 0, which will apply to any activated pixel shader until PSSetConstantBuffers is called again. The result would be the same if you called PSSetConstantBuffer again with the same buffer before using shader2, but it is better to try to reduce API calls by preventing redundant state changes.  Setting up a system to handle checking for redundancies and only calling API functions when necessary is ideal.


Edited by megadan, 05 September 2014 - 05:37 PM.


#13 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 06 September 2014 - 02:55 AM

thank you megadan :) i guess common declarations should be ignored then because as you say the actual resources are not duplicated.



#14 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 06 September 2014 - 04:57 AM

sorry for dragging this thread for a bit.

 

 

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.

 

 

there was no preprocessor/include related code in example .hlsl files, so if i go the runtime compiling of shader way i would have to pass a: 

D3D_SHADER_MACRO Shader_Macros[1] = { "FONT_COMMON", "1"  };

having something like:

 

font.hlsli

#if DEFINED(FONT_COMMON)

struct commonStruct
{
   int whatever;
   float whatever2;
}

#endif

to every compiling call of a shader that includes that common include like so:

 

font_ps.hlsl

#include "font.hlsli"

float4 PS(VS_OUTPUT input) : SV_TARGET
{
   // do pixel shader stuff...
}

Call (Second parameter Shader_Macros): 

D3DCompileFromFile(L"font_ps.hlsl", Shader_Macros, 0, "PS", "ps_5_0", 0, 0, &PS_Buffer, 0);

right?

 

because 

#ifndef COMMON_H
#define COMMON_H

struct commonStruct
{
   //... whatever ....
}

#endif

would not make sense because it's run-time right?

 

 

If so seems a bit messy.



#15 Buckeye   Crossbones+   -  Reputation: 4441

Like
0Likes
Like

Posted 06 September 2014 - 06:56 AM


because

#ifndef COMMON_H
#define COMMON_H

struct commonStruct
{
//... whatever ....
}

#endif

would not make sense because it's run-time right?

 

Two sides of the same coin. If the above code appears in a file (either in the file or #include'd), you could define COMMON_H to exclude the code blink.png , which is counter-intuitive. (Not sure what you mean by "run-time.") FYI, though that's often used in header files to prevent (well.. attempt to avoid) multiple defines, it's still just a ifndef/endif construct.


Edited by Buckeye, 06 September 2014 - 07:03 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#16 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 06 September 2014 - 08:49 AM


Two sides of the same coin. If the above code appears in a file (either in the file or #include'd), you could define COMMON_H to exclude the code , which is counter-intuitive. (Not sure what you mean by "run-time.") FYI, though that's often used in header files to prevent (well.. attempt to avoid) multiple defines, it's still just a ifndef/endif construct.

 

i have no idea what you tried to say here. what's the point of "#if DEFINED(FONT_COMMON)   //....    #endif" then?

 

run-time as in program run-time compilation of shaders instead of program compile time.


Edited by InfoGeek, 06 September 2014 - 08:50 AM.


#17 Buckeye   Crossbones+   -  Reputation: 4441

Like
0Likes
Like

Posted 06 September 2014 - 09:41 AM


i have no idea what you tried to say here. what's the point of "#if DEFINED(FONT_COMMON) //.... #endif" then?

 

Just observing that #ifndef COMMON_H is the same in kind as an #ifdef FONT_COMMON. Interpretation takes place at shader compile time, whenever that occurs.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#18 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 06 September 2014 - 09:44 AM


Just observing that #ifndef COMMON_H is the same in kind as an #ifdef FONT_COMMON.

 

oh, okay, thanks for the input.

 

Still wondering on the answers to my previous post.



#19 megadan   GDNet+   -  Reputation: 500

Like
1Likes
Like

Posted 06 September 2014 - 11:15 AM

so if i go the runtime compiling of shader way i would have to pass a ... to every compiling call of a shader that includes that common include like so:


Generally, this is correct.  Each shader will have its own set of defines.  The same is true for C++.  The compiler passes some set of defines to each individual file as it gets compiled.  

 

In C++,#ifndef/#define is used to prevent a cpp file from including two headers that both include the same header which would lead to something being defined multiple times in the cpp file.  The same is true for shaders.  So if your shader header is being included in other headers that are then being included in your shader, it's a good idea to put an #ifndef/#define to prevent multiple declarations in the same file.  If you are just including the header file directly in your shader, then it's not totally necessary, although it might still be good practice.

 

As to which method you described you should use, it depends on what you want to accomplish.  If you want to prevent things from being multiply defined, use #ifndef/#define.  If you want to conditionally use one struct or another struct, or one block of code or another block of code, use the #ifdef with the define passed to the compile function.

 

One usage example for passing a define to the compile function would be to use a different vertex layout struct for passing into the vertex shader.  You can define two structs, say one with normals and the other without normals and use an #ifdef to control which is used.  Then compile the shader twice, one with the value defined, and the other without. Now you have two shader objects, one that can handle vertex inputs with normals, and one without.  This allows you to write one shader that you can use in multiple ways with different types of geometry.



#20 InfoGeek   Members   -  Reputation: 195

Like
0Likes
Like

Posted 06 September 2014 - 11:52 AM

thanks for the explanation again megadan, i knew some of it but it helps to be reassured.

 


If you want to prevent things from being multiply defined, use #ifndef/#define.

 

i want to follow your suggestion to include common header in shaders, but if i use #ifndef/#define at runtime how will it even work?

 

i mean at program compile time(as you said, i think...) the compiler keeps track of the #define so the #ifndef/#define method works and duplicates are excluded. if it is program runtime call to compile would it always include all files anyway because it has no way of tracking the #define ?







PARTNERS