File: shader.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (295 lines) | stat: -rw-r--r-- 6,055 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef GLW_SHADER_H
#define GLW_SHADER_H

#include <string>
#include <iostream>

#include "./object.h"

namespace glw
{

class ShaderArguments : public ObjectArguments
{
	public:

		typedef ObjectArguments BaseType;
		typedef ShaderArguments ThisType;

		std::string source;

		ShaderArguments(void)
			: BaseType()
		{
			;
		}

		void clear(void)
		{
			BaseType::clear();
			this->source.clear();
		}
};

class Shader : public Object
{
	friend class Context;

	public:

		typedef Object BaseType;
		typedef Shader ThisType;

		virtual ~Shader(void)
		{
			this->destroy();
		}

		const std::string & source(void) const
		{
			return this->m_source;
		}

		const std::string & log(void) const
		{
			return this->m_log;
		}

		bool isCompiled(void) const
		{
			return this->m_compiled;
		}

	protected:

		std::string  m_source;
		std::string  m_log;
		bool         m_compiled;

		Shader(Context * ctx)
			: BaseType   (ctx)
			, m_compiled (false)
		{
			;
		}

		virtual GLenum shaderType(void) const = 0;

		bool create(const ShaderArguments & args)
		{
			this->destroy();
			const GLenum shType = this->shaderType();
			this->m_name = glCreateShader(shType);
			this->compile(args.source);
			return this->m_compiled;
		}

		virtual void doDestroy(void)
		{
			glDeleteShader(this->m_name);
			this->m_source.clear();
			this->m_log.clear();
			this->m_compiled = false;
		}

		virtual bool doIsValid(void) const
		{
			return this->m_compiled;
		}

		void compile(const std::string & source)
		{
			const char * src = source.c_str();
			glShaderSource(this->m_name, 1, &src, 0);
			glCompileShader(this->m_name);

			GLint compileStatus = 0;
			glGetShaderiv(this->m_name, GL_COMPILE_STATUS, &compileStatus);

			this->m_source   = source;
			this->m_log      = ThisType::getInfoLog(this->m_name);
			this->m_compiled = (compileStatus != GL_FALSE);

#if GLW_PRINT_LOG_TO_STDERR
			std::cerr << "---------------------------" << std::endl;
			std::cerr << "[";
			switch (this->shaderType())
			{
				case GL_VERTEX_SHADER   : std::cerr << "Vertex ";   break;
				case GL_GEOMETRY_SHADER : std::cerr << "Geometry "; break;
				case GL_FRAGMENT_SHADER : std::cerr << "Fragment "; break;
				default: break;
			}
			std::cerr << "Shader Compile Log]: " << ((this->m_compiled) ? ("OK") : ("FAILED")) << std::endl;
			std::cerr << this->m_log << std::endl;
			std::cerr << "---------------------------" << std::endl;
#endif
		}

	private:

		static std::string getInfoLog(GLuint Shader)
		{
			std::string log;

			GLint logLen = 0;
			glGetShaderiv(Shader, GL_INFO_LOG_LENGTH, &logLen);
			if (logLen > 0)
			{
				char * sLog = new char[logLen + 1];
				glGetShaderInfoLog(Shader, logLen, &logLen, sLog);
				if (logLen > 0)
				{
					if (sLog[0] != '\0')
					{
						sLog[logLen - 1] = '\0';
						log = sLog;
					}
				}
				delete [] sLog;
			}
			return log;
		}
};

namespace detail { template <> struct BaseOf <Shader> { typedef Object Type; }; };
typedef   detail::ObjectSharedPointerTraits  <Shader> ::Type ShaderPtr;

class SafeShader : public SafeObject
{
	friend class Context;
	friend class BoundShader;

	public:

		typedef SafeObject BaseType;
		typedef SafeShader ThisType;

		SafeShader(void)
			: BaseType()
		{
			;
		}

		const std::string & source(void) const
		{
			return this->object()->source();
		}

		const std::string & log(void) const
		{
			return this->object()->log();
		}

		bool isCompiled(void) const
		{
			return this->object()->isCompiled();
		}

	protected:

		SafeShader(const ShaderPtr & shader)
			: BaseType(shader)
		{
			;
		}

		const ShaderPtr & object(void) const
		{
			return static_cast<const ShaderPtr &>(BaseType::object());
		}

		ShaderPtr & object(void)
		{
			return static_cast<ShaderPtr &>(BaseType::object());
		}
};

namespace detail { template <> struct BaseOf     <SafeShader> { typedef SafeObject Type; }; };
namespace detail { template <> struct ObjectBase <SafeShader> { typedef Shader     Type; }; };
namespace detail { template <> struct ObjectSafe <Shader    > { typedef SafeShader Type; }; };
typedef   detail::ObjectSharedPointerTraits      <SafeShader> ::Type ShaderHandle;

class ShaderBindingParams : public ObjectBindingParams
{
	public:

		typedef ObjectBindingParams BaseType;
		typedef ShaderBindingParams ThisType;

		ShaderBindingParams(void)
			: BaseType()
		{
			;
		}

		ShaderBindingParams(GLenum aTarget, GLenum aUnit)
			: BaseType(aTarget, aUnit)
		{
			;
		}
};

class BoundShader : public BoundObject
{
	friend class Context;

	public:

		typedef BoundObject BaseType;
		typedef BoundShader ThisType;

		BoundShader(void)
			: BaseType()
		{
			;
		}

		const ShaderHandle & handle(void) const
		{
			return static_cast<const ShaderHandle &>(BaseType::handle());
		}

		ShaderHandle & handle(void)
		{
			return static_cast<ShaderHandle &>(BaseType::handle());
		}

	protected:

		BoundShader(const ShaderHandle & handle, const ShaderBindingParams & params)
			: BaseType(handle, params)
		{
			;
		}

		const ShaderPtr & object(void) const
		{
			return this->handle()->object();
		}

		ShaderPtr & object(void)
		{
			return this->handle()->object();
		}

		virtual void bind(void)
		{
			;
		}

		virtual void unbind(void)
		{
			;
		}
};

namespace detail { template <> struct ParamsOf    <BoundShader> { typedef ShaderBindingParams Type; }; };
namespace detail { template <> struct BaseOf      <BoundShader> { typedef BoundObject Type; }; };
namespace detail { template <> struct ObjectBase  <BoundShader> { typedef Shader      Type; }; };
namespace detail { template <> struct ObjectBound <Shader     > { typedef BoundShader Type; }; };
typedef   detail::ObjectSharedPointerTraits       <BoundShader> ::Type  BoundShaderHandle;

};

#endif // GLW_SHADER_H