Sully Chen
Sully Chen

Reputation: 339

How to create ID3D11VertexShader from .fxo file?

I have a .fx file without any techniques defined. In my code, to load my vertex and pixel shaders I use

ID3DBlob* pVSBlob = NULL;
ID3DBlob* pErrorBlob;
    hr = D3DX11CompileFromFile( "shader.fx", NULL, NULL, "VS", "vs_4_0", 
        dwShaderFlags, 0, NULL, &pVSBlob, &pErrorBlob, NULL );

then make the shader with

hr = g_pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVertexShader );

This works well, and when it comes time to render, I just use

g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );

to set the shaders. I want to compile the .fx file into a .fxo file, but the only ways I've seen to load .fxo files is using D3DX11CreateEffectFromMemory. However, I would prefer not to use the Effect interface, and I have no techniques in my .fx file anyway. Is there any way to load shaders like I did above except from a .fxo file instead of a .fx file?

Upvotes: 1

Views: 1161

Answers (1)

mrvux
mrvux

Reputation: 8963

Since compiled shader is just a bulk of binary data ready for upload, any File load API will do.

HANDLE hFile = CreateFile(shaderFileName, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );

if (hFile == INVALID_HANDLE_VALUE) 
{
    //Error opening file, handle here (GetLastError can return why this failed).
}
else
{
    int fileSize = GetFileSize(hFile,NULL);

    byte* data = (byte*)malloc(fileSize);
    //You can also check if data is NULL, hence malloc failed

    DWORD numRead = 0;

    if (ReadFile(hFile, data, fileSize, &numRead , NULL ))
    {
         hr = g_pd3dDevice->CreateVertexShader(data,fileSize,NULL,&g_pVertexShader);
    }
    else
    {
         //Read failed, handle error as you wish
    }

    CloseHandle(hFile);
    free(data);
 }

Upvotes: 2

Related Questions