Crafter.Graphics/examples/VulkanTriangle/main.cpp

102 lines
3.7 KiB
C++
Raw Normal View History

2025-06-13 23:59:36 +02:00
#include <vulkan/vulkan.h>
import Crafter.Graphics;
using namespace Crafter;
2026-01-28 01:06:48 +01:00
import std;
2026-01-29 19:46:53 +01:00
import Crafter.Event;
2026-01-29 20:35:55 +01:00
import Crafter.Math;
2025-06-13 23:59:36 +02:00
2026-02-03 21:03:11 +01:00
typedef VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR> Raygenspv;
typedef VulkanShader<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR> Closesthitspv;
typedef VulkanShader<"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;
typedef PipelineRTVulkan<AllShaders, ShaderGroups> Pipeline;
2026-02-03 21:03:11 +01:00
typedef DescriptorSetLayoutVulkan<2, {{
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
},
{
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
}
}}> descriptorSetLayout;
2026-01-28 23:37:12 +01:00
2025-06-13 23:59:36 +02:00
int main() {
2026-01-28 01:06:48 +01:00
VulkanDevice::CreateDevice();
2026-01-31 21:08:42 +01:00
WindowVulkan window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit();
2026-01-28 23:37:12 +01:00
Raygenspv::CreateShader();
2026-01-29 19:18:47 +01:00
Closesthitspv::CreateShader();
Misspv::CreateShader();
2026-01-31 21:08:42 +01:00
ShaderBindingTableVulkan<AllShaders>::Init();
2026-02-03 21:03:11 +01:00
descriptorSetLayout::Init();
std::array<VkDescriptorSetLayout, 1> layouts {{descriptorSetLayout::layout}};
2026-01-29 23:31:56 +01:00
DescriptorPool pool;
pool.sets.resize(1);
2026-02-03 21:03:11 +01:00
pool.BuildPool(DescriptorPool::GetPoolSizes<descriptorSetLayout>(), layouts);
Pipeline::Init(cmd, layouts);
2025-06-13 23:59:36 +02:00
2026-01-28 18:51:11 +01:00
Mesh triangleMesh;
2026-01-29 19:18:47 +01:00
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
std::array<std::uint32_t, 3> index {{2,1,0}};
2026-01-28 18:51:11 +01:00
triangleMesh.Build(verts, index, cmd);
2026-01-29 20:35:55 +01:00
RenderingElement3DVulkan& el = RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
std::memcpy(el.instance.transform.matrix, transform.m, sizeof(transform.m));
2026-01-29 19:46:53 +01:00
RenderingElement3DVulkan::tlases.resize(1);
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
2025-06-13 23:59:36 +02:00
2026-01-29 02:05:18 +01:00
VkDescriptorImageInfo imageInfo = {
.imageView = window.imageViews[0],
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
};
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = 1,
2026-01-29 19:46:53 +01:00
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
2026-01-29 02:05:18 +01:00
};
VkWriteDescriptorSet write[2] = {
{
2026-01-29 19:18:47 +01:00
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2026-01-29 02:05:18 +01:00
.pNext = &writeDescriptorSetAccelerationStructure,
2026-01-29 19:18:47 +01:00
.dstSet = pool.sets[0],
2026-01-29 02:05:18 +01:00
.dstBinding = 0,
2026-01-29 19:18:47 +01:00
.dstArrayElement = 0,
2026-01-29 02:05:18 +01:00
.descriptorCount = 1,
2026-01-29 19:18:47 +01:00
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
2026-01-29 02:05:18 +01:00
},
{
2026-01-29 19:18:47 +01:00
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[0],
2026-01-29 02:05:18 +01:00
.dstBinding = 1,
2026-01-29 19:18:47 +01:00
.dstArrayElement = 0,
2026-01-29 02:05:18 +01:00
.descriptorCount = 1,
2026-01-29 19:18:47 +01:00
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.pImageInfo = &imageInfo
2026-01-29 02:05:18 +01:00
}
};
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
2026-01-31 21:08:42 +01:00
window.SetPipelineRT<Pipeline>();
2026-01-29 01:31:17 +01:00
window.descriptorsRt = pool.sets;
2026-01-31 21:08:42 +01:00
window.FinishInit();
2026-01-28 01:06:48 +01:00
window.Render();
2025-06-13 23:59:36 +02:00
window.StartSync();
}