Skip to content

Commit

Permalink
Get unit and render tests running on iOS simulator (#2129)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Feb 21, 2024
1 parent 61fd33f commit 02eeec1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/mbgl/mtl/offscreen_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class OffscreenTextureResource final : public RenderableResource {
->setUsage(MTL::TextureUsageShaderRead | MTL::TextureUsageShaderWrite | MTL::TextureUsageRenderTarget);
}

// On iOS simulator, the depth target is PixelFormatDepth32Float_Stencil8
#if !TARGET_OS_SIMULATOR
if (stencil) {
stencilTexture = context.createTexture2D();
stencilTexture->setSize(size);
Expand All @@ -44,6 +46,8 @@ class OffscreenTextureResource final : public RenderableResource {
static_cast<Texture2D*>(stencilTexture.get())
->setUsage(MTL::TextureUsageShaderRead | MTL::TextureUsageShaderWrite | MTL::TextureUsageRenderTarget);
}
#endif

context.renderingStats().numFrameBuffers++;
}

Expand Down
31 changes: 31 additions & 0 deletions src/mbgl/mtl/texture2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ size_t Texture2D::numChannels() const noexcept {
}

MTL::PixelFormat Texture2D::getMetalPixelFormat() const noexcept {
// On iOS simulator, we need to use the combined depth/stencil format. If the depth and stencil
// formats are both set on a render pipeline, they have to be identical or we'll get, e.g.:
// validateWithDevice:4343: failed assertion `Render Pipeline Descriptor Validation
// depthAttachmentPixelFormat (MTLPixelFormatDepth32Float) and
// stencilAttachmentPixelFormat (MTLPixelFormatStencil8) must match.

#if TARGET_OS_SIMULATOR
if (channelType == gfx::TextureChannelDataType::Float && pixelFormat == gfx::TexturePixelType::Depth &&
(usage & MTL::TextureUsageRenderTarget)) {
return MTL::PixelFormatDepth32Float_Stencil8;
}
#endif

switch (channelType) {
case gfx::TextureChannelDataType::UnsignedByte:
switch (pixelFormat) {
Expand Down Expand Up @@ -138,6 +151,24 @@ void Texture2D::createMetalTexture() noexcept {
if (auto textureDescriptor = NS::RetainPtr(
MTL::TextureDescriptor::texture2DDescriptor(format, size.width, size.height, /*mipmapped=*/false))) {
textureDescriptor->setUsage(usage);
#if TARGET_OS_SIMULATOR
switch (format) {
case MTL::PixelFormatDepth16Unorm:
case MTL::PixelFormatDepth32Float:
case MTL::PixelFormatStencil8:
case MTL::PixelFormatDepth24Unorm_Stencil8:
case MTL::PixelFormatDepth32Float_Stencil8:
case MTL::PixelFormatX32_Stencil8:
case MTL::PixelFormatX24_Stencil8:
// On iOS simulator, the default shared mode is invalid for depth and stencil textures.
// 'Texture Descriptor Validation MTLTextureDescriptor: Depth, Stencil, DepthStencil
// textures cannot be allocated with MTLStorageModeShared on this device.
textureDescriptor->setStorageMode(MTL::StorageMode::StorageModePrivate);
break;
default:
break;
}
#endif
metalTexture = context.createMetalTexture(std::move(textureDescriptor));
}

Expand Down
3 changes: 2 additions & 1 deletion test/include/mbgl/test/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#define TEST_READ_ONLY 0

#if !ANDROID
// iOS simulator server can work if port 3000 is available
#if !ANDROID && !TARGET_OS_SIMULATOR
#ifndef TEST_HAS_SERVER
#define TEST_HAS_SERVER 1
#endif
Expand Down
1 change: 0 additions & 1 deletion test/map/map.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <mbgl/gfx/backend_scope.hpp>
#include <mbgl/gfx/headless_frontend.hpp>
#include <mbgl/gfx/shader_registry.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/map/map_options.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/renderer/renderer.hpp>
Expand Down

0 comments on commit 02eeec1

Please sign in to comment.