1

I need to have some variable/object in the graphics memory that can be accessed within my fragment shader and within my normal C# code. Preferably a 16Byte vec4

What I want to do:

  1. [In C#] Read variable from graphic memory to cpu memory
  2. [In C#] Set variable to zero
  3. [In C#] Execute normal drawing of my scene
  4. [In Shader] One of the fragment passes writes something to the variable (UPDATE)
  5. Restart the loop

(UPDATE) I pass the current mouse coordinates to the fragment shader with uniform variables. The fragment shader then checks if it is the corresponding pixel. If yes it writes a certain color for colorpicking into the variable. The reason I dont write to a FS output is that I simply didn't find any solution on the internet on how to get this output into my normal memory. Additionaly i would have an output for each pixel instead of one

What I want is basically a uniform variable that a shader can write to.

Is there any kind of variable/object that fits my needs and if so how performant will it be?

5
  • "One of the fragment passes writes something to the variable" You're going to need to explain this in greater detail. What does it write? How do you isolate a single FS invocation? Do multiple invocations need to write to it, or do they write to different locations? Is there some reason you can't just write to a FS output? Commented Nov 10, 2016 at 23:40
  • I pass the current mouse coordinates to the fragment shader with uniform variables. The fragment shader then checks if it is the corresponding pixel. If yes it writes a certain color for colorpicking into the variable. The reason I dont write to a FS output is that I simply didn't find any solution on the internet on how to get this output into my normal memory. Additionaly i would have an output for each pixel instead of one Commented Nov 10, 2016 at 23:42
  • Put this information in your question. Commented Nov 11, 2016 at 0:02
  • why not write to 1x1 texture in that pass? instead of writing to screen ... You can read the texture to CPU memory after that pass easily Commented Nov 11, 2016 at 10:01
  • Sounds good. I will give it a try later. Commented Nov 11, 2016 at 11:59

1 Answer 1

3

A "uniform" that your shader can write to is the wrong term. Uniform means uniform (as in the same value everywhere). If a specific shader invocation is changing the value, it is not uniform anymore.

You can use atomic counters for this sort of thing; increment the counter for every test that passes and later check for non-zero state. This is vastly simpler than setting up a general-purpose Shader Storage Buffer Object and then worrying about making memory access to it coherent.

Occlusion queries are also available for older hardware. They work surprisingly similarly to atomic counters, where you can (very roughly) count the number of fragments that pass a depth test. Do not count on its accuracy, use discard in your fragment shader for any pixel that does not pass your test condition and then test for a non-zero fragment count in the query readback.


As for performance, as long as you can deal with a couple frames worth of latency between issuing a command and later using the result, you should be fine.

If you try to use either, an atomic counter or an occlusion query and read-back the result during the same frame, you will stall the pipeline and eliminate CPU/GPU parallelism.

I would suggest inserting a fence sync object in the command stream and then checking the status of the fence once per-frame before attempting to read results back. This will prevent stalling.

Sign up to request clarification or add additional context in comments.

2 Comments

Too late. Already implemented SSBO and it works very well. :-)
I would suggest going the atomic counter route to be completely honest. All you are doing here is counting the number of fragments that passed, and atomic counters relax some rules that you would need to obey with SSBOs (in particular, ensuring memory coherency). Anything that supports SSBOs will support them, and it's best to use the thing that was designed specifically for this purpose when you have the chance :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.