Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some Xcode errors with Metal textures #86

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plugins/metal/src/metal_painter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
auto& pass = material.getDefinition().getPass(passNumber);
MetalShader& shader = static_cast<MetalShader&>(pass.getShader());

auto pipelineStateDescriptor = shader.setupMaterial(material);
auto& renderTarget = dynamic_cast<IMetalRenderTarget&>(getActiveRenderTarget());
auto pipelineStateDescriptor = shader.setupMaterial(material, renderTarget.getMetalTexture());
setBlending(pass.getBlend(), pipelineStateDescriptor.colorAttachments[0]);

NSError* error = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/metal/src/metal_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTLTexture> renderTargetTexture);

private:
MetalVideo& video;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/metal/src/metal_shader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
return -1;
}

MTLRenderPipelineDescriptor* MetalShader::setupMaterial(const Material& material) {
MTLRenderPipelineDescriptor* MetalShader::setupMaterial(const Material& material, id<MTLTexture> renderTargetTexture) {
if (descriptor) {
return descriptor;
}
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/metal/src/metal_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ namespace Halley {
id<MTLSamplerState> sampler;

static MTLSamplerAddressMode getMetalAddressMode(TextureDescriptor& descriptor);
static MTLTextureUsage getMetalTextureUsage(TextureDescriptor& descriptor);
};
}
10 changes: 10 additions & 0 deletions src/plugins/metal/src/metal_texture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,3 +101,12 @@
return MTLSamplerAddressModeRepeat;
}
}

MTLTextureUsage MetalTexture::getMetalTextureUsage(TextureDescriptor& descriptor)
{
MTLTextureUsage usage = MTLTextureUsageShaderRead;
if (descriptor.isRenderTarget) {
return usage | MTLTextureUsageRenderTarget;
}
return usage;
}