Use shared_ptr for dynamic draw call

This commit is contained in:
Benau 2017-12-31 12:15:55 +08:00
parent c8aea0bf9e
commit 2bde6d1325
9 changed files with 44 additions and 36 deletions

View File

@ -30,8 +30,8 @@
Shadow::Shadow(Material* shadow_mat, const AbstractKart& kart)
: m_dy_dc(NULL), m_shadow_enabled(false), m_kart(kart)
{
m_dy_dc = new SP::SPDynamicDrawCall(scene::EPT_TRIANGLE_STRIP,
SP::getSPShader("alphablend"), shadow_mat);
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("alphablend"), shadow_mat);
m_dy_dc->getVerticesVector().resize(4);
video::S3DVertexSkinnedMesh* v = m_dy_dc->getVerticesVector().data();
@ -43,16 +43,15 @@ Shadow::Shadow(Material* shadow_mat, const AbstractKart& kart)
v[3].m_all_uvs[1] = 15360;
v[2].m_all_uvs[0] = 0;
v[2].m_all_uvs[1] = 15360;
m_dy_dc->setVisible(false);
SP::addDynamicDrawCall(m_dy_dc);
} // Shadow
// ----------------------------------------------------------------------------
Shadow::~Shadow()
{
if (m_shadow_enabled)
{
SP::removeDynamicDrawCall(m_dy_dc);
}
delete m_dy_dc;
m_dy_dc->removeFromSP();
} // ~Shadow
// ----------------------------------------------------------------------------
@ -68,11 +67,11 @@ void Shadow::update(bool enabled)
m_shadow_enabled = enabled;
if (m_shadow_enabled)
{
SP::addDynamicDrawCall(m_dy_dc);
m_dy_dc->setVisible(true);
}
else
{
SP::removeDynamicDrawCall(m_dy_dc);
m_dy_dc->setVisible(false);
}
}
if (m_shadow_enabled)

View File

