Working thread impl
This commit is contained in:
parent
61e6cf17fd
commit
ade12df080
1 changed files with 88 additions and 53 deletions
|
@ -3,6 +3,8 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include <glm/matrix.hpp>
|
#include <glm/matrix.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
@ -316,8 +318,7 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(
|
||||||
lights[2].falloffStart = 3.0f;
|
lights[2].falloffStart = 3.0f;
|
||||||
lights[2].falloffEnd = 7.0f;
|
lights[2].falloffEnd = 7.0f;
|
||||||
|
|
||||||
if(threadCount <= 1) {
|
const auto yIteration = [&spheres, &lights, &image, outputWidth, outputHeight, rayPos] (unsigned int j, std::mutex *mutex) {
|
||||||
for(unsigned int j = 0; j < outputHeight; ++j) {
|
|
||||||
float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f));
|
float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f));
|
||||||
for(unsigned int i = 0; i < outputWidth; ++i) {
|
for(unsigned int i = 0; i < outputWidth; ++i) {
|
||||||
float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f));
|
float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f));
|
||||||
|
@ -378,12 +379,46 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(
|
||||||
|
|
||||||
// at this point, it is known that no spheres blocks ray
|
// at this point, it is known that no spheres blocks ray
|
||||||
// to light
|
// to light
|
||||||
|
if(mutex) {
|
||||||
|
std::lock_guard<std::mutex> lock(*mutex);
|
||||||
|
light.applyLight(
|
||||||
|
std::get<0>(*closestResult),
|
||||||
|
image.getPixel(i, j));
|
||||||
|
} else {
|
||||||
light.applyLight(
|
light.applyLight(
|
||||||
std::get<0>(*closestResult),
|
std::get<0>(*closestResult),
|
||||||
image.getPixel(i, j));
|
image.getPixel(i, j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if(threadCount <= 1) {
|
||||||
|
for(unsigned int j = 0; j < outputHeight; ++j) {
|
||||||
|
yIteration(j, nullptr);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
std::mutex mutex;
|
||||||
|
unsigned int range = outputHeight / threadCount;
|
||||||
|
for(unsigned int threadIdx = 0; threadIdx < threadCount; ++threadIdx) {
|
||||||
|
unsigned int start = range * threadIdx;
|
||||||
|
unsigned int end = range * (threadIdx + 1);
|
||||||
|
if(threadIdx + 1 == threadCount) {
|
||||||
|
end = outputHeight;
|
||||||
|
}
|
||||||
|
threads.emplace_back(std::thread([&yIteration] (unsigned int start, unsigned int end, std::mutex *mutex) {
|
||||||
|
for(unsigned int y = start; y < end; ++y) {
|
||||||
|
yIteration(y, mutex);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
&mutex));
|
||||||
|
}
|
||||||
|
for(std::thread &thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
|
Loading…
Reference in a new issue