Skip to content

Commit b78d6e7

Browse files
committed
Keep references to textures and samplers in descriptors
Previously, if the TextureManager was recreated, e.g. when changing the texture paths, all loaded textures were deleted, even if currently in use by a preset. Storing textures and samplers as shared pointers instead of weak pointers will make sure the objects are kept alive until the preset is unloaded. Signed-off-by: Kai Blaschke <kai.blaschke@kb-dev.net>
1 parent 1843752 commit b78d6e7

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

src/libprojectM/Renderer/TextureSamplerDescriptor.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,71 +20,71 @@ TextureSamplerDescriptor::TextureSamplerDescriptor(const std::shared_ptr<class T
2020

2121
auto TextureSamplerDescriptor::Empty() const -> bool
2222
{
23-
return m_texture.expired() || m_sampler.expired();
23+
return m_texture->Empty();
2424
}
2525

2626
void TextureSamplerDescriptor::Bind(GLint unit, const Shader& shader) const
2727
{
28-
auto texture = m_texture.lock();
29-
auto sampler = m_sampler.lock();
3028

31-
if (texture && sampler)
29+
if (m_texture && m_sampler)
3230
{
33-
texture->Bind(unit, sampler);
31+
m_texture->Bind(unit, m_sampler);
3432

3533
shader.SetUniformInt(std::string("sampler_" + m_samplerName).c_str(), unit);
3634
// Might be setting this more than once if the texture is used with different wrap/filter modes, but this rarely happens.
37-
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName).c_str(), {texture->Width(),
38-
texture->Height(),
39-
1.0f / static_cast<float>(texture->Width()),
40-
1.0f / static_cast<float>(texture->Height())});
35+
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName).c_str(), {m_texture->Width(),
36+
m_texture->Height(),
37+
1.0f / static_cast<float>(m_texture->Width()),
38+
1.0f / static_cast<float>(m_texture->Height())});
4139
// Bind shorthand random texture size uniform
4240
if (m_sizeName.substr(0, 4) == "rand" && m_sizeName.length() > 7 && m_sizeName.at(6) == '_')
4341
{
44-
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName.substr(0, 6)).c_str(), {texture->Width(),
45-
texture->Height(),
46-
1.0f / static_cast<float>(texture->Width()),
47-
1.0f / static_cast<float>(texture->Height())});
42+
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName.substr(0, 6)).c_str(), {m_texture->Width(),
43+
m_texture->Height(),
44+
1.0f / static_cast<float>(m_texture->Width()),
45+
1.0f / static_cast<float>(m_texture->Height())});
4846
}
4947
}
5048
}
5149

5250
void TextureSamplerDescriptor::Unbind(GLint unit)
5351
{
54-
auto texture = m_texture.lock();
55-
if (texture)
52+
if (m_texture)
5653
{
57-
texture->Unbind(unit);
54+
m_texture->Unbind(unit);
5855
}
5956
Sampler::Unbind(unit);
6057
}
6158

6259
auto TextureSamplerDescriptor::Texture() const -> std::shared_ptr<class Texture>
6360
{
64-
return m_texture.lock();
61+
return m_texture;
6562
}
6663

67-
void TextureSamplerDescriptor::Texture(std::weak_ptr<class Texture> texture)
64+
void TextureSamplerDescriptor::Texture(const std::shared_ptr<Renderer::Texture>& texture)
6865
{
6966
m_texture = texture;
7067
}
7168

69+
void TextureSamplerDescriptor::Texture(const std::weak_ptr<Renderer::Texture>& texture)
70+
{
71+
m_texture = texture.lock();
72+
}
73+
7274
auto TextureSamplerDescriptor::Sampler() const -> std::shared_ptr<class Sampler>
7375
{
74-
return m_sampler.lock();
76+
return m_sampler;
7577
}
7678

7779
auto TextureSamplerDescriptor::SamplerDeclaration() const -> std::string
7880
{
79-
auto texture = m_texture.lock();
80-
auto sampler = m_sampler.lock();
81-
if (!texture || !sampler)
81+
if (!m_texture || !m_sampler)
8282
{
8383
return {};
8484
}
8585

8686
std::string declaration = "uniform ";
87-
if (texture->Type() == GL_TEXTURE_3D)
87+
if (m_texture->Type() == GL_TEXTURE_3D)
8888
{
8989
declaration.append("sampler3D sampler_");
9090
}
@@ -109,9 +109,7 @@ auto TextureSamplerDescriptor::SamplerDeclaration() const -> std::string
109109

110110
auto TextureSamplerDescriptor::TexSizeDeclaration() const -> std::string
111111
{
112-
auto texture = m_texture.lock();
113-
auto sampler = m_sampler.lock();
114-
if (!texture || !sampler)
112+
if (!m_texture || !m_sampler)
115113
{
116114
return {};
117115
}

src/libprojectM/Renderer/TextureSamplerDescriptor.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#pragma once
22

3+
#include "Renderer/Sampler.hpp"
34
#include "Renderer/Shader.hpp"
5+
#include "Renderer/Texture.hpp"
6+
7+
#include <memory>
48

59
namespace libprojectM {
610
namespace Renderer {
@@ -65,11 +69,17 @@ class TextureSamplerDescriptor
6569
*/
6670
auto Texture() const -> std::shared_ptr<class Texture>;
6771

72+
/**
73+
* @brief Updates the internal texture with a new one.
74+
* @param texture A shared pointer to the new texture.
75+
*/
76+
void Texture(const std::shared_ptr<class Texture>& texture);
77+
6878
/**
6979
* @brief Updates the internal texture with a new one.
7080
* @param texture A weak pointer to the new texture.
7181
*/
72-
void Texture(std::weak_ptr<class Texture> texture);
82+
void Texture(const std::weak_ptr<class Texture>& texture);
7383

7484
/**
7585
* @brief Returns a pointer to the stored sampler.
@@ -96,8 +106,8 @@ class TextureSamplerDescriptor
96106
void TryUpdate(TextureManager& textureManager);
97107

98108
private:
99-
std::weak_ptr<class Texture> m_texture; //!< A weak reference to the texture.
100-
std::weak_ptr<class Sampler> m_sampler; //!< A weak reference to the sampler.
109+
std::shared_ptr<class Texture> m_texture; //!< A reference to the texture.
110+
std::shared_ptr<class Sampler> m_sampler; //!< A reference to the sampler.
101111
std::string m_samplerName; //!< The name of the texture sampler as referenced in the shader.
102112
std::string m_sizeName; //!< The name of the "texsize_" uniform as referenced in the shader.
103113
bool m_updateFailed{false}; //!< Set to true if the update try failed, e.g. texture could not be loaded.

0 commit comments

Comments
 (0)