diff --git a/src/definitions/primatives/mesh.cpp b/src/definitions/primatives/mesh.cpp index 44ad21b..6a139e6 100644 --- a/src/definitions/primatives/mesh.cpp +++ b/src/definitions/primatives/mesh.cpp @@ -13,8 +13,10 @@ Mesh::Mesh(std::vector tris) { void Mesh::Optimise(AccelerationMode mode) { m_accelerator = new Acceleration(mode); + m_accelerator->Construct(Triangles); - if (m_accelerator->Constructed) Optimised = true; + + if (m_accelerator->Constructed) Optimised = true; } bool Mesh::Intersect(Ray ray, Triangle*& intersect, float& t) { diff --git a/src/display/display.cpp b/src/display/display.cpp index 8a3ed18..22f166b 100644 --- a/src/display/display.cpp +++ b/src/display/display.cpp @@ -2,6 +2,7 @@ #include +#include "../common.hpp" #include "../pixel.hpp" Display::Display() @@ -70,33 +71,49 @@ bool Display::InitImGui() { m_imguiTexture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 100, 100); if (!m_imguiTexture) return false; - m_imGui = true; + ImGui = true; return true; } void Display::SetPixel(int x, int y, Pixel p) { + m_framebufferMutex.lock(); Framebuffer[y * this->XRes + x] = p.rgb(); + m_framebufferMutex.unlock(); } void Display::SetPixel(int x, int y, uint32_t p) { + m_framebufferMutex.lock(); Framebuffer[y * this->XRes + x] = p; + m_framebufferMutex.unlock(); } void Display::SetPixelSafe(int x, int y, Pixel p) { if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + m_framebufferMutex.lock(); Framebuffer[y * this->XRes + x] = p.rgb(); - } + m_framebufferMutex.unlock(); + } } void Display::SetPixelSafe(int x, int y, uint32_t p) { if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) { + m_framebufferMutex.lock(); Framebuffer[y * this->XRes + x] = p; - } + m_framebufferMutex.unlock(); + } } void Display::SetFramebuffer(uint32_t* fb) { + m_framebufferMutex.lock(); Framebuffer = nullptr; Framebuffer = fb; + m_framebufferMutex.unlock(); +} + +void Display::ClearFramebuffer() { + m_framebufferMutex.lock(); + memset((void*)Framebuffer, 0, (XRes * YRes) * sizeof(uint32_t)); + m_framebufferMutex.unlock(); } void Display::Update() { @@ -114,6 +131,7 @@ void Display::Update() { ImGui::Render(); ImGuiSDL::Render(ImGui::GetDrawData()); + SDL_RenderPresent(m_renderer); } diff --git a/src/display/display.hpp b/src/display/display.hpp index 51c3728..681f57c 100644 --- a/src/display/display.hpp +++ b/src/display/display.hpp @@ -1,6 +1,8 @@ #ifndef INFERNO_DISPLAY_DISPLAY_H_ #define INFERNO_DISPLAY_DISPLAY_H_ +#include + #include "../common.hpp" #include "displayinterface.hpp" @@ -19,12 +21,13 @@ public: void SetPixelSafe(int x, int y, Pixel p) override; void SetPixelSafe(int x, int y, uint32_t p) override; - void SetFramebuffer(uint32_t* fb); + void SetFramebuffer(uint32_t* fb) override; + void ClearFramebuffer() override; void Update() override; void UpdatePartial() override; - void UpdateTitle(std::string title); - void UpdateTitle(); + void UpdateTitle(std::string title) override; + void UpdateTitle() override; void Close() override; @@ -35,7 +38,7 @@ private: SDL_Renderer* m_renderer; SDL_Texture* m_texture; SDL_Texture* m_imguiTexture; - bool m_imGui = false; + std::mutex m_framebufferMutex; }; #endif diff --git a/src/display/displayinterface.hpp b/src/display/displayinterface.hpp index 52db538..8849de5 100644 --- a/src/display/displayinterface.hpp +++ b/src/display/displayinterface.hpp @@ -10,6 +10,7 @@ public: DisplayInterface(); bool Active = false; + bool ImGui = false; int XRes, YRes; std::string Title; unsigned int Scale = 1; @@ -23,6 +24,11 @@ public: virtual void SetPixelSafe(int x, int y, Pixel p) = 0; virtual void SetPixelSafe(int x, int y, uint32_t p) = 0; + virtual void SetFramebuffer(uint32_t* fb) = 0; + virtual void ClearFramebuffer() = 0; + virtual void UpdateTitle(std::string title) = 0; + virtual void UpdateTitle() = 0; + virtual void Update() = 0; virtual void UpdatePartial() = 0; diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 14967d4..8313980 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -1,8 +1,5 @@ #include "progressiverenderer.hpp" -#include -#include - #include "../common.hpp" #include "../pixel.hpp" #include "../display/displayinterface.hpp" @@ -23,31 +20,45 @@ void ProgressiveRenderer::Init(DisplayInterface* interface, Scene* scene) { m_scene = scene; } +void ProgressiveRenderer::Input() { + SDL_Event e; + while (SDL_PollEvent(&e)) + if (e.type == SDL_QUIT) m_interface->Close(); + + if (!m_interface->ImGui) return; + + ImGui::NewFrame(); + ImGui::Begin("Debug"); + ImGui::Text("Hello, world %d", 123); + if (ImGui::Button("Save")) {} + char* buf = ""; float f; + ImGui::InputText("string", buf, IM_ARRAYSIZE(buf)); + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); + ImGui::End(); +} + void ProgressiveRenderer::Render() { int frames = 0; auto startTime = std::chrono::high_resolution_clock::now(); + while (m_interface->Active) { + + + + Input(); + + m_interface->Update(); + } + + while (m_interface->Active) { auto frameStartTime = std::chrono::high_resolution_clock::now(); - ImGui::NewFrame(); - ImGui::Begin("Thing"); - ImGui::Text("Hello, world %d", 123); - if (ImGui::Button("Save")) {} - char* buf = ""; float f; - ImGui::InputText("string", buf, IM_ARRAYSIZE(buf)); - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); - ImGui::End(); - #pragma omp parallel for schedule(dynamic) for (int x = 0; x < m_scene->w; x++) for (int y = 0; y < m_scene->h; y++) { - SDL_Event e; - while (SDL_PollEvent(&e)) - if (e.type == SDL_QUIT) m_interface->Close(); - Ray ray = m_scene->camera->CastRay(x, y); float t; @@ -71,12 +82,18 @@ void ProgressiveRenderer::Render() { frames++; std::cout << "Frame: " << frames << std::endl; std::cout << "Frame Time: " << std::chrono::duration_cast(endTime - frameStartTime).count() - << ":" << std::chrono::duration_cast(endTime - frameStartTime).count() << std::endl; + << ":" << std::chrono::duration_cast(endTime - frameStartTime).count() << "s" << std::endl; std::cout << "Avg Frame Time: " << std::chrono::duration_cast(endTime - startTime).count() / frames - << ":" << std::chrono::duration_cast(endTime - startTime).count() / frames + << ":" << std::chrono::duration_cast(endTime - startTime).count() / frames << "s" << std::endl << std::endl; // Swap framebuffers m_interface->Update(); + m_interface->ClearFramebuffer(); } } + +void ProgressiveRenderer::RenderProgressive() { + +} + diff --git a/src/engine/progressiverenderer.hpp b/src/engine/progressiverenderer.hpp index c3d8835..246f6ff 100644 --- a/src/engine/progressiverenderer.hpp +++ b/src/engine/progressiverenderer.hpp @@ -1,8 +1,12 @@ #ifndef INFERNO_ENGINE_PROGRESSIVERENDERER_H_ #define INFERNO_ENGINE_PROGRESSIVERENDERER_H_ -class DisplayInterface; +#include +#include +#include +#include +class DisplayInterface; class Scene; class ProgressiveRenderer { @@ -11,12 +15,19 @@ public: void Init(DisplayInterface* interface, Scene* scene); + void Input(); void Render(); + void RenderProgressive(); private: DisplayInterface* m_interface = nullptr; Scene* m_scene = nullptr; + + int m_workerMax = 16; + std::vector m_workers; + + bool m_mxaa = true; }; #endif diff --git a/test/main.cpp b/test/main.cpp index 380f08f..ffc9a89 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -24,17 +24,17 @@ int main(int argc, char** argv) { // object->Translate({ 0.0f, -5.0f, -20.0f }); // std::vector tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/lucy-normals.obj"); - // std::vector tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/lucy-normals.obj"); - // for (const auto& object : tris) - // object->Translate({ 0.0f, -3.9f, -10.6f }); + //std::vector tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/lucy-normals.obj"); + //for (const auto& object : tris) + // object->Translate({ 0.0f, -3.9f, -10.6f }); - // std::vector tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/cornell.obj"); + //std::vector tris = LoadTrianglesBasic("/home/ben/programming/inferno/resources/cornell.obj"); std::vector tris = LoadTrianglesBasic("E:/Projects/Inferno/resources/cornell.obj"); for (const auto& object : tris) object->Translate({ 0.0f, -0.9f, -3.0f }); Mesh* mesh = new Mesh(tris); - mesh->Optimise(); + //mesh->Optimise(); scene->meshs.push_back(mesh); inferno.SetScene(scene);