Hello,
I had posted this in the beginners forum, but haven't gotten any possible ideas in a couple days now, so I'm hoping maybe someone that browses here might be able to help.
I've been trying to shift gears and move from the fixed function pipeline to shaders, and am not having the best of luck. I'm using the very basic empty .fx file from FX Composer, which follows:
// An empty material, which simply transforms the vertex and sets the color to white.
//------------------------------------
float4x4 matWVP : WorldViewProjection;
//------------------------------------
struct vertexInput {
float3 Position : POSITION;
};
struct vertexOutput {
float4 HPosition : POSITION;
float4 Diffuse : COLOR0;
};
//------------------------------------
vertexOutput VS_TransformDiffuse(vertexInput IN)
{
vertexOutput OUT;
OUT.HPosition = mul( float4(IN.Position.xyz , 1.0) , matWVP);
OUT.Diffuse = float4(1.0f, 1.0f, 1.0f, 1.0f);
return OUT;
}
//-----------------------------------
technique simplest
{
pass p0
{
VertexShader = compile vs_1_1 VS_TransformDiffuse();
// Just use the color
ColorArg1[0] = Diffuse;
AlphaArg1[0] = Diffuse;
ColorOp[0] = SelectArg1;
AlphaOp[1] = SelectArg1;
}
}
In the DX code, I made a simple class to hold a quad for learning with, which is as follows:
#include "..\include\CVSQuad.h"
void CVSQuad::Render(LPDIRECT3DDEVICE9 pDevice, D3DXMATRIX* vProj) {
if (FAILED(pDevice->SetVertexDeclaration( m_pVD ))) {
SHOWERROR("SetVertexDeclaration failed", __FILE__, __LINE__);
}
D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName("simplest");
if (FAILED(m_pEffect->SetTechnique(hTechnique))) {
SHOWERROR("Error setting the technique", __FILE__, __LINE__);
return;
}
if (FAILED(m_pEffect->GetTechniqueDesc(hTechnique, &m_TechDesc))) {
SHOWERROR("Error getting the technique descriptor", __FILE__, __LINE__);
return;
}
if(FAILED(pDevice->SetStreamSource(0, m_VB.GetVertexBuffer(), 0, sizeof(cuCustomVertex::PositionColorTextured)))) {
SHOWERROR("Failed to set the stream source", __FILE__, __LINE__);
return;
}
if(FAILED(m_pEffect->SetValue("matWVP", vProj, sizeof(D3DXMATRIX)))) {
SHOWERROR("Failed in setting effect projection", __FILE__, __LINE__);
return;
}
for( UINT p = 0; p < m_TechDesc.Passes; ++p )
{
m_pEffect->BeginPass(p);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
m_pEffect->EndPass();
}
}
void CVSQuad::Initialize(LPDIRECT3DDEVICE9 pDevice) {
m_Verts = new cuCustomVertex::PositionColorTextured[6];
m_Verts[0] = cuCustomVertex::PositionColorTextured(-20.0f, 0.0f, -20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 0.0f, 1.0f);
m_Verts[1] = cuCustomVertex::PositionColorTextured(-20.0f, 0.0f, 20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 0.0f, 0.0f);
m_Verts[2] = cuCustomVertex::PositionColorTextured( 20.0f, 0.0f, 20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 1.0f, 0.0f);
m_Verts[3] = cuCustomVertex::PositionColorTextured(-20.0f, 0.0f, -20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 0.0f, 1.0f);
m_Verts[4] = cuCustomVertex::PositionColorTextured( 20.0f, 0.0f, 20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 1.0f, 0.0f);
m_Verts[5] = cuCustomVertex::PositionColorTextured( 20.0f, 0.0f, -20.0f, D3DCOLOR_ARGB(255, 0, 0, 255), 1.0f, 1.0f);
m_VB.CreateBuffer(pDevice, 6, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, sizeof(cuCustomVertex::PositionColorTextured));
m_VB.SetData(6, m_Verts, 0);
ID3DXBuffer *pBuffer = NULL;
if (FAILED(D3DXCreateEffectFromFile(pDevice, "media/myShader.fx", NULL, NULL, 0, NULL, &m_pEffect, &pBuffer))) {
SHOWERROR("Create Effect function failed", __FILE__, __LINE__);
}
if(pBuffer != NULL) {
MessageBox(0, (const char*)pBuffer->GetBufferPointer(), "Effect Compilation Error", 0);
}
D3DVERTEXELEMENT9 vElements[] = {
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 44, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
if (FAILED(pDevice->CreateVertexDeclaration( vElements, &m_pVD ))) {
SHOWERROR("CreateVertexDeclaration failed", __FILE__, __LINE__);
}
}
CVSQuad::CVSQuad() {
}
void CVSQuad::Release() {
m_VB.Release();
SAFE_DELETE_ARRAY(m_Verts);
}
pDevice is obvious, m_VB is a from a vertex buffer class to encapsulate vertex buffer functions, and I believe everything else is self explanatory.
The problem I'm having is this... when I run the app, I get no errors at all, no failed functions and no compilation errors from the shader file, etc. I've even created bugs in the fx file to make sure it was loading, which threw the app, so I know it's being used. But - the quad still gets rendered blue, and t shouldn't. From passing through the shader file, it should be forced to white. A bigger problem is that when I through this simple quad through the FFP, my system runs it perfectly around 3000 fps. When using this version, my fps only ranks around 600 at first and as it runs will slowly continue to drop. To boot, the longer I let the app run, the longer it takes to unload and return me to the IDE. It seems as though there's a memory leak within the render call itself, but I can't figure out where.
I'm at a loss as to why this isn't working, and if anyone can throw some info my way, that would be awesome.
Thanks much, and if anything else needs to be provided, just shout..
Scorp
[edit] I know the error catches are ugly right now... added as many as possible in an attempt to find what the issue is... they'll be cleaned up once I get things running