Sign in to follow this  
RobMaddison

Getting different types into a vertex shader

Recommended Posts

Hello everyone I have a vertex buffer that contains floats, but I don't need all that precision - WORD types would suffice, so I'm trying to reduce my buffer by half by using WORDs instead of floats. My current shader works pretty well, by splicing together x and z coordinates from one vertex buffer and the y coordinate from the next buffer. If I switch the y buffer content to WORDs, and leave my shader as is, I get very unexpected results (lots of random heights). Does anyone know if it's possible for a buffer of WORDs to be passed to the vertex shader (to be cast to floats when they get there)? The [simplified] input into my shader is:
V_to_P TerrainLayerVS(float3 positionXZ : POSITION, float3 positionY)
and my vertex declaration looks like this:
...
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{1, 0, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
...
It's not a major issue if I can't, but it would save a fair amount of memory. Thanks

Share this post


Link to post
Share on other sites
This line says "I have 3 floats for position":
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
So if you have WORDs, or types other than floats, that won't work. You'll probably want to use D3DDECLTYPE_SHORT4, and have 4 WORDs for position. Or something similar.

Share this post


Link to post
Share on other sites
I changed it to this:

...
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{1, 0, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
...


And it worked straight away - 16mb saved in 2 seconds. I'll do it for the other buffer too.

Thanks for the quick answer - bumped your rating.

Share this post


Link to post
Share on other sites
Quote:
Original post by RobMaddison
I changed it to this:

*** Source Snippet Removed ***

And it worked straight away - 16mb saved in 2 seconds. I'll do it for the other buffer too.

Thanks for the quick answer - bumped your rating.
I'm still not sure what you're doing here - that's reading 3 floats from the first stream for position, and two shorts from the second stream for texture coordinates - is that what you want? I'm not sure why it works...

Share this post


Link to post
Share on other sites
Yes that's right. The first buffer contains floats which represent x, y and z - except the y is zero. There is only one of these buffers which represents a terrain patch (which is translated for each patch, etc).

The second buffer contains heights as WORD values. When I set the VertexStream for this one, I specify sizeof(WORD) which in the vertex shader expands to SHORT, SHORT, 0, 0 (the second short is zero).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this