pathtracing

This commit is contained in:
Ben
2019-08-29 22:32:43 +01:00
parent d0db6b5794
commit c98371068c
13 changed files with 301 additions and 104 deletions

View File

@@ -1,5 +1,9 @@
#include "progressiverenderer.hpp"
#include <numeric>
#include <sstream>
#include <chrono>
#include "renderengine.hpp"
#include "../common.hpp"
@@ -7,7 +11,7 @@
#include "../display/displayinterface.hpp"
#include "../display/framebuffer.hpp"
#include "../display/tonemap.hpp"
#include "../display/tonemapfb.hpp"
#include "../util/assetloader.hpp"
#include "../util/threadpool.hpp"
@@ -35,30 +39,75 @@ void ProgressiveRenderer::Input() {
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) m_interface->Close();
//const Uint8* state = SDL_GetKeyboardState(NULL);
//if (state[SDL_SCANCODE_W]) m_scene->objects[0]->center.y += 0.01f;
//if (state[SDL_SCANCODE_S]) m_scene->objects[0]->center.y -= 0.01f;
//if (state[SDL_SCANCODE_D]) m_scene->objects[0]->center.x += 0.01f;
//if (state[SDL_SCANCODE_A]) m_scene->objects[0]->center.x -= 0.01f;
if (!m_interface->ImGui) return;
ImGui::NewFrame();
ImGui::Begin("Debug");
ImGui::Checkbox("Render Normals", &m_normals);
if (m_normals) {
m_engine->Mode = MODE_RENDER_NORMALS;
} else {
m_engine->Mode = MODE_RENDER_PATHTRACE;
if (m_engine->Mode != MODE_RENDER_NORMALS) {
std::stringstream str; str << "SPP: " << m_engine->SPP;
ImGui::Text(str.str().c_str());
}
std::stringstream str0; str0 << "FPS: " << 1.0f / AverageFrameTime;
ImGui::Text(str0.str().c_str());
std::stringstream str1; str1 << "MS Per Frame: " << AverageFrameTime * 1000.0f;
ImGui::Text(str1.str().c_str());
std::stringstream str2; str2 << "S Per Frame: " << AverageFrameTime;
ImGui::Text(str2.str().c_str());
float upper = 0.0f; float lower = 0.0f;
for (int i = 0; i < AllFrameTimes.size(); i++) {
if (AllFrameTimes[i] > upper) upper = AllFrameTimes[i];
if (AllFrameTimes[i] < lower) lower = AllFrameTimes[i];
}
ImGui::PlotLines("FrameTimes", FrameTimes.data(), FrameTimes.size(), 0, NULL, lower, upper, ImVec2(0, 40));
ImGui::BeginChild("Render Settings");
const char* renderItems[] = { "PathTrace", "Normals" };
ImGui::Combo("Render Mode", &m_renderModeSelected, renderItems, IM_ARRAYSIZE(renderItems));
m_engine->Mode = (RenderMode)m_renderModeSelected;
const char* toneMapItems[] = { "Clamp", "Basic Tonemap" };
ImGui::Combo("ToneMap Mode", &m_toneMapModeSelected, toneMapItems, IM_ARRAYSIZE(toneMapItems));
ImGui::EndChild();
ImGui::End();
}
void ProgressiveRenderer::Render() {
m_threadPool->SetJobs(this, m_scene->w, m_scene->h);
// Starts render loop
std::chrono::high_resolution_clock::time_point frameStartTime = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point frameEndTime;
Ready = true;
m_threadPool->Ready = true;
while (m_interface->Active) {
if (m_threadPool->CheckAllJobs()) {
m_threadPool->MappedThreadFrameBuffer->ClampBasic(m_threadPool->ThreadFrameBuffer);
m_engine->PostProcess(m_threadPool->MappedThreadFrameBuffer->RenderTo, m_threadPool->MappedThreadFrameBuffer->ProcData, m_scene->w, m_scene->h);
if (m_engine->Mode != MODE_RENDER_NORMALS) {
if (m_toneMapModeSelected == 0) m_threadPool->MappedThreadFrameBuffer->ClampBasic(m_threadPool->ThreadFrameBuffer);
if (m_toneMapModeSelected == 1) m_threadPool->MappedThreadFrameBuffer->MapBasic(m_threadPool->ThreadFrameBuffer);
} else {
m_threadPool->MappedThreadFrameBuffer->ClampBasic(m_threadPool->ThreadFrameBuffer);
}
m_threadPool->MergeBuffers(m_interface->Framebuffer->Data, m_scene->w, m_scene->h);
m_threadPool->RunJobsAgain();
frameEndTime = std::chrono::high_resolution_clock::now();
m_calculateTimes(frameStartTime, frameEndTime);
frameStartTime = std::chrono::high_resolution_clock::now();
}
Input();
@@ -69,6 +118,17 @@ void ProgressiveRenderer::Render() {
m_threadPool->Destroy();
}
void ProgressiveRenderer::RenderProgressive() {
void ProgressiveRenderer::m_calculateTimes(std::chrono::high_resolution_clock::time_point frameStartTime,
std::chrono::high_resolution_clock::time_point frameEndTime) {
m_framesRendererd++;
float frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(frameEndTime - frameStartTime).count();
frameTime /= 1000;
FrameTimes.push_back(frameTime);
AllFrameTimes.push_back(frameTime);
if (FrameTimes.size() > 11) FrameTimes.erase(FrameTimes.begin());
AverageFrameTime = std::accumulate(AllFrameTimes.begin(), AllFrameTimes.end(), 0.0) / AllFrameTimes.size();
}