6#include <glm/gtc/matrix_inverse.hpp>
7#include <glm/gtc/type_ptr.hpp>
12void Graphics::recordFrameCapture(vk::CommandBuffer cb) {
14 if (!captureToken_.empty())
return;
18 std::vector<CaptureCopy> copies;
20 auto* gb = currentGBufferSlot();
21 for (
auto attachment :
request.attachments) {
23 copy.metadata.attachment = attachment;
24 copy.metadata.producerFrameId = captureFrameId_;
26 copy.metadata.colorSpace =
"data-unorm-storage";
27 copy.metadata.encoding =
"unorm8";
28 copy.layout = vk::ImageLayout::eShaderReadOnlyOptimal;
29 copy.aspect = vk::ImageAspectFlagBits::eColor;
30 vkb::GenericImage*
source =
nullptr;
32 const auto format = swapchain.image_format;
33 const bool bgra = format == vk::Format::eB8G8R8A8Unorm || format == vk::Format::eB8G8R8A8Srgb;
34 const bool rgba = format == vk::Format::eR8G8B8A8Unorm || format == vk::Format::eR8G8B8A8Srgb;
35 if (!bgra && !rgba) copy.metadata.unavailableReason =
"UnsupportedFormat";
36 if (presentModel.has_acquired_image && (bgra || rgba)) {
37 copy.source = swapchain.get_images()[presentModel.acquired_image_index];
38 copy.metadata.width =
static_cast<uint32_t
>(
pixelWidth);
39 copy.metadata.height =
static_cast<uint32_t
>(
pixelHeight);
41 copy.metadata.colorSpace = (format == vk::Format::eB8G8R8A8Srgb || format == vk::Format::eR8G8B8A8Srgb)
44 copy.metadata.encoding =
"presented-including-overlay";
45 copy.layout = vk::ImageLayout::ePresentSrcKHR;
49 if (deferredGraphRecorded_ && deferredGraphRecordedSlot_ == currentFrameSlot() && !shadowMaps.empty()) {
50 source = ¤tShadowMap().image;
52 copy.aspect = vk::ImageAspectFlagBits::eDepth;
54 copy.metadata.encoding =
"device-depth-0-to-1";
55 std::copy_n(glm::value_ptr(mesh3dShadows.
ubo.
lightVP[copy.layer]), 16,
56 copy.metadata.producerMatrix.begin());
58 }
else if (gb && deferredGraphRecorded_ && deferredGraphRecordedSlot_ == currentFrameSlot()) {
70 copy.aspect = vk::ImageAspectFlagBits::eDepth;
72 copy.metadata.encoding =
"device-depth-0-to-1";
74 copy.metadata.encoding =
"r-linear01-gb-motion-ndc-times-half-plus-half";
76 copy.metadata.encoding =
"rgb-world-normal-times-half-plus-half-a-legacy-pbr";
78 copy.metadata.encoding =
"rgb-albedo-times-tint-a-linear01-depth";
81 copy.metadata.colorSpace =
"linear";
82 copy.metadata.encoding =
"rgb-linear-hdr-emission";
84 copy.metadata.encoding =
"metallic-roughness-occlusion-specular";
90 const bool supportedFormat =
depth ?
info.format == vk::Format::eD32Sfloat
92 ?
info.format == vk::Format::eR16G16B16A16Sfloat
93 :
info.format == vk::Format::eR8G8B8A8Unorm;
94 if ((
info.usage & vk::ImageUsageFlagBits::eTransferSrc) && supportedFormat &&
95 info.samples == vk::SampleCountFlagBits::e1 && copy.layer <
info.arrayLayers) {
96 copy.source =
source->image();
97 copy.metadata.width =
info.extent.width;
98 copy.metadata.height =
info.extent.height;
100 copy.metadata.unavailableReason =
101 !supportedFormat ?
"UnsupportedFormat"
102 :
info.samples != vk::SampleCountFlagBits::e1 ?
"UnsupportedSampleCount"
103 : !(
info.usage & vk::ImageUsageFlagBits::eTransferSrc) ?
"TransferSourceUsageMissing"
104 :
"InvalidArrayLayer";
107 if (copy.metadata.unavailableReason.empty()) copy.metadata.unavailableReason =
"NotProducedInTargetFrame";
108 if (!
request.reportUnavailable) {
115 copy.storageIndex = copies.size();
116 for (
size_t i = 0; i < copies.size(); ++i) {
117 if (copy.source && copies[i].source == copy.source && copies[i].layer == copy.layer) {
118 copy.storageIndex = i;
122 if (copy.source && copy.storageIndex == copies.size())
123 total += size_t(copy.metadata.width) * copy.metadata.height *
framePixelBytes(copy.metadata.format);
124 copies.push_back(std::move(copy));
134 for (
size_t i = 0; i < copies.size(); ++i) {
135 auto& copy = copies[i];
136 if (copy.source && copy.storageIndex == i)
137 copy.staging = vkb::GenericBuffer(
138 device, vk::BufferUsageFlagBits::eTransferDst,
139 vk::DeviceSize(copy.metadata.width) * copy.metadata.height *
framePixelBytes(copy.metadata.format),
140 vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
142 }
catch (
const std::exception& e) {
148 captureToken_ =
token;
149 captureCopies_ = std::move(copies);
150 captureSnapshot_ = {};
152 captureSnapshot_.
depthConvention =
"Vulkan-NDC-0-to-1-positive-viewport-y";
153 const glm::mat4 inverseProjection = mesh3dFrameUbo.
view * glm::inverse(mesh3dFrameUbo.
mvp);
154 std::copy_n(glm::value_ptr(inverseProjection), 16, captureSnapshot_.
inverseProjection.begin());
155 const glm::mat4 projection = glm::inverse(inverseProjection);
156 std::copy_n(glm::value_ptr(projection), 16, captureSnapshot_.
projectionMatrix.begin());
157 std::copy_n(glm::value_ptr(mesh3dFrameUbo.
view), 16, captureSnapshot_.
viewMatrix.begin());
158 for (
size_t i = 0; i < captureCopies_.size(); ++i) {
159 auto& copy = captureCopies_[i];
160 if (!copy.source || copy.storageIndex != i)
continue;
162 vk::Extent2D{copy.metadata.width, copy.metadata.height}, copy.aspect, copy.layer,
165 captureFence_ = presentModel.in_flight_fences[presentRecording.slot().index];
166 for (
auto& copy : captureCopies_) copy.
source = nullptr;
169void Graphics::markFrameCaptureSubmitted() {
170 if (captureToken_.empty() || captureSubmitted_)
return;
173 captureSubmitted_ =
true;
176void Graphics::pollFrameCapture() {
177 pollEntityInspection();
180 if (!captureToken_.empty() && !captureSubmitted_ && device.instance) {
185 }
catch (
const vk::SystemError&) {
189 destroyFrameCapture();
192 if (!captureSubmitted_ || !captureFence_ || !device.instance)
return;
195 status = device->getFenceStatus(captureFence_);
196 }
catch (
const vk::SystemError&) {
197 status = vk::Result::eErrorDeviceLost;
199 if (
status == vk::Result::eNotReady)
return;
200 if (
status != vk::Result::eSuccess) {
207 std::vector<std::shared_ptr<const std::vector<uint8_t>>> storage(captureCopies_.size());
208 for (
size_t i = 0; i < captureCopies_.size(); ++i) {
209 auto& copy = captureCopies_[i];
210 if (copy.staging.buffer && copy.storageIndex == i) {
211 auto bytes = std::make_shared<std::vector<uint8_t>>(
212 size_t(copy.metadata.width) * copy.metadata.height *
framePixelBytes(copy.metadata.format));
213 const void* mapped = copy.staging.map();
214 if (!mapped)
throw std::runtime_error(
"ReadbackMapFailed");
215 std::memcpy(
bytes->data(), mapped,
bytes->size());
216 copy.staging.unmap();
217 storage[i] = std::move(
bytes);
219 copy.metadata.bytes = storage[copy.storageIndex];
220 captureSnapshot_.
buffers.push_back(copy.metadata);
227 }
catch (
const std::exception& e) {
234 captureCopies_.clear();
235 captureSnapshot_ = {};
236 captureToken_.clear();
237 captureFence_ =
nullptr;
238 captureSubmitted_ =
false;
241void Graphics::destroyFrameCapture() {
242 destroyEntityInspection();
243 destroyGuideCapture();
244 ++captureTargetGeneration_;
246 if (!captureToken_.empty()) {
251 captureCopies_.clear();
252 captureSnapshot_ = {};
253 captureToken_.clear();
254 captureFence_ =
nullptr;
255 captureSubmitted_ =
false;
const GltfImportRequest & request
wgpu::PopErrorScopeStatus status
std::optional< std::pair< std::string, FrameCaptureRequest > > queued()
Find pending work, or an empty value when idle.
Result< void > begin(std::string_view token, uint64_t frameId, size_t bytes)
Reserve CPU plus GPU storage before recording any commands.
Result< void > complete(std::string_view token, FrameCaptureSnapshot snapshot)
Atomically publish all copied attachments; no partial ready result.
void retire(std::string_view token)
Release staging reservations after GPU completion, including cancelled work.
Result< void > fail(std::string_view token, std::string reason)
Publish a terminal diagnostic; retain reservation until retire().
Result< void > submitted(std::string_view token)
Mark a recorded batch submitted to the GPU.
Result< FrameCaptureSnapshot > snapshot(std::string_view token) override
Return immutable owning metadata without GPU access.
void collect()
Expire queued and ready entries; also called by tool operations.
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
FrameCaptureStore & frameCaptures()
Graphics-owned frame capture authority. @thread Engine main thread only. @lifetime Borrowed until Gra...
Result< T > failed(Status status, eve::DiagnosticCode code, RuleId rule, std::string message)
Construct a failed editing result with explicit status and diagnostic categories.
void recordFrameReadback(vk::CommandBuffer cb, vk::Image source, vk::Buffer destination, vk::Extent2D extent, vk::ImageAspectFlags aspect, uint32_t layer, vk::ImageLayout originalLayout)
constexpr size_t framePixelBytes(FramePixelFormat format)
Packed CPU bytes per pixel; pure value query, callable on any thread.
std::string_view frameAttachmentName(FrameAttachment a)
Stable protocol spelling of an attachment.
std::string depthConvention
std::array< float, 16 > projectionMatrix
std::array< float, 16 > inverseProjection
std::vector< FrameCaptureBuffer > buffers
std::array< float, 16 > viewMatrix
uint64_t targetGeneration
glm::mat4 lightVP[ShadowConfig::kCascades]