# Is sampling textures slow?

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

## Recommended Posts

I have a pixel shader for simple sprites like this:

float4 ps_Sprite( vs_out input_p ) : SV_TARGET{

return tex2D.Sample( samplerMode, input_p.uv ) * input_p.color;
} 

Should I check for alpha == 0 and return float4(0.0, 0.0, 0.0, 0.0) ? Should I also check for color (0.0, 0.0, 0.0) and return the color without sampling?

Edited by Icebone1000

##### Share on other sites
Conditional statements are likely far worse for the gpu than just crunching through, because it has to orepare to do both of the possible execution paths.

##### Share on other sites

Sampling textures is something that GPUs are very specifically designed to be good at. It's cheaper than computation in a lot of cases. It's WAY better than conditionals on SIMD architectures; you're better off doing samples and multiplying by zeros at the end than making jumps which cause a bunch of pipelines to be parked and run later. Basically at each decision, one path is taken. All the pixels in the current batch which would go the other way are parked and will be restarted when the chosen path has finished. You can see how decisions are expensive.

On MIMD GPUs, there are usually massively specialised pipelines for doing texture fetches. The individual latency isn't great on these. But you don't care. You've got millions of pixels to paint. You don't care about the latency on each paint, you care about the total pixel throughput; and that can be insane. It's not unusual for 100 or 200 pixels at a time to be doing texture fetches... each one takes 200 cycles to complete, but one completes every cycle if you can keep the pipeline loaded..

Seriously; make the fetches efficient is what keeps GPU designers awake at night, so you don't have to.

##### Share on other sites

I'm not sure you got the idea where sampling textures might be slow from - this is something that 3D accelerators have been doing since before 1996 (and we're talking D3D11 so we're talking reasonably modern hardware here, which makes the whole notion even odder).  The whole question smells heavily of pre-emptive optimization to me.

##### Share on other sites

I'm not sure you got the idea where sampling textures might be slow from - this is something that 3D accelerators have been doing since before 1996 (and we're talking D3D11 so we're talking reasonably modern hardware here, which makes the whole notion even odder).  The whole question smells heavily of pre-emptive optimization to me.

Stop assuming, I asked a very simple question, I didnt get any ideas from anywhere

"The whole question smells heavily of preemptive optimization to me"

Really? explain please, cause your post smells as wanabe all over it..cant I ask a simple question on a subject I dont know about without someone using the opportunity to say "premature ..root of evil"

Your post is so contradictory in every sense. If its so obvious ("since 1996..blablabla") how can this be premature optimization? If its obvious and I didnt know about,  I should have asked much sooner since I should know the obvious information. Katie pretty much posted everything I wanted to know and more, and than theres your useless followup there just for the sake of promoting yourself over my lack of knowledge on the subject..seriously.. its unbelievable

##### Share on other sites

I'm not sure you got the idea where sampling textures might be slow from - this is something that 3D accelerators have been doing since before 1996 (and we're talking D3D11 so we're talking reasonably modern hardware here, which makes the whole notion even odder).  The whole question smells heavily of pre-emptive optimization to me.

Stop assuming, I asked a very simple question, I didnt get any ideas from anywhere

"The whole question smells heavily of preemptive optimization to me"

Really? explain please, cause your post smells as wanabe all over it..cant I ask a simple question on a subject I dont know about without someone using the opportunity to say "premature ..root of evil"

Your post is so contradictory in every sense. If its so obvious ("since 1996..blablabla") how can this be premature optimization? If its obvious and I didnt know about,  I should have asked much sooner since I should know the obvious information. Katie pretty much posted everything I wanted to know and more, and than theres your useless followup there just for the sake of promoting yourself over my lack of knowledge on the subject..seriously.. its unbelievable

You're not being persecuted by mhagain. Were you really that offended by his post?

Taking a simple, low level operation (ordinary texture sampling) and looking for a way to speed it up despite not having found it to be an actual bottleneck in a specific instance is pretty much the definition of premature optimization. I should know, I've been guilty of it far more times than I can count.

##### Share on other sites

Sampling textures is something that GPUs are very specifically designed to be good at. It's cheaper than computation in a lot of cases.

Actually it is the contrary. Yes, sampling textures is very fast, however, computations are even faster. I can't find good reference here but ALU : Tex ratio is getting higher ie. shaders should do more calculations for each texture fetch. Nowadays even look up textures can be replaced with the actual calculations, at least in the simple cases.

It is true that conditionals may be something to avoid at some point.

Should I check for alpha == 0 and return float4(0.0, 0.0, 0.0, 0.0) ?

Only if you wish to output black pixels for texels with 0 alpha (although it would be faster to preprocess such a texture).

Should I also check for color (0.0, 0.0, 0.0) and return the color without sampling?

You mean if your input_p.color is black you'd just return the color directly. Probably the hardware would still sample the texture.

For such a short shader it is pretty useless to think for optimizations. What ever you do, the speed increase is very likely zero.

Cheers!

Edited by kauna

• 10
• 48
• 12
• 10
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999681
×