From e56201ef1d7ae69f7ec55d2763718ab3d863f315 Mon Sep 17 00:00:00 2001 From: mklefrancois <38076163+mklefrancois@users.noreply.github.com> Date: Thu, 9 Jul 2020 11:30:10 +0200 Subject: [PATCH] Fixing image leak and computeDiffuse in Wavefront shading --- ray_tracing__advance/hello_vulkan.cpp | 13 ++++++---- ray_tracing__advance/shaders/wavefront.glsl | 3 ++- ray_tracing__before/hello_vulkan.cpp | 13 ++++++---- ray_tracing__before/shaders/wavefront.glsl | 3 ++- ray_tracing__simple/hello_vulkan.cpp | 13 ++++++---- ray_tracing__simple/shaders/wavefront.glsl | 3 ++- ray_tracing_animation/hello_vulkan.cpp | 13 ++++++---- ray_tracing_animation/shaders/wavefront.glsl | 3 ++- ray_tracing_anyhit/hello_vulkan.cpp | 13 ++++++---- ray_tracing_anyhit/shaders/wavefront.glsl | 3 ++- ray_tracing_callable/hello_vulkan.cpp | 13 ++++++---- ray_tracing_callable/shaders/wavefront.glsl | 3 ++- ray_tracing_gltf/hello_vulkan.cpp | 26 +++++++++++++++---- ray_tracing_instances/hello_vulkan.cpp | 13 ++++++---- ray_tracing_instances/shaders/wavefront.glsl | 3 ++- ray_tracing_intersection/hello_vulkan.cpp | 13 ++++++---- .../shaders/wavefront.glsl | 3 ++- ray_tracing_jitter_cam/hello_vulkan.cpp | 13 ++++++---- ray_tracing_jitter_cam/shaders/wavefront.glsl | 3 ++- ray_tracing_manyhits/hello_vulkan.cpp | 13 ++++++---- ray_tracing_manyhits/shaders/wavefront.glsl | 3 ++- ray_tracing_rayquery/hello_vulkan.cpp | 13 ++++++---- ray_tracing_rayquery/shaders/wavefront.glsl | 3 ++- ray_tracing_reflections/hello_vulkan.cpp | 13 ++++++---- .../shaders/wavefront.glsl | 3 ++- 25 files changed, 141 insertions(+), 77 deletions(-) diff --git a/ray_tracing__advance/hello_vulkan.cpp b/ray_tracing__advance/hello_vulkan.cpp index 7c9823b..6e086fd 100644 --- a/ray_tracing__advance/hello_vulkan.cpp +++ b/ray_tracing__advance/hello_vulkan.cpp @@ -380,16 +380,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -405,6 +406,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing__advance/shaders/wavefront.glsl b/ray_tracing__advance/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing__advance/shaders/wavefront.glsl +++ b/ray_tracing__advance/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing__before/hello_vulkan.cpp b/ray_tracing__before/hello_vulkan.cpp index 871033b..27bc7f5 100644 --- a/ray_tracing__before/hello_vulkan.cpp +++ b/ray_tracing__before/hello_vulkan.cpp @@ -313,16 +313,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -339,6 +340,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing__before/shaders/wavefront.glsl b/ray_tracing__before/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing__before/shaders/wavefront.glsl +++ b/ray_tracing__before/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing__simple/hello_vulkan.cpp b/ray_tracing__simple/hello_vulkan.cpp index 157cd9e..cdfe4ff 100644 --- a/ray_tracing__simple/hello_vulkan.cpp +++ b/ray_tracing__simple/hello_vulkan.cpp @@ -334,16 +334,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -360,6 +361,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing__simple/shaders/wavefront.glsl b/ray_tracing__simple/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing__simple/shaders/wavefront.glsl +++ b/ray_tracing__simple/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_animation/hello_vulkan.cpp b/ray_tracing_animation/hello_vulkan.cpp index 16239a8..dcaf651 100644 --- a/ray_tracing_animation/hello_vulkan.cpp +++ b/ray_tracing_animation/hello_vulkan.cpp @@ -334,16 +334,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -360,6 +361,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_animation/shaders/wavefront.glsl b/ray_tracing_animation/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_animation/shaders/wavefront.glsl +++ b/ray_tracing_animation/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_anyhit/hello_vulkan.cpp b/ray_tracing_anyhit/hello_vulkan.cpp index 159c660..dd2b108 100644 --- a/ray_tracing_anyhit/hello_vulkan.cpp +++ b/ray_tracing_anyhit/hello_vulkan.cpp @@ -337,16 +337,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -363,6 +364,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_anyhit/shaders/wavefront.glsl b/ray_tracing_anyhit/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_anyhit/shaders/wavefront.glsl +++ b/ray_tracing_anyhit/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_callable/hello_vulkan.cpp b/ray_tracing_callable/hello_vulkan.cpp index f46d1b3..8df376c 100644 --- a/ray_tracing_callable/hello_vulkan.cpp +++ b/ray_tracing_callable/hello_vulkan.cpp @@ -334,16 +334,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -360,6 +361,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_callable/shaders/wavefront.glsl b/ray_tracing_callable/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_callable/shaders/wavefront.glsl +++ b/ray_tracing_callable/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_gltf/hello_vulkan.cpp b/ray_tracing_gltf/hello_vulkan.cpp index f6fbcb5..979cdd8 100644 --- a/ray_tracing_gltf/hello_vulkan.cpp +++ b/ray_tracing_gltf/hello_vulkan.cpp @@ -44,6 +44,7 @@ extern std::vector defaultSearchPaths; #include "nvvk/pipeline_vk.hpp" #include "nvvk/renderpasses_vk.hpp" #include "nvvk/shaders_vk.hpp" +#include "nvh/nvprint.hpp" #include "shaders/binding.glsl" @@ -205,7 +206,11 @@ void HelloVulkan::loadScene(const std::string& filename) std::string warn, error; if(!tcontext.LoadASCIIFromFile(&tmodel, &error, &warn, filename)) + { assert(!"Error while loading scene"); + } + LOGW(warn.c_str()); + LOGE(error.c_str()); m_gltfScene.importMaterials(tmodel); @@ -286,31 +291,42 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, tinygltf: samplerCreateInfo.setMaxLod(FLT_MAX); vk::Format format = vk::Format::eR8G8B8A8Srgb; - if(gltfModel.images.empty()) - { + auto addDefaultTexture = [this]() { // Make dummy image(1,1), needed as we cannot have an empty array nvvk::ScopeCommandBuffer cmdBuf(m_device, m_graphicsQueueIndex); std::array white = {255, 255, 255, 255}; m_textures.emplace_back(m_alloc.createTexture( cmdBuf, 4, white.data(), nvvk::makeImage2DCreateInfo(vk::Extent2D{1, 1}), {})); - m_debug.setObjectName(m_textures[0].image, "dummy"); + m_debug.setObjectName(m_textures.back().image, "dummy"); + }; + + if(gltfModel.images.empty()) + { + addDefaultTexture(); return; } - m_textures.resize(gltfModel.images.size()); + m_textures.reserve(gltfModel.images.size()); for(size_t i = 0; i < gltfModel.images.size(); i++) { auto& gltfimage = gltfModel.images[i]; void* buffer = &gltfimage.image[0]; VkDeviceSize bufferSize = gltfimage.image.size(); auto imgSize = vk::Extent2D(gltfimage.width, gltfimage.height); + + if(bufferSize == 0 || gltfimage.width == -1 || gltfimage.height == -1) + { + addDefaultTexture(); + continue; + } + vk::ImageCreateInfo imageCreateInfo = nvvk::makeImage2DCreateInfo(imgSize, format, vkIU::eSampled, true); nvvk::Image image = m_alloc.createImage(cmdBuf, bufferSize, buffer, imageCreateInfo); nvvk::cmdGenerateMipmaps(cmdBuf, image.image, format, imgSize, imageCreateInfo.mipLevels); vk::ImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo); - m_textures[i] = m_alloc.createTexture(image, ivInfo, samplerCreateInfo); + m_textures.emplace_back(m_alloc.createTexture(image, ivInfo, samplerCreateInfo)); m_debug.setObjectName(m_textures[i].image, std::string("Txt" + std::to_string(i)).c_str()); } diff --git a/ray_tracing_instances/hello_vulkan.cpp b/ray_tracing_instances/hello_vulkan.cpp index 5b70f19..0f74645 100644 --- a/ray_tracing_instances/hello_vulkan.cpp +++ b/ray_tracing_instances/hello_vulkan.cpp @@ -350,16 +350,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -375,6 +376,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_instances/shaders/wavefront.glsl b/ray_tracing_instances/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_instances/shaders/wavefront.glsl +++ b/ray_tracing_instances/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_intersection/hello_vulkan.cpp b/ray_tracing_intersection/hello_vulkan.cpp index c26fd7d..6ff3db6 100644 --- a/ray_tracing_intersection/hello_vulkan.cpp +++ b/ray_tracing_intersection/hello_vulkan.cpp @@ -344,16 +344,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -370,6 +371,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_intersection/shaders/wavefront.glsl b/ray_tracing_intersection/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_intersection/shaders/wavefront.glsl +++ b/ray_tracing_intersection/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_jitter_cam/hello_vulkan.cpp b/ray_tracing_jitter_cam/hello_vulkan.cpp index dd1d3a8..5dfcb47 100644 --- a/ray_tracing_jitter_cam/hello_vulkan.cpp +++ b/ray_tracing_jitter_cam/hello_vulkan.cpp @@ -334,16 +334,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -360,6 +361,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_jitter_cam/shaders/wavefront.glsl b/ray_tracing_jitter_cam/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_jitter_cam/shaders/wavefront.glsl +++ b/ray_tracing_jitter_cam/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_manyhits/hello_vulkan.cpp b/ray_tracing_manyhits/hello_vulkan.cpp index ec0e23d..2cf2fdc 100644 --- a/ray_tracing_manyhits/hello_vulkan.cpp +++ b/ray_tracing_manyhits/hello_vulkan.cpp @@ -338,16 +338,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -364,6 +365,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_manyhits/shaders/wavefront.glsl b/ray_tracing_manyhits/shaders/wavefront.glsl index e3485c9..9dfdd52 100644 --- a/ray_tracing_manyhits/shaders/wavefront.glsl +++ b/ray_tracing_manyhits/shaders/wavefront.glsl @@ -36,7 +36,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_rayquery/hello_vulkan.cpp b/ray_tracing_rayquery/hello_vulkan.cpp index 162f05e..d07f0e4 100644 --- a/ray_tracing_rayquery/hello_vulkan.cpp +++ b/ray_tracing_rayquery/hello_vulkan.cpp @@ -342,16 +342,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -367,6 +368,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_rayquery/shaders/wavefront.glsl b/ray_tracing_rayquery/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_rayquery/shaders/wavefront.glsl +++ b/ray_tracing_rayquery/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal) diff --git a/ray_tracing_reflections/hello_vulkan.cpp b/ray_tracing_reflections/hello_vulkan.cpp index 69f954e..faf0b48 100644 --- a/ray_tracing_reflections/hello_vulkan.cpp +++ b/ray_tracing_reflections/hello_vulkan.cpp @@ -334,16 +334,17 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths); - stbi_uc* pixels = - stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + std::array color{255u, 0u, 255u, 255u}; + + stbi_uc* pixels = stbi_pixels; // Handle failure - if(!pixels) + if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; - std::array color{255u, 0u, 255u, 255u}; - pixels = reinterpret_cast(color.data()); + pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; @@ -360,6 +361,8 @@ void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, m_textures.push_back(texture); } + + stbi_image_free(stbi_pixels); } } } diff --git a/ray_tracing_reflections/shaders/wavefront.glsl b/ray_tracing_reflections/shaders/wavefront.glsl index 78b4b3c..d0810fb 100644 --- a/ray_tracing_reflections/shaders/wavefront.glsl +++ b/ray_tracing_reflections/shaders/wavefront.glsl @@ -35,7 +35,8 @@ vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal) float dotNL = max(dot(normal, lightDir), 0.0); vec3 c = mat.diffuse * dotNL; if(mat.illum >= 1) - return c + mat.ambient; + c += mat.ambient; + return c; } vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal)