Skip to content

Commit

Permalink
Untested handling of no textures/no tangents
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelcluster committed Jan 11, 2022
1 parent 3272134 commit 7316eb7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
16 changes: 9 additions & 7 deletions shaders/triangle.rchit
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Raytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
75 changes: 39 additions & 36 deletions src/util/ModelLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#define CGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <DebugHelper.hpp>
#include <cstring>
#include <filesystem>
#include <stb_image.h>
#include <util/ModelLoader.hpp>
#include <cstring>

void multiply_mat4(float mat1[16], const float mat2[16]) {
float tmp[16];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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);
}
Expand Down

0 comments on commit 7316eb7

Please sign in to comment.