Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a precompiled HLSL shader into memory for use with CreatePixelShader

Tags:

c++

direct3d

hlsl

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)

like image 295
Calin Leafshade Avatar asked Feb 16 '11 17:02

Calin Leafshade


People also ask

What does Hlsl compile to?

Compiling at build time to header files hlsl file specifies to compile into the g_psshader byte array that is defined in the PixelShader. h header file.

What does Hlsl stand for?

The High-Level Shader Language or High-Level Shading Language (HLSL) is a proprietary shading language developed by Microsoft for the Direct3D 9 API to augment the shader assembly language, and went on to become the required shading language for the unified shader model of Direct3D 10 and higher.

What is HLSL and GLSL?

You port your OpenGL Shader Language (GLSL) code to Microsoft High Level Shader Language (HLSL) code when you port your graphics architecture from OpenGL ES 2.0 to Direct3D 11 to create a game for Universal Windows Platform (UWP).


2 Answers

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.

like image 150
gundamb2 Avatar answered Oct 24 '22 10:10

gundamb2


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);
like image 29
Jasper Avatar answered Oct 24 '22 09:10

Jasper