Skip to content
Snippets Groups Projects
Commit 0849386d authored by Jaswant Panchumarti (Kitware)'s avatar Jaswant Panchumarti (Kitware)
Browse files

Refactor color attachment and depth stencil setup

- The new `ReadTextureFromGPU` method is used to copy a `wgpu::Texture` into a `wgpu::Buffer`, then map the buffer to the CPU. The callback function will be invoked with the mapped data.
- The GetPixelData(), GetRGBAPixelData() and GetRGBACharPixel() methods have all been refactored to now leverage `ReadTextureFromGPU`.
parent 76cdf6e3
No related branches found
No related tags found
No related merge requests found
......@@ -112,6 +112,7 @@ set(private_shader_files
wgsl/ActorColorOptions.wgsl
wgsl/ActorRenderOptions.wgsl
wgsl/ActorTransform.wgsl
wgsl/CopyDepthTextureToBuffer.wgsl
wgsl/FrustumCullingShader.wgsl
wgsl/LineFragmentShader.wgsl
wgsl/LineGlyphShader.wgsl
......
......@@ -137,7 +137,13 @@ void vtkWebGPURenderPipelineCache::CreateRenderPipeline(wgpu::RenderPipelineDesc
vtkWebGPURenderer* wgpuRenderer, const char* shaderSource)
{
auto* wgpuRenderWindow = vtkWebGPURenderWindow::SafeDownCast(wgpuRenderer->GetRenderWindow());
this->CreateRenderPipeline(descriptor, wgpuRenderWindow, shaderSource);
}
//------------------------------------------------------------------------------
void vtkWebGPURenderPipelineCache::CreateRenderPipeline(wgpu::RenderPipelineDescriptor* descriptor,
vtkWebGPURenderWindow* wgpuRenderWindow, const char* shaderSource)
{
// apply all shader source replacements.
const auto source = wgpuRenderWindow->PreprocessShaderSource(shaderSource);
......
......@@ -34,6 +34,7 @@
VTK_ABI_NAMESPACE_BEGIN
class vtkWebGPURenderer;
class vtkWebGPURenderWindow;
class vtkWindow;
class VTKRENDERINGWEBGPU_EXPORT vtkWebGPURenderPipelineCache : public vtkObject
......@@ -65,6 +66,9 @@ public:
void CreateRenderPipeline(wgpu::RenderPipelineDescriptor* descriptor,
vtkWebGPURenderer* wgpuRenderer, const char* shaderSource);
void CreateRenderPipeline(wgpu::RenderPipelineDescriptor* descriptor,
vtkWebGPURenderWindow* wgpuRenderWindow, const char* shaderSource);
/**
* Destroy the render pipeline associated with the given hash.
*/
......
This diff is collapsed.
......@@ -85,11 +85,6 @@ public:
const char* GetRenderingBackend() override;
/**
* Reads pixels into the `CachedPixelBytes` variable.
*/
void ReadPixels();
///@{
/**
* Set/Get the pixel data of an image, transmitted as RGBRGB...
......@@ -269,6 +264,9 @@ public:
vtkSmartPointer<vtkWebGPUComputeRenderTexture> AcquireFramebufferRenderTexture();
///@}
using TextureMapCallback =
std::function<void(const void* mappedData, int bytesPerRow, void* userdata)>;
protected:
vtkWebGPURenderWindow();
~vtkWebGPURenderWindow() override;
......@@ -289,14 +287,14 @@ protected:
void ConfigureSurface();
void UnconfigureSurface();
void CreateOffscreenColorAttachments();
void DestroyOffscreenColorAttachments();
void CreateOffscreenColorAttachment();
void DestroyOffscreenColorAttachment();
void CreateDepthStencilTexture();
void DestroyDepthStencilTexture();
void CreateDepthStencilAttachment();
void DestroyDepthStencilAttachment();
void CreateFSQGraphicsPipeline();
void DestroyFSQGraphicsPipeline();
void CreateColorCopyPipeline();
void DestroyColorCopyPipeline();
void RecreateComputeRenderTextures();
......@@ -309,23 +307,22 @@ protected:
int SurfaceConfiguredSize[2];
wgpu::TextureFormat PreferredSurfaceTextureFormat = wgpu::TextureFormat::BGRA8Unorm;
struct vtkWGPUDeptStencil
struct vtkWGPUDepthStencil
{
wgpu::Texture Texture;
wgpu::TextureView View;
wgpu::TextureFormat Format;
bool HasStencil;
};
vtkWGPUDeptStencil DepthStencil;
vtkWGPUDepthStencil DepthStencilAttachment;
struct vtkWGPUColorAttachment
struct vtkWGPUAttachment
{
wgpu::Texture Texture;
wgpu::TextureView View;
wgpu::TextureFormat Format;
wgpu::Buffer OffscreenBuffer;
};
vtkWGPUColorAttachment ColorAttachment;
vtkWGPUAttachment ColorAttachment;
struct vtkWGPUUserStagingPixelData
{
......@@ -338,25 +335,20 @@ protected:
struct vtkWGPUFullScreenQuad
{
wgpu::RenderPipeline Pipeline;
std::string Key;
wgpu::BindGroup BindGroup;
};
vtkWGPUFullScreenQuad ColorCopyRenderPipeline;
vtkWGPUFullScreenQuad FSQ;
struct MappingContext
{
vtkSmartPointer<vtkTypeUInt8Array> dst;
wgpu::Buffer src;
unsigned long size;
vtkWebGPURenderWindow* window;
} BufferMapReadContext;
vtkNew<vtkTypeUInt8Array> CachedPixelBytes;
vtkSmartPointer<vtkWebGPUConfiguration> WGPUConfiguration;
vtkNew<vtkWebGPUShaderDatabase> WGPUShaderDatabase;
vtkNew<vtkWebGPURenderPipelineCache> WGPUPipelineCache;
vtkSmartPointer<vtkWebGPUComputePipeline> DepthCopyPipeline;
vtkSmartPointer<vtkWebGPUComputePass> DepthCopyPass;
int DepthCopyBufferIndex = 0;
int DepthCopyTextureIndex = 0;
int ScreenSize[2];
private:
......@@ -389,11 +381,12 @@ private:
*/
void PostRasterizationRender();
/**
* Copies the current framebuffer to the offscreen buffer (used for screenshotting the render
* window for example)
*/
void CopyFramebufferToOffscreenBuffer();
void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
std::size_t mipLevel, wgpu::TextureAspect aspect, wgpu::Origin3D offsets,
wgpu::Extent3D extents, TextureMapCallback callback, void* userData);
void ReadTextureFromGPU(wgpu::Texture& wgpuTexture, wgpu::TextureFormat format,
std::size_t mipLevel, wgpu::TextureAspect aspect, TextureMapCallback callback, void* userData);
// Render textures acquired by the user on this render window. They are kept here in case the
// render window is resized, in which case, we'll need to resize the render textures --> We need
......
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
@group(0) @binding(0) var depthTexture: texture_depth_2d;
@group(0) @binding(1) var<storage, read_write> outBuffer: array<f32>;
@compute
@workgroup_size(8, 8, 1)
fn computeMain(@builtin(global_invocation_id) id: vec3<u32>)
{
let dims = textureDimensions(depthTexture);
if (id.x >= dims.x || id.y >= dims.y)
{
return;
}
outBuffer[id.x + dims.x * id.y] = textureLoad(depthTexture, id.xy, 0);
// textureStore(outTexture, id.xy, vec4f(vec3f(textureLoad(depthTexture, id.xy, 0)), 1.0f));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment