Graphics Reference
In-Depth Information
Two methods of defining the correct structure are using padding fields or explicitly defining
the structure layout. It is important to note that the total size of the structure must be
divisible by 16 bytes. Therefore, when using padding fields, you may actually need to use
one of these methods at the end of the structure, as in CorrectStructA in the following
code. The explicit layout requires the field offsets to be provided and that the size takes into
consideration the 4 bytes at the end, as shown in CorrectStructB .
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct CorrectStructA {
public float a;
public float b;
Vector2 _padding0;
public Vector3 c;
float _padding1;
}
[StructLayout(LayoutKind.Explicit, Size = 32 )]
struct CorrectStructB {
[FieldOffset(0)]
public float a;
[FieldOffset(4)]
public float b;
[FieldOffset(16)]
public Vector3 c;
}
The problem with using the explicit layout is that it can be difficult to maintain and can easily
go wrong; therefore, we have used the padding approach in our code. When trying to use an
incorrect structure, you will have bleeding of values between fields; alternatively, in the case
of an incorrect total size, you will receive a The parameter is incorrect error message when
attempting to create the buffer.
It is also possible to control the packing within the HLSL structures themselves using the
packoffset HLSL keyword.
See also
F Chapter 1 , Getting Started with Direct3D , shows how to get started with the Visual
Studio graphics debugger
F The following link provides more information on the why and how of the inverse
transpose matrix for normal transformations: http://www.arcsynthesis.org/
gltut/Illumination/Tut09%20Normal%20Transformation.html
 
Search WWH ::




Custom Search