Skip to content

Commit

Permalink
Changed roughness -> alpha routine, fixed untextured models
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelcluster committed Jan 11, 2022
1 parent 46e2be7 commit 7873365
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ glTF files specified on the command line are loaded and rendered.
"Open With" or dragging and dropping a glTF file over the executable works on Windows too.

There are restrictions about which glTF files can be opened.
- All geometries must have tangents.
- All primitives using normal maps must have tangents.
- Only one set of texture coordinates is supported.
- There must be at least one texture.
- All primitives must have a material.
- All materials must have a base color texture.
- Vertex colors are unsupported.

Many models from https://github.com/KhronosGroup/glTF-Sample-Models/, such as the Sponza, Damaged Helmet, and Lantern, fulfill these restrictions.
Many models from https://github.com/KhronosGroup/glTF-Sample-Models/, such as the Sponza, Damaged Helmet (if tangents are added), and Lantern, fulfill these restrictions.
24 changes: 15 additions & 9 deletions shaders/triangle.rchit
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ layout(location = 0) rayPayloadInEXT RayPayload payload;
hitAttributeEXT vec2 baryCoord;

float roughnessToAlpha(float roughness) {
roughness = max(roughness, 1e-3);
float x = log(roughness);
return 1.62142f + 0.819955f * x + 0.1734f * x * x + 0.0171201f * x * x * x + 0.000640711f * x * x * x * x;
return ((9.12793 * roughness - 16.3381) * roughness + 9.84534) * roughness + 0.013222;
}

