[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / guilib / Shader.h
1 #ifndef __SHADER_H__
2 #define __SHADER_H__
3
4 /*
5  *      Copyright (C) 2005-2013 Team XBMC
6  *      http://www.xbmc.org
7  *
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)
11  *  any later version.
12  *
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.
17  *
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/>.
21  *
22  */
23
24 #include "system.h" // for HAS_GL/HAS_GLES
25
26 #include <vector>
27 #include <string>
28
29 #if defined(HAS_GL) || defined(HAS_GLES)
30 #include "system_gl.h"
31
32 namespace Shaders {
33
34   using namespace std;
35
36   //////////////////////////////////////////////////////////////////////
37   // CShader - base class
38   //////////////////////////////////////////////////////////////////////
39   class CShader
40   {
41   public:
42     CShader() { m_compiled = false; }
43     virtual ~CShader() {}
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() { return m_compiled; }
50
51   protected:
52     string m_source;
53     string m_lastLog;
54     vector<string> m_attr;
55     bool m_compiled;
56
57   };
58
59
60   //////////////////////////////////////////////////////////////////////
61   // CVertexShader - vertex shader class
62   //////////////////////////////////////////////////////////////////////
63   class CVertexShader : public CShader
64   {
65   public:
66     CVertexShader() { m_vertexShader = 0; }
67     virtual ~CVertexShader() { Free(); }
68     virtual void Free() {}
69     virtual GLuint Handle() { return m_vertexShader; }
70
71   protected:
72     GLuint m_vertexShader;
73   };
74
75   class CGLSLVertexShader : public CVertexShader
76   {
77   public:
78     virtual void Free();
79     virtual bool Compile();
80   };
81
82 #ifndef HAS_GLES
83   class CARBVertexShader : public CVertexShader
84   {
85   public:
86     virtual void Free();
87     virtual bool Compile();
88   };
89 #endif
90
91
92   //////////////////////////////////////////////////////////////////////
93   // CPixelShader - abstract pixel shader class
94   //////////////////////////////////////////////////////////////////////
95   class CPixelShader : public CShader
96   {
97   public:
98     CPixelShader() { m_pixelShader = 0; }
99     virtual ~CPixelShader() { Free(); }
100     virtual void Free() {}
101     virtual GLuint Handle() { return m_pixelShader; }
102
103   protected:
104     GLuint m_pixelShader;
105   };
106
107
108   class CGLSLPixelShader : public CPixelShader
109   {
110   public:
111     virtual void Free();
112     virtual bool Compile();
113   };
114
115 #ifndef HAS_GLES
116   class CARBPixelShader : public CPixelShader
117   {
118   public:
119     virtual void Free();
120     virtual bool Compile();
121   };
122 #endif
123
124   //////////////////////////////////////////////////////////////////////
125   // CShaderProgram - the complete shader consisting of both the vertex
126   //                  and pixel programs. (abstract)
127   //////////////////////////////////////////////////////////////////////
128   class CShaderProgram
129   {
130   public:
131     CShaderProgram()
132       {
133         m_ok = false;
134         m_shaderProgram = 0;
135         m_pFP = NULL;
136         m_pVP = NULL;
137       }
138
139     virtual ~CShaderProgram()
140       {
141         Free();
142         delete m_pFP;
143         delete m_pVP;
144       }
145
146     // enable the shader
147     virtual bool Enable() = 0;
148
149     // disable the shader
150     virtual void Disable() = 0;
151
152     // returns true if shader is compiled and linked
153     bool OK() { return m_ok; }
154
155     // free resources
156     virtual void Free() {}
157
158     // return the vertex shader object
159     CVertexShader* VertexShader() { return m_pVP; }
160
161     // return the pixel shader object
162     CPixelShader* PixelShader() { return m_pFP; }
163
164     // compile and link the shaders
165     virtual bool CompileAndLink() = 0;
166
167     // override to to perform custom tasks on successfull compilation
168     // and linkage. E.g. obtaining handles to shader attributes.
169     virtual void OnCompiledAndLinked() {}
170
171     // override to to perform custom tasks before shader is enabled
172     // and after it is disabled. Return false in OnDisabled() to
173     // disable shader.
174     // E.g. setting attributes, disabling texture unites, etc
175     virtual bool OnEnabled() { return true; }
176     virtual void OnDisabled() { }
177
178     virtual GLuint ProgramHandle() { return m_shaderProgram; }
179
180   protected:
181     CVertexShader* m_pVP;
182     CPixelShader*  m_pFP;
183     GLuint         m_shaderProgram;
184     bool           m_ok;
185   };
186
187
188   class CGLSLShaderProgram
189     : virtual public CShaderProgram
190   {
191   public:
192     CGLSLShaderProgram()
193       {
194         m_pFP = new CGLSLPixelShader();
195         m_pVP = new CGLSLVertexShader();
196       }
197     CGLSLShaderProgram(const std::string& vert
198                      , const std::string& frag)
199       {
200         m_pFP = new CGLSLPixelShader();
201         m_pFP->LoadSource(frag);
202         m_pVP = new CGLSLVertexShader();
203         m_pVP->LoadSource(vert);
204       }
205
206     // enable the shader
207     virtual bool Enable();
208
209     // disable the shader
210     virtual void Disable();
211
212     // free resources
213     virtual void Free();
214
215     // compile and link the shaders
216     virtual bool CompileAndLink();
217
218   protected:
219     GLint         m_lastProgram;
220     bool          m_validated;
221   };
222
223
224 #ifndef HAS_GLES
225   class CARBShaderProgram
226     : virtual public CShaderProgram
227   {
228   public:
229     CARBShaderProgram()
230       {
231         m_pFP = new CARBPixelShader();
232         m_pVP = new CARBVertexShader();
233       }
234     CARBShaderProgram(const std::string& vert
235                     , const std::string& frag)
236       {
237         m_pFP = new CARBPixelShader();
238         m_pFP->LoadSource(vert);
239         m_pVP = new CARBVertexShader();
240         m_pVP->LoadSource(vert);
241       }
242
243     // enable the shader
244     virtual bool Enable();
245
246     // disable the shader
247     virtual void Disable();
248
249     // free resources
250     virtual void Free();
251
252     // compile and link the shaders
253     virtual bool CompileAndLink();
254
255   protected:
256
257   };
258 #endif
259
260 } // close namespace
261
262 #endif
263
264 #endif //__SHADER_H__