runtime pipeline
This commit is contained in:
parent
75c9ae5087
commit
415e523a45
20 changed files with 480 additions and 45 deletions
|
|
@ -26,6 +26,7 @@ export module Crafter.Graphics:DescriptorPoolVulkan;
|
|||
import std;
|
||||
import :VulkanDevice;
|
||||
import :Types;
|
||||
import :DescriptorSetLayoutVulkan;
|
||||
import Crafter.Event;
|
||||
|
||||
export namespace Crafter {
|
||||
|
|
@ -97,6 +98,25 @@ export namespace Crafter {
|
|||
|
||||
return types;
|
||||
}
|
||||
|
||||
constexpr static std::vector<VkDescriptorPoolSize> GetPoolSizes(const std::span<const DescriptorSetLayoutVulkan> shaders) {
|
||||
std::vector<VkDescriptorPoolSize> types;
|
||||
|
||||
for(const DescriptorSetLayoutVulkan& shader : shaders) {
|
||||
for (const VkDescriptorSetLayoutBinding& binding : shader.descriptors) {
|
||||
for(VkDescriptorPoolSize& type : types) {
|
||||
if(type.type == binding.descriptorType) {
|
||||
type.descriptorCount += binding.descriptorCount;
|
||||
goto inner;
|
||||
}
|
||||
}
|
||||
types.emplace_back(binding.descriptorType, binding.descriptorCount);
|
||||
inner:;
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
public:
|
||||
void BuildPool(std::span<const VkDescriptorPoolSize> poolSizes, std::span<const VkDescriptorSetLayout> setLayouts) {
|
||||
|
|
|
|||
|
|
@ -29,8 +29,22 @@ import :Types;
|
|||
import Crafter.Event;
|
||||
|
||||
export namespace Crafter {
|
||||
template<std::uint32_t DescriptorCount, const std::array<VkDescriptorSetLayoutBinding, DescriptorCount> Descriptors>
|
||||
class DescriptorSetLayoutVulkan {
|
||||
public:
|
||||
VkDescriptorSetLayout layout;
|
||||
std::vector<VkDescriptorSetLayoutBinding> descriptors;
|
||||
DescriptorSetLayoutVulkan(std::vector<VkDescriptorSetLayoutBinding>&& layouts) : descriptors(std::move(layouts)) {
|
||||
VkDescriptorSetLayoutCreateInfo descriptorLayoutInfoMesh = {
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
||||
.bindingCount = static_cast<std::uint32_t>(descriptors.size()),
|
||||
.pBindings = descriptors.data()
|
||||
};
|
||||
VulkanDevice::CheckVkResult(vkCreateDescriptorSetLayout(VulkanDevice::device, &descriptorLayoutInfoMesh, nullptr, &layout));
|
||||
}
|
||||
};
|
||||
|
||||
template<std::uint32_t DescriptorCount, const std::array<VkDescriptorSetLayoutBinding, DescriptorCount> Descriptors>
|
||||
class DescriptorSetLayoutVulkanConst {
|
||||
public:
|
||||
inline static VkDescriptorSetLayout layout;
|
||||
constexpr static std::span<const VkDescriptorSetLayoutBinding> descriptors = Descriptors;
|
||||
|
|
|
|||
|
|
@ -25,27 +25,24 @@ module;
|
|||
|
||||
export module Crafter.Graphics:Mesh;
|
||||
import std;
|
||||
import Crafter.Math;
|
||||
import :VulkanBuffer;
|
||||
|
||||
export namespace Crafter {
|
||||
struct __attribute__((packed)) Vertex {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
||||
class Mesh {
|
||||
public:
|
||||
VulkanBuffer<char, false, true, false> scratchBuffer;
|
||||
VulkanBuffer<char, false, true, false> blasBuffer;
|
||||
VulkanBuffer<Vertex, true, true, false> vertexBuffer;
|
||||
VulkanBuffer<Vector<float, 3, 3>, true, true, false> vertexBuffer;
|
||||
VulkanBuffer<std::uint32_t, true, true, false> indexBuffer;
|
||||
VkAccelerationStructureGeometryTrianglesDataKHR blasData;
|
||||
VkAccelerationStructureGeometryKHR blas;
|
||||
VkAccelerationStructureKHR accelerationStructure;
|
||||
VkDeviceAddress blasAddr;
|
||||
bool opaque;
|
||||
void Build(std::span<Vertex> verticies, std::span<std::uint32_t> indicies, VkCommandBuffer cmd);
|
||||
void Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32_t> indicies, VkCommandBuffer cmd);
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
|
@ -30,6 +30,91 @@ import :ShaderBindingTableVulkan;
|
|||
import :Types;
|
||||
|
||||
export namespace Crafter {
|
||||
class PipelineRTVulkan {
|
||||
public:
|
||||
VkPipeline pipeline;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
std::vector<std::uint8_t> shaderHandles;
|
||||
VulkanBuffer<std::uint8_t, true, true, false> sbtBuffer;
|
||||
VkStridedDeviceAddressRegionKHR raygenRegion;
|
||||
VkStridedDeviceAddressRegionKHR missRegion;
|
||||
VkStridedDeviceAddressRegionKHR hitRegion;
|
||||
VkStridedDeviceAddressRegionKHR callableRegion;
|
||||
|
||||
void Init(VkCommandBuffer cmd, std::span<VkDescriptorSetLayout> setLayouts, std::span<VkRayTracingShaderGroupCreateInfoKHR> raygenGroups, std::span<VkRayTracingShaderGroupCreateInfoKHR> missGroups, std::span<VkRayTracingShaderGroupCreateInfoKHR> hitGroups, ShaderBindingTableVulkan& shaderTable) {
|
||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo {
|
||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
||||
.setLayoutCount = static_cast<std::uint32_t>(setLayouts.size()),
|
||||
.pSetLayouts = setLayouts.data()
|
||||
};
|
||||
|
||||
VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
||||
|
||||
std::vector<VkRayTracingShaderGroupCreateInfoKHR> groups;
|
||||
groups.reserve(raygenGroups.size() + missGroups.size() + hitGroups.size());
|
||||
|
||||
groups.insert(groups.end(), raygenGroups.begin(), raygenGroups.end());
|
||||
groups.insert(groups.end(), missGroups.begin(), missGroups.end());
|
||||
groups.insert(groups.end(), hitGroups.begin(), hitGroups.end());
|
||||
|
||||
VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{
|
||||
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
|
||||
.stageCount = static_cast<std::uint32_t>(shaderTable.shaderStages.size()),
|
||||
.pStages = shaderTable.shaderStages.data(),
|
||||
.groupCount = static_cast<std::uint32_t>(groups.size()),
|
||||
.pGroups = groups.data(),
|
||||
.maxPipelineRayRecursionDepth = 1,
|
||||
.layout = pipelineLayout
|
||||
};
|
||||
|
||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
||||
|
||||
std::size_t dataSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
||||
shaderHandles.resize(dataSize);
|
||||
VulkanDevice::CheckVkResult(VulkanDevice::vkGetRayTracingShaderGroupHandlesKHR(VulkanDevice::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
|
||||
|
||||
std::uint32_t sbtStride = AlignUp(VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment);
|
||||
|
||||
raygenRegion.stride = sbtStride;
|
||||
raygenRegion.deviceAddress = 0;
|
||||
raygenRegion.size = raygenGroups.size() * sbtStride;
|
||||
|
||||
missRegion.stride = sbtStride;
|
||||
missRegion.deviceAddress = AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
||||
missRegion.size = missGroups.size() * sbtStride;
|
||||
|
||||
hitRegion.stride = sbtStride;
|
||||
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
||||
hitRegion.size = hitGroups.size() * sbtStride;
|
||||
|
||||
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
|
||||
sbtBuffer.Create(VK_BUFFER_USAGE_2_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bufferSize);
|
||||
|
||||
std::uint8_t* offset = sbtBuffer.value;
|
||||
std::uint8_t* handleOffset = shaderHandles.data();
|
||||
|
||||
std::memcpy(offset, handleOffset, raygenGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
||||
offset += AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
||||
handleOffset += raygenGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
|
||||
|
||||
std::memcpy(offset, handleOffset, missGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
||||
offset += AlignUp(missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
||||
handleOffset += missGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
|
||||
|
||||
std::memcpy(offset, handleOffset, hitGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
||||
|
||||
sbtBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
||||
|
||||
raygenRegion.deviceAddress += sbtBuffer.address;
|
||||
missRegion.deviceAddress += sbtBuffer.address;
|
||||
hitRegion.deviceAddress += sbtBuffer.address;
|
||||
|
||||
callableRegion.deviceAddress = 0;
|
||||
callableRegion.stride = 0;
|
||||
callableRegion.size = 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <std::uint32_t GeneralShader, std::uint32_t ClosestHitShader, std::uint32_t AnyHitShader, std::uint32_t IntersectionShader>
|
||||
struct ShaderGroup {
|
||||
static constexpr std::uint32_t generalShader = GeneralShader;
|
||||
|
|
@ -39,7 +124,7 @@ export namespace Crafter {
|
|||
};
|
||||
|
||||
template <typename Shaders, typename ShaderGroups>
|
||||
class PipelineRTVulkan {
|
||||
class PipelineRTVulkanConst {
|
||||
public:
|
||||
inline static VkPipeline pipeline;
|
||||
inline static VkPipelineLayout pipelineLayout;
|
||||
|
|
@ -65,8 +150,8 @@ export namespace Crafter {
|
|||
|
||||
VkRayTracingPipelineCreateInfoKHR rtPipelineInfo{
|
||||
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR,
|
||||
.stageCount = static_cast<std::uint32_t>(ShaderBindingTableVulkan<Shaders>::shaderStages.size()),
|
||||
.pStages = ShaderBindingTableVulkan<Shaders>::shaderStages.data(),
|
||||
.stageCount = static_cast<std::uint32_t>(ShaderBindingTableVulkanConst<Shaders>::shaderStages.size()),
|
||||
.pStages = ShaderBindingTableVulkanConst<Shaders>::shaderStages.data(),
|
||||
.groupCount = static_cast<std::uint32_t>(groups.size()),
|
||||
.pGroups = groups.data(),
|
||||
.maxPipelineRayRecursionDepth = 1,
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
*/
|
||||
|
||||
module;
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "../lib/stb_image.h"
|
||||
#include "../lib/stb_truetype.h"
|
||||
export module Crafter.Graphics:RenderingElement;
|
||||
|
|
|
|||
|
|
@ -38,12 +38,10 @@ export namespace Crafter {
|
|||
|
||||
class RenderingElement3DVulkan {
|
||||
public:
|
||||
Mesh* mesh;
|
||||
VkAccelerationStructureInstanceKHR instance;
|
||||
static std::vector<RenderingElement3DVulkan> elements;
|
||||
static std::vector<RenderingElement3DVulkan*> elements;
|
||||
inline static VulkanBuffer<char, false, true, false> scratchBuffer;
|
||||
inline static std::vector<TlasWithBuffer> tlases;
|
||||
RenderingElement3DVulkan(Mesh& mesh, std::uint32_t customIndex, std::uint32_t mask, std::uint32_t shaderOffset);
|
||||
static void BuildTLAS(VkCommandBuffer cmd, std::uint32_t index);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,11 +26,23 @@ export module Crafter.Graphics:ShaderBindingTableVulkan;
|
|||
import std;
|
||||
import :VulkanDevice;
|
||||
import :VulkanBuffer;
|
||||
import :ShaderVulkan;
|
||||
import :Types;
|
||||
|
||||
export namespace Crafter {
|
||||
template <typename Shaders>
|
||||
class ShaderBindingTableVulkan {
|
||||
public:
|
||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||
void Init(std::span<VulkanShader> shaders) {
|
||||
shaderStages.reserve(shaders.size());
|
||||
for(const VulkanShader& shader: shaders) {
|
||||
shaderStages.emplace_back(VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, shader.stage, shader.shader, shader.entrypoint.c_str(), nullptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Shaders>
|
||||
class ShaderBindingTableVulkanConst {
|
||||
public:
|
||||
inline static std::array<VkPipelineShaderStageCreateInfo, std::tuple_size_v<Shaders>> shaderStages;
|
||||
static void Init() {
|
||||
|
|
|
|||
|
|
@ -37,17 +37,49 @@ export namespace Crafter {
|
|||
char value[N];
|
||||
};
|
||||
|
||||
class VulkanShader {
|
||||
public:
|
||||
VkShaderStageFlagBits stage;
|
||||
std::string entrypoint;
|
||||
VkShaderModule shader;
|
||||
VulkanShader(const std::filesystem::path& path, std::string entrypoint, VkShaderStageFlagBits stage) : stage(stage), entrypoint(entrypoint) {
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "Error: Could not open file " << path << std::endl;
|
||||
}
|
||||
|
||||
// Move to the end of the file to determine its size
|
||||
file.seekg(0, std::ios::end);
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<std::uint32_t> spirv(size / sizeof(std::uint32_t));
|
||||
|
||||
// Read the data into the vector
|
||||
if (!file.read(reinterpret_cast<char*>(spirv.data()), size)) {
|
||||
std::cerr << "Error: Could not read data from file" << std::endl;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
|
||||
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
||||
module_info.pCode = spirv.data();
|
||||
|
||||
VulkanDevice::CheckVkResult(vkCreateShaderModule(VulkanDevice::device, &module_info, nullptr, &shader));
|
||||
}
|
||||
};
|
||||
|
||||
template <
|
||||
StringLiteral path,
|
||||
StringLiteral entrypoint,
|
||||
VkShaderStageFlagBits stage
|
||||
>
|
||||
class VulkanShader {
|
||||
class VulkanShaderConst {
|
||||
public:
|
||||
constexpr static VkShaderStageFlagBits _stage = stage;
|
||||
constexpr static StringLiteral _entrypoint = entrypoint;
|
||||
inline static VkShaderModule shader;
|
||||
|
||||
static void CreateShader() {
|
||||
std::ifstream file(path.value, std::ios::binary);
|
||||
if (!file) {
|
||||
|
|
@ -72,7 +104,6 @@ export namespace Crafter {
|
|||
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
||||
module_info.pCode = spirv.data();
|
||||
|
||||
VkShaderModule shader_module;
|
||||
VulkanDevice::CheckVkResult(vkCreateShaderModule(VulkanDevice::device, &module_info, nullptr, &shader));
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -215,6 +215,19 @@ export namespace Crafter {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr double MappedToNDCBoundless(T mapped, std::uint32_t size) requires(std::is_integral_v<T>) {
|
||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
||||
return mapped / SCALEBOUNDLESSDOUBLE8 - 0.5;
|
||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
||||
return mapped / SCALEBOUNDLESSDOUBLE16 - 0.5;
|
||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
||||
return mapped / SCALEBOUNDLESSDOUBLE32 - 0.5;
|
||||
} else {
|
||||
return mapped / SCALEBOUNDLESSDOUBLE64 - 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename T2>
|
||||
constexpr T MappedToAbsolute(T mapped, T2 absolute) requires(std::is_integral_v<T>) {
|
||||
if constexpr (std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>) {
|
||||
|
|
@ -354,6 +367,11 @@ export namespace Crafter {
|
|||
return (value + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T GetTanHalfFov(T fov) {
|
||||
return std::tan(fov * std::numbers::pi / 360.0);
|
||||
}
|
||||
|
||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
||||
struct DescriptorBinding {
|
||||
VkDescriptorType type;
|
||||
|
|
|
|||
|
|
@ -322,6 +322,15 @@ export namespace Crafter {
|
|||
hitRegion = Pipeline::hitRegion;
|
||||
callableRegion = Pipeline::callableRegion;
|
||||
}
|
||||
|
||||
void SetPipelineRT(PipelineRTVulkan& pipeline) {
|
||||
rtPipeline = pipeline.pipeline;
|
||||
rtPipelineLayout = pipeline.pipelineLayout;
|
||||
raygenRegion = pipeline.raygenRegion;
|
||||
missRegion = pipeline.missRegion;
|
||||
hitRegion = pipeline.hitRegion;
|
||||
callableRegion = pipeline.callableRegion;
|
||||
}
|
||||
inline static wl_compositor* compositor = nullptr;
|
||||
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
||||
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue