From e3c8e5a7be3265b16987e74bcbfa07dea40f16a0 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 25 Jul 2016 16:43:55 -1000 Subject: [PATCH] Buckets::Sort implementation --- Runtime/Graphics/CBooRenderer.cpp | 65 ++++++++++++++++++- Runtime/Graphics/CDrawable.hpp | 6 +- Runtime/Graphics/CDrawablePlaneObject.hpp | 5 +- .../Shaders/CThermalColdFilterGLSL.cpp | 7 +- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Runtime/Graphics/CBooRenderer.cpp b/Runtime/Graphics/CBooRenderer.cpp index 40f95411a..5c1708cd8 100644 --- a/Runtime/Graphics/CBooRenderer.cpp +++ b/Runtime/Graphics/CBooRenderer.cpp @@ -30,6 +30,7 @@ float Buckets::sMinMaxDistance[2]; void Buckets::Clear() { sData->clear(); + sBucketIndex.clear(); sPlaneObjectData->clear(); sPlaneObjectBucket->clear(); for (rstl::reserved_vector& bucket : *sBuckets) @@ -40,6 +41,65 @@ void Buckets::Clear() void Buckets::Sort() { + float delta = std::max(1.f, sMinMaxDistance[1] - sMinMaxDistance[0]); + sPlaneObjectBucket->resize(8); + + std::sort(sPlaneObjectBucket->begin(), sPlaneObjectBucket->end(), + [](u16 a, u16 b) -> bool + { + return (*sPlaneObjectData)[a].GetDistance() >= (*sPlaneObjectData)[b].GetDistance(); + }); + + u32 precision = 50 / (8 + 1); + float pitch = 1.f / (delta / float(precision - 2)); + + int accum = 0; + for (u16 idx : *sPlaneObjectBucket) + { + ++accum; + CDrawablePlaneObject& planeObj = (*sPlaneObjectData)[idx]; + planeObj.x24_targetBucket = precision * accum; + } + + for (CDrawable& drawable : *sData) + { + int slot; + if (sPlaneObjectBucket->empty()) + { + slot = zeus::clamp(1, int((drawable.GetDistance() - sMinMaxDistance[0]) * pitch), 49); + } + else + { + /* TODO: Planar sort distribution */ + } + + if (slot == -1) + slot = 49; + (*sBuckets)[slot].push_back(&drawable); + } + + int bucketIdx = sBuckets->size(); + for (auto it = sBuckets->rbegin() ; it != sBuckets->rend() ; ++it) + { + --bucketIdx; + sBucketIndex.push_back(bucketIdx); + rstl::reserved_vector& bucket = *it; + if (bucket.size()) + { + std::sort(bucket.begin(), bucket.end(), + [](CDrawable* a, CDrawable* b) -> bool + { + return a->GetDistance() >= b->GetDistance(); + }); + } + } + + for (auto it = sPlaneObjectBucket->rbegin() ; it != sPlaneObjectBucket->rend() ; ++it) + { + CDrawablePlaneObject& planeObj = (*sPlaneObjectData)[*it]; + rstl::reserved_vector& bucket = (*sBuckets)[planeObj.x24_targetBucket]; + bucket.push_back(&planeObj); + } } void Buckets::InsertPlaneObject(float dist, float something, const zeus::CAABox& aabb, bool b1, @@ -92,15 +152,14 @@ void CBooRenderer::RenderBucketItems(const std::vector& lights) static_cast((void*)drawable->GetData())->Render(); break; } - case EDrawableType::World: + case EDrawableType::Surface: { CBooSurface* surf = static_cast((void*)drawable->GetData()); CBooModel* model = surf->m_parent; if (model) { model->ActivateLights(lights); - CModelFlags flags; - model->DrawSurface(*surf, flags); + model->DrawSurface(*surf, CModelFlags{}); } break; } diff --git a/Runtime/Graphics/CDrawable.hpp b/Runtime/Graphics/CDrawable.hpp index 46d9d1cb0..7954a12e8 100644 --- a/Runtime/Graphics/CDrawable.hpp +++ b/Runtime/Graphics/CDrawable.hpp @@ -8,10 +8,10 @@ namespace urde { enum class EDrawableType : u16 { - World, + Surface, Particle, - UnsortedDrawable, - SortedDrawable + UnsortedCallback, + SortedCallback }; class CDrawable diff --git a/Runtime/Graphics/CDrawablePlaneObject.hpp b/Runtime/Graphics/CDrawablePlaneObject.hpp index d90a2da20..8bb583e51 100644 --- a/Runtime/Graphics/CDrawablePlaneObject.hpp +++ b/Runtime/Graphics/CDrawablePlaneObject.hpp @@ -8,7 +8,8 @@ namespace urde { class CDrawablePlaneObject : public CDrawable { - u16 x24_extraSort2; + friend class Buckets; + u16 x24_targetBucket; float x28_something; zeus::CPlane x2c_plane; bool x3c_24 : 1; @@ -16,7 +17,7 @@ class CDrawablePlaneObject : public CDrawable public: CDrawablePlaneObject(EDrawableType dtype, float dist, float something, const zeus::CAABox& aabb, bool b1, const zeus::CPlane& plane, bool b2, const void* data) - : CDrawable(dtype, 0, dist, aabb, data), x24_extraSort2(0), x28_something(something), + : CDrawable(dtype, 0, dist, aabb, data), x24_targetBucket(0), x28_something(something), x2c_plane(plane) {x3c_24 = b1; x3c_25 = b2;} }; } diff --git a/Runtime/Graphics/Shaders/CThermalColdFilterGLSL.cpp b/Runtime/Graphics/Shaders/CThermalColdFilterGLSL.cpp index e70dba664..1dfa3dff1 100644 --- a/Runtime/Graphics/Shaders/CThermalColdFilterGLSL.cpp +++ b/Runtime/Graphics/Shaders/CThermalColdFilterGLSL.cpp @@ -70,12 +70,7 @@ BOO_GLSL_BINDING_HEAD " float shiftScene1 = dot(texture(sceneTex, shiftCoord + vec2(vtf.shiftScale.x / 8.0, 0.0)), kRGBToYPrime);\n" " vec2 indCoord = (vtf.indMtx * vec3(shiftScene0 - 0.5, shiftScene1 - 0.5, 1.0)).xy;\n" " float indScene = dot(texture(sceneTex, vtf.sceneUv + indCoord), kRGBToYPrime);\n" -" colorOut = vtf.colorReg0 * indScene * vec4(0.00001) + vtf.colorReg1 * shiftScene0 * vec4(0.00001) + vtf.colorReg2 * vec4(0.00001);\n" -" float blue = 0.0;\n" -" float green = 0.0;\n" -" if (indCoord.y < 0.0) blue = -indCoord.y * 10.0;\n" -" else green = indCoord.y * 10.0;\n" -" colorOut += vec4(0.0, green, blue, 1.0);\n" +" colorOut = vtf.colorReg0 * indScene + vtf.colorReg1 * shiftScene0 + vtf.colorReg2;\n" "}\n"; URDE_DECL_SPECIALIZE_SHADER(CThermalColdFilter)