I am trying to programmatically figure out how many "texture2d " calls the gpu can make before the fps drops below 60fps.

My current way of doing this is to have a fragment shader with a for loop that will run a specified number of times based on a passed in uniform. So each frame the for loop will run more times, each time sampling the texture more times.

Then on the cpu I can monitor the fps and see when it drops.

My problem is if this shader runs on more then one pixel then the test is worth nothing.

How can I ensure from the cpu code and vertex shader that the fragment program only runs once?

  • 2
    $\begingroup$ I don't think that is going to measure what you think it will. Not to mention fragment shaders run (at least) in a 2x2 block for each fragment. $\endgroup$ Commented Jun 27, 2016 at 10:00
  • $\begingroup$ @ratchetfreak 2x2 block per fragment even without multisampling? Why? $\endgroup$
    – Ruslan
    Commented Jun 27, 2016 at 10:12
  • 3
    $\begingroup$ @Ruslan: fragments are treated as 2x2 blocs so the derivatives can be evaluated. $\endgroup$ Commented Jun 27, 2016 at 11:09

1 Answer 1


I don't think your approach is viable for a variety of reasons.

  1. a GPU is heavily cache-dependent for performance. Pulling the same pixels over and over will just measure how fast a cache hit is. Sampling random pixels will only measure a cache miss. Neither are useful statistics.

  2. each texture2d call will pull in multiple pixels to leverage that, in a fragment shader, neighbouring fragments will want neighbouring pixels.

  3. The driver could optimize your shader to pull out the loop if it sees that each iteration does the exact same computation or if the computed result is not used.

  • $\begingroup$ Nailed it. Of course, doing anything on a single fragment is massively wasteful anyway since you will have 31 or 63 other threads idling while it completes. If i was gonna run a test like this to get at least a ballpark figure, i'd probably draw a full-window quad at say 1024^2, load up a bunch of different textures then sample each of them in turn until the framerate dropped, making sure to actually use the results like you say. $\endgroup$
    – russ
    Commented Jun 28, 2016 at 5:51
  • $\begingroup$ Thank you for this, giving up on that quest helped me realize how stupid it was to think I could take the amount of samples I do and somehow calculate a downsample size, and radius for a gaussian blur shader. Turns out it will work much better to just have predefined combinations of varying intensity and see which ones run at the desired fps. $\endgroup$
    – J.Doe
    Commented Jun 30, 2016 at 7:34
  • $\begingroup$ Are GPU compilers more innovative then CPU ones? I mean I have heard stuff about a gpu compiler recompiling because a uniform triggers a different part of an if statement and you speak of pulling out of the loop because the calculation is the same. I haven't heard of CPU's doing that. $\endgroup$
    – J.Doe
    Commented Jun 30, 2016 at 7:35
  • $\begingroup$ @J.Doe optimizers (for gpu and cpu) try to use every trick in the book however GPU optimizers know more about the program they are trying to optimize and what hardware it will be running on. The optimizations you mention are called partial evaluation (with the uniforms as additional constant data) and Loop-invariant code motion $\endgroup$ Commented Jun 30, 2016 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.