Given an effect-technique-pass handle, how to get the PixelShader

Started by
2 comments, last by cozzie 10 years, 8 months ago
My code below is intended to do a few things in order:
- Load the effect file
- Get the main technique in the FX file
- Get the first pass of that technique
- Get the vertex and pixel shaders from that pass
All works fine up until the GetVertexShader() call. I wind up with hr=D3DERR_INVALIDCALL (0x8876086c).
I've verified that I'm getting back values for hTechnique and hPass. For completeness I've included the relevant snippet from the .fx file as well.

So, my question boils down to: give a handle to a shader pass, how do you get the vertex and pixel shaders from it?


ID3DXBuffer * pBuffer = NULL;
V_RETURN( D3DXCreateEffectFromFile(pd3dDevice, strPath.c_str(), 0, 0, D3DXSHADER_DEBUG, 0, &_pEffect, &pBuffer) );
V_RETURN( pd3dDevice->CreateVertexDeclaration(particleVertexDeclarations, &_pVertexDeclaration) );

// Get the technique, pass, and shaders we need

D3DXHANDLE hTechnique = _pEffect->GetTechniqueByName("ExplosionTechnique");
if (!hTechnique)
    return E_FAIL;

D3DXHANDLE hPass = _pEffect->GetPassByName(hTechnique, "Pass0");
if (!hPass)
    return E_FAIL;

V_RETURN( _pEffect->GetVertexShader(hPass, &_pVertexShader) );   // FAILS!
V_RETURN( _pEffect->GetPixelShader(hPass, &_pPixelShader) );

technique ExplosionTechnique
{
  pass Pass0
  {
    VertexShader = compile vs_3_0 ExplosionVertexShaderFunction( );
    PixelShader  = compile ps_3_0 ExplosionPixelShaderFunction( );
  }
}
Advertisement

According to an MSDN page, I found this:

What is the correct way to get shaders from an Effect?

Use D3DXCreateEffect to create an ID3DXEffect and then use GetPassDesc to retrieve a D3DXPASS_DESC. This structure contains pointers to vertex and pixel shaders

So I wrote this code:


D3DXPASS_DESC desc;
V_RETURN( _pEffect->GetPassDesc(hPass, &desc) );
_pPixelShader  = (D3DPixelShader *)  desc.pPixelShaderFunction;
_pVertexShader = (D3DVertexShader *) desc.pVertexShaderFunction;

However, while there are raw pointers available, they're certainly not IDirect3DPixelShader9 objects; you can see that the v-table of the IUnknown are garbage. Am I perhaps supposed to treat them like opaque (DWORD *) values rather than proper objects? If that were the case, then calling SetVertexShader on the device wouldn't be possible with them, since pd3d->SetVertexShader requires a true IDirect3DVertexShader9.

So, still hooped.

I'll answer my own question since I've seen it asked a number of times but never answered: this appears to work.

D3DXPASS_DESC desc;

V_RETURN( _pEffect->GetPassDesc(hPass, &desc) );

V_RETURN( pd3dDevice->CreateVertexShader(desc.pVertexShaderFunction, &_pVertexShader) );

V_RETURN( pd3dDevice->CreatePixelShader (desc.pPixelShaderFunction, &_pPixelShader) );

Hi.
If you create the effect from a (fx) file, you only need the right technique to proceed. The corresponding Vertex- and Pixel- shaders are defined within the specific technique you're using.

Basically this should do the trick (pseudo that is :))

Once:
- d3dx create effect from file
- get handle to the requested technique (don't think there's a main technique)
- get handles to your shader constants, matrices, materials, lights, texture etc
- create your vertex declaration and set it

For rendering:
- set the technique using the handle
- loop through the passes
- begin the pass
- set the right material/ texture
- set the streamsources, ie your mesh data
- update your matrices and set them in the shader (setmatrix, setfloatarrays etc)
- commit all changs
- draw indexed primitive
- end pass
- end technique

Present your scene :)

This is of course a basic approach, where you need to find your optimizations in looping through mesh instances, materials etc.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement