C# 闪烁网格问题:DirectX HLSL
问题:我正在使用托管DirectX 9中的HLSL(高级着色器语言)渲染网格。根据网格的复杂性(顶点和面的数量),我有时会遇到闪烁问题 显然,复杂度越高的网格渲染时间越长,闪烁的数量也会随着网格的复杂度而增加。但是,出现的情况是网格根本没有渲染,并且绘制网格所需的时间显著减少C# 闪烁网格问题:DirectX HLSL,c#,hlsl,flicker,directx-9,managed-directx,C#,Hlsl,Flicker,Directx 9,Managed Directx,问题:我正在使用托管DirectX 9中的HLSL(高级着色器语言)渲染网格。根据网格的复杂性(顶点和面的数量),我有时会遇到闪烁问题 显然,复杂度越高的网格渲染时间越长,闪烁的数量也会随着网格的复杂度而增加。但是,出现的情况是网格根本没有渲染,并且绘制网格所需的时间显著减少 增加网格复杂度->增加闪烁 代码: /* Before this is called: - The Device has been set up successfully - The Effects
增加网格复杂度->增加闪烁
代码:
/* 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();
}
任何帮助/想法/建议都将不胜感激。谢谢 解决方案是使用
VertexBuffer
和IndexBuffer
并使用DrawIndexedPrimitives
// 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
.
.
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();
}
}