DirectX - half instead of float in vertex buffer

I recently decided to compress my vertex data to make rendering more efficient, and I found a solution - using half (in particular half4 and half2 ) instead of float to store my vertex data. My vertex structure is shown below:

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

And here is my vertex declaration:

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

Everything works fine when I directly send my data as an array of MyVertex to DrawUserPrimitives.
But when I decided to put all my data in VBO to avoid excessive copy operations, my whole grid turned into a mess in pixels. I write my values ​​as indications (SlimDX shelves have a RawValue field for each component).
Here is what PIX says about my grid: crazy numbers


And here is the same part in the buffer viewer after specifying my layout (I will show only one column, since they are quite wide, hope you get this idea):
there is a nice numbers you have there
As you can see, my halves are mistakenly treated as floating. Changing float4 to half4 in the shader also does not help, it seems the problem is elsewhere. Is there any other way to tell my GPU to use my vertex buffer as half buffer, and not as a floating point buffer?

UPD:
Here is my drawing code (abbreviated):

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

And this is how this method is called (main part):

 _eff.BeginPass(0); GraphicsDevice.VertexDeclaration = vDecl; renderer.Render(GraphicsDevice, camera, world); _eff.EndPass(); 

I am using D3D9 via SlimDX with C #.

+4
source share
2 answers

I get it. I forgot to lock my buffer and call the VertexDeclaration initialization method. Such a shame for a programmer with 5 years of experience ... Thank you, it works now :)

+1
source

Conveyors for conventional graphics cards are optimized for calculating high-throughput floating-point material.

I don’t think directx can handle halves in vertexbuffers at a low level.

I support my assumptions with the documentation of Device :: CreateVertexBuffer and D3DFVF:

CreateVertexBuffer : The format is passed with the FVF parameter. Perhaps when setting this to a non-fvf buffer, you can visualize the buffer using custom shaders (but I don’t know if your infrastructure will try to do this because it can be slower). If it is nonzero:

D3DFVF : All available vertex formats with floating point only.

0
source

Source: https://habr.com/ru/post/1485189/


All Articles