Direct3D Intro - HLSL 1.x Compile Error

来源:互联网 发布:金角大王异步网络框架 编辑:程序博客网 时间:2024/05/24 07:10

On Direct3D9 Project, Following codes will cause failing to compile on Visual Studio 2013

1. Sample Code Snippet

    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(L"vertex_shader.vsh", NULL, NULL, "main",
                                    "vs_1_1", dwShaderFlags, &pCode,
                                    &pBufferErrors, &g_pConstantTableVS );

    if( FAILED(hr) )
 {
  LPVOID pCompilErrors = pBufferErrors->GetBufferPointer();
  MessageBox(NULL, (LPCTSTR)pCompilErrors, L"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( L"pixel_shader.psh", NULL, NULL, "main",
                                    "ps_1_1", dwShaderFlags, &pCode,
                                    &pBufferErrors, &g_pConstantTablePS );                      // if error, please change ps_1_1 to ps_2_0, retry to compile

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

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

2. Vertex Shader

float4x4 worldViewProj;

struct VS_INPUT
{
 float3 position  : POSITION;
 float4 color0    : COLOR0;
 float2 texcoord0 : TEXCOORD0;
};

struct VS_OUTPUT
{
 float4 hposition : POSITION;
 float4 color0    : COLOR0;
 float2 texcoord0 : TEXCOORD0;
};

VS_OUTPUT main( VS_INPUT IN )
{
 VS_OUTPUT OUT;

 float4 v = float4( IN.position.x,
                 IN.position.y,
        IN.position.z,
        1.0f );

    OUT.hposition = mul( v, worldViewProj );
    OUT.color0    = IN.color0;
    OUT.texcoord0 = IN.texcoord0;

    return OUT;
}

3. pixel shader

struct VS_OUTPUT
{
 float4 hposition : POSITION;
 float4 color0    : COLOR0;
 float2 texcoord0 : TEXCOORD0;
};

struct PS_OUTPUT
{
 float4 color : COLOR;
};

sampler testTexture;

PS_OUTPUT main( VS_OUTPUT IN )
{
 PS_OUTPUT OUT;

 //OUT.color = tex2D( testTexture, IN.texcoord0 ) * IN.color0; // Modulate texel color with vertex color
 OUT.color = tex2D( testTexture, IN.texcoord0 ) + IN.color0; // Add texel color to vertex color

 return OUT;
}

0 0