Skip to content

Fix a few issues around readTexture and iOS camera #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Core/Graphics/Source/DeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ namespace Babylon::Graphics

if (m_state.Bgfx.Initialized)
{
// Drain readTextures queue, completing them in an error state.
while (!m_readTextureRequests.empty())
{
auto error = arcana::make_unexpected(std::make_exception_ptr(std::system_error(std::make_error_code(std::errc::operation_canceled))));
m_readTextureRequests.front().second.complete(error);
m_readTextureRequests.pop();
}

// HACK: Render one more frame to drain the before/after render work queues.
StartRenderingCurrentFrame();
FinishRenderingCurrentFrame();
Expand Down Expand Up @@ -390,8 +398,7 @@ namespace Babylon::Graphics
uint32_t frameNumber{bgfx::frame()};

// Process read texture requests.
assert(m_readTextureRequests.empty() || m_readTextureRequests.front().first >= frameNumber);
while (!m_readTextureRequests.empty() && m_readTextureRequests.front().first == frameNumber)
while (!m_readTextureRequests.empty() && m_readTextureRequests.front().first <= frameNumber)
{
m_readTextureRequests.front().second.complete();
m_readTextureRequests.pop();
Expand Down
14 changes: 10 additions & 4 deletions Plugins/NativeCamera/Source/Apple/NativeCameraImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ fragment float4 fragmentShader(RasterizerData in [[stage_in]],

__block arcana::task_completion_source<Camera::Impl::CameraDimensions, std::exception_ptr> taskCompletionSource{};

dispatch_sync(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
CVMetalTextureCacheCreate(nullptr, nullptr, m_implData->metalDevice, nullptr, &m_implData->textureCache);
m_implData->cameraTextureDelegate = [[CameraTextureDelegate alloc]init:m_implData];
m_implData->avCaptureSession = [[AVCaptureSession alloc] init];

m_implData->textureRGBA = nil;

#if (TARGET_OS_IPHONE)
// Loop over all available camera configurations to find a config that most closely matches the constraints.
AVCaptureDevice* bestDevice{nullptr};
Expand Down Expand Up @@ -395,9 +396,14 @@ fragment float4 fragmentShader(RasterizerData in [[stage_in]],
width = [textureY width];
height = [textureY height];
}

// Skip processing this frame if width and height are invalid.
if (width == 0 || height == 0) {
return;
}

// Recreate the output texture when the camera dimensions change.
if (m_cameraDimensions.width != width || m_cameraDimensions.height != height)
if (m_implData->textureRGBA == nil || m_cameraDimensions.width != width || m_cameraDimensions.height != height)
{
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:width height:height mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
Expand Down Expand Up @@ -457,7 +463,7 @@ fragment float4 fragmentShader(RasterizerData in [[stage_in]],

void Camera::Impl::Close()
{
m_implData.reset();
m_implData.reset(new ImplData);
}
}

Expand Down
1 change: 1 addition & 0 deletions Plugins/NativeCapture/Source/NativeCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace
// Reading the texture is an async operation, but everything that needs to be done prior to future write operations on that texture is completed synchronously,
// so we kick off the read for the next frame prior to the read for the current frame completes.

// TODO: #1131 Address potential race condition during engine shutdown and ReadTextureAsync.
arcana::task<void, std::exception_ptr> readCurrentFrameTask{thisRef->m_graphicsContext.ReadTextureAsync(thisRef->m_blitTextureHandle, thisRef->m_textureBuffer)
.then(arcana::inline_scheduler, thisRef->m_cancellationToken, [thisRef] {
thisRef->m_frameCallback(thisRef->m_textureInfo.Width, thisRef->m_textureInfo.Height, thisRef->m_textureInfo.Format, bgfx::getCaps()->originBottomLeft, thisRef->m_textureBuffer);
Expand Down