Crafter.Graphics/examples/VulkanTriangle/main.cpp

133 lines
4.8 KiB
C++
Raw Normal View History

2026-03-02 23:53:13 +01:00
#include "vulkan/vulkan.h"
2025-06-13 23:59:36 +02:00
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-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<3, {{
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,
},
{
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
2026-02-24 02:32:37 +01:00
},
{
.binding = 2,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
2026-02-03 21:03:11 +01:00
}
}}> 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-02-24 02:32:37 +01:00
ShaderBindingTableVulkanConst<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-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 19:18:47 +01:00
std::array<std::uint32_t, 3> index {{2,1,0}};
2026-01-28 18:51:11 +01:00
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 20:35:55 +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 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
};
2026-02-24 02:32:37 +01:00
VulkanBuffer<std::uint32_t, true, false, false> lightBuffer;
lightBuffer.Create(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 1);
VkWriteDescriptorSet write[3] = {
2026-01-29 02:05:18 +01:00
{
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-02-24 02:32:37 +01:00
},
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[0],
.dstBinding = 2,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.pBufferInfo = &lightBuffer.descriptor
2026-01-29 02:05:18 +01:00
}
};
2026-02-24 02:32:37 +01:00
vkUpdateDescriptorSets(VulkanDevice::device, 3, write, 0, nullptr);
2026-01-29 02:05:18 +01:00
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();
}