diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-04-28 18:15:03 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-04-29 22:36:00 +0200 |
commit | 1c0a7a87e9e8dd630d35b2c3e57d45c42f6d12bd (patch) | |
tree | 6bba3482a817ecf5dcd732de633131e0b0a9416e /src/gui/rhi/qrhimetal.mm | |
parent | f2347077f503d103974636cae9319d127714e2e4 (diff) |
rhi: Add backing format hint to QRhiRenderBuffer
Task-number: QTBUG-83707
Change-Id: I63548f4ace70af614a2aa082663bb3ae9fbedc25
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Diffstat (limited to 'src/gui/rhi/qrhimetal.mm')
-rw-r--r-- | src/gui/rhi/qrhimetal.mm | 284 |
1 files changed, 146 insertions, 138 deletions
diff --git a/src/gui/rhi/qrhimetal.mm b/src/gui/rhi/qrhimetal.mm index f021e78097..c5afdf63eb 100644 --- a/src/gui/rhi/qrhimetal.mm +++ b/src/gui/rhi/qrhimetal.mm @@ -621,9 +621,10 @@ bool QRhiMetal::isDeviceLost() const } QRhiRenderBuffer *QRhiMetal::createRenderBuffer(QRhiRenderBuffer::Type type, const QSize &pixelSize, - int sampleCount, QRhiRenderBuffer::Flags flags) + int sampleCount, QRhiRenderBuffer::Flags flags, + QRhiTexture::Format backingFormatHint) { - return new QMetalRenderBuffer(this, type, pixelSize, sampleCount, flags); + return new QMetalRenderBuffer(this, type, pixelSize, sampleCount, flags, backingFormatHint); } QRhiTexture *QRhiMetal::createTexture(QRhiTexture::Format format, const QSize &pixelSize, @@ -2232,9 +2233,142 @@ QRhiBuffer::NativeBuffer QMetalBuffer::nativeBuffer() return { { &d->buf[0] }, 1 }; } +static inline MTLPixelFormat toMetalTextureFormat(QRhiTexture::Format format, QRhiTexture::Flags flags) +{ + const bool srgb = flags.testFlag(QRhiTexture::sRGB); + switch (format) { + case QRhiTexture::RGBA8: + return srgb ? MTLPixelFormatRGBA8Unorm_sRGB : MTLPixelFormatRGBA8Unorm; + case QRhiTexture::BGRA8: + return srgb ? MTLPixelFormatBGRA8Unorm_sRGB : MTLPixelFormatBGRA8Unorm; + case QRhiTexture::R8: +#ifdef Q_OS_MACOS + return MTLPixelFormatR8Unorm; +#else + return srgb ? MTLPixelFormatR8Unorm_sRGB : MTLPixelFormatR8Unorm; +#endif + case QRhiTexture::R16: + return MTLPixelFormatR16Unorm; + case QRhiTexture::RED_OR_ALPHA8: + return MTLPixelFormatR8Unorm; + + case QRhiTexture::RGBA16F: + return MTLPixelFormatRGBA16Float; + case QRhiTexture::RGBA32F: + return MTLPixelFormatRGBA32Float; + case QRhiTexture::R16F: + return MTLPixelFormatR16Float; + case QRhiTexture::R32F: + return MTLPixelFormatR32Float; + + case QRhiTexture::D16: +#ifdef Q_OS_MACOS + return MTLPixelFormatDepth16Unorm; +#else + return MTLPixelFormatDepth32Float; +#endif + case QRhiTexture::D32F: + return MTLPixelFormatDepth32Float; + +#ifdef Q_OS_MACOS + case QRhiTexture::BC1: + return srgb ? MTLPixelFormatBC1_RGBA_sRGB : MTLPixelFormatBC1_RGBA; + case QRhiTexture::BC2: + return srgb ? MTLPixelFormatBC2_RGBA_sRGB : MTLPixelFormatBC2_RGBA; + case QRhiTexture::BC3: + return srgb ? MTLPixelFormatBC3_RGBA_sRGB : MTLPixelFormatBC3_RGBA; + case QRhiTexture::BC4: + return MTLPixelFormatBC4_RUnorm; + case QRhiTexture::BC5: + qWarning("QRhiMetal does not support BC5"); + return MTLPixelFormatRGBA8Unorm; + case QRhiTexture::BC6H: + return MTLPixelFormatBC6H_RGBUfloat; + case QRhiTexture::BC7: + return srgb ? MTLPixelFormatBC7_RGBAUnorm_sRGB : MTLPixelFormatBC7_RGBAUnorm; +#else + case QRhiTexture::BC1: + case QRhiTexture::BC2: + case QRhiTexture::BC3: + case QRhiTexture::BC4: + case QRhiTexture::BC5: + case QRhiTexture::BC6H: + case QRhiTexture::BC7: + qWarning("QRhiMetal: BCx compression not supported on this platform"); + return MTLPixelFormatRGBA8Unorm; +#endif + +#ifndef Q_OS_MACOS + case QRhiTexture::ETC2_RGB8: + return srgb ? MTLPixelFormatETC2_RGB8_sRGB : MTLPixelFormatETC2_RGB8; + case QRhiTexture::ETC2_RGB8A1: + return srgb ? MTLPixelFormatETC2_RGB8A1_sRGB : MTLPixelFormatETC2_RGB8A1; + case QRhiTexture::ETC2_RGBA8: + return srgb ? MTLPixelFormatEAC_RGBA8_sRGB : MTLPixelFormatEAC_RGBA8; + + case QRhiTexture::ASTC_4x4: + return srgb ? MTLPixelFormatASTC_4x4_sRGB : MTLPixelFormatASTC_4x4_LDR; + case QRhiTexture::ASTC_5x4: + return srgb ? MTLPixelFormatASTC_5x4_sRGB : MTLPixelFormatASTC_5x4_LDR; + case QRhiTexture::ASTC_5x5: + return srgb ? MTLPixelFormatASTC_5x5_sRGB : MTLPixelFormatASTC_5x5_LDR; + case QRhiTexture::ASTC_6x5: + return srgb ? MTLPixelFormatASTC_6x5_sRGB : MTLPixelFormatASTC_6x5_LDR; + case QRhiTexture::ASTC_6x6: + return srgb ? MTLPixelFormatASTC_6x6_sRGB : MTLPixelFormatASTC_6x6_LDR; + case QRhiTexture::ASTC_8x5: + return srgb ? MTLPixelFormatASTC_8x5_sRGB : MTLPixelFormatASTC_8x5_LDR; + case QRhiTexture::ASTC_8x6: + return srgb ? MTLPixelFormatASTC_8x6_sRGB : MTLPixelFormatASTC_8x6_LDR; + case QRhiTexture::ASTC_8x8: + return srgb ? MTLPixelFormatASTC_8x8_sRGB : MTLPixelFormatASTC_8x8_LDR; + case QRhiTexture::ASTC_10x5: + return srgb ? MTLPixelFormatASTC_10x5_sRGB : MTLPixelFormatASTC_10x5_LDR; + case QRhiTexture::ASTC_10x6: + return srgb ? MTLPixelFormatASTC_10x6_sRGB : MTLPixelFormatASTC_10x6_LDR; + case QRhiTexture::ASTC_10x8: + return srgb ? MTLPixelFormatASTC_10x8_sRGB : MTLPixelFormatASTC_10x8_LDR; + case QRhiTexture::ASTC_10x10: + return srgb ? MTLPixelFormatASTC_10x10_sRGB : MTLPixelFormatASTC_10x10_LDR; + case QRhiTexture::ASTC_12x10: + return srgb ? MTLPixelFormatASTC_12x10_sRGB : MTLPixelFormatASTC_12x10_LDR; + case QRhiTexture::ASTC_12x12: + return srgb ? MTLPixelFormatASTC_12x12_sRGB : MTLPixelFormatASTC_12x12_LDR; +#else + case QRhiTexture::ETC2_RGB8: + case QRhiTexture::ETC2_RGB8A1: + case QRhiTexture::ETC2_RGBA8: + qWarning("QRhiMetal: ETC2 compression not supported on this platform"); + return MTLPixelFormatRGBA8Unorm; + + case QRhiTexture::ASTC_4x4: + case QRhiTexture::ASTC_5x4: + case QRhiTexture::ASTC_5x5: + case QRhiTexture::ASTC_6x5: + case QRhiTexture::ASTC_6x6: + case QRhiTexture::ASTC_8x5: + case QRhiTexture::ASTC_8x6: + case QRhiTexture::ASTC_8x8: + case QRhiTexture::ASTC_10x5: + case QRhiTexture::ASTC_10x6: + case QRhiTexture::ASTC_10x8: + case QRhiTexture::ASTC_10x10: + case QRhiTexture::ASTC_12x10: + case QRhiTexture::ASTC_12x12: + qWarning("QRhiMetal: ASTC compression not supported on this platform"); + return MTLPixelFormatRGBA8Unorm; +#endif + + default: + Q_UNREACHABLE(); + return MTLPixelFormatRGBA8Unorm; + } +} + QMetalRenderBuffer::QMetalRenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize, - int sampleCount, QRhiRenderBuffer::Flags flags) - : QRhiRenderBuffer(rhi, type, pixelSize, sampleCount, flags), + int sampleCount, QRhiRenderBuffer::Flags flags, + QRhiTexture::Format backingFormatHint) + : QRhiRenderBuffer(rhi, type, pixelSize, sampleCount, flags, backingFormatHint), d(new QMetalRenderBufferData) { } @@ -2300,7 +2434,10 @@ bool QMetalRenderBuffer::build() break; case Color: desc.storageMode = MTLStorageModePrivate; - d->format = MTLPixelFormatRGBA8Unorm; + if (m_backingFormatHint != QRhiTexture::UnknownFormat) + d->format = toMetalTextureFormat(m_backingFormatHint, {}); + else + d->format = MTLPixelFormatRGBA8Unorm; desc.pixelFormat = d->format; break; default: @@ -2325,7 +2462,10 @@ bool QMetalRenderBuffer::build() QRhiTexture::Format QMetalRenderBuffer::backingFormat() const { - return m_type == Color ? QRhiTexture::RGBA8 : QRhiTexture::UnknownFormat; + if (m_backingFormatHint != QRhiTexture::UnknownFormat) + return m_backingFormatHint; + else + return m_type == Color ? QRhiTexture::RGBA8 : QRhiTexture::UnknownFormat; } QMetalTexture::QMetalTexture(QRhiImplementation *rhi, Format format, const QSize &pixelSize, @@ -2375,138 +2515,6 @@ void QMetalTexture::release() rhiD->unregisterResource(this); } -static inline MTLPixelFormat toMetalTextureFormat(QRhiTexture::Format format, QRhiTexture::Flags flags) -{ - const bool srgb = flags.testFlag(QRhiTexture::sRGB); - switch (format) { - case QRhiTexture::RGBA8: - return srgb ? MTLPixelFormatRGBA8Unorm_sRGB : MTLPixelFormatRGBA8Unorm; - case QRhiTexture::BGRA8: - return srgb ? MTLPixelFormatBGRA8Unorm_sRGB : MTLPixelFormatBGRA8Unorm; - case QRhiTexture::R8: -#ifdef Q_OS_MACOS - return MTLPixelFormatR8Unorm; -#else - return srgb ? MTLPixelFormatR8Unorm_sRGB : MTLPixelFormatR8Unorm; -#endif - case QRhiTexture::R16: - return MTLPixelFormatR16Unorm; - case QRhiTexture::RED_OR_ALPHA8: - return MTLPixelFormatR8Unorm; - - case QRhiTexture::RGBA16F: - return MTLPixelFormatRGBA16Float; - case QRhiTexture::RGBA32F: - return MTLPixelFormatRGBA32Float; - case QRhiTexture::R16F: - return MTLPixelFormatR16Float; - case QRhiTexture::R32F: - return MTLPixelFormatR32Float; - - case QRhiTexture::D16: -#ifdef Q_OS_MACOS - return MTLPixelFormatDepth16Unorm; -#else - return MTLPixelFormatDepth32Float; -#endif - case QRhiTexture::D32F: - return MTLPixelFormatDepth32Float; - -#ifdef Q_OS_MACOS - case QRhiTexture::BC1: - return srgb ? MTLPixelFormatBC1_RGBA_sRGB : MTLPixelFormatBC1_RGBA; - case QRhiTexture::BC2: - return srgb ? MTLPixelFormatBC2_RGBA_sRGB : MTLPixelFormatBC2_RGBA; - case QRhiTexture::BC3: - return srgb ? MTLPixelFormatBC3_RGBA_sRGB : MTLPixelFormatBC3_RGBA; - case QRhiTexture::BC4: - return MTLPixelFormatBC4_RUnorm; - case QRhiTexture::BC5: - qWarning("QRhiMetal does not support BC5"); - return MTLPixelFormatRGBA8Unorm; - case QRhiTexture::BC6H: - return MTLPixelFormatBC6H_RGBUfloat; - case QRhiTexture::BC7: - return srgb ? MTLPixelFormatBC7_RGBAUnorm_sRGB : MTLPixelFormatBC7_RGBAUnorm; -#else - case QRhiTexture::BC1: - case QRhiTexture::BC2: - case QRhiTexture::BC3: - case QRhiTexture::BC4: - case QRhiTexture::BC5: - case QRhiTexture::BC6H: - case QRhiTexture::BC7: - qWarning("QRhiMetal: BCx compression not supported on this platform"); - return MTLPixelFormatRGBA8Unorm; -#endif - -#ifndef Q_OS_MACOS - case QRhiTexture::ETC2_RGB8: - return srgb ? MTLPixelFormatETC2_RGB8_sRGB : MTLPixelFormatETC2_RGB8; - case QRhiTexture::ETC2_RGB8A1: - return srgb ? MTLPixelFormatETC2_RGB8A1_sRGB : MTLPixelFormatETC2_RGB8A1; - case QRhiTexture::ETC2_RGBA8: - return srgb ? MTLPixelFormatEAC_RGBA8_sRGB : MTLPixelFormatEAC_RGBA8; - - case QRhiTexture::ASTC_4x4: - return srgb ? MTLPixelFormatASTC_4x4_sRGB : MTLPixelFormatASTC_4x4_LDR; - case QRhiTexture::ASTC_5x4: - return srgb ? MTLPixelFormatASTC_5x4_sRGB : MTLPixelFormatASTC_5x4_LDR; - case QRhiTexture::ASTC_5x5: - return srgb ? MTLPixelFormatASTC_5x5_sRGB : MTLPixelFormatASTC_5x5_LDR; - case QRhiTexture::ASTC_6x5: - return srgb ? MTLPixelFormatASTC_6x5_sRGB : MTLPixelFormatASTC_6x5_LDR; - case QRhiTexture::ASTC_6x6: - return srgb ? MTLPixelFormatASTC_6x6_sRGB : MTLPixelFormatASTC_6x6_LDR; - case QRhiTexture::ASTC_8x5: - return srgb ? MTLPixelFormatASTC_8x5_sRGB : MTLPixelFormatASTC_8x5_LDR; - case QRhiTexture::ASTC_8x6: - return srgb ? MTLPixelFormatASTC_8x6_sRGB : MTLPixelFormatASTC_8x6_LDR; - case QRhiTexture::ASTC_8x8: - return srgb ? MTLPixelFormatASTC_8x8_sRGB : MTLPixelFormatASTC_8x8_LDR; - case QRhiTexture::ASTC_10x5: - return srgb ? MTLPixelFormatASTC_10x5_sRGB : MTLPixelFormatASTC_10x5_LDR; - case QRhiTexture::ASTC_10x6: - return srgb ? MTLPixelFormatASTC_10x6_sRGB : MTLPixelFormatASTC_10x6_LDR; - case QRhiTexture::ASTC_10x8: - return srgb ? MTLPixelFormatASTC_10x8_sRGB : MTLPixelFormatASTC_10x8_LDR; - case QRhiTexture::ASTC_10x10: - return srgb ? MTLPixelFormatASTC_10x10_sRGB : MTLPixelFormatASTC_10x10_LDR; - case QRhiTexture::ASTC_12x10: - return srgb ? MTLPixelFormatASTC_12x10_sRGB : MTLPixelFormatASTC_12x10_LDR; - case QRhiTexture::ASTC_12x12: - return srgb ? MTLPixelFormatASTC_12x12_sRGB : MTLPixelFormatASTC_12x12_LDR; -#else - case QRhiTexture::ETC2_RGB8: - case QRhiTexture::ETC2_RGB8A1: - case QRhiTexture::ETC2_RGBA8: - qWarning("QRhiMetal: ETC2 compression not supported on this platform"); - return MTLPixelFormatRGBA8Unorm; - - case QRhiTexture::ASTC_4x4: - case QRhiTexture::ASTC_5x4: - case QRhiTexture::ASTC_5x5: - case QRhiTexture::ASTC_6x5: - case QRhiTexture::ASTC_6x6: - case QRhiTexture::ASTC_8x5: - case QRhiTexture::ASTC_8x6: - case QRhiTexture::ASTC_8x8: - case QRhiTexture::ASTC_10x5: - case QRhiTexture::ASTC_10x6: - case QRhiTexture::ASTC_10x8: - case QRhiTexture::ASTC_10x10: - case QRhiTexture::ASTC_12x10: - case QRhiTexture::ASTC_12x12: - qWarning("QRhiMetal: ASTC compression not supported on this platform"); - return MTLPixelFormatRGBA8Unorm; -#endif - - default: - Q_UNREACHABLE(); - return MTLPixelFormatRGBA8Unorm; - } -} - bool QMetalTexture::prepareBuild(QSize *adjustedSize) { if (d->tex) |