简体   繁体   中英

Shared variables in Fragment shaders in Metal or OpenGLES

This might be unlikely, but is there a concept of shared global variables that can be updated in each pass of fragment shaders in either Metal & OpenGLES? I want to update some statistics after processing each pixel. I assume there would be lot of issues of concurrency as each fragment shader runs in parallel.

Something like an atomic_uint? I needed to do something like that recently and that's what I used. See the answer to my question here: How to implement/use atomic counter in Metal fragment shader?

Atomic performance from fragment shaders is likely to suck to be honest, as you'll get lots of parallel reads and writes from multiple shader cores as the shaders will be very short if you go for the naieve 1 fragment per input texel approach.

The usual implementation for this is to encode the histogram as a framebuffer.

Read from the texture in a vertex shader and place a single point at position which matches the "histogram" coordinate.

Histogram can be accumulated using blend operations.

Read back the histogram on to the CPU using glReadPixels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM