diff --git a/shaders/triangle.rchit b/shaders/triangle.rchit index b42e6e9..a36a0b1 100644 --- a/shaders/triangle.rchit +++ b/shaders/triangle.rchit @@ -150,15 +150,17 @@ void main() { vec3 hitPoint = gl_WorldRayOriginEXT + gl_HitTEXT * gl_WorldRayDirectionEXT; - vec3 instanceColor = texture(textures[nonuniformEXT(albedoTexIndex)], texCoords).rgb; + vec3 instanceColor = material.albedoFactor; + if(albedoTexIndex != 65535) + instanceColor *= texture(textures[nonuniformEXT(albedoTexIndex)], texCoords).rgb; - mat3 tbn = mat3(tangent, cross(normal, tangent) * tangentData.w, normal); - vec3 normalMap = vec3(0.0f, 0.0f, 1.0f); - if(normalTexIndex != 65535 && abs(material.normalMapFactor) > 0.001f) - normalMap = (texture(textures[nonuniformEXT(normalTexIndex)], texCoords).rgb * 2.0f - 1.0f) * material.normalMapFactor; - - vec3 objectHitNormal = normalize((tbn * normalMap)) * vec3(1.0f, -1.0f, 1.0f); + vec3 objectHitNormal = normal; + if(normalTexIndex != 65535 && abs(material.normalMapFactor) > 0.001f) { + mat3 tbn = mat3(tangent, cross(normal, tangent) * tangentData.w, normal); + vec3 normalMap = (texture(textures[nonuniformEXT(normalTexIndex)], texCoords).rgb * 2.0f - 1.0f) * material.normalMapFactor; + objectHitNormal = normalize((tbn * normalMap)) * vec3(1.0f, -1.0f, 1.0f); + } vec3 incomingRadiance = vec3(0.0f); diff --git a/src/Raytracer.cpp b/src/Raytracer.cpp index d777903..7847a7c 100644 --- a/src/Raytracer.cpp +++ b/src/Raytracer.cpp @@ -311,7 +311,7 @@ bool TriangleMeshRaytracer::update() { m_modelLoader.textureDescriptorSet() }; vkCmdBindDescriptorSets(frameData.commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, - m_pipelineBuilder.pipelineLayout(), 0, 3, sets, 0, nullptr); + m_pipelineBuilder.pipelineLayout(), 0, m_modelLoader.textures().size() ? 3 : 2, sets, 0, nullptr); VkStridedDeviceAddressRegionKHR nullRegion = {}; VkStridedDeviceAddressRegionKHR raygenRegion = m_pipelineBuilder.raygenDeviceAddressRegion(); diff --git a/src/util/ModelLoader.cpp b/src/util/ModelLoader.cpp index c0f7a76..47b7c04 100644 --- a/src/util/ModelLoader.cpp +++ b/src/util/ModelLoader.cpp @@ -1,10 +1,10 @@ #define CGLTF_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #include +#include #include #include #include -#include void multiply_mat4(float mat1[16], const float mat2[16]) { float tmp[16]; @@ -413,7 +413,8 @@ ModelLoader::ModelLoader(RayTracingDevice& device, MemoryAllocator& allocator, O bufferCopy.size = normalDataSize; vkCmdCopyBuffer(commandBuffer, m_normalStagingBuffer, m_normalBuffer, 1, &bufferCopy); bufferCopy.size = tangentDataSize; - vkCmdCopyBuffer(commandBuffer, m_tangentStagingBuffer, m_tangentBuffer, 1, &bufferCopy); + if (tangentDataSize) + vkCmdCopyBuffer(commandBuffer, m_tangentStagingBuffer, m_tangentBuffer, 1, &bufferCopy); bufferCopy.size = uvDataSize; vkCmdCopyBuffer(commandBuffer, m_uvStagingBuffer, m_uvBuffer, 1, &bufferCopy); bufferCopy.size = indexDataSize; @@ -495,40 +496,42 @@ ModelLoader::ModelLoader(RayTracingDevice& device, MemoryAllocator& allocator, O addressInfo.buffer = m_indexBuffer; m_indexBufferDeviceAddress = vkGetBufferDeviceAddress(m_device.device(), &addressInfo); - VkDescriptorSetLayoutBinding binding = { .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .descriptorCount = static_cast(m_textures.size()), - .stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | - VK_SHADER_STAGE_ANY_HIT_BIT_KHR }; - - VkDescriptorSetLayoutCreateInfo layoutCreateInfo = { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 1, - .pBindings = &binding, - }; - verifyResult( - vkCreateDescriptorSetLayout(m_device.device(), &layoutCreateInfo, nullptr, &m_textureDescriptorSetLayout)); - - VkDescriptorPoolSize sampledImageSize = { .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .descriptorCount = static_cast(m_textures.size()) }; - VkDescriptorPoolCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, - .maxSets = 1, - .poolSizeCount = 1, - .pPoolSizes = &sampledImageSize }; - verifyResult(vkCreateDescriptorPool(m_device.device(), &createInfo, nullptr, &m_textureDescriptorPool)); - - VkDescriptorSetAllocateInfo setAllocateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, - .descriptorPool = m_textureDescriptorPool, - .descriptorSetCount = 1, - .pSetLayouts = &m_textureDescriptorSetLayout }; - verifyResult(vkAllocateDescriptorSets(m_device.device(), &setAllocateInfo, &m_textureDescriptorSet)); - - setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, m_textureDescriptorSetLayout, - "Texture descriptor set layout"); - setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_POOL, m_textureDescriptorPool, - "Texture descriptor pool"); - setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_SET, m_textureDescriptorSet, "Texture descriptor set"); - + if (m_textures.size() > 0) { + VkDescriptorSetLayoutBinding binding = { .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = static_cast(m_textures.size()), + .stageFlags = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | + VK_SHADER_STAGE_ANY_HIT_BIT_KHR }; + + VkDescriptorSetLayoutCreateInfo layoutCreateInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, + .pBindings = &binding, + }; + verifyResult( + vkCreateDescriptorSetLayout(m_device.device(), &layoutCreateInfo, nullptr, &m_textureDescriptorSetLayout)); + + VkDescriptorPoolSize sampledImageSize = { .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = static_cast(m_textures.size()) }; + VkDescriptorPoolCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .maxSets = 1, + .poolSizeCount = 1, + .pPoolSizes = &sampledImageSize }; + verifyResult(vkCreateDescriptorPool(m_device.device(), &createInfo, nullptr, &m_textureDescriptorPool)); + + VkDescriptorSetAllocateInfo setAllocateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = m_textureDescriptorPool, + .descriptorSetCount = 1, + .pSetLayouts = &m_textureDescriptorSetLayout }; + verifyResult(vkAllocateDescriptorSets(m_device.device(), &setAllocateInfo, &m_textureDescriptorSet)); + + setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, m_textureDescriptorSetLayout, + "Texture descriptor set layout"); + setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_POOL, m_textureDescriptorPool, + "Texture descriptor pool"); + setObjectName(m_device.device(), VK_OBJECT_TYPE_DESCRIPTOR_SET, m_textureDescriptorSet, + "Texture descriptor set"); + } for (auto& data : gltfData) { cgltf_free(data); }