Use shared_ptr SPShader in mesh buffer

This commit is contained in:
Benau 2018-01-21 15:35:38 +08:00
parent 390554eca1
commit a39977d550
17 changed files with 64 additions and 89 deletions

View File

@ -32,8 +32,7 @@ Shadow::Shadow(Material* shadow_mat, const AbstractKart& kart)
{
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP,
SP::SPShaderManager::get()->getSPShaderPtr("alphablend"),
shadow_mat);
SP::SPShaderManager::get()->getSPShader("alphablend"), shadow_mat);
m_dy_dc->getVerticesVector().resize(4);
video::S3DVertexSkinnedMesh* v = m_dy_dc->getVerticesVector().data();

View File

@ -20,8 +20,8 @@
#include "graphics/show_curve.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "utils/mini_glm.hpp"
#include "utils/vec3.hpp"
@ -33,7 +33,8 @@ ShowCurve::ShowCurve(float width, float height,
m_first_vertices(true), m_visible(true)
{
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(irr::scene::EPT_TRIANGLES, SP::getSPShader("alphablend"),
(irr::scene::EPT_TRIANGLES,
SP::SPShaderManager::get()->getSPShader("alphablend"),
material_manager->getDefaultSPMaterial("alphablend"));
SP::addDynamicDrawCall(m_dy_dc);
} // ShowCurve

View File

@ -44,8 +44,8 @@ SkidMarks::SkidMarks(const AbstractKart& kart, float width) : m_kart(kart)
m_width = width;
m_material = material_manager->getMaterialSPM("skidmarks.png", "",
"alphablend");
m_shader = SP::SPShaderManager::get()->getSPShaderPtr("alphablend");
assert(m_shader != NULL);
m_shader = SP::SPShaderManager::get()->getSPShader("alphablend");
assert(m_shader);
m_skid_marking = false;
} // SkidMark
@ -200,7 +200,7 @@ SkidMarks::SkidMarkQuads::SkidMarkQuads(const Vec3 &left,
const Vec3 &right,
const Vec3 &normal,
Material* material,
SP::SPShader* shader,
std::shared_ptr<SP::SPShader> shader,
float z_offset,
video::SColor* custom_color)
{

View File

@ -65,7 +65,7 @@ private:
Material* m_material;
/** Shader(alphablend) to use for the skid marks. */
SP::SPShader* m_shader;
std::shared_ptr<SP::SPShader> m_shader;
// ------------------------------------------------------------------------
class SkidMarkQuads : public NoCopy
@ -88,7 +88,7 @@ private:
public:
SkidMarkQuads (const Vec3 &left, const Vec3 &right,
const Vec3 &normal, Material* material,
SP::SPShader* shader,
std::shared_ptr<SP::SPShader> shader,
float z_offset, video::SColor* custom_color = NULL);
~SkidMarkQuads();
void add (const Vec3 &left,

View File

@ -21,10 +21,10 @@
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_mesh.hpp"
#include "graphics/sp/sp_mesh_node.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "graphics/sp/sp_uniform_assigner.hpp"
#include "io/file_manager.hpp"
#include "karts/abstract_kart.hpp"
@ -80,7 +80,8 @@ SlipStream::SlipStream(AbstractKart* kart) : MovingTexture(0, 0), m_kart(kart)
if (UserConfigParams::m_slipstream_debug)
{
m_debug_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("additive"),
(scene::EPT_TRIANGLE_STRIP,
SP::SPShaderManager::get()->getSPShader("additive"),
material_manager->getDefaultSPMaterial("additive"));
m_debug_dc->getVerticesVector().resize(4);
video::S3DVertexSkinnedMesh* v =

View File

@ -69,8 +69,6 @@ bool sp_culling = true;
// ----------------------------------------------------------------------------
bool g_handle_shadow = false;
// ----------------------------------------------------------------------------
std::unordered_map<std::string, SPShader*> g_shaders;
// ----------------------------------------------------------------------------
SPShader* g_normal_visualizer = NULL;
// ----------------------------------------------------------------------------
SPShader* g_glow_shader = NULL;
@ -324,13 +322,6 @@ void initSkinning()
sp_prefilled_tex[0] = g_skinning_tex;
} // initSkinning
// ----------------------------------------------------------------------------
void addShader(SPShader* shader)
{
g_shaders[shader->getName()] = shader;
} // addShader
// ----------------------------------------------------------------------------
void loadShaders()
{
@ -583,15 +574,9 @@ void init()
void destroy()
{
g_dy_dc.clear();
for (auto& p : g_shaders)
{
delete p.second;
}
g_shaders.clear();
SPShaderManager::destroy();
g_glow_shader = NULL;
g_normal_visualizer = NULL;
SPTextureManager::destroy();
#ifndef USE_GLES2
@ -636,17 +621,6 @@ SPShader* getNormalVisualizer()
return g_normal_visualizer;
} // getNormalVisualizer
// ----------------------------------------------------------------------------
SPShader* getSPShader(const std::string& name)
{
auto ret = g_shaders.find(name);
if (ret != g_shaders.end())
{
return ret->second;
}
return NULL;
} // getSPShader
// ----------------------------------------------------------------------------
inline void mathPlaneNormf(float *p)
{

View File

@ -99,8 +99,6 @@ extern irr::core::vector3df sp_wind_dir;
// ----------------------------------------------------------------------------
void init();
// ----------------------------------------------------------------------------
void addShader(SPShader*);
// ----------------------------------------------------------------------------
void destroy();
// ----------------------------------------------------------------------------
GLuint getSampler(SamplerType);
@ -109,8 +107,6 @@ SPShader* getNormalVisualizer();
// ----------------------------------------------------------------------------
SPShader* getGlowShader();
// ----------------------------------------------------------------------------
SPShader* getSPShader(const std::string& name);
// ----------------------------------------------------------------------------
void prepareDrawCalls();
// ----------------------------------------------------------------------------
void draw(RenderPass, DrawCallType dct = DCT_NORMAL);

View File

@ -16,10 +16,10 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_texture.hpp"
#include "graphics/central_settings.hpp"
#include "graphics/graphics_restrictions.hpp"
#include "graphics/material.hpp"
#include "graphics/sp/sp_shader.hpp"
#include "graphics/sp/sp_texture.hpp"
#include "graphics/sp/sp_texture_manager.hpp"
#include "utils/mini_glm.hpp"
#include "utils/string_utils.hpp"
@ -28,12 +28,13 @@ namespace SP
{
// ----------------------------------------------------------------------------
SPDynamicDrawCall::SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt,
SPShader* shader, Material* m)
std::shared_ptr<SPShader> shader,
Material* m)
: SPMeshBuffer()
{
#ifndef SERVER_ONLY
m_primitive_type = pt;
m_shader = shader;
m_shaders[0] = shader;
m_stk_material[0] = std::make_tuple(0u, 0u, m);
m_textures.resize(m_stk_material.size());
for (unsigned j = 0; j < 6; j++)
@ -41,7 +42,7 @@ SPDynamicDrawCall::SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt,
m_textures[0][j] = SPTextureManager::get()->getTexture
(std::get<2>(m_stk_material[0])->getSamplerPath(j),
j == 0 ? std::get<2>(m_stk_material[0]) : NULL,
j < 2 && CVS->isDefferedEnabled(),
m_shaders[0] && m_shaders[0]->isSrgbForTextureLayer(j),
std::get<2>(m_stk_material[0])->getContainerId());
}
m_tex_cmp[m_textures[0][0]->getPath() + m_textures[0][1]->getPath()] = 0;

View File

@ -43,8 +43,6 @@ class SPDynamicDrawCall : public SPMeshBuffer
private:
core::matrix4 m_trans;
SPShader* m_shader;
scene::ISceneNode* m_parent = NULL;
core::vector2df m_texture_trans;
@ -65,8 +63,8 @@ private:
bool initTextureDyDc();
public:
SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt, SPShader* shader,
Material* m);
SPDynamicDrawCall(scene::E_PRIMITIVE_TYPE pt,
std::shared_ptr<SPShader> shader, Material* m);
// ------------------------------------------------------------------------
~SPDynamicDrawCall() {}
// ------------------------------------------------------------------------
@ -130,8 +128,6 @@ public:
// ------------------------------------------------------------------------
virtual void enableTextureMatrix(unsigned mat_id) {}
// ------------------------------------------------------------------------
SPShader* getShader() const { return m_shader; }
// ------------------------------------------------------------------------
std::vector<video::S3DVertexSkinnedMesh>& getVerticesVector()
{ return m_vertices; }
// ------------------------------------------------------------------------

View File

@ -20,6 +20,8 @@
#include "graphics/central_settings.hpp"
#include "graphics/graphics_restrictions.hpp"
#include "graphics/material.hpp"
#include "graphics/sp/sp_shader.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "graphics/sp/sp_texture_manager.hpp"
#include "race/race_manager.hpp"
#include "utils/mini_glm.hpp"
@ -111,17 +113,22 @@ void SPMeshBuffer::uploadGLMesh()
}
m_uploaded_gl = true;
#ifndef SERVER_ONLY
if (!m_shaders[0])
{
Log::warn("SPMeshBuffer", "%s shader is missing",
std::get<2>(m_stk_material[0])->getShaderName().c_str());
return;
}
m_textures.resize(m_stk_material.size());
for (unsigned i = 0; i < m_stk_material.size(); i++)
{
for (unsigned j = 0; j < 6; j++)
{
// Undo the effect of srgb on 0 and 1 channel of textures
// which is the uv textures from .spm when advanced lighting
m_textures[i][j] = SPTextureManager::get()->getTexture
(std::get<2>(m_stk_material[i])->getSamplerPath(j),
j == 0 ? std::get<2>(m_stk_material[i]) : NULL,
j < 2 && CVS->isDefferedEnabled(),
m_shaders[0]->isSrgbForTextureLayer(j),
std::get<2>(m_stk_material[i])->getContainerId());
}
// Use .spm uv texture 1 and 2 for compare in scene manager
@ -130,9 +137,7 @@ void SPMeshBuffer::uploadGLMesh()
}
bool use_2_uv = std::get<2>(m_stk_material[0])->use2UV();
bool use_tangents =
std::get<2>(m_stk_material[0])->getShaderName() == "normalmap" &&
CVS->isDefferedEnabled();
bool use_tangents = m_shaders[0]->useTangents();
const unsigned pitch = 48 - (use_tangents ? 0 : 4) - (use_2_uv ? 0 : 4) -
(m_skinned ? 0 : 16);
m_pitch = pitch;
@ -212,10 +217,12 @@ void SPMeshBuffer::uploadGLMesh()
void SPMeshBuffer::recreateVAO(unsigned i)
{
#ifndef SERVER_ONLY
if (!m_shaders[0])
{
return;
}
bool use_2_uv = std::get<2>(m_stk_material[0])->use2UV();
bool use_tangents =
std::get<2>(m_stk_material[0])->getShaderName() == "normalmap" &&
CVS->isDefferedEnabled();
bool use_tangents = m_shaders[0]->useTangents();
const unsigned pitch = m_pitch;
size_t offset = 0;
@ -458,4 +465,14 @@ void SPMeshBuffer::reloadTextureCompare()
}
} // reloadTextureCompare
// ----------------------------------------------------------------------------
void SPMeshBuffer::setSTKMaterial(Material* m)
{
m_stk_material[0] = std::make_tuple(0u, getIndexCount(), m);
m_shaders[0] = SPShaderManager::get()->getSPShader(
std::get<2>(m_stk_material[0])->getShaderName());
m_shaders[1] = SPShaderManager::get()->getSPShader(
std::get<2>(m_stk_material[0])->getShaderName() + "_skinned");
} // setSTKMaterial
}

