载入中...
搜索中...
未找到
Canvas.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4#include "image/ImageData.h"
5#include "zeroerr/assert.h"
6
7#include <array>
8#include <cstring>
9
10namespace eve::graphics::vulkan {
11
13 : owner(owner), width(width), height(height), hdr(hdr) {
14 ASSERT(owner != nullptr);
15 ASSERT_GT(width, 0);
16 ASSERT_GT(height, 0);
17 if (!owner) throw Exception("OffscreenCanvas: null owner");
18 if (width <= 0 || height <= 0) throw Exception("OffscreenCanvas: invalid size");
19
20 auto &device = owner->getDevice();
21 const vk::Format format =
22 hdr ? vk::Format::eR16G16B16A16Sfloat : vk::Format::eR8G8B8A8Unorm;
23 // TransferDst so same-frame snapshots / blits can land on this canvas.
24 color = device.createCopyableColorTarget(uint32_t(width), uint32_t(height), format);
25
26 fb = owner->getOffscreenRenderPass(hdr).createFramebuffer(
27 device, uint32_t(width), uint32_t(height), {color.asAttachment()});
28
29 vkb::SamplerBuilder sb;
30 sampleGpu.sampler = sb.magFilter(vk::Filter::eNearest)
31 .minFilter(vk::Filter::eNearest)
32 .addressModeU(vk::SamplerAddressMode::eClampToEdge)
33 .addressModeV(vk::SamplerAddressMode::eClampToEdge)
34 .build(device);
35
36 auto sets = vkb::DescriptorSetBuilder()
37 .layout(owner->getTexSetLayout())
38 .build(device.instance, owner->getDescriptorPool());
39 sampleGpu.width = width;
40 sampleGpu.height = height;
41 sampleGpu.viewOverride = color.imageView();
42
43 vkb::UnboundSet unbound{sets[0]};
44 vkb::DescriptorSetUpdater updater;
45 updater.beginDescriptorSet(unbound)
46 .beginImages(0, 0, vk::DescriptorType::eCombinedImageSampler)
47 .image(vkb::SampledImage::forLaterSample(sampleGpu.sampler, color.imageView()))
48 .beginImages(1, 0, vk::DescriptorType::eCombinedImageSampler)
49 .image(vkb::SampledImage::forLaterSample(sampleGpu.sampler, color.imageView()))
50 .update(device.instance);
51 sampleGpu.descriptorSet = std::move(unbound).publish();
52
53 sampleTexture.width = width;
54 sampleTexture.height = height;
55 sampleTexture.pixelWidth = width;
56 sampleTexture.pixelHeight = height;
57 sampleTexture.gpuHandle = &sampleGpu;
58}
59
61 if (!owner) return;
62 auto &device = owner->getDevice();
63 device->waitIdle();
64 if (fb) {
65 device->destroyFramebuffer(fb);
66 fb = nullptr;
67 }
68 if (fb3D) {
69 device->destroyFramebuffer(fb3D);
70 fb3D = nullptr;
71 }
72 if (sampleGpu.sampler) {
73 device->destroySampler(sampleGpu.sampler);
74 sampleGpu.sampler = nullptr;
75 }
76 sampleTexture.gpuHandle = nullptr;
77}
78
80 bool v = hasPendingClear;
81 hasPendingClear = false;
82 return v;
83}
84
86 if (fb3D) return;
87 auto &device = owner->getDevice();
88 depth = device.createDepthTarget(uint32_t(width), uint32_t(height), owner->getDepthFormat(),
89 true);
90 fb3D = owner->getOffscreen3DRenderPass(hdr).createFramebuffer(
91 device, uint32_t(width), uint32_t(height), {color.asAttachment(), depth.asAttachment()});
92}
93
94void OffscreenCanvas::clear(std::optional<Color> colorOpt, std::optional<int>, std::optional<double>) {
95 clearColor = colorOpt.value_or(Color(0.f, 0.f, 0.f, 1.f));
96 hasPendingClear = true;
97}
98
99void OffscreenCanvas::readAllPixels(std::vector<uint8_t> &outRgba) {
100 if (hdr) throw Exception("HDR Canvas pixel readback requires an explicit tone-map pass");
101 if (owner->getCanvas() == this) owner->flushBatch();
102 auto &device = owner->getDevice();
103 device->waitIdle();
104
105 const vk::DeviceSize byteSize = vk::DeviceSize(width) * vk::DeviceSize(height) * 4;
106 vkb::GenericBuffer staging(device, vk::BufferUsageFlagBits::eTransferDst, byteSize,
107 vk::MemoryPropertyFlagBits::eHostVisible |
108 vk::MemoryPropertyFlagBits::eHostCoherent);
109
110 vkb::executeImmediately(device.instance, owner->getUploadPool(),
111 device.getQueue(vkb::QueueType::graphics), [&](vk::CommandBuffer cb) {
112 color.setLayout(cb, vk::ImageLayout::eTransferSrcOptimal);
113 vk::BufferImageCopy region{};
114 region.imageSubresource = {vk::ImageAspectFlagBits::eColor, 0, 0, 1};
115 region.imageExtent = vk::Extent3D{uint32_t(width), uint32_t(height), 1};
116 cb.copyImageToBuffer(color.image(), vk::ImageLayout::eTransferSrcOptimal,
117 staging.buffer, region);
118 color.setLayout(cb, vk::ImageLayout::eShaderReadOnlyOptimal);
119 });
120
121 outRgba.resize(size_t(byteSize));
122 void *mapped = staging.map();
123 // Vulkan FB row 0 is NDC -Y (top). Batcher maps logical y=0 → NDC -1, so
124 // logical top-left matches FB top-left — copy as-is into Y-down buffer.
125 std::memcpy(outRgba.data(), mapped, size_t(byteSize));
126 staging.unmap();
127 staging.release();
128}
129
130void OffscreenCanvas::readAllHDRPixels(std::vector<uint8_t> &outRgba16f) {
131 if (!hdr) throw Exception("Canvas is not an HDR render target");
132 auto &device = owner->getDevice();
133 device->waitIdle();
134
135 const vk::DeviceSize byteSize = vk::DeviceSize(width) * vk::DeviceSize(height) * 8;
136 vkb::GenericBuffer staging(device, vk::BufferUsageFlagBits::eTransferDst, byteSize,
137 vk::MemoryPropertyFlagBits::eHostVisible |
138 vk::MemoryPropertyFlagBits::eHostCoherent);
139
140 vkb::executeImmediately(device.instance, owner->getUploadPool(),
141 device.getQueue(vkb::QueueType::graphics), [&](vk::CommandBuffer cb) {
142 color.setLayout(cb, vk::ImageLayout::eTransferSrcOptimal);
143 vk::BufferImageCopy region{};
144 region.imageSubresource = {vk::ImageAspectFlagBits::eColor, 0, 0, 1};
145 region.imageExtent = vk::Extent3D{uint32_t(width), uint32_t(height), 1};
146 cb.copyImageToBuffer(color.image(), vk::ImageLayout::eTransferSrcOptimal,
147 staging.buffer, region);
148 color.setLayout(cb, vk::ImageLayout::eShaderReadOnlyOptimal);
149 });
150
151 outRgba16f.resize(size_t(byteSize));
152 void *mapped = staging.map();
153 std::memcpy(outRgba16f.data(), mapped, size_t(byteSize));
154 staging.unmap();
155 staging.release();
156}
157
158Color OffscreenCanvas::getPixel(int x, int y) {
159 ASSERT_GE(x, 0);
160 ASSERT_GE(y, 0);
161 ASSERT_LT(x, width);
162 ASSERT_LT(y, height);
163 if (x < 0 || y < 0 || x >= width || y >= height)
164 throw Exception("OffscreenCanvas::getPixel: out of bounds (%d,%d)", x, y);
165 std::vector<uint8_t> px;
166 readAllPixels(px);
167 size_t i = (size_t(y) * size_t(width) + size_t(x)) * 4;
168 return Color(px[i + 0] / 255.f, px[i + 1] / 255.f, px[i + 2] / 255.f, px[i + 3] / 255.f);
169}
170
171image::ImageData *OffscreenCanvas::newImageData() {
172 std::vector<uint8_t> px;
173 readAllPixels(px);
174 auto *img = new image::ImageData(width, height, "RGBA8");
175 std::memcpy(img->getData(), px.data(), px.size());
176 return img;
177}
178
179image::ImageData *OffscreenCanvas::newHDRImageData() {
180 if (!hdr) return nullptr;
181 std::vector<uint8_t> px;
182 readAllHDRPixels(px);
183 auto *img = new image::ImageData(width, height, "RGBA16F");
184 std::memcpy(img->getData(), px.data(), px.size());
185 return img;
186}
187
188} // namespace eve::graphics::vulkan
double y
double x
ScriptFieldRegion region
vkb::Device & device
float v
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
std::shared_ptr< OceanGpuSpectrum > owner
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
EVENGINE_API_BACKENDS public API.
Definition Graphics.h:360
Canvas * getCanvas() const override
Returns the canvas.
vk::CommandPool getUploadPool() const
Returns the upload pool.
Definition Graphics.h:1059
const vkb::BuiltRenderPass & getOffscreen3DRenderPass(bool hdr=false)
Returns the offscreen 3 d render pass.
Definition Graphics.h:1100
vkb::Device & getDevice()
Returns the device.
Definition Graphics.h:1057
vk::Format getDepthFormat() const
Returns the depth format.
Definition Graphics.h:1104
~OffscreenCanvas() override
Releases OffscreenCanvas resources.
Definition Canvas.cpp:60
void clear(std::optional< Color > color, std::optional< int > stencil, std::optional< double > depth) override
Clears .
Definition Canvas.cpp:94
OffscreenCanvas(Graphics *owner, int width, int height, bool hdr=false)
Constructs a OffscreenCanvas.
Definition Canvas.cpp:12
bool takePendingClear()
Take pending clear.
Definition Canvas.cpp:79
void ensure3D()
Ensure a D32 depth attachment + 3D (color+depth) framebuffer exist.
Definition Canvas.cpp:85
Represents raw pixel data.
Definition ImageData.h:38
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
glm::vec4 color