Skip to main content
GameDev.net gamedev.net
🔒 Locked

Converting UINT to float4 in HLSL

Started by ErnstH Nov 3, 2014 at 1:10 PM 2 replies 11.3k views
Original Post
ErnstH
ErnstH

My mesh vertices are stored this way:

[source]

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0,DXGI_FORMAT_R32G32B32_FLOAT, 0,0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{"COLOR", 0, DXGI_FORMAT_R32_UINT, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },

};

[/source]

It is presented to my shaders this way:

[source]

struct VS_INPUT{
float4 pos: POSITION;
float4 normal: NORMAL;
uint col: COLOR;
float2 tex: TEXCOORD0;
float4 tangent: TANGENT;
};

[/source]

The 3D vectors are magically converted into 4D with the w component set to 1. No idea where this is done, but it works.

I wonder if there's a similar trick to convert my UINT into a float4. The rest of my HLSL code needs colours as float4 so it has to be converted somewhere.

This is the function I use now. It works fine but something tells me that it is, as MJP would say, "extremely abnormal" and "off the beaten path". There must be a more efficient and cleaner way.

[source]
float4 MGetVertexColour(uint inCol){
if(inCol==0xffffffff) return float4(1,1,1,1);

float a=((inCol & 0xff000000) >>24);
float r=((inCol & 0xff0000) >> 16);
float g=((inCol & 0xff00) >> 8);
float b=((inCol & 0xff));
return float4(r,g,b,a)/255.0;
}

[/source]

Mona2000
Mona2000
Change
[source]{"COLOR", 0, DXGI_FORMAT_R32_UINT, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },[/source]
to
[source]{"COLOR", 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },[/source]
and
[source]uint col: COLOR;[/source]
to
[source]float4 col: COLOR;[/source]
ErnstH
ErnstH

Oh wow, that's great. Thank you!!! Love this forum.

unbird
unbird

The 3D vectors are magically converted into 4D with the w component set to 1. No idea where this is done, but it works.

AFAIK it's not documented for D3D 10/11 (but it is for D3D9).

Any three-component vector (x,y,z) will be expanded to (x,y,z,1). Similarily for two components : (x,y) -> (x,y,0,1).

So it's actually legal to have less components in the input layout than in the vertex shader input signature.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.