Metal: Don't hardcode the texture usage or dimension

This commit is contained in:
Corentin Wallez 2017-07-03 23:02:49 -04:00 committed by Corentin Wallez
parent c6f9006b02
commit 8308b1c1b3
1 changed files with 29 additions and 8 deletions

View File

@ -20,25 +20,46 @@ namespace backend {
namespace metal { namespace metal {
namespace { namespace {
MTLPixelFormat TextureFormatPixelFormat(nxt::TextureFormat format) { MTLPixelFormat MetalPixelFormat(nxt::TextureFormat format) {
switch (format) { switch (format) {
case nxt::TextureFormat::R8G8B8A8Unorm: case nxt::TextureFormat::R8G8B8A8Unorm:
return MTLPixelFormatRGBA8Unorm; return MTLPixelFormatRGBA8Unorm;
} }
} }
MTLTextureUsage MetalTextureUsage(nxt::TextureUsageBit usage) {
MTLTextureUsage result = MTLTextureUsageUnknown; // This is 0
if (usage & (nxt::TextureUsageBit::Storage)) {
result |= MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
}
if (usage & (nxt::TextureUsageBit::Sampled)) {
result |= MTLTextureUsageShaderRead;
}
if (usage & (nxt::TextureUsageBit::ColorAttachment | nxt::TextureUsageBit::DepthStencilAttachment)) {
result |= MTLTextureUsageRenderTarget;
}
return result;
}
MTLTextureType MetalTextureType(nxt::TextureDimension dimension) {
switch (dimension) {
case nxt::TextureDimension::e2D:
return MTLTextureType2D;
}
}
} }
Texture::Texture(TextureBuilder* builder) Texture::Texture(TextureBuilder* builder)
: TextureBase(builder) { : TextureBase(builder) {
auto desc = [MTLTextureDescriptor new]; auto desc = [MTLTextureDescriptor new];
[desc autorelease]; [desc autorelease];
switch (GetDimension()) { desc.textureType = MetalTextureType(GetDimension());
case nxt::TextureDimension::e2D: desc.usage = MetalTextureUsage(GetUsage());
desc.textureType = MTLTextureType2D; desc.pixelFormat = MetalPixelFormat(GetFormat());
break;
}
desc.usage = MTLTextureUsageShaderRead;
desc.pixelFormat = TextureFormatPixelFormat(GetFormat());
desc.width = GetWidth(); desc.width = GetWidth();
desc.height = GetHeight(); desc.height = GetHeight();
desc.depth = GetDepth(); desc.depth = GetDepth();