From c3ea545c995db33108d52185f6f5ee424f72eaaf Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sat, 5 Jul 2025 19:16:49 -0400 Subject: [PATCH] create multisampled textures in metal --- .../metal/src/devices/metal_physical_device.m | 7 +++++++ projects/apis/metal/src/texture/metal_texture.m | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/projects/apis/metal/src/devices/metal_physical_device.m b/projects/apis/metal/src/devices/metal_physical_device.m index 01aefd2..3cb2bd0 100644 --- a/projects/apis/metal/src/devices/metal_physical_device.m +++ b/projects/apis/metal/src/devices/metal_physical_device.m @@ -29,6 +29,13 @@ gnPhysicalDevice* getMetalDevices(gnInstanceHandle instance, uint32_t* deviceCou .queueCount = 1, .queueType = GN_QUEUE_GRAPHICS | GN_QUEUE_COMPUTE | GN_QUEUE_TRANSFER }; + + if ([device supportsTextureSampleCount:1]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_1; } + if ([device supportsTextureSampleCount:2]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_2; } + if ([device supportsTextureSampleCount:4]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_4; } + if ([device supportsTextureSampleCount:8]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_8; } + if ([device supportsTextureSampleCount:16]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_16; } + if ([device supportsTextureSampleCount:32]) { devicesList[i].features.avaliableSamples |= GN_SAMPLE_BIT_32; } } [devices release]; return devicesList; diff --git a/projects/apis/metal/src/texture/metal_texture.m b/projects/apis/metal/src/texture/metal_texture.m index 5dde993..6efd21a 100644 --- a/projects/apis/metal/src/texture/metal_texture.m +++ b/projects/apis/metal/src/texture/metal_texture.m @@ -2,6 +2,17 @@ #include "surface/metal_surface.h" #include "devices/metal_output_devices.h" +NSUInteger mtlSampleCount(gnMultisampleCountFlags flags) { + if ((flags & GN_SAMPLE_BIT_64) == GN_SAMPLE_BIT_64) { return 64; } + if ((flags & GN_SAMPLE_BIT_32) == GN_SAMPLE_BIT_32) { return 32; } + if ((flags & GN_SAMPLE_BIT_16) == GN_SAMPLE_BIT_16) { return 16; } + if ((flags & GN_SAMPLE_BIT_8) == GN_SAMPLE_BIT_8) { return 8; } + if ((flags & GN_SAMPLE_BIT_4) == GN_SAMPLE_BIT_4) { return 4; } + if ((flags & GN_SAMPLE_BIT_2) == GN_SAMPLE_BIT_2) { return 2; } + if ((flags & GN_SAMPLE_BIT_1) == GN_SAMPLE_BIT_1) { return 1; } + return 0; +} + gnReturnCode createMetalTexture(gnTexture texture, gnDevice device, const gnTextureInfo info) { texture->texture = malloc(sizeof(struct gnPlatformTexture_t)); MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init]; @@ -9,6 +20,11 @@ gnReturnCode createMetalTexture(gnTexture texture, gnDevice device, const gnText textureDescriptor.width = info.extent.width; textureDescriptor.height = info.extent.height; textureDescriptor.depth = info.extent.depth; + textureDescriptor.sampleCount = mtlSampleCount(info.samples); + if (textureDescriptor.sampleCount >= 2) + textureDescriptor.textureType = MTLTextureType2DMultisample; + else + textureDescriptor.textureType = MTLTextureType2D; texture->texture->texture = [device->outputDevice->device newTextureWithDescriptor:textureDescriptor]; [textureDescriptor release];