Skip to content
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

Flush onscreen texture to offscreen texture on context switches #158

Closed
wants to merge 2 commits into from
Closed
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
67 changes: 48 additions & 19 deletions packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,42 @@ class SurfaceInfo {
}
}

void *switchToOffscreen() {
void *switchToOffscreen() {
std::unique_lock<std::shared_mutex> lock(_mutex);
wgpu::TextureDescriptor textureDesc;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
texture = config.device.CreateTexture(&textureDesc);
_createOffscreenTexture();

if (surface) {
// Copy the current surface texture to the offscreen texture
wgpu::CommandEncoderDescriptor encoderDesc;
wgpu::CommandEncoder encoder = config.device.CreateCommandEncoder(&encoderDesc);

wgpu::ImageCopyTexture sourceTexture = {};
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);
sourceTexture.texture = surfaceTexture.texture;

wgpu::ImageCopyTexture destinationTexture = {};
destinationTexture.texture = texture;

wgpu::Extent3D copySize = {
static_cast<uint32_t>(config.width),
static_cast<uint32_t>(config.height),
1
};

encoder.CopyTextureToTexture(&sourceTexture, &destinationTexture, &copySize);

wgpu::CommandBuffer commands = encoder.Finish();
config.device.GetQueue().Submit(1, &commands);

// Unconfigure the surface
surface.Unconfigure();
}

void* oldNativeSurface = nativeSurface;
surface = nullptr;
return nativeSurface;
nativeSurface = nullptr;
return oldNativeSurface;
}

void switchToOnscreen(void *newNativeSurface, wgpu::Surface newSurface) {
Expand All @@ -69,7 +93,6 @@ class SurfaceInfo {
// If we are comming from an offscreen context, we need to configure the new
// surface
if (texture != nullptr) {
config.usage = config.usage | wgpu::TextureUsage::CopyDst;
_configure();
// We flush the offscreen texture to the onscreen one
// TODO: there is a faster way to do this without validation?
Expand Down Expand Up @@ -146,19 +169,25 @@ class SurfaceInfo {
private:
void _configure() {
if (surface) {
config.usage = config.usage | wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc;
surface.Configure(&config);
} else {
wgpu::TextureDescriptor textureDesc;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
texture = config.device.CreateTexture(&textureDesc);
_createOffscreenTexture();
}
}

void _createOffscreenTexture() {
wgpu::TextureDescriptor textureDesc;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::CopyDst |
wgpu::TextureUsage::TextureBinding;
texture = config.device.CreateTexture(&textureDesc);
}

mutable std::shared_mutex _mutex;
void *nativeSurface = nullptr;
wgpu::Surface surface = nullptr;
Expand Down
Loading