View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Compile HLSL at Runtime ?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

Posted 15 July 2013 - 06:07 PM

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 ?

### #2AvengerDr  Members

Posted 15 July 2013 - 06:26 PM

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

Posted 15 July 2013 - 08:37 PM

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.

Edited by Endemoniada, 15 July 2013 - 09:15 PM.

### #4Ryan_001  Prime Members

Posted 15 July 2013 - 09:24 PM

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.

Edited by Ryan_001, 15 July 2013 - 09:25 PM.

Posted 15 July 2013 - 09:44 PM

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;

// so far so good

// 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.

Edited by Endemoniada, 15 July 2013 - 10:43 PM.

### #6menohack  Members

Posted 15 July 2013 - 11:07 PM

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.

Posted 16 July 2013 - 10:12 AM

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.

### #8menohack  Members

Posted 16 July 2013 - 10:23 AM

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.

### #9Pink Horror  Members

Posted 16 July 2013 - 10:41 AM

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.

### #10Ryan_001  Prime Members

Posted 16 July 2013 - 11:19 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.