diff --git a/modules/rtx/src/vkcv/rtx/RTX.cpp b/modules/rtx/src/vkcv/rtx/RTX.cpp index 6db8b896a0e352e202cf179978ab94b5d391f205..aea7a62a97aaf651534c416db5a0d9a7ffe7996e 100644 --- a/modules/rtx/src/vkcv/rtx/RTX.cpp +++ b/modules/rtx/src/vkcv/rtx/RTX.cpp @@ -78,21 +78,15 @@ namespace vkcv::rtx { tlasWrite.setDescriptorCount(1); tlasWrite.setDescriptorType(vk::DescriptorType::eAccelerationStructureKHR); m_core->getContext().getDevice().updateDescriptorSets(tlasWrite, nullptr); - - vk::WriteDescriptorSet tlasWrite2; - tlasWrite2.setPNext(&AccelerationDescriptor); - tlasWrite2.setDstSet(m_core->getDescriptorSet(descriptorSetHandles[1]).vulkanHandle); - tlasWrite2.setDstBinding(5); - tlasWrite2.setDstArrayElement(0); - tlasWrite2.setDescriptorCount(1); - tlasWrite2.setDescriptorType(vk::DescriptorType::eAccelerationStructureKHR); - m_core->getContext().getDevice().updateDescriptorSets(tlasWrite2, nullptr); + tlasWrite.setDstSet(m_core->getDescriptorSet(descriptorSetHandles[1]).vulkanHandle); + m_core->getContext().getDevice().updateDescriptorSets(tlasWrite, nullptr); + tlasWrite.setDstSet(m_core->getDescriptorSet(descriptorSetHandles[2]).vulkanHandle); + m_core->getContext().getDevice().updateDescriptorSets(tlasWrite, nullptr); //INDEX & VERTEX BUFFER BottomLevelAccelerationStructure blas = m_asManager->getBLAS(0);//HARD CODED //VERTEX BUFFER - vk::DescriptorBufferInfo vertexInfo = {}; vertexInfo.setBuffer(blas.vertexBuffer.vulkanHandle); vertexInfo.setOffset(0); diff --git a/projects/rtx/resources/shaders/raytrace.rchit b/projects/rtx/resources/shaders/raytrace.rchit index 282ebff82803aa94cd27fdee79271c16fc2f2e03..28b37856857d39ba32e5ebe043cd384162fa252a 100644 --- a/projects/rtx/resources/shaders/raytrace.rchit +++ b/projects/rtx/resources/shaders/raytrace.rchit @@ -1,5 +1,18 @@ #version 460 #extension GL_EXT_ray_tracing : require +#extension GL_EXT_nonuniform_qualifier : enable + +#define M_PI 3.1415926535897932384626433832795 + +//Mat struct +/*struct Material { + vec3 ambient; + vec3 diffuse; + vec3 specular; + vec3 emission; +};*/ + +//hitAttributeEXT vec2 hitCoordinate; layout(location = 0) rayPayloadInEXT Payload { vec3 rayOrigin; @@ -13,17 +26,69 @@ layout(location = 0) rayPayloadInEXT Payload { int rayActive; } payload; +layout(location = 1) rayPayloadEXT bool isShadow; + +layout(location = 2, binding = 1, set = 0) uniform accelerationStructureEXT tlas; // top level acceleration structure (for the noobs here (you!)) +/* +layout( push_constant ) uniform constants { + vec4 camera_position; // as origin for ray generation + vec4 camera_right; // for computing ray direction + vec4 camera_up; // for computing ray direction + vec4 camera_forward; // for computing ray direction + + uint frameCount; // what is this? the actual frame? +}camera; +*/ layout(binding = 3, set = 0) buffer rtxVertices { - vec3 vertices[]; -}; + float vertices[]; +} rtxVertexBuffer; layout(binding = 4, set = 0) buffer rtxIndices { uint indices[]; -}; +} rtxIndexBuffer; + +/* +layout(binding = 0, set = 1) buffer MaterialIndexBuffer { uint data[]; } materialIndexBuffer; +layout(binding = 1, set = 1) buffer MaterialBuffer { Material data[]; } materialBuffer; +*/ + +float random(vec2 uv, float seed) { + return fract(sin(mod(dot(uv, vec2(12.9898, 78.233)) + 1113.1 * seed, M_PI)) * 43758.5453);; +} + +vec3 uniformSampleHemisphere(vec2 uv) { + float z = uv.x; + float r = sqrt(max(0, 1.0 - z * z)); + float phi = 2.0 * M_PI * uv.y; + + return vec3(r * cos(phi), z, r * sin(phi)); +} + +vec3 alignHemisphereWithCoordinateSystem(vec3 hemisphere, vec3 up) { + vec3 right = normalize(cross(up, vec3(0.0072f, 1.0f, 0.0034f))); + vec3 forward = cross(right, up); + + return hemisphere.x * right + hemisphere.y * up + hemisphere.z * forward; +} + +void main() {/* + if (payload.rayActive == 0) { + return; + }*/ + + //ivec3 rtindices = ivec3(rtxIndexBuffer.indices[3 * gl_PrimitiveID + 0], rtxIndexBuffer.indices[3 * gl_PrimitiveID + 1], rtxIndexBuffer.indices[3 * gl_PrimitiveID + 2]); + /* + vec3 barycentric = vec3(1.0 - hitCoordinate.x - hitCoordinate.y, hitCoordinate.x, hitCoordinate.y); + + vec3 vertexA = vec3(rtxVertexBuffer.vertices[3 * rtindices.x + 0], rtxVertexBuffer.vertices[3 * rtindices.x + 1], rtxVertexBuffer.vertices[3 * rtindices.x + 2]); + vec3 vertexB = vec3(rtxVertexBuffer.vertices[3 * rtindices.y + 0], rtxVertexBuffer.vertices[3 * rtindices.y + 1], rtxVertexBuffer.vertices[3 * rtindices.y + 2]); + vec3 vertexC = vec3(rtxVertexBuffer.vertices[3 * rtindices.z + 0], rtxVertexBuffer.vertices[3 * rtindices.z + 1], rtxVertexBuffer.vertices[3 * rtindices.z + 2]); + + vec3 position = vertexA * barycentric.x + vertexB * barycentric.y + vertexC * barycentric.z; + vec3 geometricNormal = normalize(cross(vertexB - vertexA, vertexC - vertexA));*/ -void main() { payload.directColor=vec3(1,0,0); } diff --git a/projects/rtx/resources/shaders/raytrace.rgen b/projects/rtx/resources/shaders/raytrace.rgen index 689e8eb78f7ed030c5660c9b5b7286f6ed010743..7f4ac352167d46e57a94eaef53f530aec934f905 100644 --- a/projects/rtx/resources/shaders/raytrace.rgen +++ b/projects/rtx/resources/shaders/raytrace.rgen @@ -36,7 +36,7 @@ void main(){ uv = (uv * 2.0f - 1.0f) * vec2(1.0f, -1.0f); payload.rayOrigin = camera.camera_position.xyz; - payload.rayDirection = normalize(uv.x * camera.camera_right + uv.y * camera.camera_up + camera.camera_forward).xyz; + payload.rayDirection = normalize(uv.x * camera.camera_right * (-1) + uv.y * camera.camera_up * (-1) + camera.camera_forward).xyz ; payload.previousNormal = vec3(0.0, 0.0, 0.0); payload.directColor = vec3(0.0, 0.0, 0.0); diff --git a/projects/rtx/resources/shaders/raytrace.rmiss b/projects/rtx/resources/shaders/raytrace.rmiss index 8304b1fbbcc63b99adb0438d43bf47e975ae4496..53910f8643be83e3faecae364bdd4be8d345a5d8 100644 --- a/projects/rtx/resources/shaders/raytrace.rmiss +++ b/projects/rtx/resources/shaders/raytrace.rmiss @@ -13,7 +13,7 @@ layout(location = 0) rayPayloadInEXT Payload { int rayActive; } payload; -layout(binding = 5, set = 0) uniform accelerationStructureEXT tlas; //not neccesary in shader but for compiling ->bug +layout(location = 1, binding = 1, set = 0) uniform accelerationStructureEXT tlas; //not neccesary in shader but for compiling ->bug void main() { payload.rayActive = 0; diff --git a/projects/rtx/src/main.cpp b/projects/rtx/src/main.cpp index 4f75f7b7420b1121898bc6c1a5cfa7738e09b056..e4099fb1b7120e03d16d08df8a77a58970d934ca 100644 --- a/projects/rtx/src/main.cpp +++ b/projects/rtx/src/main.cpp @@ -68,14 +68,14 @@ int main(int argc, const char** argv) { // TODO: replace by bigger scene float cubeVertices[8*3] = { - 0.f,0.f,0.f, - 2.f,0.f,0.f, - 2.f,2.f,0.f, - 0.f,2.f,0.f, - 0.f,0.f,2.f, - 2.f,0.f,2.f, - 2.f,2.f,2.f, - 0.f,2.f,2.f + -1.f,-1.f,-1.f, + 1.f,-1.f,-1.f, + 1.f,1.f,-1.f, + -1.f,1.f,-1.f, + -1.f,-1.f,1.f, + 1.f,-1.f,1.f, + 1.f,1.f,1.f, + -1.f,1.f,1.f }; uint32_t cubeIndices[6 * 6] = @@ -192,7 +192,7 @@ int main(int argc, const char** argv) { cameraManager.update(0.000001 * static_cast<double>(deltatime.count())); const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer }; - + RaytracingPushConstantData raytracingPushData; raytracingPushData.camera_position = glm::vec4(cameraManager.getActiveCamera().getPosition(),0); raytracingPushData.camera_right = glm::vec4(glm::cross(cameraManager.getActiveCamera().getFront(), cameraManager.getActiveCamera().getUp()), 0); @@ -215,16 +215,16 @@ int main(int argc, const char** argv) { core.prepareImageForStorage(cmdStream, swapchainInput); core.recordRayGenerationToCmdStream( - cmdStream, - rtxPipeline, - rtxPipelineLayout, + cmdStream, + rtxPipeline, + rtxPipelineLayout, rtxModule.getShaderBindingBuffer(), rtxModule.getShaderGroupBaseAlignment(), { vkcv::DescriptorSetUsage(0, core.getDescriptorSet(rayGenShaderDescriptorSet).vulkanHandle), vkcv::DescriptorSetUsage(1, core.getDescriptorSet(rayMissShaderDescriptorSet).vulkanHandle), vkcv::DescriptorSetUsage(2, core.getDescriptorSet(rayCHITShaderDescriptorSet).vulkanHandle) }, - pushConstantsRTX, + pushConstantsRTX, windowHandle); core.prepareSwapchainImageForPresent(cmdStream);