Passing a byte to shader (SlimDX, HLSL)

Started by
4 comments, last by Tubos 14 years, 1 month ago
Hi, I need to pass a byte (value 0-255) to a vertex shader. I'm using C# and SlimDX, but that's not important. As I understand it, I can use the Usage Color. Four byte values get transformed to floats (range 0-1). Inside the Shader, I can get the original values by multiplying with 255.

struct Vertex
{
	[..]
	public byte MyByteValue, Reserved1, Reserved2, Reserved3;
}
Vertex declaration:
VertexElement[] elements = new VertexElement[]
		{	[..],
			new VertexElement(0, 48, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),	
			new VertexElement(0, 52, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 1),
			VertexElement.VertexDeclarationEnd
		};
VS_OUTPUT VertexShader (..., float4 data: COLOR1)
{
	[..]
	int MyByteValue = (int)(data[0] * 255.0);
}
The Problem: Inside the shader, MyByteValue is always zero. Does anyone have an idea where the error in this float<>byte conversion is? [Edited by - Tubos on February 17, 2010 3:47:38 PM]
Advertisement
Shouldn't you use .x instead of [0] ?
------------------------------Great Little War Game
How do you actually fill the vertex buffer? It's possible the byte you are looking for isn't in the component you are looking at.
Quote: Shouldn't you use .x instead of [0] ?
Same result.

I'm filling the vertex buffer with:
Vertex[] vertices;DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None);stream.WriteRange(vertices);vertexBuffer.Unlock();stream.Dispose();

When calling WriteRange, the byte value inside the Vertex[]-Array is correct. I've checked it: the values are all >0.


Here is my test code inside the Vertex Shader:
VS_OUTPUT Main( float4 startPosition : Position0, [..], float4 color: Color0,float4 variousData: Color1 ){[..]int myByteValue = (int)(variousData.x*255.0);	if (myByteValue>0)	{		Output.Diffuse.a=0;	}
So if the value (which is >0) gets passed correctly, the geometry should disappear. But it doesn't. ;-)
Setting alpha to 0 will only remove the pixel if alpha blending is enabled, and the pixel shader does something with that per vertex alpha value (which will get interpolated between vertices).

Can you post the whole shader (in [ source ] tags)?

You might find the clip function more useful as that doesn't require alpha blending to kill pixels. However you can only use it from the pixel shader.
adam41, Thanks for replying.

Quote:Setting alpha to 0 will only remove the pixel if alpha blending is enabled, and the pixel shader does something with that per vertex alpha value (which will get interpolated between vertices).
I checked that by changing the code to:
if (MyByteValue==0) // instead of: "MyByteValue>0"{	Particle.Diffuse.a=0;}
and now the geometry disappears. That means MyByteValue is definitely zero and the test code is correct.

Quote:Can you post the whole shader (in [ source ] tags)?
Here's an outline of my shader:
VS_OUTPUT Main( float4 startPosition : Position0,float4 startVelocity: Position1,float1 lifetime: TEXCOORD0,float1 illuminationTime: TEXCOORD1,float1 brightness: TEXCOORD2,float1 particleSize: TEXCOORD3,float1 timeOffset: TEXCOORD4,float1 mass: TEXCOORD5,float4 color: Color0,float4 variousData: Color1{	float localTime=Time+timeOffset;	VS_OUTPUT Particle;	if (localTime>lifetime || localTime<illuminationTime)	{		Particle.Position.xyzw=-12345;		Particle.Diffuse=(float4)1;		Particle.Size=Particle.tex0=0;	}	else	{		Particle.Position = ...;		Particle.Position = mul(Particle.Position, WorldViewProjection);		Particle.Diffuse = ...;		Particle.Size = 3;				int myByteValue = (int)(variousData.x*255.0);		if (myByteValue==0)		{			Particle.Diffuse.a=0; // if I comment out this line, the geometry is visible.		}			}		return Particle;}

This shader makes the geometry disappear. If I comment out the test code (see above), the geometry is visible. IMHO, myByteValue must be zero.

And my vertex declaration is correct, isn't it?
.., new VertexElement(0, 48, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),	new VertexElement(0, 52, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 1), ..
Color takes 4 bytes, and 48+4 = 52.

I tried using the TEXCOORD semantic, but same result. I also tried reversing Color0 and Color1 inside the vertex structure.

Any other ideas?

This topic is closed to new replies.

Advertisement