View File

@ -47,7 +47,7 @@ class SPTexture;
class SPMeshBuffer : public IMeshBuffer, public SPPerObjectUniform
{
protected:
std::shared_ptr<SPShader> m_shaders[2];
std::vector<std::tuple<size_t/*first_index_id*/,
unsigned/*indices_count*/, Material*> > m_stk_material;
@ -273,10 +273,7 @@ public:
m_indices = std::move(indices);
}
// ------------------------------------------------------------------------
void setSTKMaterial(Material* m)
{
m_stk_material[0] = std::make_tuple(0u, getIndexCount(), m);
}
void setSTKMaterial(Material* m);
// ------------------------------------------------------------------------
void reloadTextureCompare();
// ------------------------------------------------------------------------
@ -286,6 +283,9 @@ public:
m_indices.shrink_to_fit();
}
// ------------------------------------------------------------------------
SPShader* getShader(bool skinned = false) const
{ return skinned ? m_shaders[1].get() : m_shaders[0].get(); }
// ------------------------------------------------------------------------
virtual const video::SMaterial& getMaterial() const
{
static video::SMaterial unused;

View File

@ -192,11 +192,8 @@ SPShader* SPMeshNode::getShader(unsigned mesh_buffer_id) const
#ifndef SERVER_ONLY
if (!m_mesh || mesh_buffer_id < m_mesh->getMeshBufferCount())
{
const std::string sn = (m_shader_override.empty() ?
m_mesh->getSPMeshBuffer(mesh_buffer_id)->getSTKMaterial()
->getShaderName() : m_shader_override) +
(m_animated ? "_skinned" : "");
SPShader* shader = SPShaderManager::get()->getSPShaderPtr(sn);
SPShader* shader =
m_mesh->getSPMeshBuffer(mesh_buffer_id)->getShader(m_animated);
if (shader && shader->isTransparent())
{
// Use real transparent shader first
@ -204,8 +201,8 @@ SPShader* SPMeshNode::getShader(unsigned mesh_buffer_id) const
}
if (m_first_render_info && m_first_render_info->isTransparent())
{
return SPShaderManager::get()->getSPShaderPtr
(std::string("ghost") + (m_animated ? "_skinned" : ""));
return SPShaderManager::get()->getSPShader
(std::string("ghost") + (m_animated ? "_skinned" : "")).get();
}
return shader;
}

View File

@ -52,8 +52,6 @@ private:
std::vector<std::array<float, 16> > m_skinning_matrices;
std::string m_shader_override;
video::SColorf m_glow_color;
std::vector<std::array<float, 2> > m_texture_matrices;
@ -119,8 +117,6 @@ public:
// ------------------------------------------------------------------------
SPShader* getShader(unsigned mesh_buffer_id) const;
// ------------------------------------------------------------------------
void setShaderOverride(const std::string& so) { m_shader_override = so; }
// ------------------------------------------------------------------------
const std::array<float, 16>* getSkinningMatrices() const
{ return m_skinning_matrices.data(); }
// ------------------------------------------------------------------------

View File

@ -139,11 +139,6 @@ public:
// ------------------------------------------------------------------------
~SPShaderManager();
// ------------------------------------------------------------------------
SPShader* getSPShaderPtr(const std::string& name)
{
return getSPShader(name).get();
}
// ------------------------------------------------------------------------
std::shared_ptr<SPShader> getSPShader(const std::string& name)
{
auto ret = m_shaders.find(name);

View File

@ -21,8 +21,8 @@
#include "graphics/central_settings.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "items/plunger.hpp"
#include "items/projectile_manager.hpp"
#include "karts/abstract_kart.hpp"
@ -59,8 +59,8 @@ RubberBand::RubberBand(Plunger *plunger, AbstractKart *kart)
color.setBlue(SP::srgb255ToLinear(color.getBlue()));
}
m_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("unlit"),
material_manager->getDefaultSPMaterial("unlit"));
(scene::EPT_TRIANGLE_STRIP, SP::SPShaderManager::get()->getSPShader
("unlit"), material_manager->getDefaultSPMaterial("unlit"));
m_dy_dc->getVerticesVector().resize(4);
// Set the vertex colors properly, as the new pipeline doesn't use the old
// light values

