xoreos  0.0.5
shadersurface.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/graphics/graphics.h"
26 
28 
29 namespace Graphics {
30 
31 namespace Shader {
32 
33 #define SHADER_SURFACE_VARIABLE_OWNED (0x00000001)
34 
35 ShaderSurface::ShaderSurface(Shader::ShaderObject *vertShader, const Common::UString &name) : _variableData(), _vertShader(vertShader), _flags(0), _name(name), _usageCount(0), _objectModelviewIndex(0xFFFFFFFF), _textureViewIndex(0xFFFFFFFF) {
36  vertShader->usageCount++;
37 
38  uint32 varCount = vertShader->variablesCombined.size();
39  _variableData.resize(varCount);
40  for (uint32 i = 0; i < varCount; ++i) {
41  _variableData[i].flags = 0;
42  genSurfaceVar(i);
43 
44  if (vertShader->variablesCombined[i].name == "_objectModelviewMatrix") {
46  } else if (vertShader->variablesCombined[i].name == "_textureViewMatrix") {
48  } else if (vertShader->variablesCombined[i].name == "_projectionMatrix") {
49  setVariableExternal(i, &(GfxMan.getProjectionMatrix()));
50  } else if (vertShader->variablesCombined[i].name == "_modelviewMatrix") {
51  setVariableExternal(i, &(GfxMan.getModelviewMatrix()));
52  }
53  }
54 }
55 
57  for (uint32 i = 0; i < _variableData.size(); ++i) {
58  delSurfaceVar(i);
59  }
60  // The surface doesn't own UBO's in the _uboArray, and so does not delete them.
61 }
62 
64  return _name;
65 }
66 
68  return _vertShader;
69 }
70 
72  return _flags;
73 }
74 
76  _flags = flags;
77 }
78 
80  return _vertShader->variablesCombined.size();
81  // return _variableData.size(); // Should be equal to the frag shader variable count.
82 }
83 
85  return _vertShader->variablesCombined[index].type;
86 }
87 
89  return _variableData[index].data;
90 }
91 
93  void *rval = 0;
94 
95  for (uint32 i = 0; i < _variableData.size(); ++i) {
96  if (_vertShader->variablesCombined[i].name == name) {
97  rval = _variableData[i].data;
98  break;
99  }
100  }
101 
102  return rval;
103 }
104 
106  return _vertShader->variablesCombined[index].name;
107 }
108 
110  return _variableData[index].flags;
111 }
112 
114  delSurfaceVar(index);
115  _variableData[index].data = loc;
116 }
117 
119  for (uint32 i = 0; i < _variableData.size(); ++i) {
120  if (_vertShader->variablesCombined[i].name == name) {
121  delSurfaceVar(i);
122  _variableData[i].data = loc;
123  break;
124  }
125  }
126 }
127 
129  genSurfaceVar(index);
130 }
131 
133  for (uint32 i = 0; i < _variableData.size(); ++i) {
134  if (_vertShader->variablesCombined[i].name == name) {
135  genSurfaceVar(i);
136  break;
137  }
138  }
139 }
140 
142  return (_variableData[index].flags & SHADER_SURFACE_VARIABLE_OWNED) ? true : false;
143 }
144 
146  bool rval = false;
147  for (uint32 i = 0; i < _variableData.size(); ++i) {
148  if (_vertShader->variablesCombined[i].name == name) {
149  rval = (_variableData[i].flags & SHADER_SURFACE_VARIABLE_OWNED) ? true : false;
150  break;
151  }
152  }
153 
154  return rval;
155 }
156 
157 void ShaderSurface::addUBO(uint32 index, GLuint glid) {
158  _uboArray.push_back(Shader::ShaderUBO(index, glid));
159 }
160 
162  for (uint32 i = 0; i < _variableData.size(); i++) {
163  ShaderMan.bindShaderVariable(program->vertexObject->variablesCombined[i], program->vertexVariableLocations[i], _variableData[i].data);
164  }
165 }
166 
167 void ShaderSurface::bindProgram(Shader::ShaderProgram *program, const glm::mat4 *t) {
168  for (uint32 i = 0; i < _variableData.size(); i++) {
169  if (_objectModelviewIndex == i) {
170  ShaderMan.bindShaderVariable(program->vertexObject->variablesCombined[i], program->vertexVariableLocations[i], t);
171  } else {
172  ShaderMan.bindShaderVariable(program->vertexObject->variablesCombined[i], program->vertexVariableLocations[i], _variableData[i].data);
173  }
174  }
175 }
176 
177 void ShaderSurface::bindObjectModelview(Shader::ShaderProgram *program, const glm::mat4 *t) {
178  if (_objectModelviewIndex != 0xFFFFFFFF) {
180  }
181 }
182 
183 void ShaderSurface::bindTextureView(Shader::ShaderProgram *program, const glm::mat4 *t) {
184  if (_textureViewIndex != 0xFFFFFFFF) {
186  }
187 }
188 
191  glDisable(GL_CULL_FACE);
192  }
193 
194  for (uint32 i = 0; i < _uboArray.size(); ++i) {
195  glBindBufferBase(GL_UNIFORM_BUFFER, _uboArray[i].index, _uboArray[i].glid);
196  }
197 }
198 
200  // No need to unbind the uniform buffers really. If a shader uses them, new ones should be bound.
201 
203  glEnable(GL_CULL_FACE);
204  }
205 }
206 
208 }
209 
211  if (_variableData[index].flags & SHADER_SURFACE_VARIABLE_OWNED)
212  return 0;
213 
214  void *rval = 0;
215  uint32 count = _vertShader->variablesCombined[index].count;
216 
217  switch (_vertShader->variablesCombined[index].type) {
218  case SHADER_FLOAT: rval = new float[count]; break;
219  case SHADER_VEC2: rval = new float[2 * count]; break;
220  case SHADER_VEC3: rval = new float[3 * count]; break;
221  case SHADER_VEC4: rval = new float[4 * count]; break;
222  case SHADER_INT: rval = new int[count]; break;
223  case SHADER_IVEC2: rval = new int[2 * count]; break;
224  case SHADER_IVEC3: rval = new int[3 * count]; break;
225  case SHADER_IVEC4: rval = new int[4 * count]; break;
226 // case SHADER_UINT: break;
227 // case SHADER_UVEC2: break;
228 // case SHADER_UVEC3: break;
229 // case SHADER_UVEC4: break;
230 // case SHADER_BOOL: break;
231 // case SHADER_BVEC2: break;
232 // case SHADER_BVEC3: break;
233 // case SHADER_BVEC4: break;
234  case SHADER_MAT2: rval = new float[4 * count]; break;
235  case SHADER_MAT3: rval = new float[9 * count]; break;
236  case SHADER_MAT4: rval = new float[16 * count]; break;
237  case SHADER_SAMPLER1D:
238  case SHADER_SAMPLER2D:
239  case SHADER_SAMPLER3D:
240  case SHADER_SAMPLERCUBE: break;
241  case SHADER_SAMPLER1DSHADOW: break;
242  case SHADER_SAMPLER2DSHADOW: break;
243  case SHADER_SAMPLER1DARRAY: break;
244  case SHADER_SAMPLER2DARRAY: break;
245  case SHADER_SAMPLER1DARRAYSHADOW: break;
246  case SHADER_SAMPLER2DARRAYSHADOW: break;
247  case SHADER_SAMPLERBUFFER: break;
248  case SHADER_ISAMPLER1D: break;
249  case SHADER_ISAMPLER2D: break;
250  case SHADER_ISAMPLER3D: break;
251  case SHADER_ISAMPLERCUBE: break;
252  case SHADER_ISAMPLER1DARRAY: break;
253  case SHADER_ISAMPLER2DARRAY: break;
254  case SHADER_USAMPLER1D: break;
255  case SHADER_USAMPLER2D: break;
256  case SHADER_USAMPLER3D: break;
257  case SHADER_USAMPLERCUBE: break;
258  case SHADER_USAMPLER1DARRAY: break;
259  case SHADER_USAMPLER2DARRAY: break;
260  case SHADER_UNIFORM_BUFFER: rval = new Shader::ShaderUBO; break;
261  default: break;
262  }
263 
265  _variableData[index].data = rval;
266 
267  return rval;
268 }
269 
271 {
272  if (!(_variableData[index].flags & SHADER_SURFACE_VARIABLE_OWNED))
273  return;
274 
275  _variableData[index].flags &= ~(SHADER_SURFACE_VARIABLE_OWNED);
276 
277  void *data = _variableData[index].data;
278  switch (_vertShader->variablesCombined[index].type) {
279  case SHADER_FLOAT:
280  case SHADER_VEC2:
281  case SHADER_VEC3:
282  case SHADER_VEC4: delete [] (static_cast<float *>(data)); break;
283  case SHADER_INT:
284  case SHADER_IVEC2:
285  case SHADER_IVEC3:
286  case SHADER_IVEC4: delete [] (static_cast<int *>(data)); break;
287 // case SHADER_UINT: break;
288 // case SHADER_UVEC2: break;
289 // case SHADER_UVEC3: break;
290 // case SHADER_UVEC4: break;
291 // case SHADER_BOOL: break;
292 // case SHADER_BVEC2: break;
293 // case SHADER_BVEC3: break;
294 // case SHADER_BVEC4: break;
295  case SHADER_MAT2:
296  case SHADER_MAT3:
297  case SHADER_MAT4: delete [] (static_cast<float *>(data)); break;
298  case SHADER_SAMPLER1D: break;
299  case SHADER_SAMPLER2D: break;
300  case SHADER_SAMPLER3D: break;
301  case SHADER_SAMPLERCUBE: break;
302  case SHADER_SAMPLER1DSHADOW: break;
303  case SHADER_SAMPLER2DSHADOW: break;
304  case SHADER_SAMPLER1DARRAY: break;
305  case SHADER_SAMPLER2DARRAY: break;
306  case SHADER_SAMPLER1DARRAYSHADOW: break;
307  case SHADER_SAMPLER2DARRAYSHADOW: break;
308  case SHADER_SAMPLERBUFFER: break;
309  case SHADER_ISAMPLER1D: break;
310  case SHADER_ISAMPLER2D: break;
311  case SHADER_ISAMPLER3D: break;
312  case SHADER_ISAMPLERCUBE: break;
313  case SHADER_ISAMPLER1DARRAY: break;
314  case SHADER_ISAMPLER2DARRAY: break;
315  case SHADER_USAMPLER1D: break;
316  case SHADER_USAMPLER2D: break;
317  case SHADER_USAMPLER3D: break;
318  case SHADER_USAMPLERCUBE: break;
319  case SHADER_USAMPLER1DARRAY: break;
320  case SHADER_USAMPLER2DARRAY: break;
321  case SHADER_UNIFORM_BUFFER: delete static_cast<ShaderUBO *>(data); break;
322  default: break;
323  }
324 
325  _variableData[index].data = 0;
326 }
327 
329  ++_usageCount;
330 }
331 
333  if (_usageCount) {
334  --_usageCount;
335  }
336 }
337 
339  return _usageCount;
340 }
341 
342 } // End of namespace Shader
343 
344 } // End of namespace Graphics
The global graphics manager.
void * getVariableData(uint32 index) const
Shader::ShaderVariableType getVariableType(uint32 index) const
A class holding an UTF-8 string.
Definition: ustring.h:48
void bindTextureView(Shader::ShaderProgram *program, const glm::mat4 *t)
void setVariableExternal(uint32 index, void *loc)
Shader::ShaderObject * _vertShader
Definition: shadersurface.h:98
#define SHADER_SURFACE_NOCULL
Definition: shadersurface.h:38
#define SHADER_SURFACE_VARIABLE_OWNED
void bindProgram(Shader::ShaderProgram *program)
void setVariableInternal(uint32 index)
ShaderSurface(Shader::ShaderObject *vertShader, const Common::UString &name="unnamed")
ShaderVariableType
Enum of all supported shader variable parsing and automatic binding.
Definition: shader.h:85
void addUBO(uint32 index, GLuint glid)
ShaderObject * vertexObject
Definition: shader.h:218
std::vector< ShaderSurfaceVariable > _variableData
Definition: shadersurface.h:96
void * genSurfaceVar(uint32 index)
#define ShaderMan
Shortcut for accessing the shader manager.
Definition: shader.h:293
uint32_t uint32
Definition: types.h:204
std::vector< GLint > vertexVariableLocations
Definition: shader.h:220
bool isVariableOwned(uint32 index) const
Shader::ShaderObject * getVertexShader() const
uint32 getVariableFlags(uint32 index) const
const Common::UString & getName() const
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
Shader surface, responsible for tracking data relating to a vertex shader.
std::vector< ShaderObject::ShaderObjectVariable > variablesCombined
Definition: shader.h:206
std::vector< Shader::ShaderUBO > _uboArray
Definition: shadersurface.h:97
const Common::UString & getVariableName(uint32 index) const
void bindObjectModelview(Shader::ShaderProgram *program, const glm::mat4 *t)