vec3 sampleLight(vec3 hitPoint, vec3 objectHitNormal, float alpha) {
Expand Down Expand Up @@ -150,7 +148,7 @@ void main() {

vec3 hitPoint = gl_WorldRayOriginEXT + gl_HitTEXT * gl_WorldRayDirectionEXT;

vec3 instanceColor = material.albedoFactor;
vec3 instanceColor = material.albedoScale.xyz;
if(albedoTexIndex != 65535)
instanceColor *= texture(textures[nonuniformEXT(albedoTexIndex)], texCoords).rgb;

Expand All @@ -159,17 +157,21 @@ void main() {
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);
objectHitNormal = normalize((tbn * normalMap));
}
objectHitNormal *= vec3(1.0f, -1.0f, 1.0f);

vec3 incomingRadiance = vec3(0.0f);

if(emissiveTexIndex != 65535)
incomingRadiance += texture(textures[nonuniformEXT(emissiveTexIndex)], texCoords).rgb * material.emissiveFactor.rgb * 500.0f;
incomingRadiance += texture(textures[nonuniformEXT(emissiveTexIndex)], texCoords).rgb * material.emissiveFactor.rgb * 200.0f;

float roughness = material.roughnessFactor;
if(metalRoughTexIndex != 65535)
roughness *= texture(textures[nonuniformEXT(metalRoughTexIndex)], texCoords).b;
float metal = material.metallicFactor;
if(metalRoughTexIndex != 65535) {
roughness *= texture(textures[nonuniformEXT(metalRoughTexIndex)], texCoords).g;

}

float alpha = roughnessToAlpha(roughness);

Expand All @@ -190,8 +192,12 @@ void main() {
else {
payload.rayThroughput /= 1.0f - russianRouletteWeight;
}
vec3 offset = 0.01f * objectHitNormal;

traceRayEXT(tlasStructure, gl_RayFlagsNoneEXT, 0xFF, 0, 0, 0, hitPoint + 0.01f * objectHitNormal, 0, sampleDir, 999999999.0f, 0);
if(dot(sampleDir, objectHitNormal) < 0.0f) {
offset = 0.01f * normalize(-sampleDir);
}
traceRayEXT(tlasStructure, gl_RayFlagsNoneEXT, 0xFF, 0, 0, 0, hitPoint + offset, 0, sampleDir, 999999999.0f, 0);

incomingRadiance += payload.color.rgb * max(payload.color.a, 0.0f);
}
Expand Down
14 changes: 7 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(int argc, const char** argv) {
verifyResult(volkInitialize());
glfwInit();

RayTracingDevice device = RayTracingDevice(640, 480, true);
RayTracingDevice device = RayTracingDevice(true);

std::vector<std::string_view> gltfFilenames;
gltfFilenames.reserve(argc - 1);
Expand All @@ -19,20 +19,20 @@ int main(int argc, const char** argv) {
}

std::vector<Sphere> spheres = {
/*{ .position = { -8.3395f, -0.76978f, -2.3374f, }, .radius = 0.5f, .color = { 0.8f, 0.6f, 0.6f, 500.0f } },
{ .position = { -8.3395f, -0.76978f, -2.3374f, }, .radius = 0.5f, .color = { 0.8f, 0.6f, 0.6f, 500.0f } },
{ .position = { 8.9656f, -0.76978f, -2.3374f }, .radius = 0.5f, .color = { 0.4f, 0.7f, 0.6f, 500.0f } },
{ .position = { -8.73348522f, -3.92734623f, -2.85059690f }, .radius = 0.5f, .color = { 0.8f, 0.5f, 0.4f, 500.0f } }*/
{ .position = { -8.73348522f, -3.92734623f, -2.85059690f }, .radius = 0.5f, .color = { 0.8f, 0.5f, 0.4f, 500.0f } },
{ .position = { 0.3f, -1.76978f, -2.3374f }, .radius = 0.5f, .color = { 0.7f, 0.7f, 0.7f, 500.0f } }
};

MemoryAllocator allocator = MemoryAllocator(device);
OneTimeDispatcher dispatcher = OneTimeDispatcher(device);
ModelLoader loader = ModelLoader(device, allocator, dispatcher, gltfFilenames);
AccelerationStructureBuilder builder = AccelerationStructureBuilder(
device, allocator, dispatcher, loader,
spheres, 0, 1);
AccelerationStructureBuilder builder =
AccelerationStructureBuilder(device, allocator, dispatcher, loader, spheres, 0, 1);
PipelineBuilder pipelineBuilder =
PipelineBuilder(device, allocator, dispatcher, loader.textureDescriptorSetLayout(), 8);
PipelineBuilder(device, allocator, dispatcher,
loader.textures().empty() ? VK_NULL_HANDLE : loader.textureDescriptorSetLayout(), 8);

TriangleMeshRaytracer raytracer = TriangleMeshRaytracer(device, allocator, loader, pipelineBuilder, builder);

Expand Down
40 changes: 22 additions & 18 deletions src/util/ModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,24 +563,26 @@ ModelLoader::ModelLoader(RayTracingDevice& device, MemoryAllocator& allocator, O
vkDestroyBuffer(m_device.device(), m_indexStagingBuffer, nullptr);
vkDestroyBuffer(m_device.device(), m_imageStagingBuffer, nullptr);

std::vector<VkDescriptorImageInfo> textureImageInfos;
if (m_textures.size()) {
std::vector<VkDescriptorImageInfo> textureImageInfos;

textureImageInfos.reserve(m_textures.size());
textureImageInfos.reserve(m_textures.size());

for (auto& texture : m_textures) {
textureImageInfos.push_back({ .sampler = texture.sampler,
.imageView = texture.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL });
}
for (auto& texture : m_textures) {
textureImageInfos.push_back({ .sampler = texture.sampler,
.imageView = texture.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL });
}

VkWriteDescriptorSet setWrite = { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = m_textureDescriptorSet,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = static_cast<uint32_t>(m_textures.size()),
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = textureImageInfos.data() };
vkUpdateDescriptorSets(m_device.device(), 1, &setWrite, 0, nullptr);
VkWriteDescriptorSet setWrite = { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = m_textureDescriptorSet,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = static_cast<uint32_t>(m_textures.size()),
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = textureImageInfos.data() };
vkUpdateDescriptorSets(m_device.device(), 1, &setWrite, 0, nullptr);
}
}

ModelLoader::~ModelLoader() {
Expand All @@ -601,8 +603,10 @@ ModelLoader::~ModelLoader() {
}
vkDestroySampler(m_device.device(), m_fallbackSampler, nullptr);

vkDestroyDescriptorPool(m_device.device(), m_textureDescriptorPool, nullptr);
vkDestroyDescriptorSetLayout(m_device.device(), m_textureDescriptorSetLayout, nullptr);
if (m_textures.size()) {
vkDestroyDescriptorPool(m_device.device(), m_textureDescriptorPool, nullptr);
vkDestroyDescriptorSetLayout(m_device.device(), m_textureDescriptorSetLayout, nullptr);
}
}

void ModelLoader::addScene(cgltf_data* data, cgltf_scene* scene) {
Expand Down Expand Up @@ -981,7 +985,7 @@ void ModelLoader::addMaterial(cgltf_data* data, cgltf_material* material) {
data->textures + m_globalTextureIndexOffset;
}
if (material->pbr_metallic_roughness.metallic_roughness_texture.texture) {
newMaterial.metallicRoughnessTextureIndex = material->pbr_metallic_roughness.base_color_texture.texture -
newMaterial.metallicRoughnessTextureIndex = material->pbr_metallic_roughness.metallic_roughness_texture.texture -
data->textures + m_globalTextureIndexOffset;
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/PipelineBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ PipelineBuilder::PipelineBuilder(RayTracingDevice& device, MemoryAllocator& allo
VkDescriptorSetLayout layouts[3] = { m_imageDescriptorSetLayout, m_generalDescriptorSetLayout, textureSetLayout };

VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 3,
.setLayoutCount = textureSetLayout == VK_NULL_HANDLE ? 2U : 3U,
.pSetLayouts = layouts,
.pushConstantRangeCount = 1,
.pPushConstantRanges = &constantRange };
Expand Down

0 comments on commit 7873365

Please sign in to comment.