2011-08-29 36 views
0

我有一個簡單的函數,它創建一個覆蓋整個屏幕的正方形,我用它來應用後期處理效果,但是據我所知,它已經成爲無數的原因錯誤。屏幕空間正方形在PIX中看起來扭曲

當我在PIX中運行我的代碼時,我得到下面的網格,但方形應該是直的並覆蓋屏幕,不是嗎?

Mesh information from PIX capture

我的頂點着色器做任何改造和簡單地將位置信息發送至像素着色器。

創建該廣場是如下的功能:

private void InitializeGeometry() 
    { 
     meshes = new Dictionary<Vector3, Mesh>(); 
     //build array of vertices for one square 
     ppVertex[] vertexes = new ppVertex[4]; 
     //vertexes[0].Position = new Vector3(-1f, -1f, 0.25f); 

     vertexes[0].Position = new Vector3(-1, -1, 1f); 
     vertexes[1].Position = new Vector3(-1, 1, 1f); 
     vertexes[2].Position = new Vector3(1, -1, 1f); 
     vertexes[3].Position = new Vector3(1, 1, 1f); 

     vertexes[0].TexCoords = new Vector2(0, 0); 
     vertexes[1].TexCoords = new Vector2(0, 1); 
     vertexes[2].TexCoords = new Vector2(1, 0); 
     vertexes[3].TexCoords = new Vector2(1, 1); 

     //build index array for the vertices to build a quad from two triangles 
     short[] indexes = { 0, 1, 2, 1, 3, 2 }; 

     //create the data stream to push the vertex data into the buffer 
     DataStream vertices = new DataStream(Marshal.SizeOf(typeof(Vertex)) * 4, true, true); 
     //load the data stream 
     vertices.WriteRange(vertexes); 
     //reset the data position 
     vertices.Position = 0; 

     //create the data stream to push the index data into the buffer 
     DataStream indices = new DataStream(sizeof(short) * 6, true, true); 
     //load the data stream 
     indices.WriteRange(indexes); 
     //reset the data position 
     indices.Position = 0; 

     //create the mesh object 
     Mesh mesh = new Mesh(); 

     //create the description of the vertex buffer 
     D3D.BufferDescription vbd = new BufferDescription(); 
     vbd.BindFlags = D3D.BindFlags.VertexBuffer; 
     vbd.CpuAccessFlags = D3D.CpuAccessFlags.None; 
     vbd.OptionFlags = ResourceOptionFlags.None; 
     vbd.SizeInBytes = Marshal.SizeOf(typeof(Vertex)) * 4; 
     vbd.Usage = ResourceUsage.Default; 
     //create and assign the vertex buffer to the mesh, filling it with data 
     mesh.VertexBuffer = new D3D.Buffer(device, vertices, vbd); 

     //create the description of the index buffer 
     D3D.BufferDescription ibd = new BufferDescription(); 
     ibd.BindFlags = D3D.BindFlags.IndexBuffer; 
     ibd.CpuAccessFlags = D3D.CpuAccessFlags.None; 
     ibd.OptionFlags = ResourceOptionFlags.None; 
     ibd.SizeInBytes = sizeof(short) * 6; 
     ibd.Usage = ResourceUsage.Default; 
     //create and assign the index buffer to the mesh, filling it with data 
     mesh.IndexBuffer = new D3D.Buffer(device, indices, ibd); 

     //get vertex and index counts 
     mesh.vertices = vertexes.GetLength(0); 
     mesh.indices = indexes.Length; 

     //close the data streams 
     indices.Close(); 
     vertices.Close(); 

     meshes.Add(new Vector3(0), mesh); 
    } 

,當我渲染方:

private void DrawScene() 
    { 
     lock (meshes) 
     { 
      foreach (Mesh mesh in meshes.Values) 
      { 
       if (mesh.indices > 0) 
       { 
        try 
        { 
         //if (camera.SphereInFrustum(mesh.BoundingSphere, sphereRadius)) 
         //{ 
          context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(mesh.VertexBuffer, Marshal.SizeOf(typeof(Vertex)), 0)); 
          context.InputAssembler.SetIndexBuffer(mesh.IndexBuffer, Format.R16_UInt, 0); 
          context.DrawIndexed(mesh.indices, 0, 0); 
         //} 
        } 
        catch (Exception err) 
        { 
         MessageBox.Show(err.Message); 
        } 
       } 
      } 
     } 
    } 

