Jump to content
  • Advertisement
Sign in to follow this  
DarkRonin

Pre-compiled HLSL shader question

This topic is 1047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys,

I am currently trying out how to add a precompiled shader to my program in the form of a header file.

I have compiled the shader in to a header like so as per MS documentation (https://msdn.microsoft.com/en-us/library/windows/desktop/bb509710(v=vs.85).aspx), as follows.

fxc /T ps_4_0 /EPShader /Fo PixelShader.h PixelShader.hlsl
This document (https://msdn.microsoft.com/en-us/library/windows/desktop/bb509633(v=vs.85).aspx#compiling_at_build_time_to_header_files) then says to add the precompiled header into the project as follows

// Actual MS example
#       include "PixelShader.h"
        ComPtr<ID3D11PixelShader> m_pPixelShader;
        hr = pDevice->CreatePixelShader(g_psshader, sizeof(g_psshader), nullptr, &m_pPixelShader);
My code (slightly modified to suit my project)

#include "PixelShader.h"
HRESULT	result = d3dDevice->CreatePixelShader(d3dPsBuffer->GetBufferPointer(), d3dPsBuffer->GetBufferSize(), 0, &d3dSolidColorPS);
The result is an 'unexpected end of file found', due to the include file.

Also, I have concerns as to how does the include file 'magically' associate itself with d3dPsBuffer? I can't see how the CreatePixelShader() call can possibly know that it is loading the data from that particular header file. What would happen if you had a dozen pre-compliled HLSL headers? How would you determine which belongs to what CreatePixelShader() call?

Thanks in advance smile.png

Share this post


Link to post
Share on other sites
Advertisement
Ok, I just found something. The MSDN docs are wrong. Their example compiles the shader to an object file and not a header.

The real compilation should look like this (using the Fh switch instead)

fxc /T ps_4_0 /EPShader /Fh ss.h ss.hlsl

Now that I have found this, I'll do some testing and report back.

Share this post


Link to post
Share on other sites
Also turns out that the magic association is by a global variable being set inside the HLSL header file.

So it is literally a case of doing this in-line and the g_PShader variable will scope itself out of the way when your function is called, resulting in no memory being held open.

#include "PixelShader.h"
result = d3dDevice->CreatePixelShader(g_PShader, sizeof(g_PShader), 0, &d3dSolidColorPS);

Pretty easy in the end smile.png

Share this post


Link to post
Share on other sites

I prefer to do this via property pages in VS 2015 instead of calling fxc directly. You don't have to bother that much with compile parameters that way.

However, in VS 2015, you can use this to set the variable name that holds the compiled binary shader code: /Vn

 

Via parameter pages you can use some nice placeholders, so you just need to copy/paste with new shaders, e.g. like below:

 

Page: HLSL Compiler / Output Files:

Header Variable Name:    binShader_%(Filename)

Header File Name:           $(ProjectDir)\$(Platform)\$(Configuration)\Output\$(ProjectName)\CompiledShaders\%(Filename).h

 

So all my shader headers follow the same name scheme and are all put into the same folder

Edited by clemensx

Share this post


Link to post
Share on other sites

FYI, I reported the misleading documentation (px_5_1 as the profile, and writing a file with the extension .h even though it was outputting an object file), and this has now been rectified.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!