Compiling and using shaders

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

Ah ok, I think I see what you're asking now. Compiling a shader at your program runtime with D3DCompileFromFile is actually no different from compiling the shader offline from the command line using fxc.exe. The same steps are performed and the same shader bytecode is generated. So using #ifndef/#define will work exactly the same in both cases.

Edit: The shader compilation step (either through D3DCompileFromFile or fxc.exe) is what handles processing the defines. So the defines work the same both ways. Your actual application runtime or compilation doesn't affect it.

Advertisement

yeah but...

say i have:

common.hlsli


#ifndef POOP_H
#define POOP_H

// whatever shared code goes here.

#endif

and i have two shader that use that include:

shader1.hlsl


#include "common.hlsli"

// Whatever entry.

shader2.hlsl


#include "common.hlsli"
#include "common.hlsli"

// Whatever entry.

then:


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

D3DCompileFromFile(L"shader2.hlsl", 0, 0, "PS", "ps_5_0", 0, 0, &PS_Buffer2, 0);

this is all runtime, so will the common.hlsli be included 3 times ? 2 ? 1? how does this happen? would it be different if it were at compile time?

shader1.hlsl and shader2.hlsl will both have exactly one copy of common.hlsli. This works because D3DCompileFromFile is performing all of the steps necessary to make that happen.

It opens shader1.hlsl, parses the #include, opens common.hlsli, defines POOP_H, copies the contents into shader1.hlsl, and then compiles the shader and generates the bytecode which is returned in PS_Buffer.

For shader2.hlsl, it parses the first #include, opens common.hlsli, defines POOP_H, copies the contents into shader1.hlsl, parses the second include, POOP_H is already defined so the contents are not copied again, and then compiles the shader to bytecode.

This is a slight simplification from the actual steps, but this is essentially what happens. Each shader file compilation has its own local set of defines which is why POOP_H wasn't defined at first for shader2.hlsli and the contents were able to be copied.

great stuff megadan laugh.png i am very grateful for your patience with me. i believe this thread has all the answers to compile and use shaders. i'm sure it will be useful to any beginner who comes here googling.

once again thanks everyone!

This topic is closed to new replies.

Advertisement