Based on an example in Metal shading language spec:
Example:
struct Foo {
float4 a [[color(0)]];
int4 b [[color(1)]];
};
kernel void
my_kernel(imageblock<Foo, imageblock_layout_implicit> img_blk,
ushort2 lid [[thread_index_in_threadgroup]] …)
{
…
Foo f = img_blk.read(lid); float4 r = f.a;
…
f.a = r;
…
img_blk.write(f, lid);
}
As the members of the imageblock alias color attachment I think after "imgblk.write(...)" the imageblock will be written to corresponding color attachment.
I experimented this on Apple's example: Forward-plus with tile shading, I tried to use imageblock.write(..) to write to the color attachments in the tile shader but I got very weird results:
- only pixels of the background are changed, but the result is too much darker than what I set, e.g. I set color=float4(1,0,0,1) but on screen it's float4(0.057, 0, 0, 1)
- color of other parts strangely depends on whether/what is written to the imageblock in the previous fragment shader pass, consider I set the values in the imageblock to constants.
Anyway it feels like imageblock.write() doesn't work right in tile shader. Or how to use it correctly?