forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureSamplerDescriptor.cpp
159 lines (135 loc) · 4.94 KB
/
TextureSamplerDescriptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "TextureSamplerDescriptor.hpp"
#include "TextureManager.hpp"
#include <utility>
namespace libprojectM {
namespace Renderer {
TextureSamplerDescriptor::TextureSamplerDescriptor(const std::shared_ptr<class Texture>& texture,
const std::shared_ptr<class Sampler>& sampler,
std::string samplerName,
std::string sizeName)
: m_texture(texture)
, m_sampler(sampler)
, m_samplerName(std::move(samplerName))
, m_sizeName(std::move(sizeName))
{
}
auto TextureSamplerDescriptor::Empty() const -> bool
{
return m_texture.expired() || m_sampler.expired();
}
void TextureSamplerDescriptor::Bind(GLint unit, const Shader& shader) const
{
auto texture = m_texture.lock();
auto sampler = m_sampler.lock();
if (texture && sampler)
{
texture->Bind(unit, sampler);
shader.SetUniformInt(std::string("sampler_" + m_samplerName).c_str(), unit);
// Might be setting this more than once if the texture is used with different wrap/filter modes, but this rarely happens.
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName).c_str(), {texture->Width(),
texture->Height(),
1.0f / static_cast<float>(texture->Width()),
1.0f / static_cast<float>(texture->Height())});
// Bind shorthand random texture size uniform
if (m_sizeName.substr(0, 4) == "rand" && m_sizeName.length() > 7 && m_sizeName.at(6) == '_')
{
shader.SetUniformFloat4(std::string("texsize_" + m_sizeName.substr(0, 6)).c_str(), {texture->Width(),
texture->Height(),
1.0f / static_cast<float>(texture->Width()),
1.0f / static_cast<float>(texture->Height())});
}
}
}
void TextureSamplerDescriptor::Unbind(GLint unit)
{
auto texture = m_texture.lock();
if (texture)
{
texture->Unbind(unit);
}
Sampler::Unbind(unit);
}
auto TextureSamplerDescriptor::Texture() const -> std::shared_ptr<class Texture>
{
return m_texture.lock();
}
void TextureSamplerDescriptor::Texture(std::weak_ptr<class Texture> texture)
{
m_texture = texture;
}
auto TextureSamplerDescriptor::Sampler() const -> std::shared_ptr<class Sampler>
{
return m_sampler.lock();
}
auto TextureSamplerDescriptor::SamplerDeclaration() const -> std::string
{
auto texture = m_texture.lock();
auto sampler = m_sampler.lock();
if (!texture || !sampler)
{
return {};
}
std::string declaration = "uniform ";
if (texture->Type() == GL_TEXTURE_3D)
{
declaration.append("sampler3D sampler_");
}
else
{
declaration.append("sampler2D sampler_");
}
declaration.append(m_samplerName);
declaration.append(";\n");
// Add short sampler name for prefixed random textures.
// E.g. "sampler_rand00" if a sampler "sampler_rand00_smalltiled" was declared
if (m_samplerName.substr(0, 4) == "rand" && m_samplerName.length() > 7 && m_samplerName.at(6) == '_')
{
declaration.append("uniform sampler2D sampler_");
declaration.append(m_samplerName.substr(0, 6));
declaration.append(";\n");
}
return declaration;
}
auto TextureSamplerDescriptor::TexSizeDeclaration() const -> std::string
{
auto texture = m_texture.lock();
auto sampler = m_sampler.lock();
if (!texture || !sampler)
{
return {};
}
std::string declaration;
if (!m_sizeName.empty())
{
declaration.append("uniform float4 texsize_");
declaration.append(m_sizeName);
declaration.append(";\n");
// Add short texsize uniform for prefixed random textures.
// E.g. "texsize_rand00" if a sampler "sampler_rand00_smalltiled" was declared
if (m_sizeName.substr(0, 4) == "rand" && m_sizeName.length() > 7 && m_sizeName.at(6) == '_')
{
declaration.append("uniform float4 texsize_");
declaration.append(m_sizeName.substr(0, 6));
declaration.append(";\n");
}
}
return declaration;
}
void TextureSamplerDescriptor::TryUpdate(TextureManager& textureManager)
{
if (!Empty())
{
return;
}
auto desc = textureManager.GetTexture(m_samplerName);
if (desc.Empty())
{
// Only try once, then give up.
m_updateFailed = true;
return;
}
m_texture = desc.m_texture;
m_sampler = desc.m_sampler;
}
} // namespace Renderer
} // namespace libprojectM