Page 1 of 1

Metal (MTLRenderDevice) integration

Posted: 23 Aug 2020, 00:08
by mdube
Hi,

We have been successfully using the GLRenderDevice with our own OpenGL context (thus not using GLRenderContext), by following documentation and calling the various methods and taking care of saving/restore gpu states where appropriate. Ending with something like:
void NoesisGUINode::update(float delta)
{
    _time += delta;
    _view->Update(_time);
    _view->GetRenderer()->UpdateRenderTree();
    
    // Offscreen pass
    OpenGLState glState;
    glState.Store();
    if (_view->GetRenderer()->RenderOffscreen())
    {
        glState.Restore();
    }
}

void NoesisGUINode::DrawUI() const
{
    OpenGLState glState;
    glState.Store();
    _view->GetRenderer()->Render();
    glState.Restore();
}
but now we want to start using Metal, again with our already created and managed metal 'context' (device, layer, etc).

I have noticed this comment at the beginning of MTLRenderDevice.h:
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Apple Metal render device
///
/// Metal device requires setting a command buffer and a command encoder per frame. The command
/// buffer must be set before any rendering. Offscreen command encoders will be created there. The
/// command encoder is used for the on-screen rendering phase.
///
/// .. code-block:: c++
///
///    renderer->UpdateRenderTree();
///    device->SetCommandBuffer(commands);
///    renderer->RenderOffscreen();
///
///    commandEncoder = [commands renderCommandEncoderWithDescriptor:passDescriptor];
///    device->SetOnScreenEncoder(commandEncoder, colorFormat, stencilFormat, sampleCount);
///    renderer->Render();
///
////////////////////////////////////////////////////////////////////////////////////////////////////
but yet I can't get it to work. I'd assume all methods in MTLFactory are to be used somewhere too, but I have yet to figure when one should end up calling PreCreatePipeline.

Anyone has had succes integrating Metal rendering without using the application framework to manage the view and the context?

Re: Metal (MTLRenderDevice) integration

Posted: 23 Aug 2020, 20:44
by mdube
I was finally able to make it work. Bottom line is you guys at Noesis thought about everything. I found the proper usage for all MTLFactory methods and with some modifications to the engine we use (cocos2d-x), I was finally able to hook everything together. If someone ever starts the same quest, let me know and I'll post more details.

Re: Metal (MTLRenderDevice) integration

Posted: 24 Aug 2020, 18:25
by jsantos
Glad to know it is working! Do you have plans to make public that cocos2d-x integration?

MTLFactory header it is being used to hide implementation details and improve compile times. We are doing something similar for the rest of renderer implementations. But you don't need to do that, you can just take MTLRenderDevice and use it directly. MTLRenderContext is the best example you can find about how to use MTLRenderDevice. PreCreatePipeline is optional, you don't need to call it, but in case you know a certain combination of formats is going to be used it will create the metal pipeline ahead of time instead of in the middle of the first render.

Re: Metal (MTLRenderDevice) integration

Posted: 25 Aug 2020, 15:11
by mdube
I had to make some modifications to the engine and in their current form, those changes, for sure, wouldn't make the cut as a pull request. I may revisit this and try to refactor, but as I encountered a few things that should also be cleaned in the engine itself, this is becoming a huge task for something I'm not even remotely sure that it would be accepted. I'm also new to metal so I will need to dig a bit further to ensure I'm not over doing a few things.

I also currently have everything in the render call and would like to put back the "render offscreen" part into the update call.

Again, if someone finds this thread and want some tips, I'll gladly try to help.

Re: Metal (MTLRenderDevice) integration

Posted: 25 Aug 2020, 15:19
by jsantos
Thanks for getting back, please keep us posted about it.