From 65c412f93c78046105a326bd7715a04fa45c8017 Mon Sep 17 00:00:00 2001 From: Joel Auterson Date: Fri, 17 Jul 2020 15:15:10 +0100 Subject: [PATCH] Fix some Xcode errors with Metal textures --- src/plugins/metal/src/metal_painter.mm | 3 ++- src/plugins/metal/src/metal_shader.h | 2 +- src/plugins/metal/src/metal_shader.mm | 4 ++-- src/plugins/metal/src/metal_texture.h | 1 + src/plugins/metal/src/metal_texture.mm | 10 ++++++++++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/plugins/metal/src/metal_painter.mm b/src/plugins/metal/src/metal_painter.mm index 52ca427030..646511497c 100644 --- a/src/plugins/metal/src/metal_painter.mm +++ b/src/plugins/metal/src/metal_painter.mm @@ -23,7 +23,8 @@ auto& pass = material.getDefinition().getPass(passNumber); MetalShader& shader = static_cast(pass.getShader()); - auto pipelineStateDescriptor = shader.setupMaterial(material); + auto& renderTarget = dynamic_cast(getActiveRenderTarget()); + auto pipelineStateDescriptor = shader.setupMaterial(material, renderTarget.getMetalTexture()); setBlending(pass.getBlend(), pipelineStateDescriptor.colorAttachments[0]); NSError* error = NULL; diff --git a/src/plugins/metal/src/metal_shader.h b/src/plugins/metal/src/metal_shader.h index 3753f6ec55..a9f71602e6 100644 --- a/src/plugins/metal/src/metal_shader.h +++ b/src/plugins/metal/src/metal_shader.h @@ -13,7 +13,7 @@ namespace Halley { ~MetalShader(); int getUniformLocation(const String& name, ShaderType stage) override; int getBlockLocation(const String& name, ShaderType stage) override; - MTLRenderPipelineDescriptor* setupMaterial(const Material& material); + MTLRenderPipelineDescriptor* setupMaterial(const Material& material, id renderTargetTexture); private: MetalVideo& video; diff --git a/src/plugins/metal/src/metal_shader.mm b/src/plugins/metal/src/metal_shader.mm index 18351407cf..a4d0b9d572 100644 --- a/src/plugins/metal/src/metal_shader.mm +++ b/src/plugins/metal/src/metal_shader.mm @@ -63,7 +63,7 @@ return -1; } -MTLRenderPipelineDescriptor* MetalShader::setupMaterial(const Material& material) { +MTLRenderPipelineDescriptor* MetalShader::setupMaterial(const Material& material, id renderTargetTexture) { if (descriptor) { return descriptor; } @@ -72,7 +72,7 @@ descriptor.vertexFunction = vertex_func; descriptor.fragmentFunction = fragment_func; descriptor.label = [NSString stringWithUTF8String:material.getDefinition().getName().c_str()]; - descriptor.colorAttachments[0].pixelFormat = video.getSurface().texture.pixelFormat; + descriptor.colorAttachments[0].pixelFormat = renderTargetTexture.pixelFormat; vertex_descriptor = [[MTLVertexDescriptor alloc] init]; for (size_t i = 0; i < material.getDefinition().getAttributes().size(); ++i) { diff --git a/src/plugins/metal/src/metal_texture.h b/src/plugins/metal/src/metal_texture.h index 53e7bfe187..055d6f23b2 100644 --- a/src/plugins/metal/src/metal_texture.h +++ b/src/plugins/metal/src/metal_texture.h @@ -23,5 +23,6 @@ namespace Halley { id sampler; static MTLSamplerAddressMode getMetalAddressMode(TextureDescriptor& descriptor); + static MTLTextureUsage getMetalTextureUsage(TextureDescriptor& descriptor); }; } diff --git a/src/plugins/metal/src/metal_texture.mm b/src/plugins/metal/src/metal_texture.mm index dfdd8ff65b..c83bc0c1cb 100644 --- a/src/plugins/metal/src/metal_texture.mm +++ b/src/plugins/metal/src/metal_texture.mm @@ -40,6 +40,7 @@ height:descriptor.size.y mipmapped:descriptor.useMipMap ]; + textureDescriptor.usage = getMetalTextureUsage(descriptor); metalTexture = [video.getDevice() newTextureWithDescriptor:textureDescriptor]; NSUInteger bytesPerRow = bytesPerPixel * descriptor.size.x; @@ -100,3 +101,12 @@ return MTLSamplerAddressModeRepeat; } } + +MTLTextureUsage MetalTexture::getMetalTextureUsage(TextureDescriptor& descriptor) +{ + MTLTextureUsage usage = MTLTextureUsageShaderRead; + if (descriptor.isRenderTarget) { + return usage | MTLTextureUsageRenderTarget; + } + return usage; +}