xoreos  0.0.5
shaderinstance.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 
26 
27 namespace Graphics {
28 
29 namespace Shader {
30 
31 ShaderInstance::ShaderInstance(Shader::ShaderObject *fragShader, Shader::ShaderObject *vertShader) : _vertexVariableData(), _fragmentVariableData(), _sourceProgram(0) {
32  Shader::ShaderProgram *program = ShaderMan.getShaderProgram(vertShader, fragShader);
33  if (!program) {
34  ShaderMan.registerShaderProgram(vertShader, fragShader);
35  program = ShaderMan.getShaderProgram(vertShader, fragShader);
36  }
37 
38  if (program) {
39  _sourceProgram = program;
41  for (uint32 i = 0; i < program->vertexObject->variablesCombined.size(); ++i)
42  _vertexVariableData.push_back(0);
44 
45  for (uint32 i = 0; i < program->fragmentObject->variablesCombined.size(); ++i)
46  _fragmentVariableData.push_back(0);
48  }
49 }
50 
51 ShaderInstance::ShaderInstance(const std::string &vertName, const std::string &fragName) : _vertexVariableData(), _fragmentVariableData(), _sourceProgram(0) {
52  Shader::ShaderObject *vertShader = ShaderMan.getShaderObject(vertName, Shader::SHADER_VERTEX);
53  Shader::ShaderObject *fragShader = ShaderMan.getShaderObject(fragName, Shader::SHADER_FRAGMENT);
54  Shader::ShaderProgram *program = ShaderMan.getShaderProgram(vertShader, fragShader);
55  if (!program) {
56  ShaderMan.registerShaderProgram(vertShader, fragShader);
57  program = ShaderMan.getShaderProgram(vertShader, fragShader);
58  }
59 
60  if (program) {
61  _sourceProgram = program;
63  for (uint32 i = 0; i < program->vertexObject->variablesCombined.size(); ++i) {
64  _vertexVariableData.push_back(0);
65  }
67 
68  for (uint32 i = 0; i < program->fragmentObject->variablesCombined.size(); ++i) {
69  _fragmentVariableData.push_back(0);
70  }
72  }
73 }
74 
77 }
78 
79 void ShaderInstance::setVertexVariable(const std::string &name, const void *location) {
80  for (uint32 i = 0; i < _sourceProgram->vertexObject->variablesCombined.size(); ++i) {
81  if (_sourceProgram->vertexObject->variablesCombined[i].name == name) {
82  _vertexVariableData[i] = location;
83  break;
84  }
85  }
86 }
87 
88 void ShaderInstance::setFragmentVariable(const std::string &name, const void *location) {
89  for (uint32 i = 0; i < _sourceProgram->fragmentObject->variablesCombined.size(); ++i) {
90  if (_sourceProgram->fragmentObject->variablesCombined[i].name == name) {
91  _fragmentVariableData[i] = location;
92  break;
93  }
94  }
95 }
96 
98  ShaderMan.bindShaderInstance(_sourceProgram, &(_vertexVariableData[0]), &(_fragmentVariableData[0]));
99 }
100 
102  glUseProgram(0);
103 }
104 
107 }
108 
111 }
112 
113 } // End of namespace Shader
114 
115 } // End of namespace Graphics
void rebindVertexVariable(uint32 index)
std::vector< const void * > _fragmentVariableData
ShaderInstance(Shader::ShaderObject *vertShader, Shader::ShaderObject *fragShader)
void setVertexVariable(const std::string &name, const void *location)
ShaderObject * fragmentObject
Definition: shader.h:219
std::vector< GLint > fragmentVariableLocations
Definition: shader.h:221
void setFragmentVariable(const std::string &name, const void *location)
ShaderObject * vertexObject
Definition: shader.h:218
Shader instancing, allowing for easier shader resource sharing.
void rebindFragmentVariable(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
Shader::ShaderProgram * _sourceProgram
std::vector< const void * > _vertexVariableData
std::vector< ShaderObject::ShaderObjectVariable > variablesCombined
Definition: shader.h:206