闪烁网格问题:DirectX HLSL

时间:2010-12-30 00:34:07

标签: c# hlsl flicker directx-9 managed-directx

问题:我正在使用Managed DirectX 9中的HLSL (高级着色器语言)渲染网格。取决于网格的复杂性(数字)顶点和面)我有时会遇到闪烁问题。

显然,具有更高复杂度的网格渲染需要更长时间,并且闪烁量随着网格的复杂性而增加。但是,出现的情况是网格未完全呈现 并且绘制网格所需的时间明显更低

Increased Mesh Complexity -> Increased Flickering

守则

/* Before this is called: 
     - The Device has been set up successfully
     - The Effects have been compiled successfully
     - The RenderState is correct, etc.. etc..
*/

public void Draw(Device device, MyMeshClass mesh)
{
    // Set vertex/index buffers (this has been timed, negligible)
    CustomVertex.PositionNormalTextured[] vb = mesh.Vertices;
    short[] ib = mesh.Indices

    // Set shader parameters (this has been timed, negligible)
    this.effect.SetValue("texture", mesh.Texture);
    this.effect.SetValue("color", mesh.Color);
    this.effect.SetValue("worldViewProj", mesh.ViewProjection);

    int passes = this.effect.Begin(0);
    for (int i = 0; i < passes; ++i)
    {
        this.effect.BeginPass(i);
        device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, ib.Length, ib.Length / 3, ib, true, vb);
        this.effect.EndPass();
    }
    this.effect.End();
}

结果:这是我得到的输出(为了便于阅读,我插入的计时器未包含在上面的代码中)

// This is a Copy/Paste from the debug output
.
.
Draw Mesh: 1.2851 ms
Draw Mesh: 1.3882 ms
Draw Mesh: .4819 ms     // <-- It flickered here
Draw Mesh: 1.3638 ms
Draw Mesh: 1.4972 ms
Draw Mesh: 1.4262 ms
Draw Mesh: 1.2239 ms
Draw Mesh: 1.6684 ms
Draw Mesh: 1.6963 ms
Draw Mesh: 2.3397 ms
Draw Mesh: 1.3484 ms
Draw Mesh: 1.4012 ms
Draw Mesh: .5287 ms     // <-- It flickered here
Draw Mesh: .4644 ms     // <-- It flickered here
Draw Mesh: 1.6140 ms
Draw Mesh: 1.4392 ms
Draw Mesh: 1.4579 ms
Draw Mesh: .4987 ms     // <-- It flickered here
Draw Mesh: 1.4873 ms
.
.

HLSL代码:这是缩写的 HLSL代码,请注意这不是实际代码,所以不要深入研究语法错误,我向你保证代码非常相似。

Texture texture;
sampler TextureSampler; // There is more here, I cut it out to save space
float4x4 worldViewProj : WorldViewProjection;
float4 color;


float4 mainVS(float4 pos : POSITION) : POSITION
{
    float4 Out = mul(pos, worldViewProj);           

    return Out; 
}

float4 mainPS(float4 pos : POSITION) : COLOR
{
    float4 Out = tex2D(TextureSampler, pos.xy);

    return Out;
}


technique Standard
{
    pass One
    {
        CullMode = CCW;
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;

        VertexShader = compile vs_2_0 mainVS();
        PixelShader = compile ps_2_0 mainPS();
    }
}

非常感谢任何帮助/想法/建议。谢谢!

1 个答案:

答案 0 :(得分:0)

解决方案是使用VertexBufferIndexBuffer并使用DrawIndexedPrimitives

进行渲染