Crafter.Graphics/examples/VulkanAnimation/main.cpp

175 lines
6.7 KiB
C++
Raw Normal View History

2026-01-29 23:31:56 +01:00
#include <vulkan/vulkan.h>
import Crafter.Graphics;
using namespace Crafter;
import std;
import Crafter.Event;
import Crafter.Math;
2026-02-24 02:32:37 +01:00
typedef VulkanShaderConst<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR> Raygenspv;
typedef VulkanShaderConst<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR> Closesthitspv;
typedef VulkanShaderConst<"miss.spv", "main", VK_SHADER_STAGE_MISS_BIT_KHR> Misspv;
2026-01-31 21:08:42 +01:00
typedef std::tuple<Raygenspv, Misspv, Closesthitspv> AllShaders;
typedef std::tuple<
ShaderGroup<0, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<1, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>,
ShaderGroup<VK_SHADER_UNUSED_KHR, 2, VK_SHADER_UNUSED_KHR, VK_SHADER_UNUSED_KHR>
> ShaderGroups;
2026-02-24 02:32:37 +01:00
typedef PipelineRTVulkanConst<AllShaders, ShaderGroups> Pipeline;
typedef DescriptorSetLayoutVulkanConst<1, {{
2026-02-03 21:03:11 +01:00
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
},
}}> descriptorSetLayoutTlas;
2026-02-24 02:32:37 +01:00
typedef DescriptorSetLayoutVulkanConst<1, {{
2026-02-03 21:03:11 +01:00
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
}
}}> descriptorSetLayoutImage;
2026-01-29 23:31:56 +01:00
int main() {
2026-01-31 21:08:42 +01:00
VulkanDevice::CreateDevice();
WindowVulkan window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit();
2026-01-29 23:31:56 +01:00
Raygenspv::CreateShader();
Closesthitspv::CreateShader();
Misspv::CreateShader();
2026-01-31 21:08:42 +01:00
2026-02-24 02:32:37 +01:00
ShaderBindingTableVulkanConst<AllShaders>::Init();
2026-02-03 21:03:11 +01:00
descriptorSetLayoutTlas::Init();
descriptorSetLayoutImage::Init();
std::array<VkDescriptorSetLayout, 4> layouts {{descriptorSetLayoutTlas::layout, descriptorSetLayoutImage::layout, descriptorSetLayoutImage::layout, descriptorSetLayoutImage::layout}};
2026-01-31 21:08:42 +01:00
2026-01-29 23:43:54 +01:00
DescriptorPool pool;
2026-02-03 21:03:11 +01:00
pool.sets.resize(4);
pool.BuildPool(DescriptorPool::GetPoolSizes<descriptorSetLayoutTlas, descriptorSetLayoutImage, descriptorSetLayoutImage, descriptorSetLayoutImage>(), layouts);
Pipeline::Init(cmd, layouts);
window.SetPipelineRT<Pipeline>();
2026-01-29 23:31:56 +01:00
Mesh triangleMesh;
2026-02-24 02:32:37 +01:00
std::array<Vector<float, 3, 3>, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
2026-01-29 23:31:56 +01:00
std::array<std::uint32_t, 3> index {{2,1,0}};
triangleMesh.Build(verts, index, cmd);
2026-02-24 02:32:37 +01:00
RenderingElement3DVulkan renderer = {
.instance = {
.instanceCustomIndex = 0,
.mask = 0xFF,
.instanceShaderBindingTableRecordOffset = 0,
.flags = VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR,
.accelerationStructureReference = triangleMesh.blasAddr
}
};
RenderingElement3DVulkan::elements.emplace_back(&renderer);
2026-01-29 23:31:56 +01:00
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
2026-02-24 02:32:37 +01:00
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
2026-01-29 23:31:56 +01:00
2026-02-03 21:03:11 +01:00
RenderingElement3DVulkan::tlases.resize(WindowVulkan::numFrames);
2026-01-29 23:31:56 +01:00
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
2026-01-29 23:43:54 +01:00
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
2026-01-31 21:08:42 +01:00
2026-02-03 21:03:11 +01:00
VkDescriptorImageInfo imageInfo[WindowVulkan::numFrames] = {
{
.imageView = window.imageViews[0],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
{
.imageView = window.imageViews[1],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
{
.imageView = window.imageViews[2],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
},
2026-01-29 23:31:56 +01:00
};
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = 1,
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
};
2026-02-03 21:03:11 +01:00
VkWriteDescriptorSet write[4];
write[0] = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
};
for(std::uint32_t i = 0; i < WindowVulkan::numFrames; i++) {
write[i+1] = {
2026-01-29 23:31:56 +01:00
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2026-02-03 21:03:11 +01:00
.dstSet = pool.sets[i+1],
2026-01-29 23:31:56 +01:00
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
2026-02-03 21:03:11 +01:00
.pImageInfo = &imageInfo[i]
};
}
vkUpdateDescriptorSets(VulkanDevice::device, 4, write, 0, nullptr);
window.descriptorsRt = {pool.sets[0], pool.sets[1]};
2026-01-29 23:31:56 +01:00
/*
FinishInit executes all commands recorded to StartInit.
This must be called before the the event loops starts if you called StartInit before.
*/
window.FinishInit();
2026-01-30 00:09:37 +01:00
Animation<std::tuple<float>> anim({
{std::chrono::seconds(3), -600, 600},
});
anim.Start(std::chrono::high_resolution_clock::now());
EventListener<void> updateListener(&window.onRender, [&](){
float value = std::get<0>(anim.Play(window.currentFrameTime.now));
if(anim.currentFrame == anim.keyframes.size()) {
anim.Start(window.currentFrameTime.now);
}
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Translation(value, 0, 0);
2026-02-24 02:32:37 +01:00
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
2026-01-30 00:09:37 +01:00
RenderingElement3DVulkan::BuildTLAS(window.drawCmdBuffers[window.currentBuffer], window.currentBuffer);
2026-01-29 23:31:56 +01:00
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = 1,
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[window.currentBuffer].accelerationStructure
};
2026-02-03 21:03:11 +01:00
VkWriteDescriptorSet write = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
2026-01-29 23:31:56 +01:00
};
2026-02-03 21:03:11 +01:00
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
window.descriptorsRt[1] = pool.sets[window.currentBuffer+1];
2026-01-29 23:31:56 +01:00
});
window.Render();
window.StartUpdate();
window.StartSync();
}