View File

@ -23,8 +23,8 @@
#include "config/user_config.hpp"
#include "graphics/show_curve.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "io/xml_node.hpp"
#include "items/flyable.hpp"
#include "karts/abstract_kart.hpp"
@ -68,7 +68,8 @@ CheckCannon::CheckCannon(const XMLNode &node, unsigned int index)
if (UserConfigParams::m_check_debug)
{
m_debug_target_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("additive"),
(scene::EPT_TRIANGLE_STRIP,
SP::SPShaderManager::get()->getSPShader("additive"),
material_manager->getDefaultSPMaterial("additive"));
SP::addDynamicDrawCall(m_debug_target_dy_dc);
m_debug_target_dy_dc->getVerticesVector().resize(4);

View File

@ -21,8 +21,8 @@
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_dynamic_draw_call.hpp"
#include "graphics/sp/sp_shader_manager.hpp"
#include "io/xml_node.hpp"
#include "karts/abstract_kart.hpp"
#include "modes/linear_world.hpp"
@ -77,7 +77,8 @@ CheckLine::CheckLine(const XMLNode &node, unsigned int index)
{
#ifndef SERVER_ONLY
m_debug_dy_dc = std::make_shared<SP::SPDynamicDrawCall>
(scene::EPT_TRIANGLE_STRIP, SP::getSPShader("additive"),
(scene::EPT_TRIANGLE_STRIP,
SP::SPShaderManager::get()->getSPShader("additive"),
material_manager->getDefaultSPMaterial("additive"));
SP::addDynamicDrawCall(m_debug_dy_dc);
m_debug_dy_dc->getVerticesVector().resize(4);