Archived

This topic is now archived and is closed to further replies.

Getting Cg up and running in d3d

This topic is 5584 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

Okay I decided to finally give Cg a go . Going from the docs and samples i''ve managed to get it to compile some code and spit back the assembly version back at me. I''ve created a small test application to try this out. Its 4 vert plane. It renders fine when I use an FVF, but when i try it with this shader it draws nothing . Here is the shader. All its supposed to do is project the vertex and set its colour to red .
// define inputs from application
struct appin
{
    float3 Position : POSITION;    
};

// define outputs from vertex shader
struct vertout
{
    float4 Hposition : POSITION;
    float4 Color0 : COLOR0;
};

vertout main(appin In, uniform float4x4 ModelViewProj : C0)
{
    vertout Out;
    
    float4 v; 
    v.xyz = In.Position;
    v.w = 1;
    
    Out.Hposition = mul(ModelViewProj, v);
    Out.Color0 = float4 (1.0, 0, 0, 1.0);

    return Out;
}
 
For the C++ side here is my vertex struct (which is the same as what cg''s GetVertexDeclaration function is telling me to use), and my rendering code:
struct MyVertex
{
    D3DXVECTOR3         pos;
};

...
        m_pd3dDevice->SetRenderState (D3DRS_LIGHTING, FALSE);
        m_pd3dDevice->SetRenderState (D3DRS_CULLMODE, D3DCULL_NONE);
        D3DXMATRIX viewMat;
        D3DXMATRIX projMat;
        D3DXMATRIX mat;

        m_pd3dDevice->GetTransform (D3DTS_VIEW, &viewMat);
        m_pd3dDevice->GetTransform (D3DTS_PROJECTION, &projMat);
        D3DXMatrixMultiply (&mat, &viewMat, &projMat);
        D3DXMatrixTranspose( &mat, &mat );


        m_pVertexProgram->SetShaderConstant( m_pProjMatIter, &mat  );

        //m_pd3dDevice->SetVertexShader (D3DFVF_CUSTOMVERTEX);
        m_pd3dDevice->SetStreamSource (0, m_pVB, sizeof (MyVertex));
        m_pVertexProgram->SetShaderActive ();
        m_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);
 
Can anyone see what i''m doing wrong? Thanks.

Share this post


Link to post
Share on other sites
I''m pretty sure i''m initialization cg properlly. Like i said its compiling it fine (GetProgramObjectCode returns the compiled code).

Is the matrix shader constant being calculated/set correctly?

anyway here is my cg init code:


  
cgDirect3D m_cg;
cgContextContainer *m_pContextContainer;
cgProgramContainer *m_pVertexProgram;
cgBindIter *m_pProjMatIter;

...

// Initialize the cg shader

m_pContextContainer = m_cg.CreateContextContainer (m_pd3dDevice);
if (!m_pContextContainer)
{
MessageBox (NULL, "Error creeating cg context.", 0, 0);
return S_FALSE;
}

m_pVertexProgram = m_pContextContainer->LoadCGProgramFromFile ("Simple.cg", "Simple CG Program", cgDX8VertexProfile);

if (!m_pVertexProgram)
{
const char * listing = m_pContextContainer->GetLastListing();


MessageBox (NULL, listing, 0, 0);
return S_FALSE;
}

m_pProjMatIter = m_pVertexProgram->GetParameterBindByName("ModelViewProj");

Share this post


Link to post
Share on other sites
OKAY I worked out it was something unrelated. I was multiplying the matrices together i was extracting via GetTransform. But for some reason GetTransform is failing???! o_O I tried saving the matrices out as globals when i built them and the shader works.

Does anyone have any idea why GetTransform could possibley fail? It returns D3DERR_INVALIDCALL.

Here is how i''m calling it (yes i have successfully set the matrix):

D3DXMATRIX viewMat;
hr = m_pd3dDevice->GetTransform (D3DTS_VIEW, &viewMat);
if (FAILED (hr))
MessageBox (NULL, DXGetErrorString8 (hr), "WTF?!", 0);

Thanks.

Share this post


Link to post
Share on other sites
I think you can''t use GetTransform() if the device is intitialized as a "Pure Device" - same goes for most other Get*() routines. See the SDK on pure devices for this.

- JQ
Full Speed Games. Coming soon.

Share this post


Link to post
Share on other sites