1

问题:我在 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();
    }
}

任何帮助/想法/建议将不胜感激。谢谢!

4

1 回答 1

0

解决方案是使用VertexBufferIndexBuffer渲染使用DrawIndexedPrimitives

于 2011-01-06T21:33:56.543 回答