I am currently trying to figure out how to do the following process in a parallel friendly manner. It can be boiled down to the following steps:
If I can do that then I should be able to render my scene. The problem is a texture can not have both read and write access. I am not sure if this has always been the case or if it is just in Metal.
So I turned to Apple's deferred rendering example knowing that deferred rendering works by writing to multiple textures and reading those same textures together to produce a final image to the screens drawable. However apon running it I got the same error I get with my current rendering code where the colorAttachment[0] does not have usage that specifies _
.
I am assuming this is due to changes in Metal or iOS.
An interesting thing to note is that it immediately crashes when attached to XCode but can do fine on its own for a few seconds before freezing if opened up from the home screen.
How do I use a texture for both read and writing access? Why is the sample code not working?
The sample code isn't working because Apple chose to make breaking changes without updating the sample. It happens.
Here's the minimal set of changes you can make to get the sample running:
On AAPLRenderer.mm
line 204, add:
shadowTextureDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
On AAPLView.mm
line 130, add:
desc.usage = MTLTextureUsageRenderTarget;
Repeat the above on lines 153 and 178.
For your use case, you'd want to use MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead
on your offscreen render target. That will allow you to render to it in one pass, and sample from it in a subsequent pass.