MTLClearColor not working

Hi,


I'm trying to set a background colour for my metal scene. I'm using:


descriptor.colorAttachments[0].clearColor = MTLClearColorMake(Double(newColourComponenets.r), Double(newColourComponenets.g), Double(newColourComponenets.b), 1.0)


Construction of a background colour using MTLClearColor(r:g:b:) gives the same outcome.


When I set


descriptor.colorAttachments[0].loadAction = .load


then the background colour is rendered, but changing it to .clear means I just get a black background.


I note from here that there is a circumstance when the background colour is ignored, but due to poor writing, I can't fathom when that is. Does anyone know....?

  • In 2023, I see neither clearColor nor loadAction under descriptor.colorAttachments[0]. Is there an alternative or will I just have to draw a full-screen polygon?

  • Found it! .clearColor and .loadAction are under drawInMTKView (MTLRenderPassDescriptor*).colorAttachments[0], not under _loadMetalWithView (MTLRenderPipelineDescriptor*).colorAttachments[0]

Add a Comment

Accepted Reply

Found the answer:


the MTLClearColor has to be set before the makeCommandBuffer() and makeRenderCommandEncoder(description:) is called.

Replies

Found the answer:


the MTLClearColor has to be set before the makeCommandBuffer() and makeRenderCommandEncoder(description:) is called.