I have prepared an 'improvement suggestion' for FMX.

I have prepared an 'improvement suggestion' for FMX.Types3D.TVertexBuffer, verified with 64 lines of test code in a console app. A corner case, nothing urgent, but it can make it easier to debug things.

Description:

1) TVertexBuffer is part of TMeshData which is part of TCustomMesh, which is a TShape3D, which has a TMaterialSource, which has a TMaterial, which contains the shader byte code for the VertexShader and PixelShader.

2) It is typical that user code updates Vertices, TextCoord0 and Normals of the TVertexBuffer. Normals are also updated implicitely when user code calls TMeshData.CalcFaceNormals.

P: TPoint3D;
T: TPointF;
Data.VertexBuffer.Vertices[Index] := P;
Data.VertexBuffer.TexCoord0[Index] := T;
Data.VertexBuffer.Normals[Index] := P;

4) Assume that the developer creates a new custom Material which updates Position, TexCoord and Normal as part of the VertexShader. It is no longer needed to compute TexCoords and Normals on the CPU. Only the initial position data will be filled in and uploaded to the GPU, and only when changed.

5) TVertexBuffer has a ChangeFormat method which can be used to optimize the layout of the TVertexBuffer such that space is no longer wasted for unused TVertexFormat components. TexCoord0 will be removed from the format / buffer layout.

6) It is of course wrong to assign to TexCoord0 if TexCoord0 is no longer part of the format.

7) Existing code does not change when the new vertex shader code is developed. There will still be assignments to Data.VertexBuffer.TexCoords0. And it would be convenient to allow this, for backward compatibility.

8) In this situation it would be cool if TVertexBuffer.SetTexCoord0 would check if TexCoord0 is actually part of the format and, in my opinion, exit silently with no damage done.

9) Right now the property setter will write over memory reserved for other components of the TVertexBuffer, or beyond the limits of the VertexBuffer, without raising an exception.

10) Snippets from the test project, I can paste the full text later in a comment:

vf := [TVertexFormat.Vertex];
Data.ChangeFormat(vf);
Data.VertexBuffer.Length := 2;

P := TPoint3D.Create(1, 2, 3);
T := TPointF.Create(4, 5);

Data.VertexBuffer.Vertices[0] := P;
Data.VertexBuffer.Vertices[1] := P;
Data.VertexBuffer.TexCoord0[0] := T;

P := Data.VertexBuffer.Vertices[1];
System.Writeln(P.X, P.Y, P.Z);

//should print 1 2 3, prints 4 5 3

Comments