編輯:我已經添加了頂點着色器正在運行

cbuffer EveryFrame : register(cb0) 
{ 
    float3 diffuseColor : packoffset(c0); 
    float3 lightdir : packoffset(c1); 
}; 

cbuffer EveryMotion : register(cb1) 
{ 
    float4x4 WorldViewProjection : packoffset(c0); 
    float4x4 LightWorldViewProjection : packoffset(c4); 
}; 

struct VS_IN 
{ 
    float3 position : POSITION; 
    float3 normal : NORMAL; 
    float4 col : TEXCOORD; 
}; 

struct PS_IN 
{ 
    float4 position : SV_POSITION; 
    float4 col : TEXCOORD; 
    float3 normal : NORMAL; 
}; 

PS_IN VS(VS_IN input) 
{ 
    PS_IN output; 
    output.position = float4(input.position,1); 
    output.col = input.col; 
    output.normal = input.normal; 
    return output; 
} 

這是PIX的頂點輸出。

PreVS:

preVS

PostVS:

postVS

,這裏是產生dissassembly PIX的時候我選擇調試頂點0

// 
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111 
// 
// 
// 
// Input signature: 
// 
// Name    Index Mask Register SysValue Format Used 
// ---------------- ----- ------ -------- -------- ------ ------ 
// POSITION    0 xyz   0  NONE float xyz 
// NORMAL    0 xyz   1  NONE float xyz 
// TEXCOORD    0 xyzw  2  NONE float  
// 
// 
// Output signature: 
// 
// Name    Index Mask Register SysValue Format Used 
// ---------------- ----- ------ -------- -------- ------ ------ 
// SV_POSITION   0 xyzw  0  POS float xyzw 
// TEXCOORD    0 xyzw  1  NONE float xyzw 
// NORMAL    0 xyz   2  NONE float xyz 
// 
vs_4_0 
dcl_input v0.xyz 
dcl_input v1.xyz 
dcl_output_siv o0.xyzw , position 
dcl_output o1.xyzw 
dcl_output o2.xyz 
mov o0.xyz, v0.xyzx 
mov o0.w, l(1.000000) 
mov o1.xyzw, l(1.000000, 1.000000, 1.000000, 1.000000) 
mov o2.xyz, v1.xyzx 
ret 
// Approximately 5 instruction slots used 

我也添加了輸入集成:

private void SetPPInputAssembler(Shader shader) 
     { 
      InputElement[] elements = new[] { 
       new InputElement("POSITION",0,Format.R32G32B32_Float,0), 
       new InputElement("NORMAL",0,Format.R32G32B32_Float,12,0), 
       new InputElement("TEXCOORD",0,Format.R32G32_Float,24,0), 
      }; 

      InputLayout layout = new InputLayout(device, shader.InputSignature, elements); 

      context.InputAssembler.InputLayout = layout; 
      context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; 
     } 
+0

頂點着色器是什麼樣的? – Goz

+0

我添加了頂點着色器代碼,包括當前未使用的cbuffers –

+0

Bum,毀了這個想法;) – Goz

回答

3

顯然你的頂點輸入位置不匹配你想給的值

對於第一個頂點值好看,直到紋理座標的z座標。 您正在您的程序Vertex-struct中定義Vector2D,而在VertexShader Vertex-struct中定義了Vector4D,並且事情會混淆在一起。

只是改變VS_IN這樣:

struct VS_IN 
{ 
    float3 position : POSITION; 
    float3 normal : NORMAL; 
    float2 col : TEXCOORD; // float2 instead of float4 
}; 

我不知道但如果你真的想有顏色或相當texcoords。如果你真的想有顏色float4變量將是正確的,但你不得不改變

vertexes[0].TexCoords = new Vector2(0, 0); 

vertexes[0].TexCoords = new Vector4(0, 0, 0, 0); 

無論哪種方式,這些變量之一是名不副實的,並可能是混亂的原因。

+0

我試圖改變,但它沒有影響,我害怕。當然,這與實際的頂點輸入位置有關,不是這個無關的列變量呢? –

+0

修正了它,你說的對,就是錯誤的一點。我在調用錯誤結構的draw函數中留下了一個輕微的錯字。享受你的賞金 –

相關問題