iOS-Metal:如何清除Depth Buffer?与OpenGL中的glClear(GL_DEPTH_BUFFER_BIT)类似

时间:2015-04-07 15:32:24

标签: ios objective-c opengl-es depth-buffer metal

我需要清除深度缓冲区,我在OpenGL中使用glClear(GL_DEPTH_BUFFER_BIT),如何处理金属?我已经浏览了苹果的文档,没有任何暗示。

3 个答案:

答案 0 :(得分:3)

简短的回答是,在开始渲染过程之前,要清除深度缓冲区,请添加这两行: mRenderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; mRenderPassDescriptor.depthAttachment.clearDepth = 1.0f; 并且你不能在没有结束并重新启动渲染过程的情况下进行清除。

答案很长:

在Metal中,您必须定义在开始渲染MTLTexture时要清除颜色和深度缓冲区。在OpenGL中没有明确的功能。

要执行此操作,请在MTLRenderPassDescriptor中将depthAttachment.loadAction设为MTLLoadActionClear,将depthAttachment.clearDepth设为1.0f。 您可能还需要将colorAttachments[0].loadAction设置为MTLLoadActionClear以清除颜色缓冲区。

然后将此渲染过程描述符传递给您对MTLCommandBuffer::renderCommandEncoderWithDescriptor的调用。

如果您确实希望在渲染过程中途清除深度或颜色缓冲区,则必须在endEncoding上调用MTLRenderCommandEncoder,然后再次开始编码,depthAttachment.loadAction设置为MTLLoadActionClear }。

答案 1 :(得分:1)

使用示例代码更清楚地解释解决方案

开始渲染之前:

void prepareRendering(){
    CMDBuffer = [_commandQueue commandBuffer]; // get command Buffer
    drawable = [_metalLayer nextDrawable]; // get drawable from metalLayer
    renderingTexture = drawable.texture; // set that as rendering te
    setupRenderPassDescriptorForTexture(drawable.texture); // set the depth and colour buffer properties
    RenderCMDBuffer = [CMDBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor];
    RenderCMDBuffer.label = @"MyRenderEncoder";
    setUpDepthState(CompareFunctionLessEqual,true,false); // 
    [RenderCMDBuffer setDepthStencilState:_depthState];
    [RenderCMDBuffer pushDebugGroup:@"DrawCube"];
}

void setupRenderPassDescriptorForTexture(id <MTLTexture> texture)
{
    if (_renderPassDescriptor == nil)
        _renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
        // set color buffer properties
        _renderPassDescriptor.colorAttachments[0].texture = texture;
        _renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
        _renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.0f, 1.0f,1.0f, 1.0f);
            _renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
// set depth buffer properties
            MTLTextureDescriptor* desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatDepth32Float width: texture.width height: texture.height mipmapped: NO];
            _depthTex = [device newTextureWithDescriptor: desc];
            _depthTex.label = @"Depth";
            _renderPassDescriptor.depthAttachment.texture = _depthTex;
            _renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear;
            _renderPassDescriptor.depthAttachment.clearDepth = 1.0f;
                _renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionDontCare;
   }

在此处呈现您的内容

Render();

渲染后

类似于ogles2方法[_context presentRenderbuffer:_colorRenderBuffer];

void endDisplay()
{
    [RenderCMDBuffer popDebugGroup];
    [RenderCMDBuffer endEncoding];
    [CMDBuffer presentDrawable:drawable];
    [CMDBuffer commit];
    _currentDrawable = nil;
}

上述方法在渲染每个帧后清除深度和颜色缓冲区

要在中途清除深度缓冲区

    void clearDepthBuffer(){
    // end encoding the render command buffer  
            [RenderCMDBuffer popDebugGroup];
            [RenderCMDBuffer endEncoding];

            // here MTLLoadActionClear will clear your last drawn depth values
            _renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear;            _renderPassDescriptor.depthAttachment.clearDepth = 1.0f;
            _renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionDontCare;

            // here MTLLoadActionLoad will reuse your last drawn color buffer
            _renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionLoad;
            _renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;

            RenderCMDBuffer = [CMDBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor];
            RenderCMDBuffer.label = @"MyRenderEncoder";
            [RenderCMDBuffer pushDebugGroup:@"DrawCube"];
        }

答案 2 :(得分:0)

这是Swift 5版本。运行您的第一个渲染过程:

        // RENDER PASS 1

        renderPassDescriptor = view.currentRenderPassDescriptor

        if let renderPassDescriptor = renderPassDescriptor, let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) {

            renderEncoder.label = "First Render Encoder"
            renderEncoder.pushDebugGroup("First Render Debug")

            // render stuff here...

            renderEncoder.popDebugGroup()
            renderEncoder.endEncoding()
        }
        

然后清除深度缓冲区,但保留颜色缓冲区:

        renderPassDescriptor = view.currentRenderPassDescriptor

        // Schedule Metal to clear the depth buffer
        renderPassDescriptor!.depthAttachment.loadAction = MTLLoadAction.clear
        renderPassDescriptor!.depthAttachment.clearDepth = 1.0
        renderPassDescriptor!.depthAttachment.storeAction = MTLStoreAction.dontCare

        // Schedule Metal to reuse the previous colour buffer
        renderPassDescriptor!.colorAttachments[0].loadAction = MTLLoadAction.load
        renderPassDescriptor!.colorAttachments[0].storeAction = MTLStoreAction.store

然后运行第二个渲染器:

        if let renderPassDescriptor = renderPassDescriptor, let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) {
           
            renderEncoder.label = "Second Render"
            renderEncoder.pushDebugGroup("Second Render Debug")

            // render stuff here...

            renderEncoder.popDebugGroup()
            renderEncoder.endEncoding()
        }
相关问题