Tonemap class and interfaces setup
This commit is contained in:
@@ -17,7 +17,7 @@ void FrameBuffer::SetPixel(int x, int y, uint32_t p) {
|
||||
}
|
||||
|
||||
void FrameBuffer::SetPixel(int x, int y, glm::vec3 p) {
|
||||
Pixel pixel{ (uint8_t)p.r, (uint8_t)p.g, (uint8_t)p.b };
|
||||
Pixel pixel{ (uint8_t)(p.r * 255.0f), (uint8_t)(p.g * 255.0f), (uint8_t)(p.b * 255.0f) };
|
||||
Data[y * this->XRes + x] = pixel.rgb();
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ void FrameBuffer::SetPixelSafe(int x, int y, uint32_t p) {
|
||||
|
||||
void FrameBuffer::SetPixelSafe(int x, int y, glm::vec3 p) {
|
||||
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
|
||||
Pixel pixel{ (uint8_t)p.r, (uint8_t)p.g, (uint8_t)p.b };
|
||||
Pixel pixel{ (uint8_t)(p.r * 255.0f), (uint8_t)(p.g * 255.0f), (uint8_t)(p.b * 255.0f) };
|
||||
Data[y * this->XRes + x] = pixel.rgb();
|
||||
}
|
||||
}
|
||||
|
||||
45
src/display/tonemap.cpp
Normal file
45
src/display/tonemap.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "tonemap.hpp"
|
||||
|
||||
#include "framebuffer.hpp"
|
||||
|
||||
#include "../pixel.hpp"
|
||||
|
||||
MapBuffer::MapBuffer(int xres, int yres) {
|
||||
XRes = xres; YRes = yres;
|
||||
Data = (glm::vec3*)malloc((xres * yres) * sizeof(glm::vec3));
|
||||
memset((void*)Data, 0, (xres * yres) * sizeof(glm::vec3));
|
||||
}
|
||||
|
||||
void MapBuffer::SetPixel(int x, int y, glm::vec3 p) {
|
||||
Data[y * this->XRes + x] = p;
|
||||
}
|
||||
|
||||
void MapBuffer::SetPixelSafe(int x, int y, glm::vec3 p) {
|
||||
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
|
||||
Data[y * this->XRes + x] = p;
|
||||
}
|
||||
}
|
||||
|
||||
void MapBuffer::SetFramebuffer(glm::vec3* fb) {
|
||||
free(Data);
|
||||
Data = fb;
|
||||
}
|
||||
|
||||
void MapBuffer::ClearFramebuffer() {
|
||||
memset((void*)Data, 0, (XRes * YRes) * sizeof(glm::vec3));
|
||||
}
|
||||
|
||||
void MapBuffer::ClampBasic(FrameBuffer* buffer) {
|
||||
for (int x = 0; x < XRes; x++)
|
||||
for (int y = 0; y < YRes; y++) {
|
||||
buffer->SetPixelSafe(x, y, Clamp(Data[y * this->XRes + x], 1.0f, 0.0f));
|
||||
}
|
||||
}
|
||||
|
||||
void MapBuffer::MapBasic(FrameBuffer* buffer) {
|
||||
|
||||
}
|
||||
|
||||
MapBuffer::~MapBuffer() {
|
||||
free(Data);
|
||||
}
|
||||
33
src/display/tonemap.hpp
Normal file
33
src/display/tonemap.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef INFERNO_ENGINE_TONEMAP_H_
|
||||
#define INFERNO_ENGINE_TONEMAP_H_
|
||||
|
||||
#include "../common.hpp"
|
||||
#include "../maths.hpp"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class FrameBuffer;
|
||||
class Pixel;
|
||||
|
||||
class MapBuffer {
|
||||
public:
|
||||
MapBuffer(int xres, int yres);
|
||||
|
||||
void SetPixel(int x, int y, glm::vec3 p);
|
||||
void SetPixelSafe(int x, int y, glm::vec3);
|
||||
|
||||
void SetFramebuffer(glm::vec3* fb);
|
||||
void ClearFramebuffer();
|
||||
|
||||
|
||||
void ClampBasic(FrameBuffer* buffer);
|
||||
void MapBasic(FrameBuffer* buffer);
|
||||
|
||||
|
||||
glm::vec3* Data;
|
||||
int XRes, YRes;
|
||||
|
||||
~MapBuffer();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
#include "../common.hpp"
|
||||
#include "../pixel.hpp"
|
||||
|
||||
#include "../display/displayinterface.hpp"
|
||||
#include "../display/framebuffer.hpp"
|
||||
#include "../display/tonemap.hpp"
|
||||
|
||||
#include "../util/assetloader.hpp"
|
||||
#include "../util/threadpool.hpp"
|
||||
@@ -52,7 +55,11 @@ void ProgressiveRenderer::Render() {
|
||||
Ready = true;
|
||||
m_threadPool->Ready = true;
|
||||
while (m_interface->Active) {
|
||||
if (m_threadPool->CheckAllJobs()) m_threadPool->RunJobsAgain();
|
||||
if (m_threadPool->CheckAllJobs()) {
|
||||
m_threadPool->MappedThreadFrameBuffer->ClampBasic(m_threadPool->ThreadFrameBuffer);
|
||||
m_threadPool->MergeBuffers(m_interface->Framebuffer->Data, m_scene->w, m_scene->h);
|
||||
m_threadPool->RunJobsAgain();
|
||||
}
|
||||
|
||||
Input();
|
||||
m_interface->Update();
|
||||
|
||||
@@ -1,11 +1,41 @@
|
||||
#include "renderengine.hpp"
|
||||
|
||||
#include "../pixel.hpp"
|
||||
#include "../util/threadpool.hpp"
|
||||
|
||||
#include "../definitions/primatives/primative.hpp"
|
||||
#include "../definitions/camera.hpp"
|
||||
#include "../definitions/scene.hpp"
|
||||
#include "../definitions/ray.hpp"
|
||||
|
||||
#include "../display/displayinterface.hpp"
|
||||
#include "../display/framebuffer.hpp"
|
||||
#include "../display/tonemap.hpp"
|
||||
|
||||
#include "../engine/renderengine.hpp"
|
||||
#include "../engine/progressiverenderer.hpp"
|
||||
|
||||
void workerThread(RenderThreadPool* threadpool, ProgressiveRenderer* renderer, int idd, int yStart, int yRange) {
|
||||
while (!renderer->Ready && !threadpool->Ready) {
|
||||
std::chrono::milliseconds dura(10);
|
||||
std::this_thread::sleep_for(dura);
|
||||
}
|
||||
|
||||
while (renderer->Ready && threadpool->Ready) {
|
||||
|
||||
for (int y = yStart; y < yStart + yRange; y++)
|
||||
for (int x = 0; x < renderer->m_scene->w; x++) {
|
||||
Ray ray = renderer->m_scene->camera->CastRay(x, y);
|
||||
glm::vec3 col = renderer->m_engine->GetColour(ray, 0);
|
||||
threadpool->MappedThreadFrameBuffer->SetPixelSafe(x, y, col);
|
||||
}
|
||||
|
||||
threadpool->ThreadStatus[idd] = true;
|
||||
|
||||
while (threadpool->ThreadStatus[idd]) { }
|
||||
}
|
||||
}
|
||||
|
||||
RenderEngine::RenderEngine() {
|
||||
|
||||
}
|
||||
@@ -24,12 +54,12 @@ glm::vec3 RenderEngine::GetColour(Ray ray, int depth) {
|
||||
|
||||
glm::vec3 hitPoint = ray.origin + ray.direction * t;
|
||||
|
||||
if (Mode == MODE_RENDER_NORMALS) { return GetNormalColour(hit, hitPoint); } else { }
|
||||
if (Mode == MODE_RENDER_NORMALS) return GetNormalColour(hit, hitPoint);
|
||||
|
||||
return { 1.0f, 1.0f, 1.0f };
|
||||
}
|
||||
|
||||
glm::vec3 RenderEngine::GetNormalColour(Primative* hit, glm::vec3 hitPoint) {
|
||||
glm::vec3 normal = hit->SurfaceNormal(hitPoint);
|
||||
return { (normal.x + 1.0f) * 127.5f, (normal.y + 1.0f) * 127.5f, (normal.z + 1.0f) * 127.5f };
|
||||
return { ((normal.x + 1.0f) * 127.5f) / 255.0f, ((normal.y + 1.0f) * 127.5f) / 255.0f, ((normal.z + 1.0f) * 127.5f) / 255.0f };
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
#include "tonemap.hpp"
|
||||
|
||||
// Need to map a vector of floats to a buffer. need to rework engine first
|
||||
void MapHRDLDRBasic(uint32_t* buffer, int w, int h) {
|
||||
|
||||
for (int x = 0; x < w; x++)
|
||||
for (int y = 0; y < h; y++) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int index(int x, int y, int w) {
|
||||
return (y * w + x);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef INFERNO_ENGINE_TONEMAP_H_
|
||||
#define INFERNO_ENGINE_TONEMAP_H_
|
||||
|
||||
#include "../common.hpp"
|
||||
|
||||
void MapHRDLDRBasic(uint32_t* buffer, int w, int h);
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,7 @@
|
||||
#define INFERNO_PIXEL_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include "maths.hpp"
|
||||
|
||||
struct Pixel {
|
||||
uint8_t a;
|
||||
@@ -33,6 +34,14 @@ inline uint8_t Clamp(int n, int upper, int lower) {
|
||||
return std::max(lower, std::min(n, upper));
|
||||
}
|
||||
|
||||
inline glm::vec3 Clamp(glm::vec3 in, float upper, float lower) {
|
||||
glm::vec3 ret;
|
||||
ret.r = std::max(lower, std::min(in.r, upper));
|
||||
ret.g = std::max(lower, std::min(in.g, upper));
|
||||
ret.b = std::max(lower, std::min(in.b, upper));
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline uint32_t argb8888(uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
|
||||
return a << 24 | r << 16 | g << 8 | b;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../display/displayinterface.hpp"
|
||||
#include "../display/framebuffer.hpp"
|
||||
#include "../display/tonemap.hpp"
|
||||
|
||||
#include "../engine/renderengine.hpp"
|
||||
#include "../engine/progressiverenderer.hpp"
|
||||
@@ -16,19 +17,24 @@ RenderThreadPool::RenderThreadPool() {
|
||||
}
|
||||
};
|
||||
|
||||
void RenderThreadPool::SetJobs(ProgressiveRenderer* renderer, int x, int y) {
|
||||
void RenderThreadPool::SetJobs(ProgressiveRenderer* renderer, int w, int h) {
|
||||
MappedThreadFrameBuffer = new MapBuffer(w, h);
|
||||
ThreadFrameBuffer = new FrameBuffer(w, h);
|
||||
for (int i = 0; i < ThreadCount; i++) {
|
||||
if (i == ThreadCount - 1) {
|
||||
Pool.push_back(new std::thread(workerThread, this, renderer, i,
|
||||
(x / ThreadCount) * i,
|
||||
-((x / ThreadCount) * i - x)
|
||||
(w / ThreadCount) * i,
|
||||
-((w / ThreadCount) * i - w)
|
||||
));
|
||||
}
|
||||
else {
|
||||
RenderRegions.push_back({ ((w / ThreadCount) * i) * w,
|
||||
(-((w / ThreadCount) * i - w)) * w });
|
||||
} else {
|
||||
Pool.push_back(new std::thread(workerThread, this, renderer, i,
|
||||
(y / ThreadCount) * i,
|
||||
(y / ThreadCount) * (i + 1) - (y / ThreadCount) * i
|
||||
(h / ThreadCount) * i,
|
||||
(h / ThreadCount) * (i + 1) - (h / ThreadCount) * i
|
||||
));
|
||||
RenderRegions.push_back({ ((h / ThreadCount) * i) * w,
|
||||
((h / ThreadCount) * (i + 1) - (h / ThreadCount) * i) * w });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,36 +63,10 @@ void RenderThreadPool::RunJobsAgain() {
|
||||
ThreadLock.unlock();
|
||||
}
|
||||
|
||||
void RenderThreadPool::MergeBuffers(uint32_t* Framebuffer) {
|
||||
|
||||
void RenderThreadPool::MergeBuffers(uint32_t* framebuffer, int w, int h) {
|
||||
memcpy((void*)framebuffer, (void*)ThreadFrameBuffer->Data, (w * h) * sizeof(uint32_t));
|
||||
}
|
||||
|
||||
void RenderThreadPool::Destroy() {
|
||||
|
||||
}
|
||||
|
||||
void workerThread(RenderThreadPool* threadpool, ProgressiveRenderer* renderer, int idd, int yStart, int yRange) {
|
||||
while (!renderer->Ready && !threadpool->Ready) {
|
||||
std::chrono::milliseconds dura(10);
|
||||
std::this_thread::sleep_for(dura);
|
||||
}
|
||||
|
||||
while (renderer->Ready && threadpool->Ready) {
|
||||
|
||||
for (int y = yStart; y < yStart + yRange; y++)
|
||||
for (int x = 0; x < renderer->m_scene->w; x++) {
|
||||
Ray ray = renderer->m_scene->camera->CastRay(x, y);
|
||||
glm::vec3 col = renderer->m_engine->GetColour(ray, 0);
|
||||
renderer->m_interface->Framebuffer->SetPixelSafe(x, y, col);
|
||||
}
|
||||
|
||||
threadpool->ThreadLock.lock();
|
||||
threadpool->ThreadStatus[idd] = true;
|
||||
threadpool->ThreadLock.unlock();
|
||||
|
||||
while (threadpool->ThreadStatus[idd]) {
|
||||
std::chrono::milliseconds dura(1);
|
||||
std::this_thread::sleep_for(dura);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <tuple>
|
||||
|
||||
class ProgressiveRenderer;
|
||||
class FrameBuffer;
|
||||
class MapBuffer;
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
@@ -21,15 +23,17 @@ class RenderThreadPool : public ThreadPool {
|
||||
public:
|
||||
RenderThreadPool();
|
||||
|
||||
void SetJobs(ProgressiveRenderer* renderer, int x, int y);
|
||||
void SetJobs(ProgressiveRenderer* renderer, int w, int h);
|
||||
void BeginJobs();
|
||||
bool CheckAllJobs(); // false = not ready, true = ready
|
||||
void RunJobsAgain();
|
||||
void Destroy();
|
||||
|
||||
void MergeBuffers(uint32_t* Framebuffer);
|
||||
void MergeBuffers(uint32_t* framebuffer, int w, int h);
|
||||
|
||||
std::vector<FrameBuffer*> ThreadFrameBuffers;
|
||||
std::vector<std::tuple<int, int>> RenderRegions; // offest, size
|
||||
MapBuffer* MappedThreadFrameBuffer;
|
||||
FrameBuffer* ThreadFrameBuffer;
|
||||
};
|
||||
|
||||
void workerThread(RenderThreadPool* threadpool, ProgressiveRenderer* renderer, int idd, int yStart, int yRange);
|
||||
|
||||
Reference in New Issue
Block a user