I just need to render multiple objects simultaneously. But I am getting an error as mention. Here I am passing multiple objects to my render func.

 var sceneObject:Array<Node> = [objectToDraw,temObjectToDraw] 

       for scene in sceneObject{

                  scene .render(commandQueue: commandQueue, pipelineState: pipelineState, drawable: drawable,viewportSize:viewPortSize, clearColor: nil/*,texture: texture*/)
                }

where render() class is as follows

 func render(.....) {  
    ...  
    commandBuffer.present(drawable)
    commandBuffer.commit()
    }  

But I'm getting log error message:

[CAMetalLayerDrawable texture] should not be called after already presenting this drawable. Get a nextDrawable instead.

Does someone have a clue?

2

There are 2 best solutions below

0
On

The following is my the modified code, no error tips 【CAMetalLayerDrawable present] should not be called after already presenting this drawable. Get a nextDrawable instead.】, hope it helps you.

-(void)startRender()  {         
       dispatch_block_t renderBlock = ^ { @autoreleasepool {
          ......
          ......
          id<MTLTexture> drawingTexture =  metalView.currentDrawable.texture;
          ......
          passDescriptor.colorAttachments[0].texture = drawingTexture;
          ......
          ......
          [renderEncoder popDebugGroup];
          [renderEncoder endEncoding];
          [commandBuffer presentDrawable:[(CAMetalLayer*)metalView.layer.nextDrawable]];
          [commandBuffer commit];
       }};

      ([NSThread isMainThread])?renderBlock():dispatch_async(dispatch_get_main_queue(), renderBlock);
}

It may be in other background threads before rendering, when GPU start render, used dispatch_async(get_main), make sure render work in main thread.

Show the error tips because: You need to pass "metalView.layer.nextDrawable" instead of "metalView.currentDrawable" parameters when commandBuffer calls presentDrawable.

0
On

I ran into this recently. In my case, the problem was that the MTKView.draw() command was being executed on a different thread from the buffer set up. I had been dispatching to a background thread to do some Core Image work, then dispatching back to the main thread to draw (since MTKView inherits from UIView and cannot be called safely off the main thread).