I'm working on a prototype of sorts that will use voxels, and I'm trying to make the renderer. For performance and memory preservation, I've written a struct using XNA types like this:
//32 bytes
[StructLayout(LayoutKind.Sequential)]
public struct VoxVertex
{
//12 bytes
Vector3 Position;
//8 bytes
HalfVector4 Normal;
//4 bytes
HalfVector2 UV;
//8 bytes
HalfVector4 Diffuse;
public static int Size
{
get { return 32; }
}
public static readonly VertexElement[] Format = new VertexElement[]
{
new VertexElement(0,0,VertexElementFormat.Vector3,VertexElementMethod.Default,VertexElementUsage.Position,0),
new VertexElement(0,12,VertexElementFormat.HalfVector4,VertexElementMethod.Default,VertexElementUsage.Normal,0),
new VertexElement(0,20,VertexElementFormat.HalfVector2,VertexElementMethod.Default,VertexElementUsage.TextureCoordinate,0),
new VertexElement(0,24,VertexElementFormat.HalfVector4,VertexElementMethod.Default,VertexElementUsage.Color,0)
};
}
I'm not entirely sure of how to get the data in the shader. If it were normal floats and vectors, I would have no problem, but it's not. Would it be alright to write a shader as if it's going to be accessing normal float data, or will I need to use half types, and somehow convert them to float?
If it's for memory saving Ok. If it's for performance, forget about it since in CPU there's no native support for 16-bit floats. Operations must be emulated. Most likely even to fill your values, you'll have to fill the Half16 by converting floats.
As for sending that data to the GPU, it's going to be difficult as IIRC the API only allows to send halfs directly by using the vertex declarations D3DDECLTYPE_FLOAT16_4 or D3DDECLTYPE_FLOAT16_2. And judging from the documentation it may not be super fast as the FLOAT16_2 version is expanded to 4 halfs. I don't know if XNA gives such low level access.
You can also try using (Vertex) Textures to feed such data, and it works quite well, mostly because in that case the API just copies memory regardless of the data type and is then interpreted in the GPU (where halfs are well supported). Though be aware not all GPUs work fast with VTF (vertex texture fetch). It's particularly slow in pre-Geforce 8000 series, and doesn't work in pre-Radeon HD series.
If it's for memory saving Ok. If it's for performance, forget about it since in CPU there's no native support for 16-bit floats. Operations must be emulated. Most likely even to fill your values, you'll have to fill the Half16 by converting floats.
It's for memory. Technically, I only need the position and the Texture coordinates. The Diffuse is for lighting (which I don't plan on implementing yet anyway). The vertices won't be updated too often, and XNA supports constructors for half vectors. The XNA side of it is fine, it's the HLSL side that I'm unsure of. Ideally, I'd like to be able to store the Diffuse as a 16-bit unsigned integer, but I'm unsure of how to be able to do that with XNA and HLSL. (there will only be one color of light, so the value will be a grayscale value)
I'm not entirely sure of how to get the data in the shader. If it were normal floats and vectors, I would have no problem, but it's not. Would it be alright to write a shader as if it's going to be accessing normal float data, or will I need to use half types, and somehow convert them to float?
You can use float types, and the GPU will automatically convert them to 32-bit float for the vertex shader.
[quote name='Nyxenon' timestamp='1334411063' post='4931186']
I'm not entirely sure of how to get the data in the shader. If it were normal floats and vectors, I would have no problem, but it's not. Would it be alright to write a shader as if it's going to be accessing normal float data, or will I need to use half types, and somehow convert them to float?
You can use float types, and the GPU will automatically convert them to 32-bit float for the vertex shader.
[/quote]
So what you're saying is that for the above structure that I provided, I could write the following in HLSL?
struct VoxVertexIn
{
float4 Position : POSITION;
float4 Normal : NORMAL;
float2 UV : TEXCOORD;
float4 Color : COLOR;
};
[quote name='MJP' timestamp='1334432743' post='4931248']
[quote name='Nyxenon' timestamp='1334411063' post='4931186']
I'm not entirely sure of how to get the data in the shader. If it were normal floats and vectors, I would have no problem, but it's not. Would it be alright to write a shader as if it's going to be accessing normal float data, or will I need to use half types, and somehow convert them to float?
You can use float types, and the GPU will automatically convert them to 32-bit float for the vertex shader.
[/quote]
So what you're saying is that for the above structure that I provided, I could write the following in HLSL?
struct VoxVertexIn
{
float4 Position : POSITION;
float4 Normal : NORMAL;
float2 UV : TEXCOORD;
float4 Color : COLOR;
};
[quote name='Nyxenon' timestamp='1334437414' post='4931263']
[quote name='MJP' timestamp='1334432743' post='4931248']
[quote name='Nyxenon' timestamp='1334411063' post='4931186']
I'm not entirely sure of how to get the data in the shader. If it were normal floats and vectors, I would have no problem, but it's not. Would it be alright to write a shader as if it's going to be accessing normal float data, or will I need to use half types, and somehow convert them to float?
You can use float types, and the GPU will automatically convert them to 32-bit float for the vertex shader.
[/quote]
So what you're saying is that for the above structure that I provided, I could write the following in HLSL?
struct VoxVertexIn
{
float4 Position : POSITION;
float4 Normal : NORMAL;
float2 UV : TEXCOORD;
float4 Color : COLOR;
};