5 * Copyright (C) 2005-2013 Team XBMC
8 * This Program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This Program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with XBMC; see the file COPYING. If not, see
20 * <http://www.gnu.org/licenses/>.
24 #include "system.h" // for HAS_GL/HAS_GLES
29 #if defined(HAS_GL) || defined(HAS_GLES)
30 #include "system_gl.h"
36 //////////////////////////////////////////////////////////////////////
37 // CShader - base class
38 //////////////////////////////////////////////////////////////////////
42 CShader() { m_compiled = false; }
44 virtual bool Compile() = 0;
45 virtual void Free() = 0;
46 virtual GLuint Handle() = 0;
47 virtual void SetSource(const string& src) { m_source = src; }
48 virtual bool LoadSource(const string& filename, const string& prefix = "");
49 bool OK() const { return m_compiled; }
54 vector<string> m_attr;
60 //////////////////////////////////////////////////////////////////////
61 // CVertexShader - vertex shader class
62 //////////////////////////////////////////////////////////////////////
63 class CVertexShader : public CShader
66 CVertexShader() { m_vertexShader = 0; }
67 virtual ~CVertexShader() { Free(); }
68 virtual void Free() {}
69 virtual GLuint Handle() { return m_vertexShader; }
72 GLuint m_vertexShader;
75 class CGLSLVertexShader : public CVertexShader
79 virtual bool Compile();
83 class CARBVertexShader : public CVertexShader
87 virtual bool Compile();
92 //////////////////////////////////////////////////////////////////////
93 // CPixelShader - abstract pixel shader class
94 //////////////////////////////////////////////////////////////////////
95 class CPixelShader : public CShader
98 CPixelShader() { m_pixelShader = 0; }
99 virtual ~CPixelShader() { Free(); }
100 virtual void Free() {}
101 virtual GLuint Handle() { return m_pixelShader; }
104 GLuint m_pixelShader;
108 class CGLSLPixelShader : public CPixelShader
112 virtual bool Compile();
116 class CARBPixelShader : public CPixelShader
120 virtual bool Compile();
124 //////////////////////////////////////////////////////////////////////
125 // CShaderProgram - the complete shader consisting of both the vertex
126 // and pixel programs. (abstract)
127 //////////////////////////////////////////////////////////////////////
139 virtual ~CShaderProgram()
147 virtual bool Enable() = 0;
149 // disable the shader
150 virtual void Disable() = 0;
152 // returns true if shader is compiled and linked
153 bool OK() const { return m_ok; }
156 virtual void Free() {}
158 // return the vertex shader object
159 CVertexShader* VertexShader() { return m_pVP; }
161 // return the pixel shader object
162 CPixelShader* PixelShader() { return m_pFP; }
164 // compile and link the shaders
165 virtual bool CompileAndLink() = 0;
167 // override to to perform custom tasks on successfull compilation
168 // and linkage. E.g. obtaining handles to shader attributes.
169 virtual void OnCompiledAndLinked() {}
171 // override to to perform custom tasks before shader is enabled
172 // and after it is disabled. Return false in OnDisabled() to
174 // E.g. setting attributes, disabling texture unites, etc
175 virtual bool OnEnabled() { return true; }
176 virtual void OnDisabled() { }
178 virtual GLuint ProgramHandle() { return m_shaderProgram; }
181 CVertexShader* m_pVP;
183 GLuint m_shaderProgram;
188 class CGLSLShaderProgram
189 : virtual public CShaderProgram
192 CGLSLShaderProgram() :
195 m_pFP = new CGLSLPixelShader();
196 m_pVP = new CGLSLVertexShader();
198 CGLSLShaderProgram(const std::string& vert
199 , const std::string& frag) :
202 m_pFP = new CGLSLPixelShader();
203 m_pFP->LoadSource(frag);
204 m_pVP = new CGLSLVertexShader();
205 m_pVP->LoadSource(vert);
209 virtual bool Enable();
211 // disable the shader
212 virtual void Disable();
217 // compile and link the shaders
218 virtual bool CompileAndLink();
227 class CARBShaderProgram
228 : virtual public CShaderProgram
233 m_pFP = new CARBPixelShader();
234 m_pVP = new CARBVertexShader();
236 CARBShaderProgram(const std::string& vert
237 , const std::string& frag)
239 m_pFP = new CARBPixelShader();
240 m_pFP->LoadSource(vert);
241 m_pVP = new CARBVertexShader();
242 m_pVP->LoadSource(vert);
246 virtual bool Enable();
248 // disable the shader
249 virtual void Disable();
254 // compile and link the shaders
255 virtual bool CompileAndLink();
266 #endif //__SHADER_H__