diff --git a/Build/Linux/_deps/arcana.cpp-src b/Build/Linux/_deps/arcana.cpp-src new file mode 160000 index 000000000..c726dbe58 --- /dev/null +++ b/Build/Linux/_deps/arcana.cpp-src @@ -0,0 +1 @@ +Subproject commit c726dbe58713eda65bfb139c257093c43479b894 diff --git a/Build/Linux/_deps/base-n-src b/Build/Linux/_deps/base-n-src new file mode 160000 index 000000000..7573e77c0 --- /dev/null +++ b/Build/Linux/_deps/base-n-src @@ -0,0 +1 @@ +Subproject commit 7573e77c0b9b0e8a5fb63d96dbde212c921993b4 diff --git a/Build/Linux/_deps/bgfx.cmake-src b/Build/Linux/_deps/bgfx.cmake-src new file mode 160000 index 000000000..0af3c9865 --- /dev/null +++ b/Build/Linux/_deps/bgfx.cmake-src @@ -0,0 +1 @@ +Subproject commit 0af3c9865a66aff1748a51bb466b24f05a123043 diff --git a/Build/Linux/_deps/cmakeextensions-src b/Build/Linux/_deps/cmakeextensions-src new file mode 160000 index 000000000..ea28b7689 --- /dev/null +++ b/Build/Linux/_deps/cmakeextensions-src @@ -0,0 +1 @@ +Subproject commit ea28b7689530bfdc4905806f27ecf7e8ed4b5419 diff --git a/Build/Linux/_deps/glslang-src b/Build/Linux/_deps/glslang-src new file mode 160000 index 000000000..6388a301b --- /dev/null +++ b/Build/Linux/_deps/glslang-src @@ -0,0 +1 @@ +Subproject commit 6388a301b23edf0ae6a9342d46efa9cb21f05d9b diff --git a/Build/Linux/_deps/gsl-src b/Build/Linux/_deps/gsl-src new file mode 160000 index 000000000..a35345671 --- /dev/null +++ b/Build/Linux/_deps/gsl-src @@ -0,0 +1 @@ +Subproject commit a3534567187d2edc428efd3f13466ff75fe5805c diff --git a/Build/Linux/_deps/jsruntimehost-src b/Build/Linux/_deps/jsruntimehost-src new file mode 160000 index 000000000..7964539e7 --- /dev/null +++ b/Build/Linux/_deps/jsruntimehost-src @@ -0,0 +1 @@ +Subproject commit 7964539e733a5cd2cbae7e0155f5391d64a9d45d diff --git a/Build/Linux/_deps/libwebp-src b/Build/Linux/_deps/libwebp-src new file mode 160000 index 000000000..57e324e2e --- /dev/null +++ b/Build/Linux/_deps/libwebp-src @@ -0,0 +1 @@ +Subproject commit 57e324e2eb99be46df46d77b65705e34a7ae616c diff --git a/Build/Linux/_deps/spirv-cross-src b/Build/Linux/_deps/spirv-cross-src new file mode 160000 index 000000000..6abfcf066 --- /dev/null +++ b/Build/Linux/_deps/spirv-cross-src @@ -0,0 +1 @@ +Subproject commit 6abfcf066d171e9ade7604d91381ebebe4209edc diff --git a/Build/Linux/_deps/urllib-src b/Build/Linux/_deps/urllib-src new file mode 160000 index 000000000..1f9d2c05f --- /dev/null +++ b/Build/Linux/_deps/urllib-src @@ -0,0 +1 @@ +Subproject commit 1f9d2c05f792d994b09fdfc1129b25294efbe182 diff --git a/Core/Graphics/Include/Platform/Unix/Babylon/Graphics/Platform.h b/Core/Graphics/Include/Platform/Unix/Babylon/Graphics/Platform.h index 4ae32778a..c2bdae5d4 100644 --- a/Core/Graphics/Include/Platform/Unix/Babylon/Graphics/Platform.h +++ b/Core/Graphics/Include/Platform/Unix/Babylon/Graphics/Platform.h @@ -1,6 +1,7 @@ #pragma once #include +#undef None namespace Babylon::Graphics { diff --git a/Core/Graphics/Include/RendererType/D3D11/Babylon/Graphics/RendererType.h b/Core/Graphics/Include/RendererType/D3D11/Babylon/Graphics/RendererType.h index 6bf9e1ef3..668724151 100644 --- a/Core/Graphics/Include/RendererType/D3D11/Babylon/Graphics/RendererType.h +++ b/Core/Graphics/Include/RendererType/D3D11/Babylon/Graphics/RendererType.h @@ -2,17 +2,15 @@ #include +#define GRAPHICS_BACK_BUFFER_SUPPORT + namespace Babylon::Graphics { using DeviceT = ID3D11Device*; using TextureT = ID3D11Resource*; using TextureFormatT = DXGI_FORMAT; - - struct DeviceConfiguration - { - DeviceT Device; - float DevicePixelRatio{1.f}; - }; + using BackBufferColorT = ID3D11RenderTargetView*; + using BackBufferDepthStencilT = ID3D11DepthStencilView*; struct PlatformInfo { diff --git a/Core/Graphics/Include/RendererType/D3D12/Babylon/Graphics/RendererType.h b/Core/Graphics/Include/RendererType/D3D12/Babylon/Graphics/RendererType.h index a2dccd137..75cd424f2 100644 --- a/Core/Graphics/Include/RendererType/D3D12/Babylon/Graphics/RendererType.h +++ b/Core/Graphics/Include/RendererType/D3D12/Babylon/Graphics/RendererType.h @@ -8,12 +8,6 @@ namespace Babylon::Graphics using TextureT = ID3D12Resource*; using TextureFormatT = DXGI_FORMAT; - struct DeviceConfiguration - { - DeviceT Device; - float DevicePixelRatio{1.f}; - }; - struct PlatformInfo { DeviceT Device; diff --git a/Core/Graphics/Include/RendererType/Metal/Babylon/Graphics/RendererType.h b/Core/Graphics/Include/RendererType/Metal/Babylon/Graphics/RendererType.h index 0cf537b2d..3a7d14a87 100644 --- a/Core/Graphics/Include/RendererType/Metal/Babylon/Graphics/RendererType.h +++ b/Core/Graphics/Include/RendererType/Metal/Babylon/Graphics/RendererType.h @@ -8,12 +8,6 @@ namespace Babylon::Graphics using TextureT = id; using TextureFormatT = MTLPixelFormat; - struct DeviceConfiguration - { - DeviceT Device; - float DevicePixelRatio{1.f}; - }; - struct PlatformInfo { struct MTLDevice* Device; diff --git a/Core/Graphics/Include/RendererType/OpenGL/Babylon/Graphics/RendererType.h b/Core/Graphics/Include/RendererType/OpenGL/Babylon/Graphics/RendererType.h index b3d85cb46..77bbc643b 100644 --- a/Core/Graphics/Include/RendererType/OpenGL/Babylon/Graphics/RendererType.h +++ b/Core/Graphics/Include/RendererType/OpenGL/Babylon/Graphics/RendererType.h @@ -8,12 +8,6 @@ namespace Babylon::Graphics using TextureT = unsigned int; using TextureFormatT = unsigned int; - struct DeviceConfiguration - { - DeviceT Device; - float DevicePixelRatio{1.f}; - }; - struct PlatformInfo { DeviceT Device; diff --git a/Core/Graphics/Include/Shared/Babylon/Graphics/Device.h b/Core/Graphics/Include/Shared/Babylon/Graphics/Device.h index 9a42719c1..4d7ec5332 100644 --- a/Core/Graphics/Include/Shared/Babylon/Graphics/Device.h +++ b/Core/Graphics/Include/Shared/Babylon/Graphics/Device.h @@ -9,6 +9,18 @@ namespace Babylon::Graphics { + enum class DepthStencilFormat + { + // Do not create a depth/stencil texture. + None, + + // Create a 32-bit depth texture with no stencil. + Depth32, + + // Create a combined 24-bit depth and 8-bit stencil texture. + Depth24Stencil8, + }; + struct Configuration { // Custom device to use instead of creating one internally. @@ -17,6 +29,16 @@ namespace Babylon::Graphics // The platform specific window. WindowT Window{}; +#ifdef GRAPHICS_BACK_BUFFER_SUPPORT + // Color back buffer to use instead of creating one internally. + // @remarks Only available for D3D11. + BackBufferColorT BackBufferColor{}; + + // Depth stencil back buffer to use instead of creating one internally. + // @remarks Only available for D3D11. DepthStencilFormat is ignored when specified. + BackBufferDepthStencilT BackBufferDepthStencil{}; +#endif + // The resolution width. size_t Width{}; @@ -28,6 +50,10 @@ namespace Babylon::Graphics // When enabled, back buffer will be premultiplied with alpha value. bool AlphaPremultiplied{}; + + // Format to use when creating the depth/stencil texture for the back buffer. + // Specify DepthStencilFormat::None to not create a depth/stencil texture. + DepthStencilFormat BackBufferDepthStencilFormat{DepthStencilFormat::Depth24Stencil8}; }; class Device; diff --git a/Core/Graphics/Source/DeviceImpl.cpp b/Core/Graphics/Source/DeviceImpl.cpp index 1bac3591a..7b4f3fd0d 100644 --- a/Core/Graphics/Source/DeviceImpl.cpp +++ b/Core/Graphics/Source/DeviceImpl.cpp @@ -37,6 +37,7 @@ namespace Babylon::Graphics m_state.Bgfx.Initialized = false; auto& init = m_state.Bgfx.InitState; + init.callback = &m_bgfxCallback; // Use the noop renderer if the configuration has no window and no size. if (config.Window == WindowT{} && config.Width == 0 && config.Height == 0) @@ -48,16 +49,42 @@ namespace Babylon::Graphics init.type = s_bgfxRenderType; } + // + // init.resolution + // + init.resolution.reset = BGFX_RESET_VSYNC | BGFX_RESET_MAXANISOTROPY | BGFX_RESET_FLIP_AFTER_RENDER; init.resolution.maxFrameLatency = 1; - init.callback = &m_bgfxCallback; - - init.platformData.context = config.Device; - UpdateWindow(config.Window); UpdateSize(config.Width, config.Height); UpdateMSAA(config.MSAASamples); UpdateAlphaPremultiplied(config.AlphaPremultiplied); + + switch (config.BackBufferDepthStencilFormat) + { + case DepthStencilFormat::None: + init.resolution.formatDepthStencil = bgfx::TextureFormat::UnknownDepth; + break; + case DepthStencilFormat::Depth32: + init.resolution.formatDepthStencil = bgfx::TextureFormat::D32; + break; + case DepthStencilFormat::Depth24Stencil8: + default: + init.resolution.formatDepthStencil = bgfx::TextureFormat::D24S8; + break; + } + + // + // init.platformData + // + + UpdateWindow(config.Window); + UpdateDevice(config.Device); + +#ifdef GRAPHICS_BACK_BUFFER_SUPPORT + init.platformData.backBuffer = config.BackBufferColor; + init.platformData.backBufferDS = config.BackBufferDepthStencil; +#endif } DeviceImpl::~DeviceImpl() @@ -133,18 +160,6 @@ namespace Babylon::Graphics m_state.Bgfx.Dirty = true; } - void DeviceImpl::UpdateDevicePixelRatio(float value) - { - std::scoped_lock lock{m_state.Mutex}; - m_state.Resolution.DevicePixelRatio = value; - m_state.Bgfx.Dirty = true; - } - - void DeviceImpl::SetRenderResetCallback(std::function callback) - { - m_renderResetCallback = std::move(callback); - } - void DeviceImpl::AddToJavaScript(Napi::Env env) { JsRuntime::NativeObject::GetFromJavaScript(env) @@ -164,6 +179,11 @@ namespace Babylon::Graphics return DeviceContext::Create(env, *this); } + void DeviceImpl::SetRenderResetCallback(std::function callback) + { + m_renderResetCallback = std::move(callback); + } + void DeviceImpl::EnableRendering() { std::scoped_lock lock{m_state.Mutex}; diff --git a/Core/Graphics/Source/DeviceImpl.h b/Core/Graphics/Source/DeviceImpl.h index 6387fc28e..fb546fdbb 100644 --- a/Core/Graphics/Source/DeviceImpl.h +++ b/Core/Graphics/Source/DeviceImpl.h @@ -45,14 +45,14 @@ namespace Babylon::Graphics void UpdateSize(size_t width, size_t height); void UpdateMSAA(uint8_t value); void UpdateAlphaPremultiplied(bool enabled); - void UpdateDevicePixelRatio(float value); - void SetRenderResetCallback(std::function callback); void AddToJavaScript(Napi::Env); static DeviceImpl& GetFromJavaScript(Napi::Env); Napi::Value CreateContext(Napi::Env); + void SetRenderResetCallback(std::function callback); + void EnableRendering(); void DisableRendering(); diff --git a/Dependencies/CMakeLists.txt b/Dependencies/CMakeLists.txt index a09dec6c2..d18cfc314 100644 --- a/Dependencies/CMakeLists.txt +++ b/Dependencies/CMakeLists.txt @@ -40,7 +40,12 @@ set(BGFX_OPENGL_USE_EGL ON) set(BGFX_USE_DEBUG_SUFFIX OFF) FetchContent_MakeAvailable_With_Message(bgfx.cmake) +target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_DEFAULT_MAX_ENCODERS=2) target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MAX_VERTEX_STREAMS=18) +target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE=16) +target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_UNIFORM_BUFFER_SIZE=4096) +target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE=256) +target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE=1024) if(GRAPHICS_API STREQUAL "D3D11") target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_RENDERER_DIRECT3D11=1) elseif(GRAPHICS_API STREQUAL "D3D12") diff --git a/Plugins/ExternalTexture/Include/Babylon/Plugins/ExternalTexture.h b/Plugins/ExternalTexture/Include/Babylon/Plugins/ExternalTexture.h index 716fec8a6..1834ff9b8 100644 --- a/Plugins/ExternalTexture/Include/Babylon/Plugins/ExternalTexture.h +++ b/Plugins/ExternalTexture/Include/Babylon/Plugins/ExternalTexture.h @@ -28,14 +28,16 @@ namespace Babylon::Plugins // Returns the height of the texture. uint32_t Height() const; + // Returns the underlying texture. + Graphics::TextureT Get() const; + // Adds this texture to the graphics context of the given N-API environment. // NOTE: Must call from the JavaScript thread. - Napi::Promise AddToContextAsync(Napi::Env) const; + Napi::Promise AddToContextAsync(Napi::Env, std::optional layerIndex = {}) const; // Updates to a new texture. - // Texture attributes (width, height, format, etc.) must match. // NOTE: Must call from the Graphics thread. - void Update(Graphics::TextureT, std::optional = {}); + void Update(Graphics::TextureT, std::optional = {}, std::optional layerIndex = {}); private: class Impl; diff --git a/Plugins/ExternalTexture/Source/ExternalTexture_Base.h b/Plugins/ExternalTexture/Source/ExternalTexture_Base.h index d8e75e932..b70cba3dd 100644 --- a/Plugins/ExternalTexture/Source/ExternalTexture_Base.h +++ b/Plugins/ExternalTexture/Source/ExternalTexture_Base.h @@ -16,6 +16,7 @@ namespace Babylon::Plugins uint16_t Height() const { return m_info.Height; } bgfx::TextureFormat::Enum Format() const { return m_info.Format; } bool HasMips() const { return m_info.MipLevels != 1; } + uint16_t NumLayers() const { return m_info.NumLayers; } uint64_t Flags() const { return m_info.Flags; } void AddHandle(bgfx::TextureHandle handle) @@ -62,13 +63,13 @@ namespace Babylon::Plugins return BGFX_TEXTURE_NONE; } - void UpdateHandles(uintptr_t ptr) + void UpdateHandles(Graphics::TextureT ptr, std::optional layerIndex) { std::scoped_lock lock{m_mutex}; for (auto handle : m_handles) { - if (bgfx::overrideInternal(handle, ptr) == 0) + if (bgfx::overrideInternal(handle, uintptr_t(ptr), layerIndex.value_or(0)) == 0) { assert(!"Failed to override texture"); } @@ -80,6 +81,7 @@ namespace Babylon::Plugins uint16_t Width{}; uint16_t Height{}; uint16_t MipLevels{}; + uint16_t NumLayers{}; bgfx::TextureFormat::Enum Format{bgfx::TextureFormat::Unknown}; uint64_t Flags{}; }; diff --git a/Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp b/Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp index 09bef7173..15b285c59 100644 --- a/Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp +++ b/Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp @@ -159,11 +159,11 @@ namespace Babylon::Plugins public: // Implemented in ExternalTexture_Shared.h Impl(Graphics::TextureT, std::optional); - void Update(Graphics::TextureT, std::optional); + void Update(Graphics::TextureT, std::optional, std::optional); - uintptr_t Ptr() const + Graphics::TextureT Get() const { - return reinterpret_cast(m_ptr.get()); + return m_ptr.get(); } private: @@ -185,6 +185,7 @@ namespace Babylon::Plugins info.Width = static_cast(desc.Width); info.Height = static_cast(desc.Height); info.MipLevels = static_cast(desc.MipLevels); + info.NumLayers = static_cast(desc.ArraySize); if ((desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0) { @@ -213,7 +214,7 @@ namespace Babylon::Plugins } } - void Assign(Graphics::TextureT ptr) + void Set(Graphics::TextureT ptr) { m_ptr.copy_from(ptr); } diff --git a/Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp b/Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp index 0cbc39b8c..d585aa26f 100644 --- a/Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp +++ b/Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp @@ -159,11 +159,11 @@ namespace Babylon::Plugins public: // Implemented in ExternalTexture_Shared.h Impl(Graphics::TextureT, std::optional); - void Update(Graphics::TextureT, std::optional); + void Update(Graphics::TextureT, std::optional, std::optional); - uintptr_t Ptr() const + Graphics::TextureT Get() const { - return reinterpret_cast(m_ptr.get()); + return m_ptr.get(); } private: @@ -179,6 +179,7 @@ namespace Babylon::Plugins info.Width = static_cast(desc.Width); info.Height = static_cast(desc.Height); info.MipLevels = static_cast(desc.MipLevels); + info.NumLayers = static_cast(desc.DepthOrArraySize); if ((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0) { @@ -207,7 +208,7 @@ namespace Babylon::Plugins } } - void Assign(Graphics::TextureT ptr) + void Set(Graphics::TextureT ptr) { m_ptr.copy_from(ptr); } diff --git a/Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm b/Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm index 8a3cf925f..522c74274 100644 --- a/Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm +++ b/Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm @@ -8,7 +8,122 @@ // clang-format off -// Copied fom renderer_mtl.cpp +// Copied from renderer_mtl.h with unused pixel formats removed. +namespace +{ + // Metal API has obnoxious "availability" annotations on enums causing build errors when + // referencing format, and requiring use of ifdefery to reference format. To reduce ifdefery + // bgfx uses redefined formats, and on initialization it sets up format caps and provides + // availability. + constexpr MTLPixelFormat kMtlPixelFormatInvalid = MTLPixelFormat(0); + constexpr MTLPixelFormat kMtlPixelFormatA8Unorm = MTLPixelFormat(1); + constexpr MTLPixelFormat kMtlPixelFormatR8Unorm = MTLPixelFormat(10); + constexpr MTLPixelFormat kMtlPixelFormatR8Unorm_sRGB = MTLPixelFormat(11); + constexpr MTLPixelFormat kMtlPixelFormatR8Snorm = MTLPixelFormat(12); + constexpr MTLPixelFormat kMtlPixelFormatR8Uint = MTLPixelFormat(13); + constexpr MTLPixelFormat kMtlPixelFormatR8Sint = MTLPixelFormat(14); + constexpr MTLPixelFormat kMtlPixelFormatR16Unorm = MTLPixelFormat(20); + constexpr MTLPixelFormat kMtlPixelFormatR16Snorm = MTLPixelFormat(22); + constexpr MTLPixelFormat kMtlPixelFormatR16Uint = MTLPixelFormat(23); + constexpr MTLPixelFormat kMtlPixelFormatR16Sint = MTLPixelFormat(24); + constexpr MTLPixelFormat kMtlPixelFormatR16Float = MTLPixelFormat(25); + constexpr MTLPixelFormat kMtlPixelFormatRG8Unorm = MTLPixelFormat(30); + constexpr MTLPixelFormat kMtlPixelFormatRG8Unorm_sRGB = MTLPixelFormat(31); + constexpr MTLPixelFormat kMtlPixelFormatRG8Snorm = MTLPixelFormat(32); + constexpr MTLPixelFormat kMtlPixelFormatRG8Uint = MTLPixelFormat(33); + constexpr MTLPixelFormat kMtlPixelFormatRG8Sint = MTLPixelFormat(34); + constexpr MTLPixelFormat kMtlPixelFormatB5G6R5Unorm = MTLPixelFormat(40); + constexpr MTLPixelFormat kMtlPixelFormatABGR4Unorm = MTLPixelFormat(42); + constexpr MTLPixelFormat kMtlPixelFormatBGR5A1Unorm = MTLPixelFormat(43); + constexpr MTLPixelFormat kMtlPixelFormatR32Uint = MTLPixelFormat(53); + constexpr MTLPixelFormat kMtlPixelFormatR32Sint = MTLPixelFormat(54); + constexpr MTLPixelFormat kMtlPixelFormatR32Float = MTLPixelFormat(55); + constexpr MTLPixelFormat kMtlPixelFormatRG16Unorm = MTLPixelFormat(60); + constexpr MTLPixelFormat kMtlPixelFormatRG16Snorm = MTLPixelFormat(62); + constexpr MTLPixelFormat kMtlPixelFormatRG16Uint = MTLPixelFormat(63); + constexpr MTLPixelFormat kMtlPixelFormatRG16Sint = MTLPixelFormat(64); + constexpr MTLPixelFormat kMtlPixelFormatRG16Float = MTLPixelFormat(65); + constexpr MTLPixelFormat kMtlPixelFormatRGBA8Unorm = MTLPixelFormat(70); + constexpr MTLPixelFormat kMtlPixelFormatRGBA8Unorm_sRGB = MTLPixelFormat(71); + constexpr MTLPixelFormat kMtlPixelFormatRGBA8Snorm = MTLPixelFormat(72); + constexpr MTLPixelFormat kMtlPixelFormatRGBA8Uint = MTLPixelFormat(73); + constexpr MTLPixelFormat kMtlPixelFormatRGBA8Sint = MTLPixelFormat(74); + constexpr MTLPixelFormat kMtlPixelFormatBGRA8Unorm = MTLPixelFormat(80); + constexpr MTLPixelFormat kMtlPixelFormatBGRA8Unorm_sRGB = MTLPixelFormat(81); + constexpr MTLPixelFormat kMtlPixelFormatRGB10A2Unorm = MTLPixelFormat(90); + constexpr MTLPixelFormat kMtlPixelFormatRG11B10Float = MTLPixelFormat(92); + constexpr MTLPixelFormat kMtlPixelFormatRGB9E5Float = MTLPixelFormat(93); + constexpr MTLPixelFormat kMtlPixelFormatRG32Uint = MTLPixelFormat(103); + constexpr MTLPixelFormat kMtlPixelFormatRG32Sint = MTLPixelFormat(104); + constexpr MTLPixelFormat kMtlPixelFormatRG32Float = MTLPixelFormat(105); + constexpr MTLPixelFormat kMtlPixelFormatRGBA16Unorm = MTLPixelFormat(110); + constexpr MTLPixelFormat kMtlPixelFormatRGBA16Snorm = MTLPixelFormat(112); + constexpr MTLPixelFormat kMtlPixelFormatRGBA16Uint = MTLPixelFormat(113); + constexpr MTLPixelFormat kMtlPixelFormatRGBA16Sint = MTLPixelFormat(114); + constexpr MTLPixelFormat kMtlPixelFormatRGBA16Float = MTLPixelFormat(115); + constexpr MTLPixelFormat kMtlPixelFormatRGBA32Uint = MTLPixelFormat(123); + constexpr MTLPixelFormat kMtlPixelFormatRGBA32Sint = MTLPixelFormat(124); + constexpr MTLPixelFormat kMtlPixelFormatRGBA32Float = MTLPixelFormat(125); + constexpr MTLPixelFormat kMtlPixelFormatBC1_RGBA = MTLPixelFormat(130); + constexpr MTLPixelFormat kMtlPixelFormatBC1_RGBA_sRGB = MTLPixelFormat(131); + constexpr MTLPixelFormat kMtlPixelFormatBC2_RGBA = MTLPixelFormat(132); + constexpr MTLPixelFormat kMtlPixelFormatBC2_RGBA_sRGB = MTLPixelFormat(133); + constexpr MTLPixelFormat kMtlPixelFormatBC3_RGBA = MTLPixelFormat(134); + constexpr MTLPixelFormat kMtlPixelFormatBC3_RGBA_sRGB = MTLPixelFormat(135); + constexpr MTLPixelFormat kMtlPixelFormatBC4_RUnorm = MTLPixelFormat(140); + constexpr MTLPixelFormat kMtlPixelFormatBC5_RGUnorm = MTLPixelFormat(142); + constexpr MTLPixelFormat kMtlPixelFormatBC6H_RGBFloat = MTLPixelFormat(150); + constexpr MTLPixelFormat kMtlPixelFormatBC7_RGBAUnorm = MTLPixelFormat(152); + constexpr MTLPixelFormat kMtlPixelFormatBC7_RGBAUnorm_sRGB = MTLPixelFormat(153); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGB_2BPP = MTLPixelFormat(160); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGB_2BPP_sRGB = MTLPixelFormat(161); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGB_4BPP = MTLPixelFormat(162); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGB_4BPP_sRGB = MTLPixelFormat(163); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGBA_2BPP = MTLPixelFormat(164); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGBA_2BPP_sRGB = MTLPixelFormat(165); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGBA_4BPP = MTLPixelFormat(166); + constexpr MTLPixelFormat kMtlPixelFormatPVRTC_RGBA_4BPP_sRGB = MTLPixelFormat(167); + constexpr MTLPixelFormat kMtlPixelFormatEAC_RGBA8 = MTLPixelFormat(178); + constexpr MTLPixelFormat kMtlPixelFormatEAC_RGBA8_sRGB = MTLPixelFormat(179); + constexpr MTLPixelFormat kMtlPixelFormatETC2_RGB8 = MTLPixelFormat(180); + constexpr MTLPixelFormat kMtlPixelFormatETC2_RGB8_sRGB = MTLPixelFormat(181); + constexpr MTLPixelFormat kMtlPixelFormatETC2_RGB8A1 = MTLPixelFormat(182); + constexpr MTLPixelFormat kMtlPixelFormatETC2_RGB8A1_sRGB = MTLPixelFormat(183); + constexpr MTLPixelFormat kMtlPixelFormatASTC_4x4_sRGB = MTLPixelFormat(186); + constexpr MTLPixelFormat kMtlPixelFormatASTC_5x4_sRGB = MTLPixelFormat(187); + constexpr MTLPixelFormat kMtlPixelFormatASTC_5x5_sRGB = MTLPixelFormat(188); + constexpr MTLPixelFormat kMtlPixelFormatASTC_6x5_sRGB = MTLPixelFormat(189); + constexpr MTLPixelFormat kMtlPixelFormatASTC_6x6_sRGB = MTLPixelFormat(190); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x5_sRGB = MTLPixelFormat(192); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x6_sRGB = MTLPixelFormat(193); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x8_sRGB = MTLPixelFormat(194); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x5_sRGB = MTLPixelFormat(195); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x6_sRGB = MTLPixelFormat(196); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x8_sRGB = MTLPixelFormat(197); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x10_sRGB = MTLPixelFormat(198); + constexpr MTLPixelFormat kMtlPixelFormatASTC_12x10_sRGB = MTLPixelFormat(199); + constexpr MTLPixelFormat kMtlPixelFormatASTC_12x12_sRGB = MTLPixelFormat(200); + constexpr MTLPixelFormat kMtlPixelFormatASTC_4x4_LDR = MTLPixelFormat(204); + constexpr MTLPixelFormat kMtlPixelFormatASTC_5x4_LDR = MTLPixelFormat(205); + constexpr MTLPixelFormat kMtlPixelFormatASTC_5x5_LDR = MTLPixelFormat(206); + constexpr MTLPixelFormat kMtlPixelFormatASTC_6x5_LDR = MTLPixelFormat(207); + constexpr MTLPixelFormat kMtlPixelFormatASTC_6x6_LDR = MTLPixelFormat(208); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x5_LDR = MTLPixelFormat(210); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x6_LDR = MTLPixelFormat(211); + constexpr MTLPixelFormat kMtlPixelFormatASTC_8x8_LDR = MTLPixelFormat(212); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x5_LDR = MTLPixelFormat(213); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x6_LDR = MTLPixelFormat(214); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x8_LDR = MTLPixelFormat(215); + constexpr MTLPixelFormat kMtlPixelFormatASTC_10x10_LDR = MTLPixelFormat(216); + constexpr MTLPixelFormat kMtlPixelFormatASTC_12x10_LDR = MTLPixelFormat(217); + constexpr MTLPixelFormat kMtlPixelFormatASTC_12x12_LDR = MTLPixelFormat(218); + constexpr MTLPixelFormat kMtlPixelFormatDepth16Unorm = MTLPixelFormat(250); + constexpr MTLPixelFormat kMtlPixelFormatDepth32Float = MTLPixelFormat(252); + constexpr MTLPixelFormat kMtlPixelFormatStencil8 = MTLPixelFormat(253); + constexpr MTLPixelFormat kMtlPixelFormatDepth24Unorm_Stencil8 = MTLPixelFormat(255); +} + +// Copied from renderer_mtl.cpp namespace { struct TextureFormatInfo @@ -17,123 +132,106 @@ MTLPixelFormat m_fmtSrgb; }; - static TextureFormatInfo s_textureFormat[] = - { - { MTLPixelFormat(130/*BC1_RGBA*/), MTLPixelFormat(131/*BC1_RGBA_sRGB*/) }, // BC1 - { MTLPixelFormat(132/*BC2_RGBA*/), MTLPixelFormat(133/*BC2_RGBA_sRGB*/) }, // BC2 - { MTLPixelFormat(134/*BC3_RGBA*/), MTLPixelFormat(135/*BC3_RGBA_sRGB*/) }, // BC3 - { MTLPixelFormat(140/*BC4_RUnorm*/), MTLPixelFormatInvalid }, // BC4 - { MTLPixelFormat(142/*BC5_RGUnorm*/), MTLPixelFormatInvalid }, // BC5 - { MTLPixelFormat(150/*BC6H_RGBFloat*/), MTLPixelFormatInvalid }, // BC6H - { MTLPixelFormat(152/*BC7_RGBAUnorm*/), MTLPixelFormat(153/*BC7_RGBAUnorm_sRGB*/) }, // BC7 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ETC1 - { MTLPixelFormat(180/*ETC2_RGB8*/), MTLPixelFormat(181/*ETC2_RGB8_sRGB*/) }, // ETC2 - { MTLPixelFormat(178/*EAC_RGBA8*/), MTLPixelFormat(179/*EAC_RGBA8_sRGB*/) }, // ETC2A - { MTLPixelFormat(182/*ETC2_RGB8A1*/), MTLPixelFormat(183/*ETC2_RGB8A1_sRGB*/) }, // ETC2A1 - { MTLPixelFormat(160/*PVRTC_RGB_2BPP*/), MTLPixelFormat(161/*PVRTC_RGB_2BPP_sRGB*/) }, // PTC12 - { MTLPixelFormat(162/*PVRTC_RGB_4BPP*/), MTLPixelFormat(163/*PVRTC_RGB_4BPP_sRGB*/) }, // PTC14 - { MTLPixelFormat(164/*PVRTC_RGBA_2BPP*/), MTLPixelFormat(165/*PVRTC_RGBA_2BPP_sRGB*/) }, // PTC12A - { MTLPixelFormat(166/*PVRTC_RGBA_4BPP*/), MTLPixelFormat(167/*PVRTC_RGBA_4BPP_sRGB*/) }, // PTC14A - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC22 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC24 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATC - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATCE - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATCI -#if BX_PLATFORM_IOS && !TARGET_OS_MACCATALYST - { MTLPixelFormatASTC_4x4_LDR, MTLPixelFormatASTC_4x4_sRGB }, // ASTC4x4 - { MTLPixelFormatASTC_5x4_LDR, MTLPixelFormatASTC_5x4_sRGB }, // ASTC5x4 - { MTLPixelFormatASTC_5x5_LDR, MTLPixelFormatASTC_5x5_sRGB }, // ASTC5x5 - { MTLPixelFormatASTC_6x5_LDR, MTLPixelFormatASTC_6x5_sRGB }, // ASTC6x5 - { MTLPixelFormatASTC_6x6_LDR, MTLPixelFormatASTC_6x6_sRGB }, // ASTC6x6 - { MTLPixelFormatASTC_8x5_LDR, MTLPixelFormatASTC_8x5_sRGB }, // ASTC8x5 - { MTLPixelFormatASTC_8x6_LDR, MTLPixelFormatASTC_8x6_sRGB }, // ASTC8x6 - { MTLPixelFormatASTC_8x8_LDR, MTLPixelFormatASTC_8x8_sRGB }, // ASTC8x8 - { MTLPixelFormatASTC_10x5_LDR, MTLPixelFormatASTC_10x5_sRGB }, // ASTC10x5 - { MTLPixelFormatASTC_10x6_LDR, MTLPixelFormatASTC_10x6_sRGB }, // ASTC10x6 - { MTLPixelFormatASTC_10x8_LDR, MTLPixelFormatASTC_10x8_sRGB }, // ASTC10x8 - { MTLPixelFormatASTC_10x10_LDR, MTLPixelFormatASTC_10x10_sRGB }, // ASTC10x10 - { MTLPixelFormatASTC_12x10_LDR, MTLPixelFormatASTC_12x10_sRGB }, // ASTC12x10 - { MTLPixelFormatASTC_12x12_LDR, MTLPixelFormatASTC_12x12_sRGB }, // ASTC12x12 -#else - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC4x4 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC5x4 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC5x5 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC6x5 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC6x6 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x5 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x6 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x8 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x5 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x6 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x8 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x10 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC12x10 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC12x12 -#endif // BX_PLATFORM_IOS && !TARGET_OS_MACCATALYST - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // Unknown - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // R1 - { MTLPixelFormatA8Unorm, MTLPixelFormatInvalid }, // A8 - { MTLPixelFormatR8Unorm, MTLPixelFormat(11/*R8Unorm_sRGB*/) }, // R8 - { MTLPixelFormatR8Sint, MTLPixelFormatInvalid }, // R8I - { MTLPixelFormatR8Uint, MTLPixelFormatInvalid }, // R8U - { MTLPixelFormatR8Snorm, MTLPixelFormatInvalid }, // R8S - { MTLPixelFormatR16Unorm, MTLPixelFormatInvalid }, // R16 - { MTLPixelFormatR16Sint, MTLPixelFormatInvalid }, // R16I - { MTLPixelFormatR16Uint, MTLPixelFormatInvalid }, // R16U - { MTLPixelFormatR16Float, MTLPixelFormatInvalid }, // R16F - { MTLPixelFormatR16Snorm, MTLPixelFormatInvalid }, // R16S - { MTLPixelFormatR32Sint, MTLPixelFormatInvalid }, // R32I - { MTLPixelFormatR32Uint, MTLPixelFormatInvalid }, // R32U - { MTLPixelFormatR32Float, MTLPixelFormatInvalid }, // R32F - { MTLPixelFormatRG8Unorm, MTLPixelFormat(31/*RG8Unorm_sRGB*/) }, // RG8 - { MTLPixelFormatRG8Sint, MTLPixelFormatInvalid }, // RG8I - { MTLPixelFormatRG8Uint, MTLPixelFormatInvalid }, // RG8U - { MTLPixelFormatRG8Snorm, MTLPixelFormatInvalid }, // RG8S - { MTLPixelFormatRG16Unorm, MTLPixelFormatInvalid }, // RG16 - { MTLPixelFormatRG16Sint, MTLPixelFormatInvalid }, // RG16I - { MTLPixelFormatRG16Uint, MTLPixelFormatInvalid }, // RG16U - { MTLPixelFormatRG16Float, MTLPixelFormatInvalid }, // RG16F - { MTLPixelFormatRG16Snorm, MTLPixelFormatInvalid }, // RG16S - { MTLPixelFormatRG32Sint, MTLPixelFormatInvalid }, // RG32I - { MTLPixelFormatRG32Uint, MTLPixelFormatInvalid }, // RG32U - { MTLPixelFormatRG32Float, MTLPixelFormatInvalid }, // RG32F - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8I - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8U - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8S - { MTLPixelFormatRGB9E5Float, MTLPixelFormatInvalid }, // RGB9E5F - { MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB }, // BGRA8 - { MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB }, // RGBA8 - { MTLPixelFormatRGBA8Sint, MTLPixelFormatInvalid }, // RGBA8I - { MTLPixelFormatRGBA8Uint, MTLPixelFormatInvalid }, // RGBA8U - { MTLPixelFormatRGBA8Snorm, MTLPixelFormatInvalid }, // RGBA8S - { MTLPixelFormatRGBA16Unorm, MTLPixelFormatInvalid }, // RGBA16 - { MTLPixelFormatRGBA16Sint, MTLPixelFormatInvalid }, // RGBA16I - { MTLPixelFormatRGBA16Uint, MTLPixelFormatInvalid }, // RGBA16U - { MTLPixelFormatRGBA16Float, MTLPixelFormatInvalid }, // RGBA16F - { MTLPixelFormatRGBA16Snorm, MTLPixelFormatInvalid }, // RGBA16S - { MTLPixelFormatRGBA32Sint, MTLPixelFormatInvalid }, // RGBA32I - { MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid }, // RGBA32U - { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid }, // RGBA32F - { MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid }, // B5G6R5 - { MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid }, // R5G6B5 - { MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid }, // BGRA4 - { MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid }, // RGBA4 - { MTLPixelFormatBGR5A1Unorm, MTLPixelFormatInvalid }, // BGR5A1 - { MTLPixelFormatBGR5A1Unorm, MTLPixelFormatInvalid }, // RGB5A1 - { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid }, // RGB10A2 - { MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid }, // RG11B10F - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // UnknownDepth - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24 - { MTLPixelFormat(255/*Depth24Unorm_Stencil8*/), MTLPixelFormatInvalid }, // D24S8 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16F - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24F - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32F - { MTLPixelFormatStencil8, MTLPixelFormatInvalid }, // D0S8 - }; - static_assert(bgfx::TextureFormat::Count == BX_COUNTOF(s_textureFormat)); + static TextureFormatInfo s_textureFormat[] = + { + { kMtlPixelFormatBC1_RGBA, kMtlPixelFormatBC1_RGBA_sRGB, }, // BC1 + { kMtlPixelFormatBC2_RGBA, kMtlPixelFormatBC2_RGBA_sRGB, }, // BC2 + { kMtlPixelFormatBC3_RGBA, kMtlPixelFormatBC3_RGBA_sRGB, }, // BC3 + { kMtlPixelFormatBC4_RUnorm, kMtlPixelFormatInvalid, }, // BC4 + { kMtlPixelFormatBC5_RGUnorm, kMtlPixelFormatInvalid, }, // BC5 + { kMtlPixelFormatBC6H_RGBFloat, kMtlPixelFormatInvalid, }, // BC6H + { kMtlPixelFormatBC7_RGBAUnorm, kMtlPixelFormatBC7_RGBAUnorm_sRGB, }, // BC7 + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // ETC1 + { kMtlPixelFormatETC2_RGB8, kMtlPixelFormatETC2_RGB8_sRGB, }, // ETC2 + { kMtlPixelFormatEAC_RGBA8, kMtlPixelFormatEAC_RGBA8_sRGB, }, // ETC2A + { kMtlPixelFormatETC2_RGB8A1, kMtlPixelFormatETC2_RGB8A1_sRGB, }, // ETC2A1 + { kMtlPixelFormatPVRTC_RGB_2BPP, kMtlPixelFormatPVRTC_RGB_2BPP_sRGB, }, // PTC12 + { kMtlPixelFormatPVRTC_RGB_4BPP, kMtlPixelFormatPVRTC_RGB_4BPP_sRGB, }, // PTC14 + { kMtlPixelFormatPVRTC_RGBA_2BPP, kMtlPixelFormatPVRTC_RGBA_2BPP_sRGB, }, // PTC12A + { kMtlPixelFormatPVRTC_RGBA_4BPP, kMtlPixelFormatPVRTC_RGBA_4BPP_sRGB, }, // PTC14A + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // PTC22 + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // PTC24 + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // ATC + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // ATCE + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // ATCI + { kMtlPixelFormatASTC_4x4_LDR, kMtlPixelFormatASTC_4x4_sRGB, }, // ASTC4x4 + { kMtlPixelFormatASTC_5x4_LDR, kMtlPixelFormatASTC_5x4_sRGB, }, // ASTC5x4 + { kMtlPixelFormatASTC_5x5_LDR, kMtlPixelFormatASTC_5x5_sRGB, }, // ASTC5x5 + { kMtlPixelFormatASTC_6x5_LDR, kMtlPixelFormatASTC_6x5_sRGB, }, // ASTC6x5 + { kMtlPixelFormatASTC_6x6_LDR, kMtlPixelFormatASTC_6x6_sRGB, }, // ASTC6x6 + { kMtlPixelFormatASTC_8x5_LDR, kMtlPixelFormatASTC_8x5_sRGB, }, // ASTC8x5 + { kMtlPixelFormatASTC_8x6_LDR, kMtlPixelFormatASTC_8x6_sRGB, }, // ASTC8x6 + { kMtlPixelFormatASTC_8x8_LDR, kMtlPixelFormatASTC_8x8_sRGB, }, // ASTC8x8 + { kMtlPixelFormatASTC_10x5_LDR, kMtlPixelFormatASTC_10x5_sRGB, }, // ASTC10x5 + { kMtlPixelFormatASTC_10x6_LDR, kMtlPixelFormatASTC_10x6_sRGB, }, // ASTC10x6 + { kMtlPixelFormatASTC_10x8_LDR, kMtlPixelFormatASTC_10x8_sRGB, }, // ASTC10x8 + { kMtlPixelFormatASTC_10x10_LDR, kMtlPixelFormatASTC_10x10_sRGB, }, // ASTC10x10 + { kMtlPixelFormatASTC_12x10_LDR, kMtlPixelFormatASTC_12x10_sRGB, }, // ASTC12x10 + { kMtlPixelFormatASTC_12x12_LDR, kMtlPixelFormatASTC_12x12_sRGB, }, // ASTC12x12 + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // Unknown + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // R1 + { kMtlPixelFormatA8Unorm, kMtlPixelFormatInvalid, }, // A8 + { kMtlPixelFormatR8Unorm, kMtlPixelFormatR8Unorm_sRGB, }, // R8 + { kMtlPixelFormatR8Sint, kMtlPixelFormatInvalid, }, // R8I + { kMtlPixelFormatR8Uint, kMtlPixelFormatInvalid, }, // R8U + { kMtlPixelFormatR8Snorm, kMtlPixelFormatInvalid, }, // R8S + { kMtlPixelFormatR16Unorm, kMtlPixelFormatInvalid, }, // R16 + { kMtlPixelFormatR16Sint, kMtlPixelFormatInvalid, }, // R16I + { kMtlPixelFormatR16Uint, kMtlPixelFormatInvalid, }, // R16U + { kMtlPixelFormatR16Float, kMtlPixelFormatInvalid, }, // R16F + { kMtlPixelFormatR16Snorm, kMtlPixelFormatInvalid, }, // R16S + { kMtlPixelFormatR32Sint, kMtlPixelFormatInvalid, }, // R32I + { kMtlPixelFormatR32Uint, kMtlPixelFormatInvalid, }, // R32U + { kMtlPixelFormatR32Float, kMtlPixelFormatInvalid, }, // R32F + { kMtlPixelFormatRG8Unorm, kMtlPixelFormatRG8Unorm_sRGB, }, // RG8 + { kMtlPixelFormatRG8Sint, kMtlPixelFormatInvalid, }, // RG8I + { kMtlPixelFormatRG8Uint, kMtlPixelFormatInvalid, }, // RG8U + { kMtlPixelFormatRG8Snorm, kMtlPixelFormatInvalid, }, // RG8S + { kMtlPixelFormatRG16Unorm, kMtlPixelFormatInvalid, }, // RG16 + { kMtlPixelFormatRG16Sint, kMtlPixelFormatInvalid, }, // RG16I + { kMtlPixelFormatRG16Uint, kMtlPixelFormatInvalid, }, // RG16U + { kMtlPixelFormatRG16Float, kMtlPixelFormatInvalid, }, // RG16F + { kMtlPixelFormatRG16Snorm, kMtlPixelFormatInvalid, }, // RG16S + { kMtlPixelFormatRG32Sint, kMtlPixelFormatInvalid, }, // RG32I + { kMtlPixelFormatRG32Uint, kMtlPixelFormatInvalid, }, // RG32U + { kMtlPixelFormatRG32Float, kMtlPixelFormatInvalid, }, // RG32F + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // RGB8 + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // RGB8I + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // RGB8U + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // RGB8S + { kMtlPixelFormatRGB9E5Float, kMtlPixelFormatInvalid, }, // RGB9E5F + { kMtlPixelFormatBGRA8Unorm, kMtlPixelFormatBGRA8Unorm_sRGB, }, // BGRA8 + { kMtlPixelFormatRGBA8Unorm, kMtlPixelFormatRGBA8Unorm_sRGB, }, // RGBA8 + { kMtlPixelFormatRGBA8Sint, kMtlPixelFormatInvalid, }, // RGBA8I + { kMtlPixelFormatRGBA8Uint, kMtlPixelFormatInvalid, }, // RGBA8U + { kMtlPixelFormatRGBA8Snorm, kMtlPixelFormatInvalid, }, // RGBA8S + { kMtlPixelFormatRGBA16Unorm, kMtlPixelFormatInvalid, }, // RGBA16 + { kMtlPixelFormatRGBA16Sint, kMtlPixelFormatInvalid, }, // RGBA16I + { kMtlPixelFormatRGBA16Uint, kMtlPixelFormatInvalid, }, // RGBA16U + { kMtlPixelFormatRGBA16Float, kMtlPixelFormatInvalid, }, // RGBA16F + { kMtlPixelFormatRGBA16Snorm, kMtlPixelFormatInvalid, }, // RGBA16S + { kMtlPixelFormatRGBA32Sint, kMtlPixelFormatInvalid, }, // RGBA32I + { kMtlPixelFormatRGBA32Uint, kMtlPixelFormatInvalid, }, // RGBA32U + { kMtlPixelFormatRGBA32Float, kMtlPixelFormatInvalid, }, // RGBA32F + { kMtlPixelFormatB5G6R5Unorm, kMtlPixelFormatInvalid, }, // B5G6R5 + { kMtlPixelFormatB5G6R5Unorm, kMtlPixelFormatInvalid, }, // R5G6B5 + { kMtlPixelFormatABGR4Unorm, kMtlPixelFormatInvalid, }, // BGRA4 + { kMtlPixelFormatABGR4Unorm, kMtlPixelFormatInvalid, }, // RGBA4 + { kMtlPixelFormatBGR5A1Unorm, kMtlPixelFormatInvalid, }, // BGR5A1 + { kMtlPixelFormatBGR5A1Unorm, kMtlPixelFormatInvalid, }, // RGB5A1 + { kMtlPixelFormatRGB10A2Unorm, kMtlPixelFormatInvalid, }, // RGB10A2 + { kMtlPixelFormatRG11B10Float, kMtlPixelFormatInvalid, }, // RG11B10F + { kMtlPixelFormatInvalid, kMtlPixelFormatInvalid, }, // UnknownDepth + { kMtlPixelFormatDepth16Unorm, kMtlPixelFormatInvalid, }, // D16 + { kMtlPixelFormatDepth32Float, kMtlPixelFormatInvalid, }, // D24 + { kMtlPixelFormatDepth24Unorm_Stencil8, kMtlPixelFormatInvalid, }, // D24S8 + { kMtlPixelFormatDepth32Float, kMtlPixelFormatInvalid, }, // D32 + { kMtlPixelFormatDepth32Float, kMtlPixelFormatInvalid, }, // D16F + { kMtlPixelFormatDepth32Float, kMtlPixelFormatInvalid, }, // D24F + { kMtlPixelFormatDepth32Float, kMtlPixelFormatInvalid, }, // D32F + { kMtlPixelFormatStencil8, kMtlPixelFormatInvalid, }, // D0S8 + }; + static_assert(bgfx::TextureFormat::Count == BX_COUNTOF(s_textureFormat) ); } // clang-format on @@ -145,11 +243,11 @@ public: // Implemented in ExternalTexture_Shared.h Impl(Graphics::TextureT, std::optional); - void Update(Graphics::TextureT, std::optional); + void Update(Graphics::TextureT, std::optional, std::optional); - uintptr_t Ptr() const + Graphics::TextureT Get() const { - return reinterpret_cast(m_ptr); + return m_ptr; } private: @@ -163,6 +261,7 @@ static void GetInfo(Graphics::TextureT ptr, std::optional(ptr.width); info.Height = static_cast(ptr.height); info.MipLevels = static_cast(ptr.mipmapLevelCount); + info.NumLayers = static_cast(ptr.arrayLength); if ((ptr.usage & MTLTextureUsageRenderTarget) != 0) { @@ -190,7 +289,7 @@ static void GetInfo(Graphics::TextureT ptr, std::optional); - void Update(Graphics::TextureT, std::optional); + void Update(Graphics::TextureT, std::optional, std::optional); - uintptr_t Ptr() const + Graphics::TextureT Get() const { throw std::runtime_error{"not implemented"}; } @@ -29,7 +29,7 @@ namespace Babylon::Plugins throw std::runtime_error{"not implemented"}; } - void Assign(Graphics::TextureT) + void Set(Graphics::TextureT) { throw std::runtime_error{"not implemented"}; } diff --git a/Plugins/ExternalTexture/Source/ExternalTexture_Shared.h b/Plugins/ExternalTexture/Source/ExternalTexture_Shared.h index 0734c0ab9..9fbfc3e3c 100644 --- a/Plugins/ExternalTexture/Source/ExternalTexture_Shared.h +++ b/Plugins/ExternalTexture/Source/ExternalTexture_Shared.h @@ -11,30 +11,25 @@ namespace Babylon::Plugins throw std::runtime_error{"Unsupported texture mip levels"}; } - Assign(ptr); + Set(ptr); } - void ExternalTexture::Impl::Update(Graphics::TextureT ptr, std::optional overrideFormat) + void ExternalTexture::Impl::Update(Graphics::TextureT ptr, std::optional overrideFormat, std::optional layerIndex) { Info info; GetInfo(ptr, overrideFormat, info); - if (info.Width != m_info.Width || info.Height != m_info.Height || info.MipLevels != m_info.MipLevels) - { - throw std::runtime_error{"Textures must have same width, height, and mip levels"}; - } - - DEBUG_TRACE("ExternalTexture [0x%p] Update %d x %d %d mips", this, int(info.Width), int(info.Height), int(info.MipLevels)); + DEBUG_TRACE("ExternalTexture [0x%p] Update %d x %d %d mips %d layers", + this, int(info.Width), int(info.Height), int(info.MipLevels), int(info.NumLayers)); m_info = info; - Assign(ptr); - - UpdateHandles(Ptr()); + Set(ptr); + UpdateHandles(ptr, layerIndex); } ExternalTexture::ExternalTexture(Graphics::TextureT ptr, std::optional overrideFormat) - : m_impl{std::make_unique(ptr, overrideFormat)} + : m_impl{std::make_shared(ptr, overrideFormat)} { } @@ -58,7 +53,12 @@ namespace Babylon::Plugins return m_impl->Height(); } - Napi::Promise ExternalTexture::AddToContextAsync(Napi::Env env) const + Graphics::TextureT ExternalTexture::Get() const + { + return m_impl->Get(); + } + + Napi::Promise ExternalTexture::AddToContextAsync(Napi::Env env, std::optional layerIndex) const { Graphics::DeviceContext& context = Graphics::DeviceContext::GetFromJavaScript(env); JsRuntime& runtime = JsRuntime::GetFromJavaScript(env); @@ -69,11 +69,12 @@ namespace Babylon::Plugins DEBUG_TRACE("ExternalTexture [0x%p] AddToContextAsync", m_impl.get()); arcana::make_task(context.BeforeRenderScheduler(), arcana::cancellation_source::none(), - [&context, &runtime, deferred = std::move(deferred), impl = m_impl]() { + [&context, &runtime, deferred = std::move(deferred), impl = m_impl, layerIndex = std::move(layerIndex)]() { // REVIEW: The bgfx texture handle probably needs to be an RAII object to make sure it gets clean up during the asynchrony. // For example, if any of the schedulers/dispatches below don't fire, then the texture handle will leak. - bgfx::TextureHandle handle = bgfx::createTexture2D(impl->Width(), impl->Height(), impl->HasMips(), 1, impl->Format(), impl->Flags()); - DEBUG_TRACE("ExternalTexture [0x%p] create %d x %d %d mips. Format : %d Flags : %d. (bgfx handle id %d)", impl.get(), int(impl->Width()), int(impl->Height()), int(impl->HasMips()), int(impl->Format()), int(impl->Flags()), int(handle.idx)); + bgfx::TextureHandle handle = bgfx::createTexture2D(impl->Width(), impl->Height(), impl->HasMips(), impl->NumLayers(), impl->Format(), impl->Flags()); + DEBUG_TRACE("ExternalTexture [0x%p] create %d x %d %d mips %d layers. Format : %d Flags : %d. (bgfx handle id %d)", + impl.get(), int(impl->Width()), int(impl->Height()), int(impl->HasMips()), int(impl->NumLayers()), int(impl->Format()), int(impl->Flags()), int(handle.idx)); if (!bgfx::isValid(handle)) { DEBUG_TRACE("ExternalTexture [0x%p] is not valid", impl.get()); @@ -84,8 +85,8 @@ namespace Babylon::Plugins return; } - arcana::make_task(context.AfterRenderScheduler(), arcana::cancellation_source::none(), [&runtime, &context, deferred = std::move(deferred), handle, impl = std::move(impl)]() { - if (bgfx::overrideInternal(handle, impl->Ptr()) == 0) + arcana::make_task(context.AfterRenderScheduler(), arcana::cancellation_source::none(), [&runtime, &context, deferred = std::move(deferred), handle, impl = std::move(impl), layerIndex = std::move(layerIndex)]() { + if (bgfx::overrideInternal(handle, uintptr_t(impl->Get()), layerIndex.value_or(0)) == 0) { runtime.Dispatch([deferred = std::move(deferred), handle](Napi::Env env) { bgfx::destroy(handle); @@ -119,8 +120,8 @@ namespace Babylon::Plugins return promise; } - void ExternalTexture::Update(Graphics::TextureT ptr, std::optional overrideFormat) + void ExternalTexture::Update(Graphics::TextureT ptr, std::optional overrideFormat, std::optional layerIndex) { - m_impl->Update(ptr, overrideFormat); + m_impl->Update(ptr, overrideFormat, layerIndex); } }