Loading a precompiled HLSL shader into memory for use with CreatePixelShader
Asked Answered
I

2

10

I need to load a compiled pixel shader into memory to use with CreatePixelShader but I can't use any D3DX calls.

How can I do this?

(I'm using Visual Studio 2010 as my compiler and C++ as the language)

Incautious answered 16/2, 2011 at 17:57 Comment(2)
Why can't you use D3DX? are you not using DirectX at all, or is this some sort of test?Apure
@Apure Either he, like me, likes to understand what helper functions are doing, or he could see the future and saw this link. Yes, MS decided to break legacy code because... MS... Ironicaly enough, you still have to #define NOMINMAX to build a simple Windows application. Go figure.Chilcote
C
23

I realize someone posted pseudo-code earlier. Here is C++ code using the Windows SDK (and not the D3DX libraries as requested).

Here "PixelShader.cso" is the precompiled hlsl shader generated by Visual Studio 11 from a .hlsl file in the project. The compiled .cso file is usually moved to the Projects/ProjectName/Debug folder by default. As a result it must be cut and paste into the same directory as your source code before using. Mind you this setting can be changed by right-clicking the HLSL file while inside Visual Studio 11 and editing the Output Settings. By default its: $(OutDir)%(Filename).cso, change it to: $(Directory)%(Filename).cso

ID3D11PixelShader* PS;
ID3DBlob* PS_Buffer;

D3DReadFileToBlob(L"PixelShader.cso", &PS_Buffer);
d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
d3d11DevCon->PSSetShader(PS, 0, 0);

Take note of the L before "PixelShader.cso". My project was using the Multi-Byte Character Set, if you have yours set to Unicode the L might not be necessary.

Cohosh answered 25/5, 2012 at 18:12 Comment(1)
How to calculate checksum of cso while modifying it. My createVertextShader fails on checksum validationDrayage
O
7

build your precompiled shader from the command line using fxc:

fxc filename.hlsl /E PixelShaderEntry /Fo precompiledShader.ext

load the precompiled shader data using regular c++ file loading code.

in psuedo-ish code:

byte * data = loadFile("precompiledShader.ext");
IDirect3DPixelShader9 *ps = NULL;
HRESULT hr = device->CreatePixelShader(data, ps);
Orobanchaceous answered 16/2, 2011 at 18:36 Comment(1)
This is the right answer for D3D9 shaders. Note that since VS2013, HLSL files in the solution are compiled automatically to $(OutDir)%(Filename).cso, and that you have to select a lower D3D9 compatible shader model in the HLSL property pages.Anagram

© 2022 - 2024 — McMap. All rights reserved.