Fix keymap.
[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://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() const { 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() const { 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       m_validated(false)
194       {
195         m_pFP = new CGLSLPixelShader();
196         m_pVP = new CGLSLVertexShader();
197       }
198     CGLSLShaderProgram(const std::string& vert
199                      , const std::string& frag) :
200       m_validated(false)
201       {
202         m_pFP = new CGLSLPixelShader();
203         m_pFP->LoadSource(frag);
204         m_pVP = new CGLSLVertexShader();
205         m_pVP->LoadSource(vert);
206       }
207
208     // enable the shader
209     virtual bool Enable();
210
211     // disable the shader
212     virtual void Disable();
213
214     // free resources
215     virtual void Free();
216
217     // compile and link the shaders
218     virtual bool CompileAndLink();
219
220   protected:
221     GLint         m_lastProgram;
222     bool          m_validated;
223   };
224
225
226 #ifndef HAS_GLES
227   class CARBShaderProgram
228     : virtual public CShaderProgram
229   {
230   public:
231     CARBShaderProgram()
232       {
233         m_pFP = new CARBPixelShader();
234         m_pVP = new CARBVertexShader();
235       }
236     CARBShaderProgram(const std::string& vert
237                     , const std::string& frag)
238       {
239         m_pFP = new CARBPixelShader();
240         m_pFP->LoadSource(vert);
241         m_pVP = new CARBVertexShader();
242         m_pVP->LoadSource(vert);
243       }
244
245     // enable the shader
246     virtual bool Enable();
247
248     // disable the shader
249     virtual void Disable();
250
251     // free resources
252     virtual void Free();
253
254     // compile and link the shaders
255     virtual bool CompileAndLink();
256
257   protected:
258
259   };
260 #endif
261
262 } // close namespace
263
264 #endif
265
266 #endif //__SHADER_H__