• ### What is your GameDev Story?

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

## 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);


does anyone knows a faster code?

##### Share on other sites

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 on other sites

gives 0 answers and wants my topic deleted

does someone else thinks this question is not worth an answer

##### Share on other sites

You could use a Texture1D instead of Texture2D

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 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 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 on other sites

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

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 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015291
×