载入中...
搜索中...
未找到
Canvas.cpp
浏览该文件的文档.
4
5#include "common/Exception.h"
6#include "image/ImageData.h"
7
8#include <cstring>
9
10namespace eve::graphics::webgpu {
11
13 : gfx(gfx), width(width), height(height), hdr(hdr) {
14 if (!gfx || !gfx->getDevice() || width <= 0 || height <= 0)
15 throw Exception("OffscreenCanvas: invalid size or uninitialized device");
16
17 auto &device = gfx->getDevice();
18 WGPUTextureDescriptor cd{};
19 cd.label = sv("eve_canvas_color");
20 cd.dimension = WGPUTextureDimension_2D;
21 cd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
22 cd.sampleCount = 1;
23 cd.format = hdr ? WGPUTextureFormat_RGBA16Float : WGPUTextureFormat_RGBA8Unorm;
24 cd.mipLevelCount = 1;
25 cd.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment |
26 WGPUTextureUsage_CopySrc;
27 color = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&cd));
28 colorView = color.CreateView();
29
30 WGPUTextureDescriptor dd{};
31 dd.label = sv("eve_canvas_depth");
32 dd.dimension = WGPUTextureDimension_2D;
33 dd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
34 dd.sampleCount = 1;
35 dd.format = WGPUTextureFormat_Depth32Float;
36 dd.mipLevelCount = 1;
37 dd.usage = WGPUTextureUsage_RenderAttachment;
38 depth = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&dd));
39 depthView = depth.CreateView();
40
41 colorGpu.texture = color;
42 colorGpu.view = colorView;
43 colorGpu.width = width;
44 colorGpu.height = height;
46 WGPUSamplerDescriptor sd{};
47 sd.label = sv("eve_canvas_sampler");
48 sd.addressModeU = WGPUAddressMode_ClampToEdge;
49 sd.addressModeV = WGPUAddressMode_ClampToEdge;
50 sd.addressModeW = WGPUAddressMode_ClampToEdge;
51 sd.magFilter = WGPUFilterMode_Linear;
52 sd.minFilter = WGPUFilterMode_Linear;
53 sd.mipmapFilter = WGPUMipmapFilterMode_Nearest;
54 sd.maxAnisotropy = 1;
55 colorGpu.sampler = device.CreateSampler(reinterpret_cast<const wgpu::SamplerDescriptor*>(&sd));
56
57 colorTex.gpuHandle = &colorGpu;
58 colorTex.width = width;
59 colorTex.height = height;
60 colorTex.mipmapCount = 1;
61}
62
64
65void OffscreenCanvas::clear(std::optional<Color> color, std::optional<int> /*stencil*/,
66 std::optional<double> /*depth*/) {
67 clearRequested = true;
68 clearColor = color ? *color : Color(0.f, 0.f, 0.f, 1.f);
69}
70
72 if (hdr) throw Exception("HDR Canvas pixel readback requires an explicit tone-map pass");
73 return gfx->getPixelImpl(this, x, y);
74}
75
77 if (hdr) throw Exception("HDR Canvas pixel readback requires an explicit tone-map pass");
78 return gfx->newImageDataImpl(this);
79}
80
82 if (!hdr) return nullptr;
83 return gfx->newHDRImageDataImpl(this);
84}
85
86} // namespace eve::graphics::webgpu
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
vkb::Device & device
std::uint32_t height
std::uint32_t width
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
EVENGINE_API_BACKENDS public API.
Definition Graphics.h:191
Color getPixelImpl(OffscreenCanvas *canvas, int x, int y)
Blocking CPU readback of an offscreen canvas or scene color target.
image::ImageData * newHDRImageDataImpl(OffscreenCanvas *canvas)
Blocking linear RGBA16F readback of an HDR offscreen canvas. @ownership The caller owns the returned ...
image::ImageData * newImageDataImpl(OffscreenCanvas *canvas)
Creates a image data impl. @ownership Caller deletes unless documented otherwise.
wgpu::Device & getDevice()
Returns the device.
Definition Graphics.h:754
image::ImageData * newImageData() override
Creates a image data. @ownership Caller deletes unless documented otherwise.
Definition Canvas.cpp:76
image::ImageData * newHDRImageData() override
Creates a hdr image data. @ownership Caller deletes unless documented otherwise.
Definition Canvas.cpp:81
~OffscreenCanvas() override
Releases OffscreenCanvas resources.
Color getPixel(int x, int y) override
Returns the pixel.
Definition Canvas.cpp:71
void clear(std::optional< Color > color, std::optional< int > stencil, std::optional< double > depth) override
Clears .
Definition Canvas.cpp:65
OffscreenCanvas(Graphics *gfx, int width, int height, bool hdr=false)
Constructs a OffscreenCanvas.
Definition Canvas.cpp:12
Represents raw pixel data.
Definition ImageData.h:38
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
Definition wgpu_types.h:20
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
static TextureSampler linearMipmap()
Trilinear (linear + linear mips). Caller should create the texture with generateMipmaps.
glm::vec4 color