Compile HLSL at Runtime ?

Started by
8 comments, last by Ryan_001 10 years, 9 months ago

Hi guys,

Today I switched from D3D9 to D3D10. I'm having a problem compiling my HLSL effects. I don't want to use any D3DX functions since they are deprecated. I thought I would use D3DCompile() but it can't find ID3D10BLOB.

Since I'm using Win8 all I do is #include <d3d10.h> and <d3dcompiler.h>, I don't set any library files and let it use the Windows SDK.

I tried adding #include <D3DCommon.h> but it still can't find ID3D10BLOB.

Maybe I shouldn't even be using D3DCompile() ? What do you think ?

Advertisement

Win 8 + C# here. From what I know W8 store apps can't use the Effect framework. There could be the same limitation if you are building a W8 app. If you plan to ultimately submit the app to the store then there's no other way, effects have to be compiled offline. You just load the bytecode and set the constants buffer without using any kind of reflection.

--Avengers UTD Chronicles - My game development blog

I was able to get D3DCompile() and ID3D10Blob to be recognized. I build a new project from scratch and it magically worked.

The thing I don't get is that I have to specify d3d10.lib or else it won't resolve D3D10CreateDeviceAndSwapChain() and also d3dcompiler.lib, I can understand having to manually add d3dcompiler.lib but shouldn't D3D10CreateDeviceAndSwapChain() be in the Windows 8 SDK along with all the other D3D10 stuff ?

Thanks.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb173551(v=vs.85).aspx

Is probably what you're looking for. Just give it the raw compiled pixel shader code. fxc.exe usually compiles to .cso files so you can just load them into ram and feed it directly into the function.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509703(v=vs.85).aspx

That goes into more detail.

I almost got it to work. Under D3D10CompileShader() it says "Use D3DX10CompileFromMemory instead of this function." and since I'm not using the deprecated D3DX functions I'm using D3DCompile().


char* source=
"float4 vshader(float4 Pos : POSITION) : SV_POSITION"
"{"
"return Pos;"
"}";
 
ID3D10Blob* blob=NULL;
 
hResult=D3DCompile(source,strlen(source),NULL,NULL,NULL,"vshader","vs_3_0",0,0,&blob,NULL);
 
// so far so good
 
ID3D10VertexShader* vertex_shader=NULL;
 
hResult=d3d_device->CreateVertexShader((DWORD*)blob->GetBufferPointer(),blob->GetBufferSize(),&vertex_shader);
 
// fail: E_INVALIDARG

Here is the funny part (regarding the call to CreateVertexShader): "Since you had to successfully compile the shader first, this call will almost certainly pass, unless you have a memory problem on your machine."

What am I doing wrong ? Thanks.

*EDIT* Yay, I got it to work, vs_3_0 was causing the error, I changed it to vs_4_0 and it's fine now.

It was from SV_POSITION. It used to be VPOS in D3D9.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx#VPOS

Also I recommend using visual studio 2012 and compiling offline. My solution used to take 15 seconds to load because it compiled the shaders at run-time. I didn't have that many shaders. I switched to offline compiling and the screen comes up in under a second. It's huge for productivity. And according to some guy I'm not going to cite there's no real advantage to compiling at run-time since it increases loading by so much.

menohack, I just realized that offline is better. How are you getting your compiled shaders into your program ? I found D3D10CreateEffectFromMemory() but I can't get a triangle on the screen with it and I don't want to use the Effects framework anyway. I'm having a little problem understanding how to pass my shader into CreateVertexShader() as well.

Thanks.

D3DReadFileToBlob loads from a file to a ID3DBlob, but it looks like it is only available for D3D11. I don't know if you can just fopen and fread the .cso file, but you can definitely compile at run-time the first time and just write the blob to a file. Then the next time your program runs you can just fopen the compiled file.

If you have to go with the second way then you can still have visual studio compile your shaders to make sure they are correct at build-time. You just won't actually use the object files that it produces.

I set Visual Studio to compile each shader into a header in my home project. I suppose it's better to have files if you are going to unload / swap them, but I'm not working on something where that is a concern. With a shader in a header, you just pass the array directly into the CreateVertexShader function.

D3DReadFileToBlob loads from a file to a ID3DBlob, but it looks like it is only available for D3D11. I don't know if you can just fopen and fread the .cso file, but you can definitely compile at run-time the first time and just write the blob to a file. Then the next time your program runs you can just fopen the compiled file.

If you have to go with the second way then you can still have visual studio compile your shaders to make sure they are correct at build-time. You just won't actually use the object files that it produces.

I just pass the raw file data to it and skip the ID3DBlob. I load the .cso file into a std::vector<char>, and then pass that via data() and size() to the create shader function. The blob way is a bit more direct, I'd just thought I'd mention my approach to show that it can be done.

This topic is closed to new replies.

Advertisement