@ -20,6 +20,7 @@
#define HEADER_SHADOW_HPP
#include "utils/no_copy.hpp"
#include <memory>
class AbstractKart;
class Material;
@ -39,7 +40,7 @@ class Shadow : public NoCopy
{
private:
/** The dynamic draw call of the shadow. */
SP::SPDynamicDrawCall* m_dy_dc;
std::shared_ptr<SP::SPDynamicDrawCall> m_dy_dc;
/** If a kart is flying, the shadow is disabled (since it is
* stuck to the kart, i.e. the shadow would be flying, too). */

View File

@ -208,9 +208,9 @@ SkidMarks::SkidMarkQuads::SkidMarkQuads(const Vec3 &left,
m_center_start = (left + right)/2;
m_z_offset = z_offset;
m_fade_out = 0.0f;
m_dy_dc = new SP::SPDynamicDrawCall(scene::EPT_TRIANGLE_STRIP,
shader, material);
static_cast<SP::SPPerObjectUniform*>(m_dy_dc)->addAssignerFunction
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, shader, material);
static_cast<SP::SPPerObjectUniform*>(m_dy_dc.get())->addAssignerFunction
("custom_alpha", [this](SP::SPUniformAssigner* ua)->void
{
// SP custom_alpha is assigned 1 - x, so this is correct
@ -235,8 +235,7 @@ SkidMarks::SkidMarkQuads::SkidMarkQuads(const Vec3 &left,
//-----------------------------------------------------------------------------
SkidMarks::SkidMarkQuads::~SkidMarkQuads()
{
SP::removeDynamicDrawCall(m_dy_dc);
delete m_dy_dc;
m_dy_dc->removeFromSP();
} // ~SkidMarkQuads
//-----------------------------------------------------------------------------

View File

@ -83,7 +83,7 @@ private:
/** Vector marking the start of the skidmarks (located between left and right wheel) */
Vec3 m_center_start;
SP::SPDynamicDrawCall* m_dy_dc;
std::shared_ptr<SP::SPDynamicDrawCall> m_dy_dc;
public:
SkidMarkQuads (const Vec3 &left, const Vec3 &right,

View File

@ -49,7 +49,6 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <numeric>
#include <string>
#include <unordered_map>
#include <unordered_set>
@ -94,7 +93,7 @@ std::vector<float> g_bounding_boxes;
// ----------------------------------------------------------------------------
core::vector3df g_wind_dir;
// ----------------------------------------------------------------------------
std::unordered_set<SPDynamicDrawCall*> g_dy_dc;
std::vector<std::shared_ptr<SPDynamicDrawCall> > g_dy_dc;
// ----------------------------------------------------------------------------
float g_frustums[5][24] = { { } };
// ----------------------------------------------------------------------------
@ -1373,9 +1372,11 @@ void handleDynamicDrawCall()
{
return;
}
for (SPDynamicDrawCall* dydc : g_dy_dc)
for (unsigned dc_num = 0; dc_num < g_dy_dc.size(); dc_num++)
{
if (!dydc->isVisible() || dydc->getVertexCount() < 3)
SPDynamicDrawCall* dydc = g_dy_dc[dc_num].get();
if (!dydc->isVisible() || dydc->notReadyFromDrawing() ||
dydc->isRemoving())
{
continue;
}
@ -1640,6 +1641,12 @@ void uploadAll()
g_stk_sbr->getShadowMatrices()->getMatricesData());
glBindBuffer(GL_UNIFORM_BUFFER, 0);
g_dy_dc.erase(std::remove_if(g_dy_dc.begin(), g_dy_dc.end(),
[] (std::shared_ptr<SPDynamicDrawCall> dc)
{
return dc->isRemoving();
}), g_dy_dc.end());
if (!sp_culling)
{
return;
@ -1784,15 +1791,9 @@ void drawBoundingBoxes()
} // drawBoundingBoxes
// ----------------------------------------------------------------------------
void addDynamicDrawCall(SPDynamicDrawCall* dy_dc)
void addDynamicDrawCall(std::shared_ptr<SPDynamicDrawCall> dy_dc)
{
g_dy_dc.insert(dy_dc);
g_dy_dc.push_back(dy_dc);
} // addDynamicDrawCall
// ----------------------------------------------------------------------------
void removeDynamicDrawCall(SPDynamicDrawCall* dy_dc)
{
g_dy_dc.erase(dy_dc);
} // removeDynamicDrawCall
}

View File

@ -125,9 +125,7 @@ void prepareScene();
// ----------------------------------------------------------------------------
void handleDynamicDrawCall();
// ----------------------------------------------------------------------------
void addDynamicDrawCall(SPDynamicDrawCall*);
// ----------------------------------------------------------------------------
void removeDynamicDrawCall(SPDynamicDrawCall*);
void addDynamicDrawCall(std::shared_ptr<SPDynamicDrawCall>);
// ----------------------------------------------------------------------------
void updateModelMatrix();
// ----------------------------------------------------------------------------

View File

@ -59,6 +59,8 @@ private:
bool m_update_trans = false;
bool m_removing = false;
// ------------------------------------------------------------------------
bool initTextureDyDc();
@ -136,6 +138,12 @@ public:
// ------------------------------------------------------------------------
const core::matrix4& getAbsoluteTransformation() const { return m_trans; }
// ------------------------------------------------------------------------
void removeFromSP() { m_removing = true; }
// ------------------------------------------------------------------------
bool isRemoving() const { return m_removing; }
// ------------------------------------------------------------------------
bool notReadyFromDrawing() const { return m_vertices.size() < 3; }
// ------------------------------------------------------------------------
void setAbsoluteTransformation(core::matrix4& mat)
{
m_trans = mat;

View File

@ -54,8 +54,9 @@ RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
color.setGreen(SP::srgbToLinear(tmp.g));
color.setBlue(SP::srgbToLinear(tmp.b));
}
m_dy_dc = new SP::SPDynamicDrawCall(scene::EPT_TRIANGLE_STRIP,
SP::getSPShader("unlit"), material_manager->getSPMaterial("unlit"));
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("unlit"),
material_manager->getSPMaterial("unlit"));
m_dy_dc->getVerticesVector().resize(4);
// Set the vertex colors properly, as the new pipeline doesn't use the old
// light values
@ -72,8 +73,7 @@ RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
RubberBand::~RubberBand()
{
#ifndef SERVER_ONLY
SP::removeDynamicDrawCall(m_dy_dc);
delete m_dy_dc;
m_dy_dc->removeFromSP();
#endif
} // RubberBand

View File

@ -22,6 +22,8 @@
#include "utils/no_copy.hpp"
#include "utils/vec3.hpp"
#include <memory>
namespace SP
{
class SPDynamicDrawCall;
@ -50,7 +52,7 @@ private:
AbstractKart *m_owner;
/** The dynamic draw call of the rubber band. */
SP::SPDynamicDrawCall *m_dy_dc;
std::shared_ptr<SP::SPDynamicDrawCall> m_dy_dc;
/** The kart a plunger might have hit. */
AbstractKart *m_hit_kart;