VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
ShaderBase.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SHADERBASE_H
18 #define SHADERBASE_H
19 
20 #include "AttributeSet.h"
21 #include "RendererCommon.h"
22 #include "Shader.h"
23 #include <assert.h>
24 #include <string>
25 #include <vector>
26 
37 class ShaderBase : public Shader {
38 public:
49  ShaderBase(const std::string& vertexShaderCode, const std::string& fragmentShaderCode,
50  const AttributeSetPtr& attributes, const std::vector<std::string>& uniformNames);
51  virtual ~ShaderBase();
52 
53  virtual bool isCurrent() const override;
54  virtual bool isValid() const override;
55  virtual bool isCompiled() const override;
56  virtual GLuint getProgramId() const override;
57 
61  virtual void begin() override;
62  virtual void end() override;
70  virtual void compile() override;
71  virtual std::string getErrorLog() const override;
75  void dispose();
76 
77  virtual int getAttributeLocation(int index) const override;
78  virtual int getUniformLocation(int index) const override;
79 
80 protected:
86  virtual void performCompile();
87 
88 private:
89  void checkCompile();
90  void compileCode();
91  void bindLocations();
92 
93  bool mCurrentShader = false;
94  bool mValid = false;
95  bool mCompiled = false;
96  bool mProgramCreated = false;
97  GLuint mProgramId = 0;
98  bool mVertexShaderCreated = false;
99  GLuint mVertexShaderId = 0;
100  bool mFragmentShaderCreated = false;
101  GLuint mFragmentShaderId = 0;
102  std::string mVertexShaderCode;
103  std::string mFragmentShaderCode;
104  std::vector<int> mAttributeLocations;
105  std::vector<int> mUniformLocations;
106 };
107 
108 #endif // SHADERBASE_H
virtual void begin() override
Binds this shader, compiling if necessary.
virtual void compile() override
Compiles this Shader's GLSL code and links it into a GL program.
void dispose()
Destroys GL resources that were allocated for this ShaderBase.
Header declaring and including types common to renderer classes such as Vector2. Also includes GL hea...
virtual void performCompile()
Compiles and initializes the GL shader program from shader code.
Implementation of Shader which compiles itself from shader code.
Definition: ShaderBase.h:37
virtual GLuint getProgramId() const override
Returns the GL program id for this shader.
Shader program abstract base class.
Definition: Shader.h:47
std::shared_ptr< AttributeSet > AttributeSetPtr
Shared pointer typedef for AttributeSet.
Definition: PointerDeclarations.h:63
virtual void end() override
Unbinds this shader if bound by a corresponding call to begin().
virtual int getUniformLocation(int index) const override
Returns the location of an indexed uniform.
virtual bool isValid() const override
Returns whether or not this shader is valid (compiled and linked) and can be used for rendering...
virtual int getAttributeLocation(int index) const override
Returns the location of an indexed Attribute.
ShaderBase(const std::string &vertexShaderCode, const std::string &fragmentShaderCode, const AttributeSetPtr &attributes, const std::vector< std::string > &uniformNames)
Constructs a ShaderBase with the given code for its vertex and fragment shader.
virtual bool isCurrent() const override
Returns whether or not this shader is bound.
virtual std::string getErrorLog() const override
Retrieves the string describing compilation errors, if the call to compile() failed and isValid() is ...
virtual bool isCompiled() const override
Returns whether or not this shader has been processed for compilation.