From 7eb251e14586505fcbfd9e5d867172c02e99c8a0 Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Mon, 23 Jan 2023 16:12:31 +0000 Subject: [PATCH] WELL WELL LOOK WHOS INSIDE AGAIN --- hart/inferno-hart-cpu/src/main.cpp | 13 ++-- src/hart_module.cpp | 6 +- src/renderer/ray_source.cpp | 4 +- src/renderer/renderer.cpp | 98 ++++++++++-------------------- src/renderer/renderer.hpp | 2 + src/scene/mesh.cpp | 10 +-- src/scene/mesh.hpp | 4 +- 7 files changed, 52 insertions(+), 85 deletions(-) diff --git a/hart/inferno-hart-cpu/src/main.cpp b/hart/inferno-hart-cpu/src/main.cpp index f9c306c..e7e5e92 100644 --- a/hart/inferno-hart-cpu/src/main.cpp +++ b/hart/inferno-hart-cpu/src/main.cpp @@ -44,7 +44,7 @@ public: { std::unique_lock lock(_mData); mLastState = mState.load(); - if (mToTrace.empty()) + if (mToTrace.size() == 0) { lock.unlock(); mState = EModuleState::Ready; @@ -57,7 +57,7 @@ public: spdlog::info("[hartcpu] Signal start"); } - auto* ray = mToTrace.front(); + Ray* ray = mToTrace.front(); int bestIdx = -1; glm::vec2 coords; glm::vec2 bestTexcoord; @@ -78,7 +78,8 @@ public: bestTexcoord = coords; } - HitInfo* hit = new HitInfo{}; + HitInfo hit; + hit.Caller = ray; // If no hit, we still need to inform the HHM if (bestIdx < 0) { @@ -86,10 +87,10 @@ public: continue; } - hit->Distance = bestDist; - hit->UV = bestTexcoord; + hit.Distance = bestDist; + hit.UV = bestTexcoord; - Hit(mCtx, hit); + Hit(mCtx, &hit); mToTrace.pop(); } diff --git a/src/hart_module.cpp b/src/hart_module.cpp index 2df1433..aeffcb4 100644 --- a/src/hart_module.cpp +++ b/src/hart_module.cpp @@ -44,8 +44,8 @@ void HHM::newScene(Scene* scene) // which would involve a lot of mesh copying (avoid!) if i were to chain them for (auto* mesh : meshs) { void* verticies; void* normals; void* indicies; - int vertexCount = mesh->getVerticies(verticies, normals); - int indexCount = mesh->getIndicies(indicies); + int vertexCount = mesh->getVerticies(&verticies, &normals); + int indexCount = mesh->getIndicies(&indicies); spdlog::debug("Mesh for module ready... {} {}", verticies, normals); mod->submitTris(verticies, normals, vertexCount, indicies, indexCount); } @@ -64,13 +64,11 @@ void rayHitCallback(void* hhm, HitInfo* hit) void HHM::rayReturn(HitInfo* hit) { - HARTModule* mod = mDirectory.getActiveModule(); Renderer->computeHit(hit); } void HHM::bounce(Ray* newRay) { - HARTModule* mod = mDirectory.getActiveModule(); } diff --git a/src/renderer/ray_source.cpp b/src/renderer/ray_source.cpp index fb9c41d..e7d4a00 100644 --- a/src/renderer/ray_source.cpp +++ b/src/renderer/ray_source.cpp @@ -45,14 +45,14 @@ ReferencedRayField RaySource::getInitialRays(bool MSAA) std::unordered_map reference; - int i = 0; + uint32_t i = 0; for (int x = 0; x < mReferenceCamera->getRayViewport().x; x++) for (int y = 0; y < mReferenceCamera->getRayViewport().y; y++) { float Px = (2.0f * ((x + 0.5f) / mReferenceCamera->getRayViewport().x) - 1.0f) * scale * aspect; float Py = (1.0f - 2.0f * ((y + 0.5f) / mReferenceCamera->getRayViewport().y) * scale); - Ray* ray = new Ray; + Ray* ray = new Ray{}; ray->Origin = origin; ray->Direction = glm::normalize((glm::vec4(Px, Py, -1.0f, 1.0f) * cameraToWorld)); ray->Reference = i; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 0f3381e..33d4e7c 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -83,10 +83,33 @@ void RayRenderer::prepare() assert(mCurrentScene != NULL); if (mCurrentScene->didUpdate()) { + spdlog::debug("New Scene!"); mIface->newScene(mCurrentScene); } } +void RayRenderer::mHaultWait() +{ + bool frameStatus = false; + while (!frameStatus) + { + std::lock_guard lock(this->_mTarget); + switch(mIface->getModuleState()) + { + case EModuleState::Bad: + spdlog::error("MODULE STATE BAD"); + break; + case EModuleState::Build: + spdlog::error("MODULE STATE BUILD"); + case EModuleState::Trace: + break; + case EModuleState::Ready: + frameStatus = true; + break; + } + } +} + void RayRenderer::draw() { mCurrentScene->newFrame(); @@ -99,74 +122,12 @@ void RayRenderer::draw() mTarget[y * mRenderTargetSize.x + x] = { 1.0f, 0.0f, 0.0f, 1.0f }; } + mHaultWait(); + mCurrentRefTable = &startRays.Reference; - // mIface->startTrace(startRays.Field); + mIface->startTrace(startRays.Field); - // hault wait for the module to finish - // bool frameStatus = false; - // while (!frameStatus) - // { - // switch(mIface->getModuleState()) - // { - // case EModuleState::Bad: - // spdlog::error("MODULE STATE BAD"); - // break; - // case EModuleState::Build: - // spdlog::error("MODULE STATE BUILD"); - // case EModuleState::Trace: - // break; - // case EModuleState::Ready: - // frameStatus = true; - // break; - // } - // } - - while(!startRays.Field.empty()) - { - void* verticies; void* normals; void* indicies; - int vertexCount = mCurrentScene->getRenderables()[0]->getVerticies(verticies, normals); - int indexCount = mCurrentScene->getRenderables()[0]->getIndicies(indicies); - float* mVert = (float*)verticies; float* mNorm = (float*)normals; - int mVc = vertexCount; uint32_t* mIndicies = (uint32_t*)indicies; int mIc = indexCount; - - auto* ray = startRays.Field.front(); - int bestIdx = -1; - glm::vec2 coords; - glm::vec2 bestTexcoord; - float bestDist = INFINITY; - float dist; - for (int i = 0; i < mCurrentScene->getRenderables()[0]->getIndexCount() ; i += 9) - { - uint32_t ind = mIndicies[i]; - // Check if the ray intersects - const glm::vec3 a = { mVert[ind + 1], mVert[ind + 2], mVert[ind + 3] }; - const glm::vec3 b = { mVert[ind + 4], mVert[ind + 5], mVert[ind + 6] }; - const glm::vec3 c = { mVert[ind + 7], mVert[ind + 8], mVert[ind + 9] }; - - if (!glm::intersectRayTriangle(ray->Origin, ray->Direction, a, b, c, coords, dist)) { continue; } - if (dist > bestDist || dist < 0.0f) { continue; } - bestIdx = i; - bestDist = dist; - bestTexcoord = coords; - } - - HitInfo* hit = new HitInfo{}; - // If no hit, we still need to inform the HHM - if (bestIdx < 0) - { - startRays.Field.pop_back(); - continue; - } - - hit->Distance = bestDist; - hit->UV = bestTexcoord; - - glm::ivec2 pos = (*mCurrentRefTable)[ray->Reference]; - spdlog::debug("HIT!!"); - mTarget[pos.y * mRenderTargetSize.x + pos.x] = { bestDist, bestDist, bestDist, 1.0f }; - startRays.Field.pop_back(); - // this->computeHit(hit); - } + mHaultWait(); spdlog::info("Sample complete"); @@ -179,6 +140,11 @@ void RayRenderer::draw() void RayRenderer::computeHit(HitInfo* info) { spdlog::debug("HIT!!"); + if (!(*mCurrentRefTable).count(info->Caller->Reference)) + { + spdlog::warn("Why is the ray not in the map?!"); + return; + } glm::ivec2 pos = (*mCurrentRefTable)[info->Caller->Reference]; std::lock_guard lock(this->_mTarget); float d = info->Distance; diff --git a/src/renderer/renderer.hpp b/src/renderer/renderer.hpp index 4f35017..4f3ddef 100644 --- a/src/renderer/renderer.hpp +++ b/src/renderer/renderer.hpp @@ -34,6 +34,8 @@ public: void computeHit(HitInfo* info); private: + void mHaultWait(); + std::unordered_map* mCurrentRefTable; private: diff --git a/src/scene/mesh.cpp b/src/scene/mesh.cpp index c0f39ff..1fc1172 100644 --- a/src/scene/mesh.cpp +++ b/src/scene/mesh.cpp @@ -74,17 +74,17 @@ void Mesh::ready() spdlog::debug("Mesh for preview ready..."); } -int Mesh::getVerticies(void*& v, void*& n) +int Mesh::getVerticies(void** v, void** n) { - v = (void*)&mObjLoader->getPositions()[0]; - n = (void*)&mObjLoader->getNormals()[0]; + *v = (void*)&mObjLoader->getPositions()[0]; + *n = (void*)&mObjLoader->getNormals()[0]; // spdlog::debug("Mesh get {} {}", v, n); return mObjLoader->getVertCount(); } -int Mesh::getIndicies(void*& i) +int Mesh::getIndicies(void** i) { - i = (void*)&mObjLoader->getFaces()[0]; + *i = (void*)&mObjLoader->getFaces()[0]; return mObjLoader->getIndexCount(); } diff --git a/src/scene/mesh.hpp b/src/scene/mesh.hpp index 12d4336..05fc01c 100644 --- a/src/scene/mesh.hpp +++ b/src/scene/mesh.hpp @@ -26,8 +26,8 @@ public: void loadOBJ(std::filesystem::path file); void ready(); - int getVerticies(void*& v, void*& n); - int getIndicies(void*& i); + int getVerticies(void** v, void** n); + int getIndicies(void** i); int getIndexCount();