最近我决定压缩我的顶点数据以使渲染更加高效,我找到了一个解决方案 - 使用half(具体来说是half4和half2)而不是float来存储我的顶点数据。我的顶点结构如下:
[StructLayout(LayoutKind.Sequential)]
public struct MyVertex
{
public Half4 Position; //8 bytes
public Half4 Normal; //8 bytes
public Half2 UVW; //4 bytes
public Half4 TextureID; //8 bytes
public Half4 BlendFactor; //8 bytes
public const int SizeInBytes = (2 * 4) * 4 + (2 * 2);
}
这是我的顶点声明:
MyVertexDecl = new VertexDeclaration(device,
new VertexElement[6]
{
new VertexElement(0, 0, DeclarationType.HalfFour, DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, 8, DeclarationType.HalfFour, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
new VertexElement(0, 16, DeclarationType.HalfTwo, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
new VertexElement(0, 20, DeclarationType.HalfFour, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
new VertexElement(0, 28, DeclarationType.HalfFour, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
VertexElement.VertexDeclarationEnd
});
当我直接将数据作为MyVertex数组发送到DrawUserPrimitives时,一切都正常。但是,当我决定将所有数据放入VBO中以避免过多复制操作时,我的所有网格都变成了乱码。我将值写为ushorts(SlimDX halfs每个组件都有RawValue字段)。
这是PIX关于我的网格的显示:
下面是在指定布局后缓冲区查看器中相同部分的显示(仅显示一列,因为它们很宽,希望您能理解):
可以看出,我的halfs被错误地视为floats。在着色器中将float4更改为half4也没有帮助,似乎问题出在其他地方。有没有另一种方法可以告诉GPU将我的顶点缓冲区用作halfs缓冲区而不是floats缓冲区?
更新:
这是我的绘图代码(简化):
public void Render(GraphicsDevice device, ICamera camera, MyModel model)
{
//there are some SetValue calls
model.Effect.CommitChanges();
foreach (D3D9.VertexBuffer buf in model.Meshes)
{
device.SetStreamSource(0, buf, 0, MyVertex.SizeInBytes);
device.DrawPrimitives(D3D9.PrimitiveType.TriangleList, 0, mesh.VertexCount);
}
}
以下是该方法的调用方式(主要部分):
_eff.BeginPass(0);
GraphicsDevice.VertexDeclaration = vDecl;
renderer.Render(GraphicsDevice, camera, world);
_eff.EndPass();
我正在使用C#通过SlimDX来使用D3D9。
data.indices.Count()
,应该是顶点的数量吧?并且您解锁了缓冲区吗? - Gnietschow