载入中...
搜索中...
未找到
VulkanRayTracing.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Result.h"
6#include "vkbuilder.hpp"
7
8#include <glm/mat4x4.hpp>
9#include <glm/vec3.hpp>
10
11#include <cstdint>
12#include <memory>
13#include <vector>
14
15namespace eve::graphics {
16class Canvas;
17class Graphics;
18class Mesh;
19class Texture;
20} // namespace eve::graphics
21
23
29 vk::Buffer buffer{};
30 vk::DeviceMemory memory{};
31 vk::DeviceSize size = 0;
32 vk::DeviceAddress address = 0;
33 vkb::Device* device = nullptr;
34
45 if (this != &o) {
47 release();
49 steal(o);
50 }
51 return *this;
52 }
53
55 void release();
57 void steal(DeviceAddressBuffer& o) noexcept;
58
60 [[nodiscard]] Result<void> allocate(vkb::Device& dev, vk::DeviceSize bytes, vk::BufferUsageFlags usage);
61
63 [[nodiscard]] Result<void> upload(vk::CommandPool pool, vk::Queue queue, const void* data, vk::DeviceSize bytes);
64};
65
70 vk::AccelerationStructureKHR handle{};
72 vk::DeviceAddress deviceAddress = 0;
73 vkb::Device* device = nullptr;
74
85 if (this != &o) {
87 release();
89 steal(o);
90 }
91 return *this;
92 }
93
95 void release();
97 void steal(AccelerationStructure& o) noexcept;
98};
99
109
113class VulkanRayTracing final : public IRayTracing {
114public:
116 VulkanRayTracing() = default;
118 ~VulkanRayTracing() override;
119
122
124 bool isAvailable() const override;
126 RayTracingCaps caps() const override;
127
129 [[nodiscard]] Result<void> applyReflections(Graphics* gfx, Texture* sceneColor, Texture* hwDepth,
130 Texture* worldNormal, Canvas* dest, const glm::mat4& invViewProj,
131 const glm::mat4& viewProj, const glm::vec3& eyeWorld) override;
132
134 [[nodiscard]] Result<void> rebuildScene() override;
135
137 [[nodiscard]] Result<uint32_t> addTriangleMesh(const float* positionsXYZ, int vertexCount, const uint32_t* indices,
138 int indexCount, const glm::mat4& transform) override;
139
141 [[nodiscard]] Result<uint32_t> addMesh(Mesh* mesh, const glm::mat4& transform) override;
142
144 void clearScene() override;
145
147 void detachFromGraphics() override { detachDevice(); }
148
156 void attachDevice(vkb::Device* device, const RayTracingCaps& caps, vk::CommandPool uploadPool,
157 vk::Queue graphicsQueue);
158
160 void detachDevice();
161
166 [[nodiscard]] Result<void> ensureAttached();
167
168private:
169 [[nodiscard]] Result<void> ensurePipeline();
170 [[nodiscard]] Result<void> ensureSampler();
171 [[nodiscard]] Result<void> ensureOutputImage(uint32_t width, uint32_t height);
172 [[nodiscard]] Result<void> ensureDescriptorSets(vk::ImageView outputView, vk::ImageView sceneView,
173 vk::ImageView depthView, vk::ImageView normalView);
174 [[nodiscard]] Result<void> buildBlas(TriangleMeshRecord& mesh);
175 [[nodiscard]] Result<void> buildTlas();
176 [[nodiscard]] Result<void> createShaderBindingTable();
177
179 struct StorageColorImage : vkb::GenericImage {
181 void allocate(vkb::Device& device, uint32_t width, uint32_t height) {
182 vk::ImageCreateInfo info{};
183 info.imageType = vk::ImageType::e2D;
184 info.format = vk::Format::eR16G16B16A16Sfloat;
185 info.extent = vk::Extent3D{width, height, 1};
186 info.mipLevels = 1;
187 info.arrayLayers = 1;
188 info.samples = vk::SampleCountFlagBits::e1;
189 info.tiling = vk::ImageTiling::eOptimal;
190 info.usage = vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc |
191 vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled;
192 info.sharingMode = vk::SharingMode::eExclusive;
193 info.initialLayout = vk::ImageLayout::eUndefined;
195 create(device, info, vk::ImageViewType::e2D, vk::ImageAspectFlagBits::eColor, false);
196 }
197 };
198
199 vkb::Device* device_ = nullptr;
200 vk::CommandPool uploadPool_{};
201 vk::Queue graphicsQueue_{};
202 RayTracingCaps caps_{};
203
204 std::vector<std::unique_ptr<TriangleMeshRecord>> meshes_;
205 AccelerationStructure tlas_;
206
207 vk::DescriptorSetLayout setLayout_{};
208 vk::PipelineLayout pipelineLayout_{};
209 vk::Pipeline pipeline_{};
210 vk::DescriptorPool descriptorPool_{};
211 vk::DescriptorSet descriptorSet_{};
212 vk::Sampler sampler_{};
213
214 StorageColorImage outputImage_{};
215 uint32_t outputWidth_ = 0;
216 uint32_t outputHeight_ = 0;
217
218 DeviceAddressBuffer sbtBuffer_;
219 vk::StridedDeviceAddressRegionKHR raygenRegion_{};
220 vk::StridedDeviceAddressRegionKHR missRegion_{};
221 vk::StridedDeviceAddressRegionKHR hitRegion_{};
222 vk::StridedDeviceAddressRegionKHR callableRegion_{};
223
224 vk::ShaderModule raygenModule_{};
225 vk::ShaderModule missModule_{};
226 vk::ShaderModule closestHitModule_{};
227
228 struct PushConstants {
229 glm::mat4 invViewProj;
230 glm::mat4 viewProj;
231 glm::vec4 eye; // xyz = eye, w unused
232 };
233};
234
236VulkanRayTracing& vulkanRayTracing();
237
238} // namespace eve::graphics::raytracing
std::string usage
std::uint32_t vertexCount
std::uint32_t indexCount
vkb::Device & device
std::vector< std::uint32_t > indices
std::uint32_t height
std::uint32_t width
std::uint64_t bytes
glm::vec3 eye
Mesh * mesh
glm::mat4 viewProj
Move-only, checked operation results for the common layer.
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Canvas public API.
Definition Canvas.h:17
Optional hardware ray-tracing backend (Vulkan KHR RT).
Definition IRayTracing.h:26
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:25
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
Vulkan implementation of IRayTracing (KHR ray-tracing pipeline).
Result< void > rebuildScene() override
Rebuild scene.
~VulkanRayTracing() override
Releases VulkanRayTracing resources.
Result< uint32_t > addMesh(Mesh *mesh, const glm::mat4 &transform) override
Adds mesh.
RayTracingCaps caps() const override
Caps.
void detachFromGraphics() override
Detaches from graphics.
void attachDevice(vkb::Device *device, const RayTracingCaps &caps, vk::CommandPool uploadPool, vk::Queue graphicsQueue)
Bind to the live Vulkan Graphics device (call after init). @ownership device is borrowed; Graphics re...
Result< void > ensureAttached()
Lazily bind to the process Graphics singleton when RT is available.
bool isAvailable() const override
True when available.
void detachDevice()
Drop GPU resources when Graphics tears down.
VulkanRayTracing(const VulkanRayTracing &)=delete
VulkanRayTracing()=default
Constructs a VulkanRayTracing.
Result< uint32_t > addTriangleMesh(const float *positionsXYZ, int vertexCount, const uint32_t *indices, int indexCount, const glm::mat4 &transform) override
Adds triangle mesh.
Result< void > applyReflections(Graphics *gfx, Texture *sceneColor, Texture *hwDepth, Texture *worldNormal, Canvas *dest, const glm::mat4 &invViewProj, const glm::mat4 &viewProj, const glm::vec3 &eyeWorld) override
Applies reflections.
VulkanRayTracing & operator=(const VulkanRayTracing &)=delete
VulkanRayTracing & vulkanRayTracing()
Process-wide Vulkan RT backend; also registered as IRayTracing.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Hardware ray-tracing capabilities probed once at Vulkan device creation.
Bottom- or top-level acceleration structure handle.
AccelerationStructure & operator=(AccelerationStructure &&o) noexcept
Operator =.
AccelerationStructure & operator=(const AccelerationStructure &)=delete
AccelerationStructure()=default
Constructs a AccelerationStructure.
AccelerationStructure(const AccelerationStructure &)=delete
void steal(AccelerationStructure &o) noexcept
Steal.
~AccelerationStructure()
Releases AccelerationStructure resources.
AccelerationStructure(AccelerationStructure &&o) noexcept
Constructs a AccelerationStructure.
Device-local buffer with shader device address (for AS builds / SBT). @ownership Owns the Vulkan buff...
DeviceAddressBuffer(const DeviceAddressBuffer &)=delete
DeviceAddressBuffer()=default
Constructs a DeviceAddressBuffer.
~DeviceAddressBuffer()
Releases DeviceAddressBuffer resources.
DeviceAddressBuffer & operator=(DeviceAddressBuffer &&o) noexcept
Operator =.
Result< void > allocate(vkb::Device &dev, vk::DeviceSize bytes, vk::BufferUsageFlags usage)
Allocate a buffer usage that includes SHADER_DEVICE_ADDRESS.
DeviceAddressBuffer(DeviceAddressBuffer &&o) noexcept
Constructs a DeviceAddressBuffer.
Result< void > upload(vk::CommandPool pool, vk::Queue queue, const void *data, vk::DeviceSize bytes)
Upload host bytes via a staging buffer (stalls the queue).
void steal(DeviceAddressBuffer &o) noexcept
Steal.
DeviceAddressBuffer & operator=(const DeviceAddressBuffer &)=delete
glm::mat4 invViewProj
glm::uvec4 info