Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uniform_value.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06597fea200650b07bb5059bd9b82a4e521b0e9f (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "drape/uniform_value.hpp"
#include "drape/glfunctions.hpp"
#include "base/assert.hpp"
#include "std/cstring.hpp"

namespace dp
{

namespace
{

template<typename T>
void CopyValues(T * dstPointer, T * values, size_t valuesCount)
{
  for (size_t i = 0; i < valuesCount; ++i)
    dstPointer[i] = values[i];
}

void ApplyInt(int8_t location, int32_t const * pointer, size_t componentCount)
{
  switch (componentCount)
  {
  case 1:
    GLFunctions::glUniformValuei(location, pointer[0]);
    break;
  case 2:
    GLFunctions::glUniformValuei(location, pointer[0], pointer[1]);
    break;
  case 3:
    GLFunctions::glUniformValuei(location, pointer[0], pointer[1], pointer[2]);
    break;
  case 4:
    GLFunctions::glUniformValuei(location, pointer[0], pointer[1], pointer[2], pointer[3]);
    break;
  default:
    ASSERT(false, ());
  }
}

void ApplyFloat(int8_t location, float const * pointer, size_t componentCount)
{
  switch (componentCount)
  {
  case 1:
    GLFunctions::glUniformValuef(location, pointer[0]);
    break;
  case 2:
    GLFunctions::glUniformValuef(location, pointer[0], pointer[1]);
    break;
  case 3:
    GLFunctions::glUniformValuef(location, pointer[0], pointer[1], pointer[2]);
    break;
  case 4:
    GLFunctions::glUniformValuef(location, pointer[0], pointer[1], pointer[2], pointer[3]);
    break;
  default:
    ASSERT(false, ());
  }
}

void ApplyMatrix(uint8_t location, float const * matrix)
{
  GLFunctions::glUniformMatrix4x4Value(location, matrix);
}

} // namespace

UniformValue::UniformValue(string const & name, int32_t v)
  : m_name(name)
  , m_type(Int)
  , m_componentCount(1)
{
  Allocate(sizeof(int32_t));
  SetIntValue(v);
}

UniformValue::UniformValue(string const & name, int32_t v1, int32_t v2)
  : m_name(name)
  , m_type(Int)
  , m_componentCount(2)
{
  Allocate(2 * sizeof(int32_t));
  SetIntValue(v1, v2);
}

UniformValue::UniformValue(string const & name, int32_t v1, int32_t v2, int32_t v3)
  : m_name(name)
  , m_type(Int)
  , m_componentCount(3)
{
  Allocate(3 * sizeof(int32_t));
  SetIntValue(v1, v2, v3);
}

UniformValue::UniformValue(string const & name, int32_t v1, int32_t v2, int32_t v3, int32_t v4)
  : m_name(name)
  , m_type(Int)
  , m_componentCount(4)
{
  Allocate(4 * sizeof(int32_t));
  SetIntValue(v1, v2, v3, v4);
}

UniformValue::UniformValue(string const & name, float v)
  : m_name(name)
  , m_type(Float)
  , m_componentCount(1)
{
  Allocate(sizeof(float));
  SetFloatValue(v);
}

UniformValue::UniformValue(string const & name, float v1, float v2)
  : m_name(name)
  , m_type(Float)
  , m_componentCount(2)
{
  Allocate(2 * sizeof(float));
  SetFloatValue(v1, v2);
}

UniformValue::UniformValue(string const & name, float v1, float v2, float v3)
  : m_name(name)
  , m_type(Float)
  , m_componentCount(3)
{
  Allocate(3 * sizeof(float));
  SetFloatValue(v1, v2, v3);
}

UniformValue::UniformValue(string const & name, float v1, float v2, float v3, float v4)
  : m_name(name)
  , m_type(Float)
  , m_componentCount(4)
{
  Allocate(4 * sizeof(float));
  SetFloatValue(v1, v2, v3, v4);
}

UniformValue::UniformValue(string const & name, const float * matrixValue)
  : m_name(name)
  , m_type(Matrix4x4)
  , m_componentCount(16)
{
  Allocate(4 * 4 * sizeof(float));
  SetMatrix4x4Value(matrixValue);
}

string const & UniformValue::GetName() const
{
  return m_name;
}

UniformValue::Type UniformValue::GetType() const
{
  return m_type;
}

size_t UniformValue::GetComponentCount() const
{
  return m_componentCount;
}

void UniformValue::SetIntValue(int32_t v)
{
  ASSERT(m_type == Int, ());
  ASSERT(m_componentCount == 1, ());
  CopyValues(CastMemory<int32_t>(), &v, m_componentCount);
}

void UniformValue::SetIntValue(int32_t v1, int32_t v2)
{
  ASSERT(m_type == Int, ());
  ASSERT(m_componentCount == 2, ());
  int v[2] = { v1, v2 };
  CopyValues(CastMemory<int32_t>(), v, m_componentCount);
}

void UniformValue::SetIntValue(int32_t v1, int32_t v2, int32_t v3)
{
  ASSERT(m_type == Int, ());
  ASSERT(m_componentCount == 3, ());
  int v[3] = { v1, v2, v3 };
  CopyValues(CastMemory<int32_t>(), v, m_componentCount);
}

void UniformValue::SetIntValue(int32_t v1, int32_t v2, int32_t v3, int32_t v4)
{
  ASSERT(m_type == Int, ());
  ASSERT(m_componentCount == 4, ());
  int v[4] = { v1, v2, v3, v4 };
  CopyValues(CastMemory<int32_t>(), v, m_componentCount);
}

void UniformValue::SetFloatValue(float v)
{
  ASSERT(m_type == Float, ());
  ASSERT(m_componentCount == 1, ());
  CopyValues(CastMemory<float>(), &v, m_componentCount);
}

void UniformValue::SetFloatValue(float v1, float v2)
{
  ASSERT(m_type == Float, ());
  ASSERT(m_componentCount == 2, ());
  float v[2] = { v1, v2 };
  CopyValues(CastMemory<float>(), v, m_componentCount);
}

void UniformValue::SetFloatValue(float v1, float v2, float v3)
{
  ASSERT(m_type == Float, ());
  ASSERT(m_componentCount == 3, ());
  float v[3] = { v1, v2, v3 };
  CopyValues(CastMemory<float>(), v, m_componentCount);
}

void UniformValue::SetFloatValue(float v1, float v2, float v3, float v4)
{
  ASSERT(m_type == Float, ());
  ASSERT(m_componentCount == 4, ());
  float v[4] = { v1, v2, v3, v4 };
  CopyValues(CastMemory<float>(), v, m_componentCount);
}

void UniformValue::SetMatrix4x4Value(float const * matrixValue)
{
  ASSERT(m_type == Matrix4x4, ());
  ASSERT(m_componentCount == 16, ());
  memcpy(CastMemory<float>(), matrixValue, 4 * 4 * sizeof(float));
}

void UniformValue::Apply(ref_ptr<GpuProgram> program) const
{
  int8_t location = program->GetUniformLocation(m_name);
  if (location == -1)
    return;

  switch (m_type)
  {
  case Int:
    ApplyInt(location, CastMemory<int32_t>(), m_componentCount);
    break;
  case Float:
    ApplyFloat(location, CastMemory<float>(), m_componentCount);
    break;
  case Matrix4x4:
    ApplyMatrix(location, CastMemory<float>());
    break;
  default:
    ASSERT(false, ());
  }
}

void UniformValue::Allocate(size_t byteCount)
{
  m_values.reset(new uint8_t[byteCount]);
}

} // namespace dp