• Advertisement
Sign in to follow this  

Problem with compile shader from file

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

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 );

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
oops, replace WCHAR with char. BTW OutputDebugString will output that data to the Visual Studio output window not in your app

Share this post


Link to post
Share on other sites
you better use shader shader compiler (fxc) to compile that shader/to find errors in your shader

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Well, now fix those errors...looking at those errors, seems like it's very easy to fix them

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
...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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I find only one problem:
cfae492ce2e9.jpg
before VS all vertexes have different position
2500eb7f542f.jpg
and after VS all 0...so how to fix it?

Share this post


Link to post
Share on other sites
see that column VTX in PostVS tab? 0 1 2 3...click one of those and debug your vertex shader. IDX are indices, looking that picture, they are not correct either. Am guessing, the homogeneous matrix is not being update in the shader. Debug and find it out

Share this post


Link to post
Share on other sites

//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
// Parameters:
//
// float4x4 worldViewProj;
//
//
// Registers:
//
// Name Reg Size
// ------------- ----- ----
// worldViewProj c0 4
//

vs_2_0
def c4, 1, 0, 0, 0
dcl_position v0
dcl_color v1
dcl_texcoord v2
mad r0, v0.xyzx, c4.xxxy, c4.yyyx
dp4 oPos.x, r0, c0
dp4 oPos.y, r0, c1
dp4 oPos.z, r0, c2
dp4 oPos.w, r0, c3
mov oD0, v1
mov oT0.xy, v2

// approximately 7 instruction slots used

if i understand correctly this is 0 vertex debug.
and Registry:
c4 (1.000,0.000,0.000,0.000) float4
v0 (909.163,59.139,-1585.124,1.000) float4

and what is wrong? I can post my VS too.

Share this post


Link to post
Share on other sites
As far as I remember c# are constant registers and v# are output registers but don't remember much though. Debug your shader in via HLSL rather than going through the disassembled code...anyways, when debugging your shader monitor the value of variables from the variables tab. Look at the value of homogeneous matrix from that tab.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement