Jump to content
  • Advertisement
Sign in to follow this  
lomateron

change 1D addres to 2D addres

This topic is 2020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I do this change a lot in my shaders and this is what I use

 

uint  width = w; //w is always a float
uint  1d    = x; //x is always a float
uint3 2d    = 0;
2d.xy = uint2(1d%width, 1d/width);
texel = Tex2D.Load(2d);

does anyone knows a faster code?

 

Share this post


Link to post
Share on other sites
Advertisement

Do you expect someone to have a faster method ? Do you have a performance issue with this code ?
 

If you are stressing over optimizations with code like this, I'll be surprised if you ever get anything done, really!

Just write your code, there is nothing wrong with your code unless you know there is something wrong. Especially this code, it's about as simple as it gets. There are a couple of ways to do it, but they boil down to the same thing, a few ops, which is totally not worth worrying about.

Come back when you have a function that's taking 10,000 ticks to complete, then we can talk about optimization.

Share this post


Link to post
Share on other sites

You could use a Texture1D instead of Texture2D wink.png

 

Why are w/x always floats?

 

Integer division is extremely slow, it might be faster to use float division and/or fmod.

 

If w is a power of two, then you can optimize hugely though by replacing the % with a mask (&) and the / with a shift (>>).

Share this post


Link to post
Share on other sites

You could use a Texture1D instead of Texture2D: x is not defined by a texture

Why are w/x always floats?: because that how it is

 

I though you people used this conversion a lot too, and already have one at hand to exchange like:

ohh in nvidia hardware I will use this code... but in AMD ones I will use this one that works anywhere

Share this post


Link to post
Share on other sites
If w is a power of two, then you can optimize hugely though by replacing the % with a mask (&) and the / with a shift (>>).

An extension to this suggestion: If x need not to be continuous but is a kind of packed address, w can be chosen "arbitrarily" as a suitable constant (probably 12 bits in the case that x is a float, because it provides 24 bits in its mantissa). This would allow for 4k by 4k textures at most.

Edited by haegarr

Share this post


Link to post
Share on other sites


Why are w/x always floats?: because that how it is
...I was asking because by giving us some more information, you might get better answers...

Is there a need for w to be float? Is it ever fractional; do you have 2d arrays with width 11.25?

 

What kind of resource are you Loading from? Is there a requirement for it to be a specific kind of D3D/GL resource - does it have to be 2 dimensional?

Share this post


Link to post
Share on other sites

w and x are never fractional and can have any value from 0 to very big number

 

I cannot change this line: texel = Tex2D.Load(...);

Edited by lomateron

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!