开发者

Flickering Mesh Problem: DirectX HLSL

The Problem: I am rendering a mesh using HLSL (High Level Shader Language) in Managed DirectX 9. Depending on the complexity of the mesh (number of vertices and faces) I sometimes get problems with flickering.

Obviously a mesh with higher complexity should take longer to render, and the amount of flickering increases with the complexity of the mesh. However, what occurs is the mesh isn't rendered at all and the time it takes to draw the mesh is significantly lower.

Increased Mesh Complexity -> Increased Flickering

The Code:

/* 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();
}

Results: This is the output I get (I inserted timers which are not included in the above code for readability):

// 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 Mes开发者_如何学Ch: 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 Code: Here is the abbreviated HLSL code, please note that this is NOT the actual code so don't dig into syntax errors, I assure you the actual code is extremely similar.

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();
    }
}

Any help / ideas / suggestions would be much appreciated. Thanks!


The solution is to use a VertexBuffer and IndexBuffer and render using DrawIndexedPrimitives

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