16byte alignment needed?

Was trying to find out if 16byte alignments are required for shaders to work right.
ie: I did this:
struct VS_Out
{
float4 Position: POSITION0;
float2 UV: TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 worldPos: TEXCOORD2;
};

  • And it would have no problems if pass the worldPos if I subtract the CameraPos (also float3)…
  • But if I passed ONLY worldPos it would explode at the Apply() during runtime with:
    ‘Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.’
    All variables appear to be in use so that didn’t seem to be the issue.
    Then I thought maybe it needs 16byte alignment so I changed Normal to float4… suddenly everything works - so I assume it needs 16 byte alignment (ie: doesn’t pad things itself).
    Is this the case?

Sounds like you’ve got a mismatch with your data, vertex-declaration, and the shader signature.

What’s your vertex-data look like (just the struct and the declaration)?

Yah sounds like something like that.
I have this now (which works ok) but if in VS_Out (which shouldn’t matter as far as I know) I was using this:
float3 Normal: TEXCOORD1;
then… for some reason this would cause an explosion:
output.worldPos = mul(input.Position, World); // <— not ok for some odd reason
… unless I did this:
output.worldPos = mul(input.Position, World) - CameraPosition; //(is float 3 btw) <— with subtraction is ok…
Which is SOOOO weird… ( O __ o ) ’
so then I change Normal to float 4 and voila… no more weirdness… Could be something like you said with data mismatch somehow. Not sure how or where tho. That part’s a mystery.
I’m assuming the vertex declaration is set when I do this:

vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), verts.Length, BufferUsage.WriteOnly);
            
vertexBuffer.SetData<VertexPositionNormalTexture>(verts);

// and later I draw with this: 

GraphicsDevice.SetVertexBuffer(vertexBuffer);
lightFX.CurrentTechnique.Passes[0].Apply(); 
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 6); // draw vertices 0 to 5 for count of 6

Maybe the model renderer changes the declaration and I have to set it back each time?

struct VS_In 
{	 
	float4 Position: POSITION0;
	float4 Normal : NORMAL0;
	float2 UV: TEXCOORD0;	
};
struct VS_Out
{
	float4 Position: POSITION0;
	float2 UV: TEXCOORD0;
	float4 Normal: TEXCOORD1;
	float3 worldPos: TEXCOORD2;	
};

VS_Out VertShader(VS_In input) 
{
	VS_Out output;
	output.Position = mul(input.Position, WorldViewProj);	
	output.worldPos = mul(input.Position, World);
	output.UV       = input.UV;
	output.Normal   = mul(input.Normal, World);		
	return output;
}
float4 PixShader(VS_Out input) : COLOR0
{
// ... bla bla bla  ...
}

Anyway - thanks for the idea - I’m gonna see if it’s something like that.