Skip to content
Snippets Groups Projects
Commit 696cd7bd authored by Yoel's avatar Yoel
Browse files

Implemented multithreading in setPixels- and raytracing-method

parent 0f05d361
No related branches found
No related tags found
No related merge requests found
...@@ -10,15 +10,32 @@ Image::Image(int width, int height) : width(width), height(height) { ...@@ -10,15 +10,32 @@ Image::Image(int width, int height) : width(width), height(height) {
vec.insert(vec.end(), color); vec.insert(vec.end(), color);
} }
} }
/*
void Image::setPixels(std::shared_ptr<Sampler> sampler) {
Threadpool tp(4);
for (int x = 0; x != width; x++) {
for (int y = 0; y != height; y++) {
vec[width * y + x] = sampler->color(x, y);
}
}
} */
void Image::setPixel(int x, int y, Vec3 color) { void Image::setPixel(int x, int y, Vec3 color) {
vec[width * y + x] = color; vec[width * y + x] = color;
} }
void Image::setPixels(std::shared_ptr<Sampler> sampler) { void Image::setPixels(size_t threadcount, std::shared_ptr<Sampler> sampler) {
Threadpool tp(threadcount);
for (int x = 0; x != width; x++) { for (int x = 0; x != width; x++) {
for (int y = 0; y != height; y++) { for (int y = 0; y != height; y++) {
setPixel(x, y, sampler->color(x, y)); tp.queueTask(std::bind([this, x, y, sampler]() {
this->setPixelsTask(x, y, sampler);
}));
} }
} }
// std::cout << "Done queing" << std::endl;
}
void Image::setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler) {
setPixel(x, y, sampler->color(x, y));
} }
Vec3 Image::operator[](const std::array<int, 2>& i) const { Vec3 Image::operator[](const std::array<int, 2>& i) const {
return vec[width * i[1] + i[0]]; return vec[width * i[1] + i[0]];
...@@ -26,13 +43,13 @@ Vec3 Image::operator[](const std::array<int, 2>& i) const { ...@@ -26,13 +43,13 @@ Vec3 Image::operator[](const std::array<int, 2>& i) const {
Vec3& Image::operator[](const std::array<int, 2>& i) { Vec3& Image::operator[](const std::array<int, 2>& i) {
return vec[width * i[1] + i[0]]; return vec[width * i[1] + i[0]];
} }
Image raytrace(const cam::CamObs& cam, const std::shared_ptr<Sampler>& sampler, Image raytrace(size_t threadcount, const cam::CamObs& cam,
size_t n) { const std::shared_ptr<Sampler>& sampler, size_t n) {
Image result(cam.width, cam.height); Image result(cam.width, cam.height);
result.setPixels( result.setPixels(threadcount, std::make_shared<StratifiedSampler>(
std::make_shared<StratifiedSampler>(StratifiedSampler(sampler, n)) StratifiedSampler(sampler, n))
// sampler // sampler
); );
return result; return result;
} }
......
...@@ -12,7 +12,7 @@ class Image { ...@@ -12,7 +12,7 @@ class Image {
Image(int width, int height); Image(int width, int height);
void setPixel(int x, int y, Vec3 color); void setPixel(int x, int y, Vec3 color);
void setPixels(std::shared_ptr<Sampler> sampler); void setPixels(size_t threadcount, std::shared_ptr<Sampler> sampler);
Vec3 operator[](const std::array<int, 2>& i) const; // int x, int y Vec3 operator[](const std::array<int, 2>& i) const; // int x, int y
Vec3& operator[](const std::array<int, 2>& i); Vec3& operator[](const std::array<int, 2>& i);
...@@ -20,11 +20,14 @@ class Image { ...@@ -20,11 +20,14 @@ class Image {
const int width; const int width;
const int height; const int height;
protected:
void setPixelsTask(int x, int y, std::shared_ptr<Sampler> sampler);
private: private:
std::vector<Vec3> vec; std::vector<Vec3> vec;
}; };
Image raytrace(const cam::CamObs& cam, const std::shared_ptr<Sampler>& sampler, Image raytrace(size_t threadcount, const cam::CamObs& cam,
size_t n); const std::shared_ptr<Sampler>& sampler, size_t n);
} // namespace util } // namespace util
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment