vulkan triangle

This commit is contained in:
Jorijn van der Graaf 2026-01-29 19:18:47 +01:00
commit 96b5d1a299
8 changed files with 170 additions and 48 deletions

View file

@ -0,0 +1,12 @@
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_nonuniform_qualifier : enable
layout(location = 0) rayPayloadInEXT vec3 hitValue;
hitAttributeEXT vec2 attribs;
void main()
{
const vec3 barycentricCoords = vec3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
hitValue = barycentricCoords;
}

View file

@ -5,9 +5,12 @@ using namespace Crafter;
import std;
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;
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;
int main() {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
/*
This sets up all necessary things and creates the vulkan device.
This must be called before any vulkan related things.
@ -15,9 +18,11 @@ int main() {
*/
VulkanDevice::CreateDevice();
Raygenspv::CreateShader();
DescriptorLayoutVulkan<Raygenspv>::Init();
PipelineRTVulkan<Raygenspv, Raygenspv>::Init();
DescriptorPool<1, Raygenspv> pool;
Closesthitspv::CreateShader();
Misspv::CreateShader();
DescriptorLayoutVulkan<Raygenspv, Closesthitspv, Misspv>::Init();
PipelineRTVulkan<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>::Init();
DescriptorPool<1, Raygenspv, Closesthitspv, Misspv> pool;
pool.setsCount = 1;
pool.BuildPool(0);
@ -30,8 +35,8 @@ int main() {
VkCommandBuffer cmd = window.StartInit();
Mesh triangleMesh;
std::array<Vertex, 3> verts {{{-0.1, 0, 0}, {0, 0.1, 0}, {0.1, 0, 0}}};
std::array<std::uint32_t, 3> index {{0,1,2}};
std::array<Vertex, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
std::array<std::uint32_t, 3> index {{2,1,0}};
triangleMesh.Build(verts, index, cmd);
RenderingElement3DVulkan::elements.emplace_back(triangleMesh);
RenderingElement3DVulkan::BuildTLAS(cmd);
@ -41,8 +46,6 @@ int main() {
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
};
std::cout << pool.sets.size() << std::endl;
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = 1,
@ -51,21 +54,22 @@ int main() {
VkWriteDescriptorSet write[2] = {
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = &writeDescriptorSetAccelerationStructure,
.dstSet = pool.sets[0],
.dstSet = pool.sets[0],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
.pBufferInfo = &RenderingElement3DVulkan::tlasBuffer.descriptor
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
},
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[0],
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = pool.sets[0],
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.pImageInfo = &imageInfo
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.pImageInfo = &imageInfo
}
};
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
@ -77,9 +81,18 @@ int main() {
*/
window.FinishInit();
window.SetPipelineRT<Raygenspv, Raygenspv>();
window.SetPipelineRT<Raygenspv, Closesthitspv, Misspv, Raygenspv, Closesthitspv, Misspv>();
window.descriptorsRt = pool.sets;
window.Render();
window.Render();
window.Render();
window.Render();
// window.Render();
// window.Render();
// window.Render();
// window.Render();
// window.Render();
// window.Render();
window.StartSync();
}

View file

@ -0,0 +1,9 @@
#version 460
#extension GL_EXT_ray_tracing : enable
layout(location = 0) rayPayloadInEXT vec3 hitValue;
void main()
{
hitValue = vec3(1, 1, 1);
}

View file

@ -13,7 +13,17 @@
"shaders": [
{
"path":"raygen.glsl",
"type":6,
"type": 6,
"entrypoint":"main"
},
{
"path":"closesthit.glsl",
"type": 9,
"entrypoint":"main"
},
{
"path":"miss.glsl",
"type": 10,
"entrypoint":"main"
}
]

View file

@ -3,10 +3,45 @@
#extension GL_EXT_shader_image_load_formatted : enable
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
layout(binding = 1, set = 0) uniform image2D image;
layout(binding = 1, set = 0, rgba32f) uniform image2D image;
layout(location = 0) rayPayloadEXT vec3 hitValue;
void main() {
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(1,1,1, 0.0));
}
void main()
{
// Pixel coordinates
uvec2 pixel = gl_LaunchIDEXT.xy;
uvec2 resolution = gl_LaunchSizeEXT.xy;
// Normalized coordinates in range [-1, 1]
vec2 uv = (vec2(pixel) + 0.5) / vec2(resolution);
vec2 ndc = uv * 2.0 - 1.0;
// Camera parameters
vec3 origin = vec3(0.0, 0.0, -300.0);
float aspect = float(resolution.x) / float(resolution.y);
float fov = radians(60.0);
float tanHalfFov = tan(fov * 0.5);
// Simple pinhole camera facing +Z
vec3 direction = normalize(vec3(
ndc.x * aspect * tanHalfFov,
-ndc.y * tanHalfFov,
1.0
));
traceRayEXT(
topLevelAS,
gl_RayFlagsNoneEXT,
0xff,
0, 0, 0,
origin,
0.001,
direction,
10000.0,
0
);
imageStore(image, ivec2(pixel), vec4(hitValue, 1.0));
}