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-01-28 23:37:12 +01:00
|
|
|
typedef VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, 2, {{{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1}}}> Raygenspv;
|
2026-01-29 19:18:47 +01:00
|
|
|
typedef VulkanShader<"closesthit.spv", "main", VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, 0, {{}}> Closesthitspv;
|
|
|
|
|
typedef VulkanShader<"miss.spv", "main", VK_SHADER_STAGE_MISS_BIT_KHR, 0, {{}}> Misspv;
|
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
|
|
|
This sets up all necessary things and creates the vulkan device.
|
|
|
|
|
This must be called before any vulkan related things.
|
|
|
|
|
Things like VkDevice are static members of the VulkanDevice class.
|
2025-06-13 23:59:36 +02:00
|
|
|
*/
|
2026-01-28 01:06:48 +01:00
|
|
|
VulkanDevice::CreateDevice();
|
2026-01-28 23:37:12 +01:00
|
|
|
Raygenspv::CreateShader();
|
2026-01-29 19:18:47 +01:00
|
|
|
Closesthitspv::CreateShader();
|
|
|
|
|
Misspv::CreateShader();
|
2026-01-29 23:43:54 +01:00
|
|
|
std::array<VkDescriptorSetLayout, 1> layouts {{Raygenspv::layout}};
|
2026-01-29 23:31:56 +01:00
|
|
|
PipelineRTVulkan<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>::Init(layouts);
|
|
|
|
|
DescriptorPool pool;
|
|
|
|
|
pool.sets.resize(1);
|
|
|
|
|
pool.BuildPool(DescriptorPool::GetPoolSizes<Raygenspv, Closesthitspv, Misspv>(), layouts);
|
2026-01-28 23:45:33 +01:00
|
|
|
|
2026-01-28 01:06:48 +01:00
|
|
|
WindowVulkan window(1280, 720, "HelloVulkan");
|
2025-06-13 23:59:36 +02:00
|
|
|
|
|
|
|
|
/*
|
2026-01-28 01:06:48 +01:00
|
|
|
StartInit gives you a VkCommandBuffer to use before the event loop starts
|
|
|
|
|
Use this for inititializing things like textures.
|
2025-06-13 23:59:36 +02:00
|
|
|
*/
|
2026-01-28 01:06:48 +01:00
|
|
|
VkCommandBuffer cmd = window.StartInit();
|
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);
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 23:59:36 +02:00
|
|
|
/*
|
2026-01-28 01:06:48 +01:00
|
|
|
FinishInit executes all commands recorded to StartInit.
|
|
|
|
|
This must be called before the the event loops starts if you called StartInit before.
|
2025-06-13 23:59:36 +02:00
|
|
|
*/
|
2026-01-28 01:06:48 +01:00
|
|
|
window.FinishInit();
|
2025-06-13 23:59:36 +02:00
|
|
|
|
2026-01-29 19:18:47 +01:00
|
|
|
window.SetPipelineRT<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>();
|
2026-01-29 01:31:17 +01:00
|
|
|
window.descriptorsRt = pool.sets;
|
|
|
|
|
|
2026-01-29 19:46:53 +01:00
|
|
|
|
2026-01-28 01:06:48 +01:00
|
|
|
window.Render();
|
2025-06-13 23:59:36 +02:00
|
|
|
window.StartSync();
|
|
|
|
|
}
|