载入中...
搜索中...
未找到
Canvas.cpp
浏览该文件的文档.
4
5#include "common/Exception.h"
6#include "image/ImageData.h"
7
8#include <cstring>
9
11
12namespace {
14WGPUStringView sv(const char *s) { return WGPUStringView{s, s ? std::strlen(s) : 0}; }
15} // namespace
16
18 : gfx(gfx), width(width), height(height) {
19 if (!gfx || !gfx->getDevice() || width <= 0 || height <= 0)
20 throw Exception("OffscreenCanvas: invalid size or uninitialized device");
21
22 auto &device = gfx->getDevice();
23 WGPUTextureDescriptor cd{};
24 cd.label = sv("eve_canvas_color");
25 cd.dimension = WGPUTextureDimension_2D;
26 cd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
27 cd.sampleCount = 1;
28 cd.format = WGPUTextureFormat_RGBA8Unorm;
29 cd.mipLevelCount = 1;
30 cd.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment |
31 WGPUTextureUsage_CopySrc;
32 color = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&cd));
33 colorView = color.CreateView();
34
35 WGPUTextureDescriptor dd{};
36 dd.label = sv("eve_canvas_depth");
37 dd.dimension = WGPUTextureDimension_2D;
38 dd.size = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1};
39 dd.sampleCount = 1;
40 dd.format = WGPUTextureFormat_Depth32Float;
41 dd.mipLevelCount = 1;
42 dd.usage = WGPUTextureUsage_RenderAttachment;
43 depth = device.CreateTexture(reinterpret_cast<const wgpu::TextureDescriptor*>(&dd));
44 depthView = depth.CreateView();
45
46 colorGpu.texture = color;
47 colorGpu.view = colorView;
48 colorGpu.width = width;
49 colorGpu.height = height;
51 WGPUSamplerDescriptor sd{};
52 sd.label = sv("eve_canvas_sampler");
53 sd.addressModeU = WGPUAddressMode_ClampToEdge;
54 sd.addressModeV = WGPUAddressMode_ClampToEdge;
55 sd.addressModeW = WGPUAddressMode_ClampToEdge;
56 sd.magFilter = WGPUFilterMode_Linear;
57 sd.minFilter = WGPUFilterMode_Linear;
58 sd.mipmapFilter = WGPUMipmapFilterMode_Nearest;
59 colorGpu.sampler = device.CreateSampler(reinterpret_cast<const wgpu::SamplerDescriptor*>(&sd));
60
61 colorTex.gpuHandle = &colorGpu;
62 colorTex.width = width;
63 colorTex.height = height;
64 colorTex.mipmapCount = 1;
65}
66
68
69void OffscreenCanvas::clear(std::optional<Color> color, std::optional<int> /*stencil*/,
70 std::optional<double> /*depth*/) {
71 clearRequested = true;
72 clearColor = color ? *color : Color(0.f, 0.f, 0.f, 1.f);
73}
74
76 return gfx->getPixelImpl(this, x, y);
77}
78
82
83} // namespace eve::graphics::webgpu
vkb::Device & device
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
int width
image::ImageData::Colorf color
uint32_t s
Definition Weather.cpp:28
Color getPixelImpl(OffscreenCanvas *canvas, int x, int y)
Blocking CPU readback of an offscreen canvas or scene color target.
image::ImageData * newImageDataImpl(OffscreenCanvas *canvas)
image::ImageData * newImageData() override
Definition Canvas.cpp:79
Color getPixel(int x, int y) override
Definition Canvas.cpp:75
void clear(std::optional< Color > color, std::optional< int > stencil, std::optional< double > depth) override
Definition Canvas.cpp:69
OffscreenCanvas(Graphics *gfx, int width, int height)
Definition Canvas.cpp:17
Represents raw pixel data.
Definition ImageData.h:26
glm::vec4 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.