In CG Shader, I can pass data from vertex function to surface function using this predefined struct.
structInput{
float4 pos : SV_POSITION;
float2 uv_MainTex : TEXCOORD0;
float4 numBuffer : TEXCOORD1;
//float4 numBuffer2 : TEXCOORD2; //not support some devices.
};
I need more variables to pass, But I don’t want to add TEXCOORD2.
Because It is supported OpenGL 3.0 device only.
So, I am trying to pack my float variables into one float with some data loss.
This is my code snipet. But, it’s doesn’t work. Maybe pack and unpack function have some error logic. In C#, pack/unpacking function works perfectly. In Cg, is there any differenece?
float pack_float2(float2 val)
{
return (float)(floor(10000.0f*val.x)*65536.0f + floor(10000.0f*val.y));
}
float2 unpack_float(float val)
{
float2 result;
result.x = round(val/65536.0f) / 10000.0f;
result.y = (val - 10000.0f*65536.0f*result.x) / 10000.0f;
return result;
}
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
...
//test code. meaningful data will be passed in real code.
float2 xy = float2(0.1234, 0.1234);
o.numBuffer.x = pack_float2(xy);
xy = float2(0.5566, 0.5566);
o.numBuffer.y = pack_float2(xy);
xy = float2(0.2233, 0.2233);
o.numBuffer.z = pack_float2(xy);
xy = float2(0.3344, 0.3344);
o.numBuffer.w = pack_float2(xy);
...
}
void surf(Input IN, inout SurfaceOutput o)
{
...
float2 data = unpack_float(IN.numBuffer.x);
...
}
Any Idea please.