From e764ed3547ecaf90f2118d1ff782a64fd42cfc64 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 24 Aug 2021 11:52:24 +0900 Subject: [PATCH] Ex02: Tweak use of mutex when threaded --- .../src/rayTracer.cpp | 66 +++++++++++++++---- .../src/rayTracer.hpp | 2 + 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/example02_threaded_raytracing/src/rayTracer.cpp b/example02_threaded_raytracing/src/rayTracer.cpp index 4313a9a..2b1a121 100644 --- a/example02_threaded_raytracing/src/rayTracer.cpp +++ b/example02_threaded_raytracing/src/rayTracer.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -115,6 +114,57 @@ void Ex02::RT::Internal::LightSource::applyLight( } } +void Ex02::RT::Internal::LightSource::applyLight( + glm::vec3 pos, Pixel &pixelOut, std::mutex* mutex) const { + pos = this->pos - pos; + float dist = std::sqrt( + pos.x * pos.x + + pos.y * pos.y + + pos.z * pos.z); + if(dist < falloffStart) { + const auto applyColor = [] (auto *color, unsigned char *out) { + unsigned int temp = (unsigned int)*out + + (unsigned int)(*color * 255.0f); + if(temp > 255) { + *out = 255; + } else { + *out = temp; + } + }; + if(mutex) { + std::lock_guard lock(*mutex); + applyColor(&color.x, &pixelOut.r); + applyColor(&color.y, &pixelOut.g); + applyColor(&color.z, &pixelOut.b); + } else { + applyColor(&color.x, &pixelOut.r); + applyColor(&color.y, &pixelOut.g); + applyColor(&color.z, &pixelOut.b); + } + } else if(dist >= falloffStart && dist <= falloffEnd) { + const auto applyFalloffColor = [] (auto *color, unsigned char *out, float f) { + unsigned int temp = (unsigned int)*out + + (unsigned int)(*color * 255.0f * f); + if(temp > 255) { + *out = 255; + } else { + *out = temp; + } + }; + float f = (1.0f - (dist - falloffStart) / (falloffEnd - falloffStart)); + if(mutex) { + std::lock_guard lock(*mutex); + applyFalloffColor(&color.x, &pixelOut.r, f); + applyFalloffColor(&color.y, &pixelOut.g, f); + applyFalloffColor(&color.z, &pixelOut.b, f); + } else { + applyFalloffColor(&color.x, &pixelOut.r, f); + applyFalloffColor(&color.y, &pixelOut.g, f); + applyFalloffColor(&color.z, &pixelOut.b, f); + } + } +} + Ex02::RT::Internal::Sphere::Sphere() : pos{0.0f, 0.0f, 0.0f}, radius(2.5f) @@ -399,16 +449,10 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres( // at this point, it is known that no spheres blocks ray // to light - if(mutex) { - std::lock_guard lock(*mutex); - light.applyLight( - std::get<0>(*closestResult), - image.getPixel(i, j)); - } else { - light.applyLight( - std::get<0>(*closestResult), - image.getPixel(i, j)); - } + light.applyLight( + std::get<0>(*closestResult), + image.getPixel(i, j), + mutex); } } }; diff --git a/example02_threaded_raytracing/src/rayTracer.hpp b/example02_threaded_raytracing/src/rayTracer.hpp index 72c72d0..0102e9c 100644 --- a/example02_threaded_raytracing/src/rayTracer.hpp +++ b/example02_threaded_raytracing/src/rayTracer.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -51,6 +52,7 @@ namespace Internal { glm::vec3 color; void applyLight(glm::vec3 pos, Pixel &pixelOut) const; + void applyLight(glm::vec3 pos, Pixel &pixelOut, std::mutex *mutex) const; }; struct Sphere {