4D星宇

c++

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  57 随笔 :: 0 文章 :: 39 评论 :: 0 Trackbacks

有Effect版:
    DWORD         g_dwShaderFlags; // Shader compilate and link flags
      LPD3DXBUFFER  g_pCompiledFragments = NULL; 

    D3DXGatherFragmentsFromFile( L"FragmentLinker.fx", NULL,
                NULL, g_dwShaderFlags, &g_pCompiledFragments, NULL );

D3DXGatherFragmentsFromFile requires the .fx file, pointers to the #define and #include handlers (both set to NULL in this example), and the shader compile flags. The method returns a buffer which contains the compiled shader fragment. The method can return a second buffer with compile errors, which is set to NULL in this example because it is not used. D3DXGatherFragments is overloaded to handle loading fragments from a string, a file, or a resource.

Set your debugger to break on this method to look for compile errors in the debugger. The compiler can catch errors in syntax, but it cannot check for registers that are shared incorrectly due to the fact that it has no way to predict which parameters a user may want to share between fragments.

You need a fragment linker to manage the compiled fragments. Create the fragment linker by calling D3DXCreateFragmentLinker:

ID3DXFragmentLinker* g_pFragmentLinker = NULL;     // Fragment linker interface
IDirect3DDevice9*    pd3dDevice        = NULL;

    // Initialize the device before using it
 ...
 
    // Create the fragment linker interface
    D3DXCreateFragmentLinker( pd3dDevice, 0, &g_pFragmentLinker );

Then simply add the compiled fragments to the fragment linker using ID3DXFragmentLinker::AddFragments.

    // Add the compiled fragments to a list
    g_pFragmentLinker->AddFragments(    
              (DWORD*)g_pCompiledFragments->GetBufferPointer() );

ID3DXFragmentLinker::AddFragments requires a pointer to the DWORD stream that contains the compiled shader.

After compiling fragments and creating a fragment linker, there are several ways to link fragments. One way to link a vertex shader fragment is to call ID3DXFragmentLinker::LinkVertexShader. Here is an example that links two vertex shader fragments:

    // Get a handle to each fragment   
    D3DXHANDLE fragmentHandle[2];
 fragmentHandle[0] =
     (D3DXHANDLE)g_pFragmentLinker->GetFragmentHandleByName("Ambient");
 fragmentHandle[1] =
     (D3DXHANDLE)g_pFragmentLinker->GetFragmentHandleByName("AmbientDiffuseFragment");
   
    // Link the fragments together to form a vertex shader
    IDirect3DVertexShader9* pVertexShader = NULL;
    g_pFragmentLinker->LinkVertexShader( "vs_1_1", g_dwShaderFlags,
           fragmentHandle, 2, &pVertexShader, NULL );

This requires a shader compile target, the shader compile and link flags, and a handle to each of the fragments to link. If the fragments are successfully linked, ID3DXFragmentLinker::LinkVertexShader returns a vertex shader (IDirect3DVertexShader9). The vertex shader needs to be set in the effect before rendering. But before this, here's how the shader is declared in the effect:

VertexShader MyVertexShader; // Vertex shader set by the application

The effect technique contains all the state set for a pass. This pass specifies the vertex shader like this:

technique RenderScene
{
    pass P0
    {
        VertexShader = <MyVertexShader>;   
        PixelShader = compile ps_1_1 ModulateTexture();   
    }

With the effect's vertex shader created and initialized, the render code also sets the uniform constants and calls the render loop. Set the uniform constants similar to this:

    // Update the uniform shader constants.
    g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection );
    g_pEffect->SetMatrix( "g_mWorld", &mWorld );
    g_pEffect->SetFloat( "g_fTime", (float)fTime );   
Then render the effect by setting the current technique and pass:

    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    { 
        // Apply the technique contained in the effect
        UINT cPasses, iPass;
        g_pEffect->Begin(&cPasses, 0);

        for (iPass = 0; iPass < cPasses; iPass++)
        {
            g_pEffect->BeginPass(iPass);

            // Render the mesh with the applied technique
            g_pMesh->DrawSubset(0);

            g_pEffect->EndPass();
        }
        g_pEffect->End();

        pd3dDevice->EndScene();
    }

When setting uniform shader constants, it is more efficient to cache a handle to the parameter by calling ID3DXBaseEffect::GetParameterByName. This avoids the string lookup that is necessary when calling effect methods like ID3DXBaseEffect::SetMatrix.

  // Instead of setting a uniform constant like this in the render loop
  g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection );

  // Get a handle to a uniform constant outside of the render loop
  D3DXHANDLE hParameter;
  GetParameterByName( hParameter,"g_mWorldViewProjection");

  ...
 
  // Use the handle to set the uniform constant in the render loop
  g_pEffect->SetMatrix(hParameter);

无Effect版:
 LPD3DXCONSTANTTABLE pConstantTable;
    LPD3DXBUFFER pShaderBuf;
    IDirect3DVertexShader9* pVertexShader = NULL;

    // Compile the fragments to a buffer.
    D3DXGatherFragmentsFromFile( L"FragmentLinker.fx", NULL, NULL,
         g_dwShaderFlags, &g_pCompiledFragments, NULL );
   
    g_pFragmentLinker->AddFragments((DWORD*)g_pCompiledFragments->GetBufferPointer());
    g_pFragmentLinker->LinkShader(
     "vs_1_1",
     g_dwShaderFlags,
     aHandles,
     NUM_FRAGMENTS,
     &pShaderBuf,
     NULL);
    D3DXGetShaderConstantTable(
     (DWORD*)pShaderBuf->GetBufferPointer(),
     &pConstantTable );
   
    pDevice->CreateVertexShader(
     (DWORD*)pShaderBuf->GetBufferPointer(),
     &pVertexShader);
    RELEASE(pShaderBuf);

posted on 2008-05-12 19:51 bloodbao 阅读(448) 评论(0)  编辑 收藏 引用 所属分类: c++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理