diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm index 8a0a865ff..3f17197af 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm +++ b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm @@ -443,19 +443,36 @@ using BufferingScanTarget = Outputs::Display::BufferingScanTarget; depthStencilDescriptor.frontFaceStencil.depthStencilPassOperation = MTLStencilOperationReplace; _drawStencilState = [_view.device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - // Draw from _oldFrameBuffer to _frameBuffer. + // Draw from _oldFrameBuffer to _frameBuffer; otherwise clear the new framebuffer. if(_oldFrameBuffer) { [self copyTexture:_oldFrameBuffer to:_frameBuffer]; - - // Don't clear the framebuffer at the end of this frame. - _dontClearFrameBuffer = YES; + } else { + [self clearTexture:_frameBuffer]; } + + // Don't clear the framebuffer at the end of this frame. + _dontClearFrameBuffer = YES; } - (BOOL)shouldApplyGamma { return fabsf(float(uniforms()->outputGamma) - 1.0f) > 0.01f; } +- (void)clearTexture:(id)texture { + id library = [_view.device newDefaultLibrary]; + + // Ensure finalised line texture is initially clear. + id clearPipeline = [_view.device newComputePipelineStateWithFunction:[library newFunctionWithName:@"clearKernel"] error:nil]; + id commandBuffer = [_commandQueue commandBuffer]; + id computeEncoder = [commandBuffer computeCommandEncoder]; + + [computeEncoder setTexture:texture atIndex:0]; + [self dispatchComputeCommandEncoder:computeEncoder pipelineState:clearPipeline width:texture.width height:texture.height offsetBuffer:[self bufferForOffset:0]]; + + [computeEncoder endEncoding]; + [commandBuffer commit]; +} + - (void)updateModalBuffers { // Build a descriptor for any intermediate line texture. MTLTextureDescriptor *const lineTextureDescriptor = [MTLTextureDescriptor @@ -490,20 +507,10 @@ using BufferingScanTarget = Outputs::Display::BufferingScanTarget; // The finalised texture will definitely exist, and may or may not require a gamma conversion when written to. if(!_finalisedLineTexture) { _finalisedLineTexture = [_view.device newTextureWithDescriptor:lineTextureDescriptor]; + [self clearTexture:_finalisedLineTexture]; NSString *const kernelFunction = [self shouldApplyGamma] ? @"filterChromaKernelWithGamma" : @"filterChromaKernelNoGamma"; _finalisedLineState = [_view.device newComputePipelineStateWithFunction:[library newFunctionWithName:kernelFunction] error:nil]; - - // Ensure finalised line texture is initially clear. - id clearPipeline = [_view.device newComputePipelineStateWithFunction:[library newFunctionWithName:@"clearKernel"] error:nil]; - id commandBuffer = [_commandQueue commandBuffer]; - id computeEncoder = [commandBuffer computeCommandEncoder]; - - [computeEncoder setTexture:_finalisedLineTexture atIndex:0]; - [self dispatchComputeCommandEncoder:computeEncoder pipelineState:clearPipeline width:lineTextureDescriptor.width height:lineTextureDescriptor.height offsetBuffer:[self bufferForOffset:0]]; - - [computeEncoder endEncoding]; - [commandBuffer commit]; } // A luma separation texture will exist only for composite colour.