Hello,
This exact question was already asked in this forum (8 years ago) but I can't find a definitive answer:
Does Metal allow using the same color texture as both an input and output (color attachment) of a fragment shader? Is the behavior defined somewhere?
I believe this results in undefined behavior under both DirectX and OpenGL, so I'd assume the same for Metal, but then why doesn't Metal warn me about this as it does on some many other "misconfigurations"? It also seems to work correctly in my case, as I found out by accident.
Would love to get a clarification!
Thanks ahead!
What I'm trying to do (simplifying a bit) is blend multiple color layers using my own blending functions into a "master" texture.
So what we have is a fragment shader taking in a bunch of inputs, among them the master texture for reading, and outputting to that same master texture as an output color attachment.
You can do your own blending in a fragment shader without this complexity. A fragment shader can read the current value of the framebuffer ("color attachment") pixel without having to make it an input texture (on iOS). In OpenGL ES, this is defined by GL_EXT_shader_framebuffer_fetch
(https://registry.khronos.org/OpenGL/extensions/EXT/EXT_shader_framebuffer_fetch.txt). In metal, I believe that you need to add [[color(0)]]
to a fragment shader input - see table 5.5 of the spec at https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf - but I've never tried this myself.