Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with compile shader from file

Started by Arkanoid Mar 12, 2012 at 3:21 PM 37 replies 8.5k views
Original Post
Arkanoid
Arkanoid
Hi,

i wanna to add to my project shaders, but always have a problem here:

hr =D3DXCompileShaderFromFile( L"vertex.vsh", NULL, NULL,
"main", D3DXGetVertexShaderProfile(g_pd3dDevice),dwFlags, &Code, &BufferErrors,
&ConstantTable )


i cannot understand why? here is the part of my shader code:

LPDIRECT3DVERTEXDECLARATION9 VertexDeclaration = NULL;
g_pd3dDevice->CreateVertexDeclaration( declaration, &VertexDeclaration );

g_pd3dDevice->SetVertexDeclaration( VertexDeclaration );
HRESULT hr;
DWORD dwFlags = 0;
dwFlags |= D3DXSHADER_SKIPOPTIMIZATION;
hr =D3DXCompileShaderFromFile( L"vertex.vsh", NULL, NULL,
"main", D3DXGetVertexShaderProfile(g_pd3dDevice),dwFlags, &Code, &BufferErrors,
&ConstantTable );
if(FAILED(hr))
{
MessageBox( NULL, LPCWSTR(BufferErrors), L"Meshes.exe", MB_OK );
}

g_pd3dDevice->CreateVertexShader( (DWORD*)Code->GetBufferPointer(),&VertexShader );
Asesh
Asesh

did you mean [color=#000000][size=2]D3DXCompileShaderFromFile fails? you can see what went wrong by calling: ::OutputDebugString(reinterpret_cast([color=#660066][size=2]BufferErrors->GetBufferPointer()));

Arkanoid
Arkanoid
if(FAILED(hr))
{
::OutputDebugString(reinterpret_cast<WCHAR *>(BufferErrors->GetBufferPointer()));
}
like that?...no it didn't show anything...

or like this:
if(FAILED(hr))
{
//::OutputDebugString(reinterpret_cast<WCHAR *>(BufferErrors->GetBufferPointer()));
MessageBox( NULL,reinterpret_cast<WCHAR *>(BufferErrors->GetBufferPointer()), L"Meshes.exe", MB_OK );
}
then it shows me many ?hinese characters...i cannot understand ?hinese....
RobinsonUK
RobinsonUK
I don't think BufferErrors is WCHAR. I'm pretty sure it's ordinary char.
Asesh
Asesh
oops, replace WCHAR with char. BTW OutputDebugString will output that data to the Visual Studio output window not in your app
Asesh
Asesh
you better use shader shader compiler (fxc) to compile that shader/to find errors in your shader
Arkanoid
Arkanoid
oh...sorry can you please say more correctly what function i need to use? And how can i using it understand what error i have?
Arkanoid
Arkanoid
964b3f33308d.jpg
i cannot read it)...i don't know how to see message in English.
Anddos
Anddos
change the project from unicode to multibyte..
:)
Arkanoid
Arkanoid
9fca6430a47c.jpg


I changed, here is all errors maybe...
Asesh
Asesh
Well, now fix those errors...looking at those errors, seems like it's very easy to fix them
Arkanoid
Arkanoid

Well, now fix those errors...looking at those errors, seems like it's very easy to fix them

haha...but...i understand that the error was in shader code - this mas the main that i wanted to understand...

Now situation is better but - models don't render...i tried many shader examples code - and with all them model don't render...where can be the problem?
Have sb 100% working vertex-shader code? I'll try it in my program to understand where is the problem - in shader or in code.
Asesh
Asesh
Use PIX to find out what's wrong. It's part of the SDK. Here are some D3D 9 samples: http://www.codesampler.com/dx9src.htm try using effect framework instead, since it will be very easy to use both vertex and pixel shaders
Arkanoid
Arkanoid
I understand where can be the problem...in all samples vertexes have special format:
struct Vertex
{
float x, y, z;
DWORD color;
float tu, tv;

enum FVF
{
FVF_Flags = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1
};
};

but i use .X model and i what vertexes i need to use? For example:
g_pd3dDevice->SetFVF( Vertex::FVF_Flags );
g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0,sizeof(Vertex) );

i don't know where i can find Vertex of my mesh...and how can i change it.
Asesh
Asesh
You are mixing shaders and fixed function pipeline. Those FVFs should be replaced with vertex declarations. But since you are using x file you don't have to do that. Just went through my old D3D 9 code and seems like simply setting x file's corresponding texture and then calling DrawSubset would suffice. Like I said, try using effect frame instead. For reference:
Frank Luna's Dx 9.0c a shader approach: http://d3dcoder.net/d3d9c.htm you can download source code from that page and scroll down to see the output
Arkanoid
Arkanoid
hm...i downloaded this sample:
Simple Vertex & Pixel Shader (HLSL)
from here: http://www.codesampler.com/dx9src/dx9src_8.htm#dx9_hlsl_fx_simple
cause it works....i tryed to add it to my code:
void initShader( void )
{
//
// Create a test texture for our effect to use...
//

//D3DXCreateTextureFromFile( g_pd3dDevice, "test.bmp", &g_pTexture );

//
// Create a HLSL based vertex shader.
//

//
// If your program uses explicit binding semantics (like this one),
// you can create a vertex declaration using those semantics.
//

D3DVERTEXELEMENT9 declaration[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

g_pd3dDevice->CreateVertexDeclaration( declaration, &g_pVertexDeclaration );

HRESULT hr;
LPD3DXBUFFER pCode;
DWORD dwShaderFlags = 0;
LPD3DXBUFFER pBufferErrors = NULL;

// Assemble the vertex shader from the file
hr = D3DXCompileShaderFromFile( "vertex_shader.vsh", NULL, NULL, "main",
"vs_2_0", dwShaderFlags, &pCode,
&pBufferErrors, &g_pConstantTableVS );

if( FAILED(hr) )
{
LPVOID pCompilErrors = pBufferErrors->GetBufferPointer();
MessageBox(NULL, (const char*)pCompilErrors, "Vertex Shader Compile Error",
MB_OK|MB_ICONEXCLAMATION);
}

// Create the vertex shader
g_pd3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(),
&g_pVertexShader );
pCode->Release();

//
// Create a HLSL based pixel shader.
//

// Assemble the vertex shader from the file
hr = D3DXCompileShaderFromFile( "pixel_shader.psh", NULL, NULL, "main",
"ps_2_0", dwShaderFlags, &pCode,
&pBufferErrors, &g_pConstantTablePS );

if( FAILED(hr) )
{
LPVOID pCompilErrors = pBufferErrors->GetBufferPointer();
MessageBox(NULL, (const char*)pCompilErrors, "Pixel Shader Compile Error",
MB_OK|MB_ICONEXCLAMATION);
}

// Create the vertex shader
g_pd3dDevice->CreatePixelShader( (DWORD*)pCode->GetBufferPointer(),
&g_pPixelShader );
pCode->Release();
}


than to render:
for(int b =1;b<MeshCount+1;b++)
{
for( DWORD i = 0; i < g_dwNumMaterials; i++ )
{
if( g_bUseShaders == true )
{
//
// Use vertex and pixel shaders...
//

D3DXMATRIX worldViewProjection = matWorld * matView * g_matProj;
g_pConstantTableVS->SetMatrix( g_pd3dDevice, "worldViewProj", &worldViewProjection );

g_pd3dDevice->SetVertexDeclaration( g_pVertexDeclaration );
g_pd3dDevice->SetVertexShader( g_pVertexShader );

// Set the material and texture for this subset

// shiftTextureCoordinates(g_pMesh);
// Draw the mesh subset

//g_pd3dDevice->SetTexture(0, g_baseTexture);
g_pd3dDevice->SetPixelShader( g_pPixelShader );

g_pd3dDevice->SetMaterial( &entity.g_pMeshMaterials );
g_pd3dDevice->SetTexture( 0,entity.g_pMeshTextures );

g_pMesh->DrawSubset( i );
}
}


in sample everything works, in my program mesh doesn't draw. I don't think that i'll find better code than this one...what i missed?
Asesh
Asesh
Use PIX to find out what's going on, like I said it's part of the SDK. You can use PIX to see what happened before vertex shader and after vertex shader and debug your vertex/pixel shader too
Arkanoid
Arkanoid
...how? PIX show me all frames but i Draw my mesh after i set up shader....and what shoud i see? I can see only all commands. I know witch command
is problem here:
g_pd3dDevice->SetPixelShader( g_pPixelShader );
or
g_pd3dDevice->SetVertexShader( g_pVertexShader );
I know this without PIX...everything that i see is clean screen in all frames in PIX. How can i see what i need?wacko.png
Asesh
Asesh
Run your app via PIX by choosing single frame capture option, then capture a frame by hitting F12 button. Now in the events panel choose a frame. Now to debug vertex shader you have to locate draw call, so locate that Draw* call then goto Mesh tab, there you can see what's happening to your primitive in pre VS and post VS stages and can debug your vertex shader too

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.