From 6244508ce0b38d99a6640915f7e4b90e0aaa217c Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 4 Dec 2021 19:51:04 +0100 Subject: [PATCH] #5584: Make the client code slimmer, the size changes are detected by the base class now --- libs/render/RenderableGeometry.h | 23 +++++++++++++++---- .../entity/target/RenderableTargetLines.h | 22 ++---------------- radiantcore/patch/PatchRenderables.h | 19 ++------------- 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/libs/render/RenderableGeometry.h b/libs/render/RenderableGeometry.h index 259cccd8a6..8c34564ee8 100644 --- a/libs/render/RenderableGeometry.h +++ b/libs/render/RenderableGeometry.h @@ -21,9 +21,14 @@ class RenderableGeometry : ShaderPtr _shader; IGeometryRenderer::Slot _surfaceSlot; + std::size_t _lastVertexSize; // To detect size changes when updating geometry + std::size_t _lastIndexSize; // To detect size changes when updating geometry + protected: RenderableGeometry() : - _surfaceSlot(IGeometryRenderer::InvalidSlot) + _surfaceSlot(IGeometryRenderer::InvalidSlot), + _lastVertexSize(0), + _lastIndexSize(0) {} public: @@ -54,14 +59,15 @@ class RenderableGeometry : } // Removes the geometry and clears the shader reference - virtual void clear() + void clear() { removeGeometry(); _shader.reset(); } - virtual void render(const RenderInfo& info) const override + // Renders the geometry stored in our single slot + void render(const RenderInfo& info) const override { if (_surfaceSlot != IGeometryRenderer::InvalidSlot && _shader) { @@ -89,10 +95,19 @@ class RenderableGeometry : // Submits the given geometry to the known _shader reference // This method is supposed to be called from within updateGeometry() // to ensure that the _shader reference is already up to date. - virtual void addOrUpdateGeometry(GeometryType type, + void updateGeometry(GeometryType type, const std::vector& vertices, const std::vector& indices) { + // Size changes require removal of the geometry before update + if (_lastVertexSize != vertices.size() || _lastIndexSize != indices.size()) + { + removeGeometry(); + + _lastVertexSize = vertices.size(); + _lastIndexSize = indices.size(); + } + if (_surfaceSlot == IGeometryRenderer::InvalidSlot) { _surfaceSlot = _shader->addGeometry(type, vertices, indices); diff --git a/radiantcore/entity/target/RenderableTargetLines.h b/radiantcore/entity/target/RenderableTargetLines.h index f59d2fa925..a956e90b66 100644 --- a/radiantcore/entity/target/RenderableTargetLines.h +++ b/radiantcore/entity/target/RenderableTargetLines.h @@ -31,12 +31,10 @@ class RenderableTargetLines : const TargetKeyCollection& _targetKeys; Vector3 _worldPosition; - std::size_t _numVisibleLines; public: RenderableTargetLines(const TargetKeyCollection& targetKeys) : - _targetKeys(targetKeys), - _numVisibleLines(0) + _targetKeys(targetKeys) {} bool hasTargets() const @@ -44,13 +42,6 @@ class RenderableTargetLines : return !_targetKeys.empty(); } - void clear() override - { - RenderableGeometry::clear(); - - _numVisibleLines = 0; - } - void update(const ShaderPtr& shader, const Vector3& worldPosition) { // Store the new world position for use in updateGeometry() @@ -71,7 +62,6 @@ class RenderableTargetLines : std::vector vertices; std::vector indices; auto maxTargets = _targetKeys.getNumTargets(); - auto numVisibleLines = 0; vertices.reserve(6 * maxTargets); indices.reserve(6 * maxTargets); @@ -83,20 +73,12 @@ class RenderableTargetLines : return; } - numVisibleLines++; auto targetPosition = target->getPosition(); addTargetLine(_worldPosition, targetPosition, vertices, indices); }); - // Size changes requires detaching our geometry first - if (numVisibleLines != _numVisibleLines) - { - removeGeometry(); - _numVisibleLines = numVisibleLines; - } - - addOrUpdateGeometry(render::GeometryType::Lines, vertices, indices); + RenderableGeometry::updateGeometry(render::GeometryType::Lines, vertices, indices); } private: diff --git a/radiantcore/patch/PatchRenderables.h b/radiantcore/patch/PatchRenderables.h index 1c0bbad83e..c827a78823 100644 --- a/radiantcore/patch/PatchRenderables.h +++ b/radiantcore/patch/PatchRenderables.h @@ -167,21 +167,13 @@ class RenderablePatchTesselation : const PatchTesselation& _tess; bool _needsUpdate; - std::size_t _size; public: RenderablePatchTesselation(const PatchTesselation& tess) : _tess(tess), - _needsUpdate(true), - _size(0) + _needsUpdate(true) {} - void clear() override - { - RenderableGeometry::clear(); - _size = 0; - } - void queueUpdate() { _needsUpdate = true; @@ -194,19 +186,12 @@ class RenderablePatchTesselation : _needsUpdate = false; - // Tesselation size change requires to remove the geometry first - if (_tess.vertices.size() != _size) - { - removeGeometry(); - _size = _tess.vertices.size(); - } - // Generate the new index array std::vector indices; indices.reserve(_indexer.getNumIndices(_tess)); _indexer.generateIndices(_tess, std::back_inserter(indices)); - addOrUpdateGeometry(_indexer.getType(), _tess.vertices, indices); + RenderableGeometry::updateGeometry(_indexer.getType(), _tess.vertices, indices); } };