Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with compile shader from file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
38 replies to this topic

#1 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 12 March 2012 - 09:21 AM

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


Sponsor:

#2 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 12 March 2012 - 08:24 PM

did you mean D3DXCompileShaderFromFile fails? you can see what went wrong by calling: ::OutputDebugString(reinterpret_cast<WCHAR *>(BufferErrors->GetBufferPointer()));



#3 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 13 March 2012 - 11:51 AM

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....

#4 RobinsonUK   Members   -  Reputation: 108

Like
0Likes
Like

Posted 13 March 2012 - 02:31 PM

I don't think BufferErrors is WCHAR. I'm pretty sure it's ordinary char.

#5 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 14 March 2012 - 07:00 AM

oops, replace WCHAR with char. BTW OutputDebugString will output that data to the Visual Studio output window not in your app

#6 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 14 March 2012 - 07:02 AM

you better use shader shader compiler (fxc) to compile that shader/to find errors in your shader

#7 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 14 March 2012 - 11:46 AM

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?

#8 RobinsonUK   Members   -  Reputation: 108

Like
0Likes
Like

Posted 14 March 2012 - 03:52 PM

Post the error message.

#9 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 14 March 2012 - 11:19 PM

Posted Image
i cannot read it)...i don't know how to see message in English.

#10 Anddos   Members   -  Reputation: 416

Like
0Likes
Like

Posted 15 March 2012 - 07:52 AM

change the project from unicode to multibyte..
:)

#11 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 15 March 2012 - 08:12 AM

Posted Image


I changed, here is all errors maybe...

#12 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 March 2012 - 10:46 AM

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

#13 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 15 March 2012 - 02:10 PM

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.

#14 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 16 March 2012 - 08:55 AM

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

#15 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 18 March 2012 - 07:34 AM

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.

#16 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 18 March 2012 - 09:28 AM

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

#17 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 18 March 2012 - 10:58 AM

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[b]; 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[b]);
// Draw the mesh subset

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

g_pd3dDevice->SetMaterial( &entity[b].g_pMeshMaterials[i] );
g_pd3dDevice->SetTexture( 0,entity[b].g_pMeshTextures[i] );

g_pMesh[b]->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?

#18 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 18 March 2012 - 11:15 AM

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

#19 Arkanoid   Members   -  Reputation: 109

Like
0Likes
Like

Posted 19 March 2012 - 09:01 AM

...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?Posted Image

#20 Asesh   Members   -  Reputation: 268

Like
0Likes
Like

Posted 19 March 2012 - 06:20 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS