Merge lp:~glcompbench-dev/glcompbench/libmatrix-util into lp:glcompbench
- libmatrix-util
- Merge into trunk
Proposed by
Jesse Barker
Status: | Merged |
---|---|
Merged at revision: | 75 |
Proposed branch: | lp:~glcompbench-dev/glcompbench/libmatrix-util |
Merge into: | lp:glcompbench |
Diff against target: |
4402 lines (+2219/-1075) 33 files modified
src/glcompbench.cc (+4/-0) src/libmatrix/Makefile (+14/-5) src/libmatrix/gl-if.h (+18/-0) src/libmatrix/log.cc (+178/-0) src/libmatrix/log.h (+46/-0) src/libmatrix/mat.h (+120/-2) src/libmatrix/program.cc (+3/-25) src/libmatrix/program.h (+2/-4) src/libmatrix/shader-source.cc (+615/-0) src/libmatrix/shader-source.h (+103/-0) src/libmatrix/test/basic-global-const.vert (+15/-0) src/libmatrix/test/basic.frag (+7/-0) src/libmatrix/test/basic.vert (+14/-0) src/libmatrix/test/const_vec_test.cc (+60/-0) src/libmatrix/test/const_vec_test.h (+39/-0) src/libmatrix/test/inverse_test.cc (+11/-0) src/libmatrix/test/inverse_test.h (+11/-0) src/libmatrix/test/libmatrix_test.cc (+11/-0) src/libmatrix/test/libmatrix_test.h (+11/-0) src/libmatrix/test/options.cc (+20/-0) src/libmatrix/test/shader_source_test.cc (+49/-0) src/libmatrix/test/shader_source_test.h (+32/-0) src/libmatrix/test/transpose_test.cc (+297/-0) src/libmatrix/test/transpose_test.h (+38/-0) src/libmatrix/util.cc (+165/-0) src/libmatrix/util.h (+71/-0) src/libmatrix/vec.h (+265/-49) src/log.cc (+0/-58) src/log.h (+0/-35) src/shader-source.cc (+0/-625) src/shader-source.h (+0/-113) src/util.cc (+0/-90) src/util.h (+0/-69) |
To merge this branch: | bzr merge lp:~glcompbench-dev/glcompbench/libmatrix-util |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Alexandros Frantzis | Pending | ||
Review via email: mp+90329@code.launchpad.net |
Commit message
Description of the change
libmatrix: Update to reflect current trunk revision (revno 33 of lp:libmatrix). As libmatrix now provides the Log, Util and ShaderSource classes, remove the duplicate copies from glcompbench and rely on the libmatrix ones. This consolidates common code between glcompbench and glmark2.
To post a comment you must log in.
- 77. By Jesse Barker
-
Update local libmatrix to reflect recent changes in lp:libmatrix from glmark2 integration.
- 78. By Jesse Barker
-
Initialize logging object once option parsing is complete to ensure proper debug behavior.
- 79. By Jesse Barker
-
Log, Util: Log::init() gets an appname parameter and Util gets a new member to
generate the simple appname from a full path to the binary (argv[0]). Update
main accordingly.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/glcompbench.cc' |
2 | --- src/glcompbench.cc 2011-12-12 13:16:47 +0000 |
3 | +++ src/glcompbench.cc 2012-01-27 22:25:27 +0000 |
4 | @@ -40,6 +40,7 @@ |
5 | #include "benchmark.h" |
6 | #include "options.h" |
7 | #include "log.h" |
8 | +#include "util.h" |
9 | |
10 | static const char *default_benchmarks[] = { |
11 | "default", |
12 | @@ -148,6 +149,9 @@ |
13 | if (!Options::parse_args(argc, argv)) |
14 | return 1; |
15 | |
16 | + // Initialize Log class |
17 | + Log::init(Util::appname_from_path(argv[0]), Options::show_debug); |
18 | + |
19 | if (Options::show_help || |
20 | Options::backend == Options::BACKEND_NONE) { |
21 | Options::print_help(); |
22 | |
23 | === modified file 'src/libmatrix/Makefile' |
24 | --- src/libmatrix/Makefile 2011-06-22 08:59:00 +0000 |
25 | +++ src/libmatrix/Makefile 2012-01-27 22:25:27 +0000 |
26 | @@ -1,11 +1,14 @@ |
27 | CXXFLAGS = -Wall -Werror -pedantic -O3 |
28 | LIBMATRIX = libmatrix.a |
29 | -LIBSRCS = mat.cc program.cc |
30 | +LIBSRCS = mat.cc program.cc log.cc util.cc shader-source.cc |
31 | LIBOBJS = $(LIBSRCS:.cc=.o) |
32 | TESTDIR = test |
33 | LIBMATRIX_TESTS = $(TESTDIR)/libmatrix_test |
34 | TESTSRCS = $(TESTDIR)/options.cc \ |
35 | + $(TESTDIR)/const_vec_test.cc \ |
36 | $(TESTDIR)/inverse_test.cc \ |
37 | + $(TESTDIR)/transpose_test.cc \ |
38 | + $(TESTDIR)/shader_source_test.cc \ |
39 | $(TESTDIR)/libmatrix_test.cc |
40 | TESTOBJS = $(TESTSRCS:.cc=.o) |
41 | |
42 | @@ -16,15 +19,21 @@ |
43 | # Main library targets here. |
44 | mat.o : mat.cc mat.h vec.h |
45 | program.o: program.cc program.h mat.h vec.h |
46 | -libmatrix.a : mat.o mat.h stack.h vec.h program.o program.h |
47 | +log.o: log.cc log.h |
48 | +util.o: util.cc util.h |
49 | +shader-source.o: shader-source.cc shader-source.h mat.h vec.h |
50 | +libmatrix.a : mat.o stack.h program.o log.o util.o shader-source.o |
51 | $(AR) -r $@ $(LIBOBJS) |
52 | |
53 | # Tests and execution targets here. |
54 | $(TESTDIR)/options.o: $(TESTDIR)/options.cc $(TESTDIR)/libmatrix_test.h |
55 | -$(TESTDIR)/libmatrix_test.o: $(TESTDIR)/libmatrix_test.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/inverse_test.h |
56 | +$(TESTDIR)/libmatrix_test.o: $(TESTDIR)/libmatrix_test.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/inverse_test.h $(TESTDIR)/transpose_test.h |
57 | +$(TESTDIR)/const_vec_test.o: $(TESTDIR)/const_vec_test.cc $(TESTDIR)/const_vec_test.h $(TESTDIR)/libmatrix_test.h vec.h |
58 | $(TESTDIR)/inverse_test.o: $(TESTDIR)/inverse_test.cc $(TESTDIR)/inverse_test.h $(TESTDIR)/libmatrix_test.h mat.h |
59 | -$(TESTDIR)/libmatrix_test: $(TESTDIR)/options.o $(TESTDIR)/libmatrix_test.o $(TESTDIR)/inverse_test.o libmatrix.a |
60 | - $(CXX) -o $@ $? |
61 | +$(TESTDIR)/transpose_test.o: $(TESTDIR)/transpose_test.cc $(TESTDIR)/transpose_test.h $(TESTDIR)/libmatrix_test.h mat.h |
62 | +$(TESTDIR)/shader_source_test.o: $(TESTDIR)/shader_source_test.cc $(TESTDIR)/shader_source_test.h $(TESTDIR)/libmatrix_test.h shader-source.h |
63 | +$(TESTDIR)/libmatrix_test: $(TESTOBJS) libmatrix.a |
64 | + $(CXX) -o $@ $^ |
65 | run_tests: $(LIBMATRIX_TESTS) |
66 | $(LIBMATRIX_TESTS) |
67 | clean : |
68 | |
69 | === added file 'src/libmatrix/gl-if.h' |
70 | --- src/libmatrix/gl-if.h 1970-01-01 00:00:00 +0000 |
71 | +++ src/libmatrix/gl-if.h 2012-01-27 22:25:27 +0000 |
72 | @@ -0,0 +1,18 @@ |
73 | +// |
74 | +// Copyright (c) 2012 Linaro Limited |
75 | +// |
76 | +// All rights reserved. This program and the accompanying materials |
77 | +// are made available under the terms of the MIT License which accompanies |
78 | +// this distribution, and is available at |
79 | +// http://www.opensource.org/licenses/mit-license.php |
80 | +// |
81 | +// Contributors: |
82 | +// Jesse Barker - original implementation. |
83 | +// |
84 | +#ifndef GL_IF_H_ |
85 | +#define GL_IF_H_ |
86 | +// Inclusion abstraction to provide project specific interface headers for |
87 | +// whatever flavor of OpenGL(|ES) is appropriate. For core libmatrix, this |
88 | +// is GLEW. |
89 | +#include "gl-headers.h" |
90 | +#endif // GL_IF_H_ |
91 | |
92 | === added file 'src/libmatrix/log.cc' |
93 | --- src/libmatrix/log.cc 1970-01-01 00:00:00 +0000 |
94 | +++ src/libmatrix/log.cc 2012-01-27 22:25:27 +0000 |
95 | @@ -0,0 +1,178 @@ |
96 | +// |
97 | +// Copyright (c) 2010-2012 Linaro Limited |
98 | +// |
99 | +// All rights reserved. This program and the accompanying materials |
100 | +// are made available under the terms of the MIT License which accompanies |
101 | +// this distribution, and is available at |
102 | +// http://www.opensource.org/licenses/mit-license.php |
103 | +// |
104 | +// Contributors: |
105 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
106 | +// Jesse Barker <jesse.barker@linaro.org> |
107 | +// |
108 | +#include <cstdio> |
109 | +#include <cstdarg> |
110 | +#include <string> |
111 | +#include <sstream> |
112 | +#include <iostream> |
113 | +#include "log.h" |
114 | + |
115 | +#ifdef ANDROID |
116 | +#include <android/log.h> |
117 | +#endif |
118 | + |
119 | +using std::string; |
120 | + |
121 | +const string Log::continuation_prefix("\x10"); |
122 | +string Log::appname_; |
123 | +bool Log::do_debug_(false); |
124 | + |
125 | +#ifndef ANDROID |
126 | + |
127 | +static const string terminal_color_normal("\033[0m"); |
128 | +static const string terminal_color_red("\033[1;31m"); |
129 | +static const string terminal_color_cyan("\033[36m"); |
130 | +static const string terminal_color_yellow("\033[33m"); |
131 | +static const string empty; |
132 | + |
133 | +static void |
134 | +print_prefixed_message(std::ostream& stream, const string& color, const string& prefix, |
135 | + const string& fmt, va_list ap) |
136 | +{ |
137 | + va_list aq; |
138 | + |
139 | + /* Estimate message size */ |
140 | + va_copy(aq, ap); |
141 | + int msg_size = vsnprintf(NULL, 0, fmt.c_str(), aq); |
142 | + va_end(aq); |
143 | + |
144 | + /* Create the buffer to hold the message */ |
145 | + char *buf = new char[msg_size + 1]; |
146 | + |
147 | + /* Store the message in the buffer */ |
148 | + va_copy(aq, ap); |
149 | + vsnprintf(buf, msg_size + 1, fmt.c_str(), aq); |
150 | + va_end(aq); |
151 | + |
152 | + /* |
153 | + * Print the message lines prefixed with the supplied prefix. |
154 | + * If the target stream is a terminal make the prefix colored. |
155 | + */ |
156 | + string linePrefix; |
157 | + if (!prefix.empty()) |
158 | + { |
159 | + static const string colon(": "); |
160 | + string start_color; |
161 | + string end_color; |
162 | + if (!color.empty()) |
163 | + { |
164 | + start_color = color; |
165 | + end_color = terminal_color_normal; |
166 | + } |
167 | + linePrefix = start_color + prefix + end_color + colon; |
168 | + } |
169 | + |
170 | + std::string line; |
171 | + std::stringstream ss(buf); |
172 | + |
173 | + while(std::getline(ss, line)) { |
174 | + /* |
175 | + * If this line is a continuation of a previous log message |
176 | + * just print the line plainly. |
177 | + */ |
178 | + if (line[0] == Log::continuation_prefix[0]) { |
179 | + stream << line.c_str() + 1; |
180 | + } |
181 | + else { |
182 | + /* Normal line, emit the prefix. */ |
183 | + stream << linePrefix << line; |
184 | + } |
185 | + |
186 | + /* Only emit a newline if the original message has it. */ |
187 | + if (!(ss.rdstate() & std::stringstream::eofbit)) |
188 | + stream << std::endl; |
189 | + } |
190 | + |
191 | + delete[] buf; |
192 | +} |
193 | + |
194 | +void |
195 | +Log::info(const char *fmt, ...) |
196 | +{ |
197 | + static const string infoprefix("Info"); |
198 | + static const string& infocolor(isatty(fileno(stdout)) ? terminal_color_cyan : empty); |
199 | + va_list ap; |
200 | + va_start(ap, fmt); |
201 | + if (do_debug_) |
202 | + print_prefixed_message(std::cout, infocolor, infoprefix, fmt, ap); |
203 | + else |
204 | + print_prefixed_message(std::cout, empty, empty, fmt, ap); |
205 | + va_end(ap); |
206 | +} |
207 | + |
208 | +void |
209 | +Log::debug(const char *fmt, ...) |
210 | +{ |
211 | + static const string dbgprefix("Debug"); |
212 | + static const string& dbgcolor(isatty(fileno(stdout)) ? terminal_color_yellow : empty); |
213 | + if (!do_debug_) |
214 | + return; |
215 | + va_list ap; |
216 | + va_start(ap, fmt); |
217 | + print_prefixed_message(std::cout, dbgcolor, dbgprefix, fmt, ap); |
218 | + va_end(ap); |
219 | +} |
220 | + |
221 | +void |
222 | +Log::error(const char *fmt, ...) |
223 | +{ |
224 | + static const string errprefix("Error"); |
225 | + static const string& errcolor(isatty(fileno(stderr)) ? terminal_color_red : empty); |
226 | + va_list ap; |
227 | + va_start(ap, fmt); |
228 | + print_prefixed_message(std::cerr, errcolor, errprefix, fmt, ap); |
229 | + va_end(ap); |
230 | +} |
231 | + |
232 | +void |
233 | +Log::flush() |
234 | +{ |
235 | + std::cout.flush(); |
236 | + std::cerr.flush(); |
237 | +} |
238 | +#else |
239 | +void |
240 | +Log::info(const char *fmt, ...) |
241 | +{ |
242 | + va_list ap; |
243 | + va_start(ap, fmt); |
244 | + __android_log_vprint(ANDROID_LOG_INFO, appname_.c_str(), fmt, ap); |
245 | + va_end(ap); |
246 | +} |
247 | + |
248 | +void |
249 | +Log::debug(const char *fmt, ...) |
250 | +{ |
251 | + if (!do_debug_) |
252 | + return; |
253 | + va_list ap; |
254 | + va_start(ap, fmt); |
255 | + __android_log_vprint(ANDROID_LOG_DEBUG, appname_.c_str(), fmt, ap); |
256 | + va_end(ap); |
257 | +} |
258 | + |
259 | +void |
260 | +Log::error(const char *fmt, ...) |
261 | +{ |
262 | + va_list ap; |
263 | + va_start(ap, fmt); |
264 | + __android_log_vprint(ANDROID_LOG_ERROR, appname_.c_str(), fmt, ap); |
265 | + va_end(ap); |
266 | +} |
267 | + |
268 | +void |
269 | +Log::flush() |
270 | +{ |
271 | +} |
272 | + |
273 | +#endif |
274 | |
275 | === added file 'src/libmatrix/log.h' |
276 | --- src/libmatrix/log.h 1970-01-01 00:00:00 +0000 |
277 | +++ src/libmatrix/log.h 2012-01-27 22:25:27 +0000 |
278 | @@ -0,0 +1,46 @@ |
279 | +// |
280 | +// Copyright (c) 2010-2012 Linaro Limited |
281 | +// |
282 | +// All rights reserved. This program and the accompanying materials |
283 | +// are made available under the terms of the MIT License which accompanies |
284 | +// this distribution, and is available at |
285 | +// http://www.opensource.org/licenses/mit-license.php |
286 | +// |
287 | +// Contributors: |
288 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
289 | +// Jesse Barker <jesse.barker@linaro.org> |
290 | +// |
291 | +#ifndef LOG_H_ |
292 | +#define LOG_H_ |
293 | + |
294 | +#include <string> |
295 | + |
296 | +class Log |
297 | +{ |
298 | +public: |
299 | + static void init(const std::string& appname, bool do_debug = false) |
300 | + { |
301 | + appname_ = appname; |
302 | + do_debug_ = do_debug; |
303 | + } |
304 | + // Emit an informational message |
305 | + static void info(const char *fmt, ...); |
306 | + // Emit a debugging message |
307 | + static void debug(const char *fmt, ...); |
308 | + // Emit an error message |
309 | + static void error(const char *fmt, ...); |
310 | + // Explicit flush of the log buffer |
311 | + static void flush(); |
312 | + // A prefix constant that informs the logging infrastructure that the log |
313 | + // message is a continuation of a previous log message to be put on the |
314 | + // same line. |
315 | + static const std::string continuation_prefix; |
316 | +private: |
317 | + // A constant for identifying the log messages as originating from a |
318 | + // particular application. |
319 | + static std::string appname_; |
320 | + // Indicates whether debug level messages should generate any output |
321 | + static bool do_debug_; |
322 | +}; |
323 | + |
324 | +#endif /* LOG_H_ */ |
325 | |
326 | === modified file 'src/libmatrix/mat.h' |
327 | --- src/libmatrix/mat.h 2011-06-22 08:59:00 +0000 |
328 | +++ src/libmatrix/mat.h 2012-01-27 22:25:27 +0000 |
329 | @@ -46,6 +46,11 @@ |
330 | // However, the internal data representation is column-major, so when using |
331 | // the raw data access member to treat the data as a singly-dimensioned array, |
332 | // it does not have to be transposed. |
333 | +// |
334 | +// A template class for creating, managing and operating on a 2x2 matrix |
335 | +// of any type you like (intended for built-in types, but as long as it |
336 | +// supports the basic arithmetic and assignment operators, any type should |
337 | +// work). |
338 | template<typename T> |
339 | class tmat2 |
340 | { |
341 | @@ -70,6 +75,7 @@ |
342 | } |
343 | ~tmat2() {} |
344 | |
345 | + // Reset this to the identity matrix. |
346 | void setIdentity() |
347 | { |
348 | m_[0] = 1; |
349 | @@ -78,6 +84,7 @@ |
350 | m_[3] = 1; |
351 | } |
352 | |
353 | + // Transpose this. Return a reference to this. |
354 | tmat2& transpose() |
355 | { |
356 | T tmp_val = m_[1]; |
357 | @@ -86,11 +93,16 @@ |
358 | return *this; |
359 | } |
360 | |
361 | + // Compute the determinant of this and return it. |
362 | T determinant() |
363 | { |
364 | return (m_[0] * m_[3]) - (m_[2] * m_[1]); |
365 | } |
366 | |
367 | + // Invert this. Return a reference to this. |
368 | + // |
369 | + // NOTE: If this is non-invertible, we will |
370 | + // throw to avoid undefined behavior. |
371 | tmat2& inverse() throw(std::runtime_error) |
372 | { |
373 | T d(determinant()); |
374 | @@ -109,6 +121,8 @@ |
375 | return *this; |
376 | } |
377 | |
378 | + // Print the elements of the matrix to standard out. |
379 | + // Really only useful for debug and test. |
380 | void print() const |
381 | { |
382 | static const int precision(6); |
383 | @@ -126,8 +140,12 @@ |
384 | std::cout << " |" << std::endl; |
385 | } |
386 | |
387 | + // Allow raw data access for API calls and the like. |
388 | + // For example, it is valid to pass a tmat2<float> into a call to |
389 | + // the OpenGL command "glUniformMatrix2fv()". |
390 | operator const T*() const { return &m_[0];} |
391 | |
392 | + // Test if 'rhs' is equal to this. |
393 | bool operator==(const tmat2& rhs) const |
394 | { |
395 | return m_[0] == rhs.m_[0] && |
396 | @@ -136,11 +154,13 @@ |
397 | m_[3] == rhs.m_[3]; |
398 | } |
399 | |
400 | + // Test if 'rhs' is not equal to this. |
401 | bool operator!=(const tmat2& rhs) const |
402 | { |
403 | return !(*this == rhs); |
404 | } |
405 | |
406 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
407 | tmat2& operator=(const tmat2& rhs) |
408 | { |
409 | if (this != &rhs) |
410 | @@ -153,6 +173,7 @@ |
411 | return *this; |
412 | } |
413 | |
414 | + // Add another matrix to this. Return a reference to this. |
415 | tmat2& operator+=(const tmat2& rhs) |
416 | { |
417 | m_[0] += rhs.m_[0]; |
418 | @@ -162,11 +183,13 @@ |
419 | return *this; |
420 | } |
421 | |
422 | + // Add another matrix to a copy of this. Return the copy. |
423 | const tmat2 operator+(const tmat2& rhs) |
424 | { |
425 | return tmat2(*this) += rhs; |
426 | } |
427 | |
428 | + // Subtract another matrix from this. Return a reference to this. |
429 | tmat2& operator-=(const tmat2& rhs) |
430 | { |
431 | m_[0] -= rhs.m_[0]; |
432 | @@ -176,11 +199,13 @@ |
433 | return *this; |
434 | } |
435 | |
436 | + // Subtract another matrix from a copy of this. Return the copy. |
437 | const tmat2 operator-(const tmat2& rhs) |
438 | { |
439 | return tmat2(*this) += rhs; |
440 | } |
441 | |
442 | + // Multiply this by another matrix. Return a reference to this. |
443 | tmat2& operator*=(const tmat2& rhs) |
444 | { |
445 | T c0r0((m_[0] * rhs.m_[0]) + (m_[2] * rhs.m_[1])); |
446 | @@ -194,11 +219,13 @@ |
447 | return *this; |
448 | } |
449 | |
450 | + // Multiply a copy of this by another matrix. Return the copy. |
451 | const tmat2 operator*(const tmat2& rhs) |
452 | { |
453 | return tmat2(*this) *= rhs; |
454 | } |
455 | |
456 | + // Multiply this by a scalar. Return a reference to this. |
457 | tmat2& operator*=(const T& rhs) |
458 | { |
459 | m_[0] *= rhs; |
460 | @@ -208,11 +235,13 @@ |
461 | return *this; |
462 | } |
463 | |
464 | + // Multiply a copy of this by a scalar. Return the copy. |
465 | const tmat2 operator*(const T& rhs) |
466 | { |
467 | return tmat2(*this) *= rhs; |
468 | } |
469 | |
470 | + // Divide this by a scalar. Return a reference to this. |
471 | tmat2& operator/=(const T& rhs) |
472 | { |
473 | m_[0] /= rhs; |
474 | @@ -222,11 +251,15 @@ |
475 | return *this; |
476 | } |
477 | |
478 | + // Divide a copy of this by a scalar. Return the copy. |
479 | const tmat2 operator/(const T& rhs) |
480 | { |
481 | return tmat2(*this) /= rhs; |
482 | } |
483 | |
484 | + // Use an instance of the ArrayProxy class to support double-indexed |
485 | + // references to a matrix (i.e., m[1][1]). See comments above the |
486 | + // ArrayProxy definition for more details. |
487 | ArrayProxy<T, 2> operator[](int index) |
488 | { |
489 | return ArrayProxy<T, 2>(&m_[index]); |
490 | @@ -240,12 +273,16 @@ |
491 | T m_[4]; |
492 | }; |
493 | |
494 | +// Multiply a scalar and a matrix just like the member operator, but allow |
495 | +// the scalar to be the left-hand operand. |
496 | template<typename T> |
497 | const tmat2<T> operator*(const T& lhs, const tmat2<T>& rhs) |
498 | { |
499 | return tmat2<T>(rhs) * lhs; |
500 | } |
501 | |
502 | +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). |
503 | +// Return the copy. |
504 | template<typename T> |
505 | const tvec2<T> operator*(const tvec2<T>& lhs, const tmat2<T>& rhs) |
506 | { |
507 | @@ -254,6 +291,8 @@ |
508 | return tvec2<T>(x,y); |
509 | } |
510 | |
511 | +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). |
512 | +// Return the copy. |
513 | template<typename T> |
514 | const tvec2<T> operator*(const tmat2<T>& lhs, const tvec2<T>& rhs) |
515 | { |
516 | @@ -262,6 +301,7 @@ |
517 | return tvec2<T>(x, y); |
518 | } |
519 | |
520 | +// Compute the outer product of two vectors. Return the resultant matrix. |
521 | template<typename T> |
522 | const tmat2<T> outer(const tvec2<T>& a, const tvec2<T>& b) |
523 | { |
524 | @@ -273,6 +313,10 @@ |
525 | return product; |
526 | } |
527 | |
528 | +// A template class for creating, managing and operating on a 3x3 matrix |
529 | +// of any type you like (intended for built-in types, but as long as it |
530 | +// supports the basic arithmetic and assignment operators, any type should |
531 | +// work). |
532 | template<typename T> |
533 | class tmat3 |
534 | { |
535 | @@ -294,8 +338,8 @@ |
536 | m_[8] = m.m_[8]; |
537 | } |
538 | tmat3(const T& c0r0, const T& c0r1, const T& c0r2, |
539 | - const T& c1r0, const T& c1r1, const T& c1r2, |
540 | - const T& c2r0, const T& c2r1, const T& c2r2) |
541 | + const T& c1r0, const T& c1r1, const T& c1r2, |
542 | + const T& c2r0, const T& c2r1, const T& c2r2) |
543 | { |
544 | m_[0] = c0r0; |
545 | m_[1] = c0r1; |
546 | @@ -309,6 +353,7 @@ |
547 | } |
548 | ~tmat3() {} |
549 | |
550 | + // Reset this to the identity matrix. |
551 | void setIdentity() |
552 | { |
553 | m_[0] = 1; |
554 | @@ -322,6 +367,7 @@ |
555 | m_[8] = 1; |
556 | } |
557 | |
558 | + // Transpose this. Return a reference to this. |
559 | tmat3& transpose() |
560 | { |
561 | T tmp_val = m_[1]; |
562 | @@ -336,6 +382,7 @@ |
563 | return *this; |
564 | } |
565 | |
566 | + // Compute the determinant of this and return it. |
567 | T determinant() |
568 | { |
569 | tmat2<T> minor0(m_[4], m_[5], m_[7], m_[8]); |
570 | @@ -346,6 +393,10 @@ |
571 | (m_[6] * minor6.determinant()); |
572 | } |
573 | |
574 | + // Invert this. Return a reference to this. |
575 | + // |
576 | + // NOTE: If this is non-invertible, we will |
577 | + // throw to avoid undefined behavior. |
578 | tmat3& inverse() throw(std::runtime_error) |
579 | { |
580 | T d(determinant()); |
581 | @@ -374,6 +425,8 @@ |
582 | return *this; |
583 | } |
584 | |
585 | + // Print the elements of the matrix to standard out. |
586 | + // Really only useful for debug and test. |
587 | void print() const |
588 | { |
589 | static const int precision(6); |
590 | @@ -403,8 +456,12 @@ |
591 | std::cout << " |" << std::endl; |
592 | } |
593 | |
594 | + // Allow raw data access for API calls and the like. |
595 | + // For example, it is valid to pass a tmat3<float> into a call to |
596 | + // the OpenGL command "glUniformMatrix3fv()". |
597 | operator const T*() const { return &m_[0];} |
598 | |
599 | + // Test if 'rhs' is equal to this. |
600 | bool operator==(const tmat3& rhs) const |
601 | { |
602 | return m_[0] == rhs.m_[0] && |
603 | @@ -418,11 +475,13 @@ |
604 | m_[8] == rhs.m_[8]; |
605 | } |
606 | |
607 | + // Test if 'rhs' is not equal to this. |
608 | bool operator!=(const tmat3& rhs) const |
609 | { |
610 | return !(*this == rhs); |
611 | } |
612 | |
613 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
614 | tmat3& operator=(const tmat3& rhs) |
615 | { |
616 | if (this != &rhs) |
617 | @@ -440,6 +499,7 @@ |
618 | return *this; |
619 | } |
620 | |
621 | + // Add another matrix to this. Return a reference to this. |
622 | tmat3& operator+=(const tmat3& rhs) |
623 | { |
624 | m_[0] += rhs.m_[0]; |
625 | @@ -454,11 +514,13 @@ |
626 | return *this; |
627 | } |
628 | |
629 | + // Add another matrix to a copy of this. Return the copy. |
630 | const tmat3 operator+(const tmat3& rhs) |
631 | { |
632 | return tmat3(*this) += rhs; |
633 | } |
634 | |
635 | + // Subtract another matrix from this. Return a reference to this. |
636 | tmat3& operator-=(const tmat3& rhs) |
637 | { |
638 | m_[0] -= rhs.m_[0]; |
639 | @@ -473,11 +535,13 @@ |
640 | return *this; |
641 | } |
642 | |
643 | + // Subtract another matrix from a copy of this. Return the copy. |
644 | const tmat3 operator-(const tmat3& rhs) |
645 | { |
646 | return tmat3(*this) -= rhs; |
647 | } |
648 | |
649 | + // Multiply this by another matrix. Return a reference to this. |
650 | tmat3& operator*=(const tmat3& rhs) |
651 | { |
652 | T c0r0((m_[0] * rhs.m_[0]) + (m_[3] * rhs.m_[1]) + (m_[6] * rhs.m_[2])); |
653 | @@ -501,11 +565,13 @@ |
654 | return *this; |
655 | } |
656 | |
657 | + // Multiply a copy of this by another matrix. Return the copy. |
658 | const tmat3 operator*(const tmat3& rhs) |
659 | { |
660 | return tmat3(*this) *= rhs; |
661 | } |
662 | |
663 | + // Multiply this by a scalar. Return a reference to this. |
664 | tmat3& operator*=(const T& rhs) |
665 | { |
666 | m_[0] *= rhs; |
667 | @@ -520,11 +586,13 @@ |
668 | return *this; |
669 | } |
670 | |
671 | + // Multiply a copy of this by a scalar. Return the copy. |
672 | const tmat3 operator*(const T& rhs) |
673 | { |
674 | return tmat3(*this) *= rhs; |
675 | } |
676 | |
677 | + // Divide this by a scalar. Return a reference to this. |
678 | tmat3& operator/=(const T& rhs) |
679 | { |
680 | m_[0] /= rhs; |
681 | @@ -539,11 +607,15 @@ |
682 | return *this; |
683 | } |
684 | |
685 | + // Divide a copy of this by a scalar. Return the copy. |
686 | const tmat3 operator/(const T& rhs) |
687 | { |
688 | return tmat3(*this) /= rhs; |
689 | } |
690 | |
691 | + // Use an instance of the ArrayProxy class to support double-indexed |
692 | + // references to a matrix (i.e., m[1][1]). See comments above the |
693 | + // ArrayProxy definition for more details. |
694 | ArrayProxy<T, 3> operator[](int index) |
695 | { |
696 | return ArrayProxy<T, 3>(&m_[index]); |
697 | @@ -557,12 +629,16 @@ |
698 | T m_[9]; |
699 | }; |
700 | |
701 | +// Multiply a scalar and a matrix just like the member operator, but allow |
702 | +// the scalar to be the left-hand operand. |
703 | template<typename T> |
704 | const tmat3<T> operator*(const T& lhs, const tmat3<T>& rhs) |
705 | { |
706 | return tmat3<T>(rhs) * lhs; |
707 | } |
708 | |
709 | +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). |
710 | +// Return the copy. |
711 | template<typename T> |
712 | const tvec3<T> operator*(const tvec3<T>& lhs, const tmat3<T>& rhs) |
713 | { |
714 | @@ -572,6 +648,8 @@ |
715 | return tvec3<T>(x, y, z); |
716 | } |
717 | |
718 | +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). |
719 | +// Return the copy. |
720 | template<typename T> |
721 | const tvec3<T> operator*(const tmat3<T>& lhs, const tvec3<T>& rhs) |
722 | { |
723 | @@ -581,6 +659,7 @@ |
724 | return tvec3<T>(x, y, z); |
725 | } |
726 | |
727 | +// Compute the outer product of two vectors. Return the resultant matrix. |
728 | template<typename T> |
729 | const tmat3<T> outer(const tvec3<T>& a, const tvec3<T>& b) |
730 | { |
731 | @@ -597,6 +676,10 @@ |
732 | return product; |
733 | } |
734 | |
735 | +// A template class for creating, managing and operating on a 4x4 matrix |
736 | +// of any type you like (intended for built-in types, but as long as it |
737 | +// supports the basic arithmetic and assignment operators, any type should |
738 | +// work). |
739 | template<typename T> |
740 | class tmat4 |
741 | { |
742 | @@ -626,6 +709,7 @@ |
743 | } |
744 | ~tmat4() {} |
745 | |
746 | + // Reset this to the identity matrix. |
747 | void setIdentity() |
748 | { |
749 | m_[0] = 1; |
750 | @@ -646,6 +730,7 @@ |
751 | m_[15] = 1; |
752 | } |
753 | |
754 | + // Transpose this. Return a reference to this. |
755 | tmat4& transpose() |
756 | { |
757 | T tmp_val = m_[1]; |
758 | @@ -669,6 +754,7 @@ |
759 | return *this; |
760 | } |
761 | |
762 | + // Compute the determinant of this and return it. |
763 | T determinant() |
764 | { |
765 | tmat3<T> minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); |
766 | @@ -681,6 +767,10 @@ |
767 | (m_[12] * minor12.determinant()); |
768 | } |
769 | |
770 | + // Invert this. Return a reference to this. |
771 | + // |
772 | + // NOTE: If this is non-invertible, we will |
773 | + // throw to avoid undefined behavior. |
774 | tmat4& inverse() throw(std::runtime_error) |
775 | { |
776 | T d(determinant()); |
777 | @@ -726,6 +816,8 @@ |
778 | return *this; |
779 | } |
780 | |
781 | + // Print the elements of the matrix to standard out. |
782 | + // Really only useful for debug and test. |
783 | void print() const |
784 | { |
785 | static const int precision(6); |
786 | @@ -771,8 +863,12 @@ |
787 | std::cout << " |" << std::endl; |
788 | } |
789 | |
790 | + // Allow raw data access for API calls and the like. |
791 | + // For example, it is valid to pass a tmat4<float> into a call to |
792 | + // the OpenGL command "glUniformMatrix4fv()". |
793 | operator const T*() const { return &m_[0];} |
794 | |
795 | + // Test if 'rhs' is equal to this. |
796 | bool operator==(const tmat4& rhs) const |
797 | { |
798 | return m_[0] == rhs.m_[0] && |
799 | @@ -793,11 +889,13 @@ |
800 | m_[15] == rhs.m_[15]; |
801 | } |
802 | |
803 | + // Test if 'rhs' is not equal to this. |
804 | bool operator!=(const tmat4& rhs) const |
805 | { |
806 | return !(*this == rhs); |
807 | } |
808 | |
809 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
810 | tmat4& operator=(const tmat4& rhs) |
811 | { |
812 | if (this != &rhs) |
813 | @@ -822,6 +920,7 @@ |
814 | return *this; |
815 | } |
816 | |
817 | + // Add another matrix to this. Return a reference to this. |
818 | tmat4& operator+=(const tmat4& rhs) |
819 | { |
820 | m_[0] += rhs.m_[0]; |
821 | @@ -843,11 +942,13 @@ |
822 | return *this; |
823 | } |
824 | |
825 | + // Add another matrix to a copy of this. Return the copy. |
826 | const tmat4 operator+(const tmat4& rhs) |
827 | { |
828 | return tmat4(*this) += rhs; |
829 | } |
830 | |
831 | + // Subtract another matrix from this. Return a reference to this. |
832 | tmat4& operator-=(const tmat4& rhs) |
833 | { |
834 | m_[0] -= rhs.m_[0]; |
835 | @@ -869,11 +970,13 @@ |
836 | return *this; |
837 | } |
838 | |
839 | + // Subtract another matrix from a copy of this. Return the copy. |
840 | const tmat4 operator-(const tmat4& rhs) |
841 | { |
842 | return tmat4(*this) -= rhs; |
843 | } |
844 | |
845 | + // Multiply this by another matrix. Return a reference to this. |
846 | tmat4& operator*=(const tmat4& rhs) |
847 | { |
848 | T c0r0((m_[0] * rhs.m_[0]) + (m_[4] * rhs.m_[1]) + (m_[8] * rhs.m_[2]) + (m_[12] * rhs.m_[3])); |
849 | @@ -911,11 +1014,13 @@ |
850 | return *this; |
851 | } |
852 | |
853 | + // Multiply a copy of this by another matrix. Return the copy. |
854 | const tmat4 operator*(const tmat4& rhs) |
855 | { |
856 | return tmat4(*this) *= rhs; |
857 | } |
858 | |
859 | + // Multiply this by a scalar. Return a reference to this. |
860 | tmat4& operator*=(const T& rhs) |
861 | { |
862 | m_[0] *= rhs; |
863 | @@ -937,11 +1042,13 @@ |
864 | return *this; |
865 | } |
866 | |
867 | + // Multiply a copy of this by a scalar. Return the copy. |
868 | const tmat4 operator*(const T& rhs) |
869 | { |
870 | return tmat4(*this) *= rhs; |
871 | } |
872 | |
873 | + // Divide this by a scalar. Return a reference to this. |
874 | tmat4& operator/=(const T& rhs) |
875 | { |
876 | m_[0] /= rhs; |
877 | @@ -963,11 +1070,15 @@ |
878 | return *this; |
879 | } |
880 | |
881 | + // Divide a copy of this by a scalar. Return the copy. |
882 | const tmat4 operator/(const T& rhs) |
883 | { |
884 | return tmat4(*this) /= rhs; |
885 | } |
886 | |
887 | + // Use an instance of the ArrayProxy class to support double-indexed |
888 | + // references to a matrix (i.e., m[1][1]). See comments above the |
889 | + // ArrayProxy definition for more details. |
890 | ArrayProxy<T, 4> operator[](int index) |
891 | { |
892 | return ArrayProxy<T, 4>(&m_[index]); |
893 | @@ -981,12 +1092,16 @@ |
894 | T m_[16]; |
895 | }; |
896 | |
897 | +// Multiply a scalar and a matrix just like the member operator, but allow |
898 | +// the scalar to be the left-hand operand. |
899 | template<typename T> |
900 | const tmat4<T> operator*(const T& lhs, const tmat4<T>& rhs) |
901 | { |
902 | return tmat4<T>(rhs) * lhs; |
903 | } |
904 | |
905 | +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). |
906 | +// Return the copy. |
907 | template<typename T> |
908 | const tvec4<T> operator*(const tvec4<T>& lhs, const tmat4<T>& rhs) |
909 | { |
910 | @@ -997,6 +1112,8 @@ |
911 | return tvec4<T>(x, y, z, w); |
912 | } |
913 | |
914 | +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). |
915 | +// Return the copy. |
916 | template<typename T> |
917 | const tvec4<T> operator*(const tmat4<T>& lhs, const tvec4<T>& rhs) |
918 | { |
919 | @@ -1007,6 +1124,7 @@ |
920 | return tvec4<T>(x, y, z, w); |
921 | } |
922 | |
923 | +// Compute the outer product of two vectors. Return the resultant matrix. |
924 | template<typename T> |
925 | const tmat4<T> outer(const tvec4<T>& a, const tvec4<T>& b) |
926 | { |
927 | |
928 | === modified file 'src/libmatrix/program.cc' |
929 | --- src/libmatrix/program.cc 2011-09-05 15:23:11 +0000 |
930 | +++ src/libmatrix/program.cc 2012-01-27 22:25:27 +0000 |
931 | @@ -1,5 +1,5 @@ |
932 | // |
933 | -// Copyright (c) 2011 Linaro Limited |
934 | +// Copyright (c) 2011-2012 Linaro Limited |
935 | // |
936 | // All rights reserved. This program and the accompanying materials |
937 | // are made available under the terms of the MIT License which accompanies |
938 | @@ -8,16 +8,15 @@ |
939 | // |
940 | // Contributors: |
941 | // Jesse Barker - original implementation. |
942 | -// Alexandros Frantzis - local changes for better integration with glcompbench |
943 | +// Alexandros Frantzis - local integration changes |
944 | // |
945 | #include <string> |
946 | #include <vector> |
947 | #include <sstream> |
948 | #include <fstream> |
949 | #include <iostream> |
950 | -#include "gl-headers.h" |
951 | +#include "gl-if.h" |
952 | #include "program.h" |
953 | -#include "log.h" |
954 | |
955 | using std::string; |
956 | using LibMatrix::mat4; |
957 | @@ -25,27 +24,6 @@ |
958 | using LibMatrix::vec3; |
959 | using LibMatrix::vec4; |
960 | |
961 | -bool |
962 | -gotSource(const string& filename, string& source) |
963 | -{ |
964 | - using std::ifstream; |
965 | - ifstream inputFile(filename.c_str()); |
966 | - if (!inputFile) |
967 | - { |
968 | - std::cerr << "Failed to open \"" << filename << "\"" << std::endl; |
969 | - return false; |
970 | - } |
971 | - |
972 | - string curLine; |
973 | - while (getline(inputFile, curLine)) |
974 | - { |
975 | - source += curLine; |
976 | - source += '\n'; |
977 | - } |
978 | - |
979 | - return true; |
980 | -} |
981 | - |
982 | Shader::Shader(unsigned int type, const string& source) : |
983 | handle_(0), |
984 | type_(type), |
985 | |
986 | === modified file 'src/libmatrix/program.h' |
987 | --- src/libmatrix/program.h 2011-09-05 15:23:11 +0000 |
988 | +++ src/libmatrix/program.h 2012-01-27 22:25:27 +0000 |
989 | @@ -1,5 +1,5 @@ |
990 | // |
991 | -// Copyright (c) 2011 Linaro Limited |
992 | +// Copyright (c) 2011-2012 Linaro Limited |
993 | // |
994 | // All rights reserved. This program and the accompanying materials |
995 | // are made available under the terms of the MIT License which accompanies |
996 | @@ -8,6 +8,7 @@ |
997 | // |
998 | // Contributors: |
999 | // Jesse Barker - original implementation. |
1000 | +// Alexandros Frantzis - local integration changes |
1001 | // |
1002 | #ifndef PROGRAM_H_ |
1003 | #define PROGRAM_H_ |
1004 | @@ -161,7 +162,4 @@ |
1005 | bool valid_; |
1006 | }; |
1007 | |
1008 | -// Handy utility for extracting shader source from a named file |
1009 | -bool gotSource(const std::string& filename, std::string& sourceOut); |
1010 | - |
1011 | #endif // PROGRAM_H_ |
1012 | |
1013 | === added file 'src/libmatrix/shader-source.cc' |
1014 | --- src/libmatrix/shader-source.cc 1970-01-01 00:00:00 +0000 |
1015 | +++ src/libmatrix/shader-source.cc 2012-01-27 22:25:27 +0000 |
1016 | @@ -0,0 +1,615 @@ |
1017 | +// |
1018 | +// Copyright (c) 2010-2012 Linaro Limited |
1019 | +// |
1020 | +// All rights reserved. This program and the accompanying materials |
1021 | +// are made available under the terms of the MIT License which accompanies |
1022 | +// this distribution, and is available at |
1023 | +// http://www.opensource.org/licenses/mit-license.php |
1024 | +// |
1025 | +// Contributors: |
1026 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
1027 | +// Jesse Barker <jesse.barker@linaro.org> |
1028 | +// |
1029 | +#include <istream> |
1030 | +#include <memory> |
1031 | + |
1032 | +#include "shader-source.h" |
1033 | +#include "log.h" |
1034 | +#include "vec.h" |
1035 | +#include "util.h" |
1036 | + |
1037 | +/** |
1038 | + * Holds default precision values for all shader types |
1039 | + * (even the unknown type, which is hardwired to default precision values) |
1040 | + */ |
1041 | +std::vector<ShaderSource::Precision> |
1042 | +ShaderSource::default_precision_(ShaderSource::ShaderTypeUnknown + 1); |
1043 | + |
1044 | +/** |
1045 | + * Loads the contents of a file into a string. |
1046 | + * |
1047 | + * @param filename the name of the file |
1048 | + * @param str the string to put the contents of the file into |
1049 | + */ |
1050 | +bool |
1051 | +ShaderSource::load_file(const std::string& filename, std::string& str) |
1052 | +{ |
1053 | + std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename)); |
1054 | + std::istream& inputFile(*is_ptr); |
1055 | + |
1056 | + if (!inputFile) |
1057 | + { |
1058 | + Log::error("Failed to open \"%s\"\n", filename.c_str()); |
1059 | + return false; |
1060 | + } |
1061 | + |
1062 | + std::string curLine; |
1063 | + while (getline(inputFile, curLine)) |
1064 | + { |
1065 | + str += curLine; |
1066 | + str += '\n'; |
1067 | + } |
1068 | + |
1069 | + return true; |
1070 | +} |
1071 | + |
1072 | + |
1073 | +/** |
1074 | + * Appends a string to the shader source. |
1075 | + * |
1076 | + * @param str the string to append |
1077 | + */ |
1078 | +void |
1079 | +ShaderSource::append(const std::string &str) |
1080 | +{ |
1081 | + source_ << str; |
1082 | +} |
1083 | + |
1084 | +/** |
1085 | + * Appends the contents of a file to the shader source. |
1086 | + * |
1087 | + * @param filename the name of the file to append |
1088 | + */ |
1089 | +void |
1090 | +ShaderSource::append_file(const std::string &filename) |
1091 | +{ |
1092 | + std::string source; |
1093 | + if (load_file(filename, source)) |
1094 | + source_ << source; |
1095 | +} |
1096 | + |
1097 | +/** |
1098 | + * Replaces a string in the source with another string. |
1099 | + * |
1100 | + * @param remove the string to replace |
1101 | + * @param insert the string to replace with |
1102 | + */ |
1103 | +void |
1104 | +ShaderSource::replace(const std::string &remove, const std::string &insert) |
1105 | +{ |
1106 | + std::string::size_type pos = 0; |
1107 | + std::string str(source_.str()); |
1108 | + |
1109 | + while ((pos = str.find(remove, pos)) != std::string::npos) { |
1110 | + str.replace(pos, remove.size(), insert); |
1111 | + pos++; |
1112 | + } |
1113 | + |
1114 | + source_.clear(); |
1115 | + source_.str(str); |
1116 | +} |
1117 | + |
1118 | +/** |
1119 | + * Replaces a string in the source with the contents of a file. |
1120 | + * |
1121 | + * @param remove the string to replace |
1122 | + * @param filename the name of the file to read from |
1123 | + */ |
1124 | +void |
1125 | +ShaderSource::replace_with_file(const std::string &remove, const std::string &filename) |
1126 | +{ |
1127 | + std::string source; |
1128 | + if (load_file(filename, source)) |
1129 | + replace(remove, source); |
1130 | +} |
1131 | + |
1132 | +/** |
1133 | + * Adds a string (usually containing a constant definition) at |
1134 | + * global (per shader) scope. |
1135 | + * |
1136 | + * The string is placed after any default precision qualifiers. |
1137 | + * |
1138 | + * @param str the string to add |
1139 | + */ |
1140 | +void |
1141 | +ShaderSource::add_global(const std::string &str) |
1142 | +{ |
1143 | + std::string::size_type pos = 0; |
1144 | + std::string source(source_.str()); |
1145 | + |
1146 | + /* Find the last precision qualifier */ |
1147 | + pos = source.rfind("precision"); |
1148 | + |
1149 | + if (pos != std::string::npos) { |
1150 | + /* |
1151 | + * Find the next #endif line of a preprocessor block that contains |
1152 | + * the precision qualifier. |
1153 | + */ |
1154 | + std::string::size_type pos_if = source.find("#if", pos); |
1155 | + std::string::size_type pos_endif = source.find("#endif", pos); |
1156 | + |
1157 | + if (pos_endif != std::string::npos && pos_endif < pos_if) |
1158 | + pos = pos_endif; |
1159 | + |
1160 | + /* Go to the next line */ |
1161 | + pos = source.find("\n", pos); |
1162 | + if (pos != std::string::npos) |
1163 | + pos++; |
1164 | + } |
1165 | + else |
1166 | + pos = 0; |
1167 | + |
1168 | + source.insert(pos, str); |
1169 | + |
1170 | + source_.clear(); |
1171 | + source_.str(source); |
1172 | +} |
1173 | + |
1174 | +/** |
1175 | + * Adds a string (usually containing a constant definition) at |
1176 | + * global (per shader) scope. |
1177 | + * |
1178 | + * The string is placed after any default precision qualifiers. |
1179 | + * |
1180 | + * @param function the function to add the string into |
1181 | + * @param str the string to add |
1182 | + */ |
1183 | +void |
1184 | +ShaderSource::add_local(const std::string &str, const std::string &function) |
1185 | +{ |
1186 | + std::string::size_type pos = 0; |
1187 | + std::string source(source_.str()); |
1188 | + |
1189 | + /* Find the function */ |
1190 | + pos = source.find(function); |
1191 | + pos = source.find('{', pos); |
1192 | + |
1193 | + /* Go to the next line */ |
1194 | + pos = source.find("\n", pos); |
1195 | + if (pos != std::string::npos) |
1196 | + pos++; |
1197 | + |
1198 | + source.insert(pos, str); |
1199 | + |
1200 | + source_.clear(); |
1201 | + source_.str(source); |
1202 | +} |
1203 | + |
1204 | +/** |
1205 | + * Adds a string (usually containing a constant definition) to a shader source |
1206 | + * |
1207 | + * If the function parameter is empty, the string will be added to global |
1208 | + * scope, after any precision definitions. |
1209 | + * |
1210 | + * @param str the string to add |
1211 | + * @param function if not empty, the function to add the string into |
1212 | + */ |
1213 | +void |
1214 | +ShaderSource::add(const std::string &str, const std::string &function) |
1215 | +{ |
1216 | + if (!function.empty()) |
1217 | + add_local(str, function); |
1218 | + else |
1219 | + add_global(str); |
1220 | +} |
1221 | + |
1222 | +/** |
1223 | + * Adds a float constant definition. |
1224 | + * |
1225 | + * @param name the name of the constant |
1226 | + * @param f the value of the constant |
1227 | + * @param function if not empty, the function to put the definition in |
1228 | + */ |
1229 | +void |
1230 | +ShaderSource::add_const(const std::string &name, float f, |
1231 | + const std::string &function) |
1232 | +{ |
1233 | + std::stringstream ss; |
1234 | + |
1235 | + ss << "const float " << name << " = " << std::fixed << f << ";" << std::endl; |
1236 | + |
1237 | + add(ss.str(), function); |
1238 | +} |
1239 | + |
1240 | +/** |
1241 | + * Adds a float array constant definition. |
1242 | + * |
1243 | + * Note that various GLSL versions (including ES) don't support |
1244 | + * array constants. |
1245 | + * |
1246 | + * @param name the name of the constant |
1247 | + * @param v the value of the constant |
1248 | + * @param function if not empty, the function to put the definition in |
1249 | + */ |
1250 | +void |
1251 | +ShaderSource::add_const(const std::string &name, std::vector<float> &array, |
1252 | + const std::string &function) |
1253 | +{ |
1254 | + std::stringstream ss; |
1255 | + |
1256 | + ss << "const float " << name << "[" << array.size() << "] = {" << std::fixed; |
1257 | + for(std::vector<float>::const_iterator iter = array.begin(); |
1258 | + iter != array.end(); |
1259 | + iter++) |
1260 | + { |
1261 | + ss << *iter; |
1262 | + if (iter + 1 != array.end()) |
1263 | + ss << ", " << std::endl; |
1264 | + } |
1265 | + |
1266 | + ss << "};" << std::endl; |
1267 | + |
1268 | + add(ss.str(), function); |
1269 | +} |
1270 | + |
1271 | +/** |
1272 | + * Adds a vec2 constant definition. |
1273 | + * |
1274 | + * @param name the name of the constant |
1275 | + * @param v the value of the constant |
1276 | + * @param function if not empty, the function to put the definition in |
1277 | + */ |
1278 | +void |
1279 | +ShaderSource::add_const(const std::string &name, const LibMatrix::vec2 &v, |
1280 | + const std::string &function) |
1281 | +{ |
1282 | + std::stringstream ss; |
1283 | + |
1284 | + ss << "const vec2 " << name << " = vec2(" << std::fixed; |
1285 | + ss << v.x() << ", " << v.y() << ");" << std::endl; |
1286 | + |
1287 | + add(ss.str(), function); |
1288 | +} |
1289 | + |
1290 | +/** |
1291 | + * Adds a vec3 constant definition. |
1292 | + * |
1293 | + * @param name the name of the constant |
1294 | + * @param v the value of the constant |
1295 | + * @param function if not empty, the function to put the definition in |
1296 | + */ |
1297 | +void |
1298 | +ShaderSource::add_const(const std::string &name, const LibMatrix::vec3 &v, |
1299 | + const std::string &function) |
1300 | +{ |
1301 | + std::stringstream ss; |
1302 | + |
1303 | + ss << "const vec3 " << name << " = vec3(" << std::fixed; |
1304 | + ss << v.x() << ", " << v.y() << ", " << v.z() << ");" << std::endl; |
1305 | + |
1306 | + add(ss.str(), function); |
1307 | +} |
1308 | + |
1309 | +/** |
1310 | + * Adds a vec4 constant definition. |
1311 | + * |
1312 | + * @param name the name of the constant |
1313 | + * @param v the value of the constant |
1314 | + * @param function if not empty, the function to put the definition in |
1315 | + */ |
1316 | +void |
1317 | +ShaderSource::add_const(const std::string &name, const LibMatrix::vec4 &v, |
1318 | + const std::string &function) |
1319 | +{ |
1320 | + std::stringstream ss; |
1321 | + |
1322 | + ss << "const vec4 " << name << " = vec4(" << std::fixed; |
1323 | + ss << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ");" << std::endl; |
1324 | + |
1325 | + add(ss.str(), function); |
1326 | +} |
1327 | + |
1328 | +/** |
1329 | + * Adds a mat3 constant definition. |
1330 | + * |
1331 | + * @param name the name of the constant |
1332 | + * @param v the value of the constant |
1333 | + * @param function if not empty, the function to put the definition in |
1334 | + */ |
1335 | +void |
1336 | +ShaderSource::add_const(const std::string &name, const LibMatrix::mat3 &m, |
1337 | + const std::string &function) |
1338 | +{ |
1339 | + std::stringstream ss; |
1340 | + |
1341 | + ss << "const mat3 " << name << " = mat3(" << std::fixed; |
1342 | + ss << m[0][0] << ", " << m[1][0] << ", " << m[2][0] << "," << std::endl; |
1343 | + ss << m[0][1] << ", " << m[1][1] << ", " << m[2][1] << "," << std::endl; |
1344 | + ss << m[0][2] << ", " << m[1][2] << ", " << m[2][2] << std::endl; |
1345 | + ss << ");" << std::endl; |
1346 | + |
1347 | + add(ss.str(), function); |
1348 | +} |
1349 | + |
1350 | +/** |
1351 | + * Adds a float array declaration and initialization. |
1352 | + * |
1353 | + * @param name the name of the array |
1354 | + * @param array the array values |
1355 | + * @param init_function the function to put the initialization in |
1356 | + * @param decl_function if not empty, the function to put the declaration in |
1357 | + */ |
1358 | +void |
1359 | +ShaderSource::add_array(const std::string &name, std::vector<float> &array, |
1360 | + const std::string &init_function, |
1361 | + const std::string &decl_function) |
1362 | +{ |
1363 | + if (init_function.empty() || name.empty()) |
1364 | + return; |
1365 | + |
1366 | + std::stringstream ss; |
1367 | + ss << "float " << name << "[" << array.size() << "];" << std::endl; |
1368 | + |
1369 | + std::string decl(ss.str()); |
1370 | + |
1371 | + ss.clear(); |
1372 | + ss.str(""); |
1373 | + ss << std::fixed; |
1374 | + |
1375 | + for(std::vector<float>::const_iterator iter = array.begin(); |
1376 | + iter != array.end(); |
1377 | + iter++) |
1378 | + { |
1379 | + ss << name << "[" << iter - array.begin() << "] = " << *iter << ";" << std::endl; |
1380 | + } |
1381 | + |
1382 | + add(ss.str(), init_function); |
1383 | + |
1384 | + add(decl, decl_function); |
1385 | +} |
1386 | + |
1387 | +/** |
1388 | + * Gets the ShaderType for this ShaderSource. |
1389 | + * |
1390 | + * If the ShaderType is unknown, an attempt is made to infer |
1391 | + * the type from the shader source contents. |
1392 | + * |
1393 | + * @return the ShaderType |
1394 | + */ |
1395 | +ShaderSource::ShaderType |
1396 | +ShaderSource::type() |
1397 | +{ |
1398 | + /* Try to infer the type from the source contents */ |
1399 | + if (type_ == ShaderSource::ShaderTypeUnknown) { |
1400 | + std::string source(source_.str()); |
1401 | + |
1402 | + if (source.find("gl_FragColor") != std::string::npos) |
1403 | + type_ = ShaderSource::ShaderTypeFragment; |
1404 | + else if (source.find("gl_Position") != std::string::npos) |
1405 | + type_ = ShaderSource::ShaderTypeVertex; |
1406 | + else |
1407 | + Log::debug("Cannot infer shader type from contents. Leaving it Unknown.\n"); |
1408 | + } |
1409 | + |
1410 | + return type_; |
1411 | +} |
1412 | + |
1413 | +/** |
1414 | + * Helper function that emits a precision statement. |
1415 | + * |
1416 | + * @param ss the stringstream to add the statement to |
1417 | + * @param val the precision value |
1418 | + * @param type_str the variable type to apply the precision value to |
1419 | + */ |
1420 | +void |
1421 | +ShaderSource::emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, |
1422 | + const std::string& type_str) |
1423 | +{ |
1424 | + static const char *precision_map[] = { |
1425 | + "lowp", "mediump", "highp", NULL |
1426 | + }; |
1427 | + |
1428 | + if (val == ShaderSource::PrecisionValueHigh) { |
1429 | + if (type_ == ShaderSource::ShaderTypeFragment) |
1430 | + ss << "#ifdef GL_FRAGMENT_PRECISION_HIGH" << std::endl; |
1431 | + |
1432 | + ss << "precision highp " << type_str << ";" << std::endl; |
1433 | + |
1434 | + if (type_ == ShaderSource::ShaderTypeFragment) { |
1435 | + ss << "#else" << std::endl; |
1436 | + ss << "precision mediump " << type_str << ";" << std::endl; |
1437 | + ss << "#endif" << std::endl; |
1438 | + } |
1439 | + } |
1440 | + else if (val >= 0 && val < ShaderSource::PrecisionValueDefault) { |
1441 | + ss << "precision " << precision_map[val] << " "; |
1442 | + ss << type_str << ";" << std::endl; |
1443 | + } |
1444 | + |
1445 | + /* There is no default precision in the fragment shader, so set it to mediump */ |
1446 | + if (val == ShaderSource::PrecisionValueDefault |
1447 | + && type_str == "float" && type_ == ShaderSource::ShaderTypeFragment) |
1448 | + { |
1449 | + ss << "precision mediump float;" << std::endl; |
1450 | + } |
1451 | +} |
1452 | + |
1453 | +/** |
1454 | + * Gets a string containing the complete shader source. |
1455 | + * |
1456 | + * Precision statements are applied at this point. |
1457 | + * |
1458 | + * @return the shader source |
1459 | + */ |
1460 | +std::string |
1461 | +ShaderSource::str() |
1462 | +{ |
1463 | + /* Decide which precision values to use */ |
1464 | + ShaderSource::Precision precision; |
1465 | + |
1466 | + /* Ensure we have tried to infer the type from the contents */ |
1467 | + type(); |
1468 | + |
1469 | + if (precision_has_been_set_) |
1470 | + precision = precision_; |
1471 | + else |
1472 | + precision = default_precision(type_); |
1473 | + |
1474 | + /* Create the precision statements */ |
1475 | + std::stringstream ss; |
1476 | + |
1477 | + emit_precision(ss, precision.int_precision, "int"); |
1478 | + emit_precision(ss, precision.float_precision, "float"); |
1479 | + emit_precision(ss, precision.sampler2d_precision, "sampler2D"); |
1480 | + emit_precision(ss, precision.samplercube_precision, "samplerCube"); |
1481 | + |
1482 | + std::string precision_str(ss.str()); |
1483 | + if (!precision_str.empty()) { |
1484 | + precision_str.insert(0, "#ifdef GL_ES\n"); |
1485 | + precision_str.insert(precision_str.size(), "#endif\n"); |
1486 | + } |
1487 | + |
1488 | + return precision_str + source_.str(); |
1489 | +} |
1490 | + |
1491 | +/** |
1492 | + * Sets the precision that will be used for this shader. |
1493 | + * |
1494 | + * This overrides any default values set with ShaderSource::default_*_precision(). |
1495 | + * |
1496 | + * @param precision the precision to set |
1497 | + */ |
1498 | +void |
1499 | +ShaderSource::precision(const ShaderSource::Precision& precision) |
1500 | +{ |
1501 | + precision_ = precision; |
1502 | + precision_has_been_set_ = true; |
1503 | +} |
1504 | + |
1505 | +/** |
1506 | + * Gets the precision that will be used for this shader. |
1507 | + * |
1508 | + * @return the precision |
1509 | + */ |
1510 | +const ShaderSource::Precision& |
1511 | +ShaderSource::precision() |
1512 | +{ |
1513 | + return precision_; |
1514 | +} |
1515 | + |
1516 | +/** |
1517 | + * Sets the default precision that will be used for a shaders type. |
1518 | + * |
1519 | + * If type is ShaderTypeUnknown the supplied precision is used for all |
1520 | + * shader types. |
1521 | + * |
1522 | + * This can be overriden per ShaderSource object by using ::precision(). |
1523 | + * |
1524 | + * @param precision the default precision to set |
1525 | + * @param type the ShaderType to use the precision for |
1526 | + */ |
1527 | +void |
1528 | +ShaderSource::default_precision(const ShaderSource::Precision& precision, |
1529 | + ShaderSource::ShaderType type) |
1530 | +{ |
1531 | + if (type < 0 || type > ShaderSource::ShaderTypeUnknown) |
1532 | + type = ShaderSource::ShaderTypeUnknown; |
1533 | + |
1534 | + if (type == ShaderSource::ShaderTypeUnknown) { |
1535 | + for (size_t i = 0; i < ShaderSource::ShaderTypeUnknown; i++) |
1536 | + default_precision_[i] = precision; |
1537 | + } |
1538 | + else { |
1539 | + default_precision_[type] = precision; |
1540 | + } |
1541 | +} |
1542 | + |
1543 | +/** |
1544 | + * Gets the default precision that will be used for a shader type. |
1545 | + * |
1546 | + * It is valid to use a type of ShaderTypeUnknown. This will always |
1547 | + * return a Precision with default values. |
1548 | + * |
1549 | + * @param type the ShaderType to get the precision of |
1550 | + * |
1551 | + * @return the precision |
1552 | + */ |
1553 | +const ShaderSource::Precision& |
1554 | +ShaderSource::default_precision(ShaderSource::ShaderType type) |
1555 | +{ |
1556 | + if (type < 0 || type > ShaderSource::ShaderTypeUnknown) |
1557 | + type = ShaderSource::ShaderTypeUnknown; |
1558 | + |
1559 | + return default_precision_[type]; |
1560 | +} |
1561 | + |
1562 | +/**************************************** |
1563 | + * ShaderSource::Precision constructors * |
1564 | + ****************************************/ |
1565 | + |
1566 | +/** |
1567 | + * Creates a ShaderSource::Precision with default precision values. |
1568 | + */ |
1569 | +ShaderSource::Precision::Precision() : |
1570 | + int_precision(ShaderSource::PrecisionValueDefault), |
1571 | + float_precision(ShaderSource::PrecisionValueDefault), |
1572 | + sampler2d_precision(ShaderSource::PrecisionValueDefault), |
1573 | + samplercube_precision(ShaderSource::PrecisionValueDefault) |
1574 | +{ |
1575 | +} |
1576 | + |
1577 | +/** |
1578 | + * Creates a ShaderSource::Precision using the supplied precision values. |
1579 | + */ |
1580 | +ShaderSource::Precision::Precision(ShaderSource::PrecisionValue int_p, |
1581 | + ShaderSource::PrecisionValue float_p, |
1582 | + ShaderSource::PrecisionValue sampler2d_p, |
1583 | + ShaderSource::PrecisionValue samplercube_p) : |
1584 | + int_precision(int_p), float_precision(float_p), |
1585 | + sampler2d_precision(sampler2d_p), samplercube_precision(samplercube_p) |
1586 | +{ |
1587 | +} |
1588 | + |
1589 | +/** |
1590 | + * Creates a ShaderSource::Precision from a string representation of |
1591 | + * precision values. |
1592 | + * |
1593 | + * The string format is: |
1594 | + * "<int>,<float>,<sampler2d>,<samplercube>" |
1595 | + * |
1596 | + * Each precision value is one of "high", "medium", "low" or "default". |
1597 | + * |
1598 | + * @param precision_values the string representation of the precision values |
1599 | + */ |
1600 | +ShaderSource::Precision::Precision(const std::string& precision_values) : |
1601 | + int_precision(ShaderSource::PrecisionValueDefault), |
1602 | + float_precision(ShaderSource::PrecisionValueDefault), |
1603 | + sampler2d_precision(ShaderSource::PrecisionValueDefault), |
1604 | + samplercube_precision(ShaderSource::PrecisionValueDefault) |
1605 | +{ |
1606 | + std::vector<std::string> elems; |
1607 | + |
1608 | + Util::split(precision_values, ',', elems); |
1609 | + |
1610 | + for (size_t i = 0; i < elems.size() && i < 4; i++) { |
1611 | + const std::string& pstr(elems[i]); |
1612 | + ShaderSource::PrecisionValue pval; |
1613 | + |
1614 | + if (pstr == "high") |
1615 | + pval = ShaderSource::PrecisionValueHigh; |
1616 | + else if (pstr == "medium") |
1617 | + pval = ShaderSource::PrecisionValueMedium; |
1618 | + else if (pstr == "low") |
1619 | + pval = ShaderSource::PrecisionValueLow; |
1620 | + else |
1621 | + pval = ShaderSource::PrecisionValueDefault; |
1622 | + |
1623 | + switch(i) { |
1624 | + case 0: int_precision = pval; break; |
1625 | + case 1: float_precision = pval; break; |
1626 | + case 2: sampler2d_precision = pval; break; |
1627 | + case 3: samplercube_precision = pval; break; |
1628 | + default: break; |
1629 | + } |
1630 | + } |
1631 | +} |
1632 | |
1633 | === added file 'src/libmatrix/shader-source.h' |
1634 | --- src/libmatrix/shader-source.h 1970-01-01 00:00:00 +0000 |
1635 | +++ src/libmatrix/shader-source.h 2012-01-27 22:25:27 +0000 |
1636 | @@ -0,0 +1,103 @@ |
1637 | +// |
1638 | +// Copyright (c) 2010-2012 Linaro Limited |
1639 | +// |
1640 | +// All rights reserved. This program and the accompanying materials |
1641 | +// are made available under the terms of the MIT License which accompanies |
1642 | +// this distribution, and is available at |
1643 | +// http://www.opensource.org/licenses/mit-license.php |
1644 | +// |
1645 | +// Contributors: |
1646 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
1647 | +// Jesse Barker <jesse.barker@linaro.org> |
1648 | +// |
1649 | +#include <string> |
1650 | +#include <sstream> |
1651 | +#include <vector> |
1652 | +#include "vec.h" |
1653 | +#include "mat.h" |
1654 | + |
1655 | +/** |
1656 | + * Helper class for loading and manipulating shader sources. |
1657 | + */ |
1658 | +class ShaderSource |
1659 | +{ |
1660 | +public: |
1661 | + enum ShaderType { |
1662 | + ShaderTypeVertex, |
1663 | + ShaderTypeFragment, |
1664 | + ShaderTypeUnknown |
1665 | + }; |
1666 | + |
1667 | + ShaderSource(ShaderType type = ShaderTypeUnknown) : |
1668 | + precision_has_been_set_(false), type_(type) {} |
1669 | + ShaderSource(const std::string &filename, ShaderType type = ShaderTypeUnknown) : |
1670 | + precision_has_been_set_(false), type_(type) { append_file(filename); } |
1671 | + |
1672 | + void append(const std::string &str); |
1673 | + void append_file(const std::string &filename); |
1674 | + |
1675 | + void replace(const std::string &remove, const std::string &insert); |
1676 | + void replace_with_file(const std::string &remove, const std::string &filename); |
1677 | + |
1678 | + void add(const std::string &str, const std::string &function = ""); |
1679 | + |
1680 | + void add_const(const std::string &name, float f, |
1681 | + const std::string &function = ""); |
1682 | + void add_const(const std::string &name, std::vector<float> &f, |
1683 | + const std::string &function = ""); |
1684 | + void add_const(const std::string &name, const LibMatrix::vec2 &v, |
1685 | + const std::string &function = ""); |
1686 | + void add_const(const std::string &name, const LibMatrix::vec3 &v, |
1687 | + const std::string &function = ""); |
1688 | + void add_const(const std::string &name, const LibMatrix::vec4 &v, |
1689 | + const std::string &function = ""); |
1690 | + void add_const(const std::string &name, const LibMatrix::mat3 &m, |
1691 | + const std::string &function = ""); |
1692 | + |
1693 | + void add_array(const std::string &name, std::vector<float> &array, |
1694 | + const std::string &init_function, |
1695 | + const std::string &decl_function = ""); |
1696 | + |
1697 | + ShaderType type(); |
1698 | + std::string str(); |
1699 | + |
1700 | + enum PrecisionValue { |
1701 | + PrecisionValueLow, |
1702 | + PrecisionValueMedium, |
1703 | + PrecisionValueHigh, |
1704 | + PrecisionValueDefault |
1705 | + }; |
1706 | + |
1707 | + struct Precision { |
1708 | + Precision(); |
1709 | + Precision(PrecisionValue int_p, PrecisionValue float_p, |
1710 | + PrecisionValue sampler2d_p, PrecisionValue samplercube_p); |
1711 | + Precision(const std::string& list); |
1712 | + |
1713 | + PrecisionValue int_precision; |
1714 | + PrecisionValue float_precision; |
1715 | + PrecisionValue sampler2d_precision; |
1716 | + PrecisionValue samplercube_precision; |
1717 | + }; |
1718 | + |
1719 | + void precision(const Precision& precision); |
1720 | + const Precision& precision(); |
1721 | + |
1722 | + static void default_precision(const Precision& precision, |
1723 | + ShaderType type = ShaderTypeUnknown); |
1724 | + static const Precision& default_precision(ShaderType type); |
1725 | + |
1726 | +private: |
1727 | + void add_global(const std::string &str); |
1728 | + void add_local(const std::string &str, const std::string &function); |
1729 | + bool load_file(const std::string& filename, std::string& str); |
1730 | + void emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, |
1731 | + const std::string& type_str); |
1732 | + |
1733 | + std::stringstream source_; |
1734 | + Precision precision_; |
1735 | + bool precision_has_been_set_; |
1736 | + ShaderType type_; |
1737 | + |
1738 | + static std::vector<Precision> default_precision_; |
1739 | +}; |
1740 | |
1741 | === added file 'src/libmatrix/test/basic-global-const.vert' |
1742 | --- src/libmatrix/test/basic-global-const.vert 1970-01-01 00:00:00 +0000 |
1743 | +++ src/libmatrix/test/basic-global-const.vert 2012-01-27 22:25:27 +0000 |
1744 | @@ -0,0 +1,15 @@ |
1745 | +const vec4 ConstantColor = vec4(1.000000, 1.000000, 1.000000, 1.000000); |
1746 | +attribute vec3 position; |
1747 | + |
1748 | +uniform mat4 modelview; |
1749 | +uniform mat4 projection; |
1750 | + |
1751 | +varying vec4 color; |
1752 | + |
1753 | +void |
1754 | +main(void) |
1755 | +{ |
1756 | + vec4 curVertex = vec4(position, 1.0); |
1757 | + gl_Position = projection * modelview * curVertex; |
1758 | + color = ConstantColor; |
1759 | +} |
1760 | |
1761 | === added file 'src/libmatrix/test/basic.frag' |
1762 | --- src/libmatrix/test/basic.frag 1970-01-01 00:00:00 +0000 |
1763 | +++ src/libmatrix/test/basic.frag 2012-01-27 22:25:27 +0000 |
1764 | @@ -0,0 +1,7 @@ |
1765 | +varying vec4 color; |
1766 | + |
1767 | +void |
1768 | +main(void) |
1769 | +{ |
1770 | + gl_FragColor = color; |
1771 | +} |
1772 | |
1773 | === added file 'src/libmatrix/test/basic.vert' |
1774 | --- src/libmatrix/test/basic.vert 1970-01-01 00:00:00 +0000 |
1775 | +++ src/libmatrix/test/basic.vert 2012-01-27 22:25:27 +0000 |
1776 | @@ -0,0 +1,14 @@ |
1777 | +attribute vec3 position; |
1778 | + |
1779 | +uniform mat4 modelview; |
1780 | +uniform mat4 projection; |
1781 | + |
1782 | +varying vec4 color; |
1783 | + |
1784 | +void |
1785 | +main(void) |
1786 | +{ |
1787 | + vec4 curVertex = vec4(position, 1.0); |
1788 | + gl_Position = projection * modelview * curVertex; |
1789 | + color = ConstantColor; |
1790 | +} |
1791 | |
1792 | === added file 'src/libmatrix/test/const_vec_test.cc' |
1793 | --- src/libmatrix/test/const_vec_test.cc 1970-01-01 00:00:00 +0000 |
1794 | +++ src/libmatrix/test/const_vec_test.cc 2012-01-27 22:25:27 +0000 |
1795 | @@ -0,0 +1,60 @@ |
1796 | +// |
1797 | +// Copyright (c) 2011 Linaro Limited |
1798 | +// |
1799 | +// All rights reserved. This program and the accompanying materials |
1800 | +// are made available under the terms of the MIT License which accompanies |
1801 | +// this distribution, and is available at |
1802 | +// http://www.opensource.org/licenses/mit-license.php |
1803 | +// |
1804 | +// Contributors: |
1805 | +// Jesse Barker - original implementation. |
1806 | +// |
1807 | +#include <iostream> |
1808 | +#include "libmatrix_test.h" |
1809 | +#include "const_vec_test.h" |
1810 | +#include "../vec.h" |
1811 | + |
1812 | +using LibMatrix::vec2; |
1813 | +using LibMatrix::vec3; |
1814 | +using LibMatrix::vec4; |
1815 | +using std::cout; |
1816 | +using std::endl; |
1817 | + |
1818 | +void |
1819 | +Vec2TestConstOperator::run(const Options& options) |
1820 | +{ |
1821 | + const vec2 a(1.0, 1.0); |
1822 | + const vec2 b(2.0, 2.0); |
1823 | + vec2 aplusb(a + b); |
1824 | + vec2 aminusb(a - b); |
1825 | + vec2 atimesb(a * b); |
1826 | + vec2 adivb(a / b); |
1827 | + const float s(2.5); |
1828 | + vec2 stimesb(s * b); |
1829 | +} |
1830 | + |
1831 | +void |
1832 | +Vec3TestConstOperator::run(const Options& options) |
1833 | +{ |
1834 | + const vec3 a(1.0, 1.0, 1.0); |
1835 | + const vec3 b(2.0, 2.0, 2.0); |
1836 | + vec3 aplusb(a + b); |
1837 | + vec3 aminusb(a - b); |
1838 | + vec3 atimesb(a * b); |
1839 | + vec3 adivb(a / b); |
1840 | + const float s(2.5); |
1841 | + vec3 stimesb(s * b); |
1842 | +} |
1843 | + |
1844 | +void |
1845 | +Vec4TestConstOperator::run(const Options& options) |
1846 | +{ |
1847 | + const vec4 a(1.0, 1.0, 1.0, 1.0); |
1848 | + const vec4 b(2.0, 2.0, 2.0, 2.0); |
1849 | + vec4 aplusb(a + b); |
1850 | + vec4 aminusb(a - b); |
1851 | + vec4 atimesb(a * b); |
1852 | + vec4 adivb(a / b); |
1853 | + const float s(2.5); |
1854 | + vec4 stimesb(s * b); |
1855 | +} |
1856 | |
1857 | === added file 'src/libmatrix/test/const_vec_test.h' |
1858 | --- src/libmatrix/test/const_vec_test.h 1970-01-01 00:00:00 +0000 |
1859 | +++ src/libmatrix/test/const_vec_test.h 2012-01-27 22:25:27 +0000 |
1860 | @@ -0,0 +1,39 @@ |
1861 | +// |
1862 | +// Copyright (c) 2011 Linaro Limited |
1863 | +// |
1864 | +// All rights reserved. This program and the accompanying materials |
1865 | +// are made available under the terms of the MIT License which accompanies |
1866 | +// this distribution, and is available at |
1867 | +// http://www.opensource.org/licenses/mit-license.php |
1868 | +// |
1869 | +// Contributors: |
1870 | +// Jesse Barker - original implementation. |
1871 | +// |
1872 | +#ifndef CONST_VEC_TEST_H_ |
1873 | +#define CONST_VEC_TEST_H_ |
1874 | + |
1875 | +class MatrixTest; |
1876 | +class Options; |
1877 | + |
1878 | +class Vec2TestConstOperator : public MatrixTest |
1879 | +{ |
1880 | +public: |
1881 | + Vec2TestConstOperator() : MatrixTest("vec2::const") {} |
1882 | + virtual void run(const Options& options); |
1883 | +}; |
1884 | + |
1885 | +class Vec3TestConstOperator : public MatrixTest |
1886 | +{ |
1887 | +public: |
1888 | + Vec3TestConstOperator() : MatrixTest("vec3::const") {} |
1889 | + virtual void run(const Options& options); |
1890 | +}; |
1891 | + |
1892 | +class Vec4TestConstOperator : public MatrixTest |
1893 | +{ |
1894 | +public: |
1895 | + Vec4TestConstOperator() : MatrixTest("vec4::const") {} |
1896 | + virtual void run(const Options& options); |
1897 | +}; |
1898 | + |
1899 | +#endif // CONST_VEC_TEST_H_ |
1900 | |
1901 | === modified file 'src/libmatrix/test/inverse_test.cc' |
1902 | --- src/libmatrix/test/inverse_test.cc 2011-06-22 08:59:00 +0000 |
1903 | +++ src/libmatrix/test/inverse_test.cc 2012-01-27 22:25:27 +0000 |
1904 | @@ -1,3 +1,14 @@ |
1905 | +// |
1906 | +// Copyright (c) 2010 Linaro Limited |
1907 | +// |
1908 | +// All rights reserved. This program and the accompanying materials |
1909 | +// are made available under the terms of the MIT License which accompanies |
1910 | +// this distribution, and is available at |
1911 | +// http://www.opensource.org/licenses/mit-license.php |
1912 | +// |
1913 | +// Contributors: |
1914 | +// Jesse Barker - original implementation. |
1915 | +// |
1916 | #include <iostream> |
1917 | #include "libmatrix_test.h" |
1918 | #include "inverse_test.h" |
1919 | |
1920 | === modified file 'src/libmatrix/test/inverse_test.h' |
1921 | --- src/libmatrix/test/inverse_test.h 2011-06-22 08:59:00 +0000 |
1922 | +++ src/libmatrix/test/inverse_test.h 2012-01-27 22:25:27 +0000 |
1923 | @@ -1,3 +1,14 @@ |
1924 | +// |
1925 | +// Copyright (c) 2010 Linaro Limited |
1926 | +// |
1927 | +// All rights reserved. This program and the accompanying materials |
1928 | +// are made available under the terms of the MIT License which accompanies |
1929 | +// this distribution, and is available at |
1930 | +// http://www.opensource.org/licenses/mit-license.php |
1931 | +// |
1932 | +// Contributors: |
1933 | +// Jesse Barker - original implementation. |
1934 | +// |
1935 | #ifndef INVERSE_TEST_H_ |
1936 | #define INVERSE_TEST_H_ |
1937 | |
1938 | |
1939 | === modified file 'src/libmatrix/test/libmatrix_test.cc' |
1940 | --- src/libmatrix/test/libmatrix_test.cc 2011-06-22 08:59:00 +0000 |
1941 | +++ src/libmatrix/test/libmatrix_test.cc 2012-01-27 22:25:27 +0000 |
1942 | @@ -14,6 +14,9 @@ |
1943 | #include <vector> |
1944 | #include "libmatrix_test.h" |
1945 | #include "inverse_test.h" |
1946 | +#include "transpose_test.h" |
1947 | +#include "const_vec_test.h" |
1948 | +#include "shader_source_test.h" |
1949 | |
1950 | using std::cerr; |
1951 | using std::cout; |
1952 | @@ -35,12 +38,20 @@ |
1953 | testVec.push_back(new MatrixTest2x2Inverse()); |
1954 | testVec.push_back(new MatrixTest3x3Inverse()); |
1955 | testVec.push_back(new MatrixTest4x4Inverse()); |
1956 | + testVec.push_back(new MatrixTest2x2Transpose()); |
1957 | + testVec.push_back(new MatrixTest3x3Transpose()); |
1958 | + testVec.push_back(new MatrixTest4x4Transpose()); |
1959 | + testVec.push_back(new ShaderSourceBasic()); |
1960 | |
1961 | for (vector<MatrixTest*>::iterator testIt = testVec.begin(); |
1962 | testIt != testVec.end(); |
1963 | testIt++) |
1964 | { |
1965 | MatrixTest* curTest = *testIt; |
1966 | + if (testOptions.beVerbose()) |
1967 | + { |
1968 | + cout << "Running test " << curTest->name() << endl; |
1969 | + } |
1970 | curTest->run(testOptions); |
1971 | if (!curTest->passed()) |
1972 | { |
1973 | |
1974 | === modified file 'src/libmatrix/test/libmatrix_test.h' |
1975 | --- src/libmatrix/test/libmatrix_test.h 2011-06-22 08:59:00 +0000 |
1976 | +++ src/libmatrix/test/libmatrix_test.h 2012-01-27 22:25:27 +0000 |
1977 | @@ -1,3 +1,14 @@ |
1978 | +// |
1979 | +// Copyright (c) 2010 Linaro Limited |
1980 | +// |
1981 | +// All rights reserved. This program and the accompanying materials |
1982 | +// are made available under the terms of the MIT License which accompanies |
1983 | +// this distribution, and is available at |
1984 | +// http://www.opensource.org/licenses/mit-license.php |
1985 | +// |
1986 | +// Contributors: |
1987 | +// Jesse Barker - original implementation. |
1988 | +// |
1989 | #ifndef LIBMATRIX_TEST_H_ |
1990 | #define LIBMATRIX_TEST_H_ |
1991 | |
1992 | |
1993 | === modified file 'src/libmatrix/test/options.cc' |
1994 | --- src/libmatrix/test/options.cc 2011-06-22 08:59:00 +0000 |
1995 | +++ src/libmatrix/test/options.cc 2012-01-27 22:25:27 +0000 |
1996 | @@ -1,3 +1,14 @@ |
1997 | +// |
1998 | +// Copyright (c) 2010 Linaro Limited |
1999 | +// |
2000 | +// All rights reserved. This program and the accompanying materials |
2001 | +// are made available under the terms of the MIT License which accompanies |
2002 | +// this distribution, and is available at |
2003 | +// http://www.opensource.org/licenses/mit-license.php |
2004 | +// |
2005 | +// Contributors: |
2006 | +// Jesse Barker - original implementation. |
2007 | +// |
2008 | #include <iostream> |
2009 | #include <iomanip> |
2010 | #include <getopt.h> |
2011 | @@ -21,6 +32,15 @@ |
2012 | int c = getopt_long(argc, argv, "", long_options, &option_index); |
2013 | while (c != -1) |
2014 | { |
2015 | + // getopt_long() returns '?' and prints an "unrecognized option" error |
2016 | + // to stderr if it does not recognize an option. Just trigger |
2017 | + // the help/usage message, stop processing and get out. |
2018 | + if (c == '?') |
2019 | + { |
2020 | + show_help_ = true; |
2021 | + break; |
2022 | + } |
2023 | + |
2024 | std::string optname(long_options[option_index].name); |
2025 | |
2026 | if (optname == verbose_name_) |
2027 | |
2028 | === added file 'src/libmatrix/test/shader_source_test.cc' |
2029 | --- src/libmatrix/test/shader_source_test.cc 1970-01-01 00:00:00 +0000 |
2030 | +++ src/libmatrix/test/shader_source_test.cc 2012-01-27 22:25:27 +0000 |
2031 | @@ -0,0 +1,49 @@ |
2032 | +// |
2033 | +// Copyright (c) 2012 Linaro Limited |
2034 | +// |
2035 | +// All rights reserved. This program and the accompanying materials |
2036 | +// are made available under the terms of the MIT License which accompanies |
2037 | +// this distribution, and is available at |
2038 | +// http://www.opensource.org/licenses/mit-license.php |
2039 | +// |
2040 | +// Contributors: |
2041 | +// Jesse Barker - original implementation. |
2042 | +// |
2043 | +#include <string> |
2044 | +#include "libmatrix_test.h" |
2045 | +#include "shader_source_test.h" |
2046 | +#include "../shader-source.h" |
2047 | +#include "../vec.h" |
2048 | + |
2049 | +using std::string; |
2050 | +using LibMatrix::vec4; |
2051 | + |
2052 | +void |
2053 | +ShaderSourceBasic::run(const Options& options) |
2054 | +{ |
2055 | + static const string vtx_shader_filename("test/basic.vert"); |
2056 | + |
2057 | + ShaderSource vtx_source(vtx_shader_filename); |
2058 | + ShaderSource vtx_source2(vtx_shader_filename); |
2059 | + |
2060 | + pass_ = (vtx_source.str() == vtx_source2.str()); |
2061 | +} |
2062 | + |
2063 | +void |
2064 | +ShaderSourceAddConstGlobal::run(const Options& options) |
2065 | +{ |
2066 | + // Load the original shader source. |
2067 | + static const string src_shader_filename("test/basic.vert"); |
2068 | + ShaderSource src_shader(src_shader_filename); |
2069 | + |
2070 | + // Add constant at global scope |
2071 | + static const vec4 constantColor(1.0, 1.0, 1.0, 1.0); |
2072 | + src_shader.add_const("ConstantColor", constantColor); |
2073 | + |
2074 | + // Load the pre-modified shader |
2075 | + static const string result_shader_filename("test/basic-global-const.vert"); |
2076 | + ShaderSource result_shader(result_shader_filename); |
2077 | + |
2078 | + // Compare the output strings to confirm the results. |
2079 | + pass_ = (src_shader.str() == result_shader.str()); |
2080 | +} |
2081 | |
2082 | === added file 'src/libmatrix/test/shader_source_test.h' |
2083 | --- src/libmatrix/test/shader_source_test.h 1970-01-01 00:00:00 +0000 |
2084 | +++ src/libmatrix/test/shader_source_test.h 2012-01-27 22:25:27 +0000 |
2085 | @@ -0,0 +1,32 @@ |
2086 | +// |
2087 | +// Copyright (c) 2012 Linaro Limited |
2088 | +// |
2089 | +// All rights reserved. This program and the accompanying materials |
2090 | +// are made available under the terms of the MIT License which accompanies |
2091 | +// this distribution, and is available at |
2092 | +// http://www.opensource.org/licenses/mit-license.php |
2093 | +// |
2094 | +// Contributors: |
2095 | +// Jesse Barker - original implementation. |
2096 | +// |
2097 | +#ifndef SHADER_SOURCE_TEST_H_ |
2098 | +#define SHADER_SOURCE_TEST_H_ |
2099 | + |
2100 | +class MatrixTest; |
2101 | +class Options; |
2102 | + |
2103 | +class ShaderSourceBasic : public MatrixTest |
2104 | +{ |
2105 | +public: |
2106 | + ShaderSourceBasic() : MatrixTest("ShaderSource::basic") {} |
2107 | + virtual void run(const Options& options); |
2108 | +}; |
2109 | + |
2110 | +class ShaderSourceAddConstGlobal : public MatrixTest |
2111 | +{ |
2112 | +public: |
2113 | + ShaderSourceAddConstGlobal() : MatrixTest("ShaderSource::AddConstGlobal") {} |
2114 | + virtual void run(const Options& options); |
2115 | +}; |
2116 | + |
2117 | +#endif // SHADER_SOURCE_TEST_H |
2118 | |
2119 | === added file 'src/libmatrix/test/transpose_test.cc' |
2120 | --- src/libmatrix/test/transpose_test.cc 1970-01-01 00:00:00 +0000 |
2121 | +++ src/libmatrix/test/transpose_test.cc 2012-01-27 22:25:27 +0000 |
2122 | @@ -0,0 +1,297 @@ |
2123 | +// |
2124 | +// Copyright (c) 2010 Linaro Limited |
2125 | +// |
2126 | +// All rights reserved. This program and the accompanying materials |
2127 | +// are made available under the terms of the MIT License which accompanies |
2128 | +// this distribution, and is available at |
2129 | +// http://www.opensource.org/licenses/mit-license.php |
2130 | +// |
2131 | +// Contributors: |
2132 | +// Jesse Barker - original implementation. |
2133 | +// |
2134 | +#include <iostream> |
2135 | +#include "libmatrix_test.h" |
2136 | +#include "transpose_test.h" |
2137 | +#include "../mat.h" |
2138 | + |
2139 | +using LibMatrix::mat2; |
2140 | +using LibMatrix::mat3; |
2141 | +using LibMatrix::mat4; |
2142 | +using std::cout; |
2143 | +using std::endl; |
2144 | + |
2145 | +void |
2146 | +MatrixTest2x2Transpose::run(const Options& options) |
2147 | +{ |
2148 | + // First, a simple test to ensure that the transpose of the identity is |
2149 | + // the identity. |
2150 | + if (options.beVerbose()) |
2151 | + { |
2152 | + cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; |
2153 | + } |
2154 | + |
2155 | + mat2 m; |
2156 | + |
2157 | + if (options.beVerbose()) |
2158 | + { |
2159 | + cout << "Starting with mat2 (should be identity): " << endl << endl; |
2160 | + m.print(); |
2161 | + } |
2162 | + |
2163 | + m.transpose(); |
2164 | + |
2165 | + if (options.beVerbose()) |
2166 | + { |
2167 | + cout << endl << "Transpose of identity (should be identity): " << endl << endl; |
2168 | + m.print(); |
2169 | + } |
2170 | + |
2171 | + mat2 mi; |
2172 | + if (m != mi) |
2173 | + { |
2174 | + // FAIL! Transpose of the identity is the identity. |
2175 | + return; |
2176 | + } |
2177 | + |
2178 | + // At this point, we have 2 identity matrices. |
2179 | + // Next, set an element in the matrix and transpose twice. We should see |
2180 | + // the original matrix (with i,j set). |
2181 | + if (options.beVerbose()) |
2182 | + { |
2183 | + cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; |
2184 | + } |
2185 | + |
2186 | + m[0][1] = 6.3; |
2187 | + |
2188 | + if (options.beVerbose()) |
2189 | + { |
2190 | + cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2191 | + m.print(); |
2192 | + } |
2193 | + |
2194 | + mi = m; |
2195 | + |
2196 | + m.transpose().transpose(); |
2197 | + |
2198 | + if (options.beVerbose()) |
2199 | + { |
2200 | + cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2201 | + m.print(); |
2202 | + } |
2203 | + |
2204 | + if (m != mi) |
2205 | + { |
2206 | + // FAIL! Transposing the same matrix twice should yield the original. |
2207 | + return; |
2208 | + } |
2209 | + |
2210 | + // Next, reset mi back to the identity. Set element element j,i in this |
2211 | + // matrix and transpose m. They should now be equal. |
2212 | + if (options.beVerbose()) |
2213 | + { |
2214 | + cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; |
2215 | + } |
2216 | + |
2217 | + mi.setIdentity(); |
2218 | + mi[1][0] = 6.3; |
2219 | + |
2220 | + m.transpose(); |
2221 | + |
2222 | + if (options.beVerbose()) |
2223 | + { |
2224 | + cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; |
2225 | + m.print(); |
2226 | + cout << endl; |
2227 | + } |
2228 | + |
2229 | + if (m == mi) |
2230 | + { |
2231 | + pass_ = true; |
2232 | + } |
2233 | + |
2234 | + // FAIL! Transposing the same matrix twice should yield the original. |
2235 | +} |
2236 | + |
2237 | +void |
2238 | +MatrixTest3x3Transpose::run(const Options& options) |
2239 | +{ |
2240 | + // First, a simple test to ensure that the transpose of the identity is |
2241 | + // the identity. |
2242 | + if (options.beVerbose()) |
2243 | + { |
2244 | + cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; |
2245 | + } |
2246 | + |
2247 | + mat3 m; |
2248 | + |
2249 | + if (options.beVerbose()) |
2250 | + { |
2251 | + cout << "Starting with mat2 (should be identity): " << endl << endl; |
2252 | + m.print(); |
2253 | + } |
2254 | + |
2255 | + m.transpose(); |
2256 | + |
2257 | + if (options.beVerbose()) |
2258 | + { |
2259 | + cout << endl << "Transpose of identity (should be identity): " << endl << endl; |
2260 | + m.print(); |
2261 | + } |
2262 | + |
2263 | + mat3 mi; |
2264 | + if (m != mi) |
2265 | + { |
2266 | + // FAIL! Transpose of the identity is the identity. |
2267 | + return; |
2268 | + } |
2269 | + |
2270 | + // At this point, we have 2 identity matrices. |
2271 | + // Next, set an element in the matrix and transpose twice. We should see |
2272 | + // the original matrix (with i,j set). |
2273 | + if (options.beVerbose()) |
2274 | + { |
2275 | + cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; |
2276 | + } |
2277 | + |
2278 | + m[0][1] = 6.3; |
2279 | + |
2280 | + if (options.beVerbose()) |
2281 | + { |
2282 | + cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2283 | + m.print(); |
2284 | + } |
2285 | + |
2286 | + mi = m; |
2287 | + |
2288 | + m.transpose().transpose(); |
2289 | + |
2290 | + if (options.beVerbose()) |
2291 | + { |
2292 | + cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2293 | + m.print(); |
2294 | + } |
2295 | + |
2296 | + if (m != mi) |
2297 | + { |
2298 | + // FAIL! Transposing the same matrix twice should yield the original. |
2299 | + return; |
2300 | + } |
2301 | + |
2302 | + // Next, reset mi back to the identity. Set element element j,i in this |
2303 | + // matrix and transpose m. They should now be equal. |
2304 | + if (options.beVerbose()) |
2305 | + { |
2306 | + cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; |
2307 | + } |
2308 | + |
2309 | + mi.setIdentity(); |
2310 | + mi[1][0] = 6.3; |
2311 | + |
2312 | + m.transpose(); |
2313 | + |
2314 | + if (options.beVerbose()) |
2315 | + { |
2316 | + cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; |
2317 | + m.print(); |
2318 | + cout << endl; |
2319 | + } |
2320 | + |
2321 | + if (m == mi) |
2322 | + { |
2323 | + pass_ = true; |
2324 | + } |
2325 | + |
2326 | + // FAIL! Transposing the same matrix twice should yield the original. |
2327 | +} |
2328 | + |
2329 | +void |
2330 | +MatrixTest4x4Transpose::run(const Options& options) |
2331 | +{ |
2332 | + // First, a simple test to ensure that the transpose of the identity is |
2333 | + // the identity. |
2334 | + if (options.beVerbose()) |
2335 | + { |
2336 | + cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl; |
2337 | + } |
2338 | + |
2339 | + mat4 m; |
2340 | + |
2341 | + if (options.beVerbose()) |
2342 | + { |
2343 | + cout << "Starting with mat2 (should be identity): " << endl << endl; |
2344 | + m.print(); |
2345 | + } |
2346 | + |
2347 | + m.transpose(); |
2348 | + |
2349 | + if (options.beVerbose()) |
2350 | + { |
2351 | + cout << endl << "Transpose of identity (should be identity): " << endl << endl; |
2352 | + m.print(); |
2353 | + } |
2354 | + |
2355 | + mat4 mi; |
2356 | + if (m != mi) |
2357 | + { |
2358 | + // FAIL! Transpose of the identity is the identity. |
2359 | + return; |
2360 | + } |
2361 | + |
2362 | + // At this point, we have 2 identity matrices. |
2363 | + // Next, set an element in the matrix and transpose twice. We should see |
2364 | + // the original matrix (with i,j set). |
2365 | + if (options.beVerbose()) |
2366 | + { |
2367 | + cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl; |
2368 | + } |
2369 | + |
2370 | + m[0][1] = 6.3; |
2371 | + |
2372 | + if (options.beVerbose()) |
2373 | + { |
2374 | + cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2375 | + m.print(); |
2376 | + } |
2377 | + |
2378 | + mi = m; |
2379 | + |
2380 | + m.transpose().transpose(); |
2381 | + |
2382 | + if (options.beVerbose()) |
2383 | + { |
2384 | + cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl; |
2385 | + m.print(); |
2386 | + } |
2387 | + |
2388 | + if (m != mi) |
2389 | + { |
2390 | + // FAIL! Transposing the same matrix twice should yield the original. |
2391 | + return; |
2392 | + } |
2393 | + |
2394 | + // Next, reset mi back to the identity. Set element element j,i in this |
2395 | + // matrix and transpose m. They should now be equal. |
2396 | + if (options.beVerbose()) |
2397 | + { |
2398 | + cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl; |
2399 | + } |
2400 | + |
2401 | + mi.setIdentity(); |
2402 | + mi[1][0] = 6.3; |
2403 | + |
2404 | + m.transpose(); |
2405 | + |
2406 | + if (options.beVerbose()) |
2407 | + { |
2408 | + cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl; |
2409 | + m.print(); |
2410 | + cout << endl; |
2411 | + } |
2412 | + |
2413 | + if (m == mi) |
2414 | + { |
2415 | + pass_ = true; |
2416 | + } |
2417 | + |
2418 | + // FAIL! Transposing the same matrix twice should yield the original. |
2419 | +} |
2420 | |
2421 | === added file 'src/libmatrix/test/transpose_test.h' |
2422 | --- src/libmatrix/test/transpose_test.h 1970-01-01 00:00:00 +0000 |
2423 | +++ src/libmatrix/test/transpose_test.h 2012-01-27 22:25:27 +0000 |
2424 | @@ -0,0 +1,38 @@ |
2425 | +// |
2426 | +// Copyright (c) 2010 Linaro Limited |
2427 | +// |
2428 | +// All rights reserved. This program and the accompanying materials |
2429 | +// are made available under the terms of the MIT License which accompanies |
2430 | +// this distribution, and is available at |
2431 | +// http://www.opensource.org/licenses/mit-license.php |
2432 | +// |
2433 | +// Contributors: |
2434 | +// Jesse Barker - original implementation. |
2435 | +// |
2436 | +#ifndef TRANSPOSE_TEST_H_ |
2437 | +#define TRANSPOSE_TEST_H_ |
2438 | + |
2439 | +class MatrixTest; |
2440 | +class Options; |
2441 | + |
2442 | +class MatrixTest2x2Transpose : public MatrixTest |
2443 | +{ |
2444 | +public: |
2445 | + MatrixTest2x2Transpose() : MatrixTest("mat2::transpose") {} |
2446 | + virtual void run(const Options& options); |
2447 | +}; |
2448 | + |
2449 | +class MatrixTest3x3Transpose : public MatrixTest |
2450 | +{ |
2451 | +public: |
2452 | + MatrixTest3x3Transpose() : MatrixTest("mat3::transpose") {} |
2453 | + virtual void run(const Options& options); |
2454 | +}; |
2455 | + |
2456 | +class MatrixTest4x4Transpose : public MatrixTest |
2457 | +{ |
2458 | +public: |
2459 | + MatrixTest4x4Transpose() : MatrixTest("mat4::transpose") {} |
2460 | + virtual void run(const Options& options); |
2461 | +}; |
2462 | +#endif // TRANSPOSE_TEST_H_ |
2463 | |
2464 | === added file 'src/libmatrix/util.cc' |
2465 | --- src/libmatrix/util.cc 1970-01-01 00:00:00 +0000 |
2466 | +++ src/libmatrix/util.cc 2012-01-27 22:25:27 +0000 |
2467 | @@ -0,0 +1,165 @@ |
2468 | +// |
2469 | +// Copyright (c) 2010-2011 Linaro Limited |
2470 | +// |
2471 | +// All rights reserved. This program and the accompanying materials |
2472 | +// are made available under the terms of the MIT License which accompanies |
2473 | +// this distribution, and is available at |
2474 | +// http://www.opensource.org/licenses/mit-license.php |
2475 | +// |
2476 | +// Contributors: |
2477 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
2478 | +// Jesse Barker <jesse.barker@linaro.org> |
2479 | +// |
2480 | +#include <sstream> |
2481 | +#include <fstream> |
2482 | +#include <sys/time.h> |
2483 | +#ifdef ANDROID |
2484 | +#include <android/asset_manager.h> |
2485 | +#else |
2486 | +#include <dirent.h> |
2487 | +#endif |
2488 | + |
2489 | +#include "log.h" |
2490 | +#include "util.h" |
2491 | + |
2492 | +/** |
2493 | + * Splits a string using a delimiter |
2494 | + * |
2495 | + * @param s the string to split |
2496 | + * @param delim the delimitir to use |
2497 | + * @param elems the string vector to populate |
2498 | + */ |
2499 | +void |
2500 | +Util::split(const std::string &s, char delim, std::vector<std::string> &elems) |
2501 | +{ |
2502 | + std::stringstream ss(s); |
2503 | + |
2504 | + std::string item; |
2505 | + while(std::getline(ss, item, delim)) |
2506 | + elems.push_back(item); |
2507 | +} |
2508 | + |
2509 | +uint64_t |
2510 | +Util::get_timestamp_us() |
2511 | +{ |
2512 | + struct timeval tv; |
2513 | + gettimeofday(&tv, NULL); |
2514 | + uint64_t now = static_cast<uint64_t>(tv.tv_sec) * 1000000 + |
2515 | + static_cast<double>(tv.tv_usec); |
2516 | + return now; |
2517 | +} |
2518 | + |
2519 | +std::string |
2520 | +Util::appname_from_path(const std::string& path) |
2521 | +{ |
2522 | + std::string::size_type slashPos = path.rfind("/"); |
2523 | + std::string::size_type startPos(0); |
2524 | + if (slashPos != std::string::npos) |
2525 | + { |
2526 | + startPos = slashPos + 1; |
2527 | + } |
2528 | + return std::string(path, startPos, std::string::npos); |
2529 | +} |
2530 | + |
2531 | +#ifndef ANDROID |
2532 | + |
2533 | +std::istream * |
2534 | +Util::get_resource(const std::string &path) |
2535 | +{ |
2536 | + std::ifstream *ifs = new std::ifstream(path.c_str()); |
2537 | + |
2538 | + return static_cast<std::istream *>(ifs); |
2539 | +} |
2540 | + |
2541 | +void |
2542 | +Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec) |
2543 | +{ |
2544 | + DIR* dir = opendir(dirName.c_str()); |
2545 | + if (!dir) |
2546 | + { |
2547 | + Log::error("Failed to open models directory '%s'\n", dirName.c_str()); |
2548 | + return; |
2549 | + } |
2550 | + |
2551 | + struct dirent* entry = readdir(dir); |
2552 | + while (entry) |
2553 | + { |
2554 | + std::string pathname(dirName + "/"); |
2555 | + pathname += std::string(entry->d_name); |
2556 | + // Skip '.' and '..' |
2557 | + if (entry->d_name[0] != '.') |
2558 | + { |
2559 | + fileVec.push_back(pathname); |
2560 | + } |
2561 | + entry = readdir(dir); |
2562 | + } |
2563 | + closedir(dir); |
2564 | +} |
2565 | + |
2566 | +#else |
2567 | + |
2568 | +AAssetManager *Util::android_asset_manager = 0; |
2569 | + |
2570 | +void |
2571 | +Util::android_set_asset_manager(AAssetManager *asset_manager) |
2572 | +{ |
2573 | + Util::android_asset_manager = asset_manager; |
2574 | +} |
2575 | + |
2576 | +AAssetManager * |
2577 | +Util::android_get_asset_manager() |
2578 | +{ |
2579 | + return Util::android_asset_manager; |
2580 | +} |
2581 | + |
2582 | +std::istream * |
2583 | +Util::get_resource(const std::string &path) |
2584 | +{ |
2585 | + std::string path2(path); |
2586 | + /* Remove leading '/' from path name, it confuses the AssetManager */ |
2587 | + if (path2.size() > 0 && path2[0] == '/') |
2588 | + path2.erase(0, 1); |
2589 | + |
2590 | + std::stringstream *ss = new std::stringstream; |
2591 | + AAsset *asset = AAssetManager_open(Util::android_asset_manager, |
2592 | + path2.c_str(), AASSET_MODE_RANDOM); |
2593 | + if (asset) { |
2594 | + ss->write(reinterpret_cast<const char *>(AAsset_getBuffer(asset)), |
2595 | + AAsset_getLength(asset)); |
2596 | + Log::debug("Load asset %s\n", path2.c_str()); |
2597 | + AAsset_close(asset); |
2598 | + } |
2599 | + else { |
2600 | + Log::error("Couldn't load asset %s\n", path2.c_str()); |
2601 | + } |
2602 | + |
2603 | + return static_cast<std::istream *>(ss); |
2604 | +} |
2605 | + |
2606 | +void |
2607 | +Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec) |
2608 | +{ |
2609 | + AAssetManager *mgr(Util::android_get_asset_manager()); |
2610 | + std::string dir_name(dirName); |
2611 | + |
2612 | + /* Remove leading '/' from path, it confuses the AssetManager */ |
2613 | + if (dir_name.size() > 0 && dir_name[0] == '/') |
2614 | + dir_name.erase(0, 1); |
2615 | + |
2616 | + AAssetDir* dir = AAssetManager_openDir(mgr, dir_name.c_str()); |
2617 | + if (!dir) |
2618 | + { |
2619 | + Log::error("Failed to open models directory '%s'\n", dir_name.c_str()); |
2620 | + return; |
2621 | + } |
2622 | + |
2623 | + const char *filename(0); |
2624 | + while ((filename = AAssetDir_getNextFileName(dir)) != 0) |
2625 | + { |
2626 | + std::string pathname(dir_name + "/"); |
2627 | + pathname += std::string(filename); |
2628 | + fileVec.push_back(pathname); |
2629 | + } |
2630 | + AAssetDir_close(dir); |
2631 | +} |
2632 | +#endif |
2633 | |
2634 | === added file 'src/libmatrix/util.h' |
2635 | --- src/libmatrix/util.h 1970-01-01 00:00:00 +0000 |
2636 | +++ src/libmatrix/util.h 2012-01-27 22:25:27 +0000 |
2637 | @@ -0,0 +1,71 @@ |
2638 | +// |
2639 | +// Copyright (c) 2010-2011 Linaro Limited |
2640 | +// |
2641 | +// All rights reserved. This program and the accompanying materials |
2642 | +// are made available under the terms of the MIT License which accompanies |
2643 | +// this distribution, and is available at |
2644 | +// http://www.opensource.org/licenses/mit-license.php |
2645 | +// |
2646 | +// Contributors: |
2647 | +// Alexandros Frantzis <alexandros.frantzis@linaro.org> |
2648 | +// Jesse Barker <jesse.barker@linaro.org> |
2649 | +// |
2650 | +#ifndef UTIL_H_ |
2651 | +#define UTIL_H_ |
2652 | + |
2653 | +#include <string> |
2654 | +#include <vector> |
2655 | +#include <istream> |
2656 | +#include <sstream> |
2657 | +#include <stdint.h> |
2658 | + |
2659 | +#ifdef ANDROID |
2660 | +#include <android/asset_manager_jni.h> |
2661 | +#endif |
2662 | + |
2663 | +struct Util { |
2664 | + static void split(const std::string &s, char delim, std::vector<std::string> &elems); |
2665 | + static uint64_t get_timestamp_us(); |
2666 | + static std::istream *get_resource(const std::string &path); |
2667 | + static void list_files(const std::string& dirName, std::vector<std::string>& fileVec); |
2668 | + template <class T> static void dispose_pointer_vector(std::vector<T*> &vec) |
2669 | + { |
2670 | + for (typename std::vector<T*>::const_iterator iter = vec.begin(); |
2671 | + iter != vec.end(); |
2672 | + iter++) |
2673 | + { |
2674 | + delete *iter; |
2675 | + } |
2676 | + |
2677 | + vec.clear(); |
2678 | + } |
2679 | + template<typename T> |
2680 | + static T |
2681 | + fromString(const std::string& asString) |
2682 | + { |
2683 | + std::stringstream ss(asString); |
2684 | + T retVal; |
2685 | + ss >> retVal; |
2686 | + return retVal; |
2687 | + } |
2688 | + |
2689 | + template<typename T> |
2690 | + static std::string |
2691 | + toString(const T t) |
2692 | + { |
2693 | + std::stringstream ss; |
2694 | + ss << t; |
2695 | + return ss.str(); |
2696 | + } |
2697 | + static std::string |
2698 | + appname_from_path(const std::string& path); |
2699 | + |
2700 | +#ifdef ANDROID |
2701 | + static void android_set_asset_manager(AAssetManager *asset_manager); |
2702 | + static AAssetManager *android_get_asset_manager(void); |
2703 | +private: |
2704 | + static AAssetManager *android_asset_manager; |
2705 | +#endif |
2706 | +}; |
2707 | + |
2708 | +#endif /* UTIL_H */ |
2709 | |
2710 | === modified file 'src/libmatrix/vec.h' |
2711 | --- src/libmatrix/vec.h 2011-01-17 18:15:21 +0000 |
2712 | +++ src/libmatrix/vec.h 2012-01-27 22:25:27 +0000 |
2713 | @@ -1,5 +1,5 @@ |
2714 | // |
2715 | -// Copyright (c) 2010 Linaro Limited |
2716 | +// Copyright (c) 2010-2011 Linaro Limited |
2717 | // |
2718 | // All rights reserved. This program and the accompanying materials |
2719 | // are made available under the terms of the MIT License which accompanies |
2720 | @@ -17,6 +17,10 @@ |
2721 | |
2722 | namespace LibMatrix |
2723 | { |
2724 | +// A template class for creating, managing and operating on a 2-element vector |
2725 | +// of any type you like (intended for built-in types, but as long as it |
2726 | +// supports the basic arithmetic and assignment operators, any type should |
2727 | +// work). |
2728 | template<typename T> |
2729 | class tvec2 |
2730 | { |
2731 | @@ -24,10 +28,10 @@ |
2732 | tvec2() : |
2733 | x_(0), |
2734 | y_(0) {} |
2735 | - tvec2(T t) : |
2736 | + tvec2(const T t) : |
2737 | x_(t), |
2738 | y_(t) {} |
2739 | - tvec2(T x, T y) : |
2740 | + tvec2(const T x, const T y) : |
2741 | x_(x), |
2742 | y_(y) {} |
2743 | tvec2(const tvec2& v) : |
2744 | @@ -35,18 +39,26 @@ |
2745 | y_(v.y_) {} |
2746 | ~tvec2() {} |
2747 | |
2748 | + // Print the elements of the vector to standard out. |
2749 | + // Really only useful for debug and test. |
2750 | void print() const |
2751 | { |
2752 | std::cout << "| " << x_ << " " << y_ << " |" << std::endl; |
2753 | } |
2754 | + |
2755 | + // Allow raw data access for API calls and the like. |
2756 | + // For example, it is valid to pass a tvec2<float> into a call to |
2757 | + // the OpenGL command "glUniform2fv()". |
2758 | operator const T*() const { return &x_;} |
2759 | |
2760 | + // Get and set access members for the individual elements. |
2761 | const T x() const { return x_; } |
2762 | const T y() const { return y_; } |
2763 | |
2764 | void x(const T& val) { x_ = val; } |
2765 | void y(const T& val) { y_ = val; } |
2766 | |
2767 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
2768 | tvec2& operator=(const tvec2& rhs) |
2769 | { |
2770 | if (this != &rhs) |
2771 | @@ -57,6 +69,7 @@ |
2772 | return *this; |
2773 | } |
2774 | |
2775 | + // Divide this by a scalar. Return a reference to this. |
2776 | tvec2& operator/=(const T& rhs) |
2777 | { |
2778 | x_ /= rhs; |
2779 | @@ -64,11 +77,29 @@ |
2780 | return *this; |
2781 | } |
2782 | |
2783 | - const tvec2 operator/(const T& rhs) |
2784 | - { |
2785 | - return tvec2(*this) /= rhs; |
2786 | - } |
2787 | - |
2788 | + // Divide a copy of this by a scalar. Return the copy. |
2789 | + const tvec2 operator/(const T& rhs) const |
2790 | + { |
2791 | + return tvec2(*this) /= rhs; |
2792 | + } |
2793 | + |
2794 | + // Component-wise divide of this by another vector. |
2795 | + // Return a reference to this. |
2796 | + tvec2& operator/=(const tvec2& rhs) |
2797 | + { |
2798 | + x_ /= rhs.x_; |
2799 | + y_ /= rhs.y_; |
2800 | + return *this; |
2801 | + } |
2802 | + |
2803 | + // Component-wise divide of a copy of this by another vector. |
2804 | + // Return the copy. |
2805 | + const tvec2 operator/(const tvec2& rhs) const |
2806 | + { |
2807 | + return tvec2(*this) /= rhs; |
2808 | + } |
2809 | + |
2810 | + // Multiply this by a scalar. Return a reference to this. |
2811 | tvec2& operator*=(const T& rhs) |
2812 | { |
2813 | x_ *= rhs; |
2814 | @@ -76,11 +107,29 @@ |
2815 | return *this; |
2816 | } |
2817 | |
2818 | - const tvec2 operator*(const T& rhs) |
2819 | - { |
2820 | - return tvec2(*this) *= rhs; |
2821 | - } |
2822 | - |
2823 | + // Multiply a copy of this by a scalar. Return the copy. |
2824 | + const tvec2 operator*(const T& rhs) const |
2825 | + { |
2826 | + return tvec2(*this) *= rhs; |
2827 | + } |
2828 | + |
2829 | + // Component-wise multiply of this by another vector. |
2830 | + // Return a reference to this. |
2831 | + tvec2& operator*=(const tvec2& rhs) |
2832 | + { |
2833 | + x_ *= rhs.x_; |
2834 | + y_ *= rhs.y_; |
2835 | + return *this; |
2836 | + } |
2837 | + |
2838 | + // Component-wise multiply of a copy of this by another vector. |
2839 | + // Return the copy. |
2840 | + const tvec2 operator*(const tvec2& rhs) const |
2841 | + { |
2842 | + return tvec2(*this) *= rhs; |
2843 | + } |
2844 | + |
2845 | + // Add a scalar to this. Return a reference to this. |
2846 | tvec2& operator+=(const T& rhs) |
2847 | { |
2848 | x_ += rhs; |
2849 | @@ -88,11 +137,14 @@ |
2850 | return *this; |
2851 | } |
2852 | |
2853 | - const tvec2 operator+(const T& rhs) |
2854 | + // Add a scalar to a copy of this. Return the copy. |
2855 | + const tvec2 operator+(const T& rhs) const |
2856 | { |
2857 | return tvec2(*this) += rhs; |
2858 | } |
2859 | |
2860 | + // Component-wise addition of another vector to this. |
2861 | + // Return a reference to this. |
2862 | tvec2& operator+=(const tvec2& rhs) |
2863 | { |
2864 | x_ += rhs.x_; |
2865 | @@ -100,11 +152,14 @@ |
2866 | return *this; |
2867 | } |
2868 | |
2869 | - const tvec2 operator+(const tvec2& rhs) |
2870 | + // Component-wise addition of another vector to a copy of this. |
2871 | + // Return the copy. |
2872 | + const tvec2 operator+(const tvec2& rhs) const |
2873 | { |
2874 | return tvec2(*this) += rhs; |
2875 | } |
2876 | |
2877 | + // Subtract a scalar from this. Return a reference to this. |
2878 | tvec2& operator-=(const T& rhs) |
2879 | { |
2880 | x_ -= rhs; |
2881 | @@ -112,11 +167,14 @@ |
2882 | return *this; |
2883 | } |
2884 | |
2885 | - const tvec2 operator-(const T& rhs) |
2886 | + // Subtract a scalar from a copy of this. Return the copy. |
2887 | + const tvec2 operator-(const T& rhs) const |
2888 | { |
2889 | return tvec2(*this) -= rhs; |
2890 | } |
2891 | |
2892 | + // Component-wise subtraction of another vector from this. |
2893 | + // Return a reference to this. |
2894 | tvec2& operator-=(const tvec2& rhs) |
2895 | { |
2896 | x_ -= rhs.x_; |
2897 | @@ -124,16 +182,20 @@ |
2898 | return *this; |
2899 | } |
2900 | |
2901 | - const tvec2 operator-(const tvec2& rhs) |
2902 | + // Component-wise subtraction of another vector from a copy of this. |
2903 | + // Return the copy. |
2904 | + const tvec2 operator-(const tvec2& rhs) const |
2905 | { |
2906 | return tvec2(*this) -= rhs; |
2907 | } |
2908 | |
2909 | + // Compute the length of this and return it. |
2910 | float length() const |
2911 | { |
2912 | return sqrt(dot(*this, *this)); |
2913 | } |
2914 | |
2915 | + // Make this a unit vector. |
2916 | void normalize() |
2917 | { |
2918 | float l = length(); |
2919 | @@ -141,6 +203,7 @@ |
2920 | y_ /= l; |
2921 | } |
2922 | |
2923 | + // Compute the dot product of two vectors. |
2924 | static T dot(const tvec2& v1, const tvec2& v2) |
2925 | { |
2926 | return (v1.x_ * v2.x_) + (v1.y_ * v2.y_); |
2927 | @@ -151,6 +214,10 @@ |
2928 | T y_; |
2929 | }; |
2930 | |
2931 | +// A template class for creating, managing and operating on a 3-element vector |
2932 | +// of any type you like (intended for built-in types, but as long as it |
2933 | +// supports the basic arithmetic and assignment operators, any type should |
2934 | +// work). |
2935 | template<typename T> |
2936 | class tvec3 |
2937 | { |
2938 | @@ -159,11 +226,11 @@ |
2939 | x_(0), |
2940 | y_(0), |
2941 | z_(0) {} |
2942 | - tvec3(T t) : |
2943 | + tvec3(const T t) : |
2944 | x_(t), |
2945 | y_(t), |
2946 | z_(t) {} |
2947 | - tvec3(T x, T y, T z) : |
2948 | + tvec3(const T x, const T y, const T z) : |
2949 | x_(x), |
2950 | y_(y), |
2951 | z_(z) {} |
2952 | @@ -173,12 +240,19 @@ |
2953 | z_(v.z_) {} |
2954 | ~tvec3() {} |
2955 | |
2956 | + // Print the elements of the vector to standard out. |
2957 | + // Really only useful for debug and test. |
2958 | void print() const |
2959 | { |
2960 | std::cout << "| " << x_ << " " << y_ << " " << z_ << " |" << std::endl; |
2961 | } |
2962 | + |
2963 | + // Allow raw data access for API calls and the like. |
2964 | + // For example, it is valid to pass a tvec3<float> into a call to |
2965 | + // the OpenGL command "glUniform3fv()". |
2966 | operator const T*() const { return &x_;} |
2967 | |
2968 | + // Get and set access members for the individual elements. |
2969 | const T x() const { return x_; } |
2970 | const T y() const { return y_; } |
2971 | const T z() const { return z_; } |
2972 | @@ -187,6 +261,7 @@ |
2973 | void y(const T& val) { y_ = val; } |
2974 | void z(const T& val) { z_ = val; } |
2975 | |
2976 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
2977 | tvec3& operator=(const tvec3& rhs) |
2978 | { |
2979 | if (this != &rhs) |
2980 | @@ -198,6 +273,7 @@ |
2981 | return *this; |
2982 | } |
2983 | |
2984 | + // Divide this by a scalar. Return a reference to this. |
2985 | tvec3& operator/=(const T& rhs) |
2986 | { |
2987 | x_ /= rhs; |
2988 | @@ -206,11 +282,30 @@ |
2989 | return *this; |
2990 | } |
2991 | |
2992 | - const tvec3 operator/(const T& rhs) |
2993 | - { |
2994 | - return tvec3(*this) /= rhs; |
2995 | - } |
2996 | - |
2997 | + // Divide a copy of this by a scalar. Return the copy. |
2998 | + const tvec3 operator/(const T& rhs) const |
2999 | + { |
3000 | + return tvec3(*this) /= rhs; |
3001 | + } |
3002 | + |
3003 | + // Component-wise divide of this by another vector. |
3004 | + // Return a reference to this. |
3005 | + tvec3& operator/=(const tvec3& rhs) |
3006 | + { |
3007 | + x_ /= rhs.x_; |
3008 | + y_ /= rhs.y_; |
3009 | + z_ /= rhs.z_; |
3010 | + return *this; |
3011 | + } |
3012 | + |
3013 | + // Component-wise divide of a copy of this by another vector. |
3014 | + // Return the copy. |
3015 | + const tvec3 operator/(const tvec3& rhs) const |
3016 | + { |
3017 | + return tvec3(*this) /= rhs; |
3018 | + } |
3019 | + |
3020 | + // Multiply this by a scalar. Return a reference to this. |
3021 | tvec3& operator*=(const T& rhs) |
3022 | { |
3023 | x_ *= rhs; |
3024 | @@ -219,11 +314,30 @@ |
3025 | return *this; |
3026 | } |
3027 | |
3028 | - const tvec3 operator*(const T& rhs) |
3029 | - { |
3030 | - return tvec3(*this) *= rhs; |
3031 | - } |
3032 | - |
3033 | + // Multiply a copy of this by a scalar. Return the copy. |
3034 | + const tvec3 operator*(const T& rhs) const |
3035 | + { |
3036 | + return tvec3(*this) *= rhs; |
3037 | + } |
3038 | + |
3039 | + // Component-wise multiply of this by another vector. |
3040 | + // Return a reference to this. |
3041 | + tvec3& operator*=(const tvec3& rhs) |
3042 | + { |
3043 | + x_ *= rhs.x_; |
3044 | + y_ *= rhs.y_; |
3045 | + z_ *= rhs.z_; |
3046 | + return *this; |
3047 | + } |
3048 | + |
3049 | + // Component-wise multiply of a copy of this by another vector. |
3050 | + // Return the copy. |
3051 | + const tvec3 operator*(const tvec3& rhs) const |
3052 | + { |
3053 | + return tvec3(*this) *= rhs; |
3054 | + } |
3055 | + |
3056 | + // Add a scalar to this. Return a reference to this. |
3057 | tvec3& operator+=(const T& rhs) |
3058 | { |
3059 | x_ += rhs; |
3060 | @@ -232,11 +346,14 @@ |
3061 | return *this; |
3062 | } |
3063 | |
3064 | - const tvec3 operator+(const T& rhs) |
3065 | + // Add a scalar to a copy of this. Return the copy. |
3066 | + const tvec3 operator+(const T& rhs) const |
3067 | { |
3068 | return tvec3(*this) += rhs; |
3069 | } |
3070 | |
3071 | + // Component-wise addition of another vector to this. |
3072 | + // Return a reference to this. |
3073 | tvec3& operator+=(const tvec3& rhs) |
3074 | { |
3075 | x_ += rhs.x_; |
3076 | @@ -245,11 +362,14 @@ |
3077 | return *this; |
3078 | } |
3079 | |
3080 | - const tvec3 operator+(const tvec3& rhs) |
3081 | + // Component-wise addition of another vector to a copy of this. |
3082 | + // Return the copy. |
3083 | + const tvec3 operator+(const tvec3& rhs) const |
3084 | { |
3085 | return tvec3(*this) += rhs; |
3086 | } |
3087 | |
3088 | + // Subtract a scalar from this. Return a reference to this. |
3089 | tvec3& operator-=(const T& rhs) |
3090 | { |
3091 | x_ -= rhs; |
3092 | @@ -258,11 +378,14 @@ |
3093 | return *this; |
3094 | } |
3095 | |
3096 | - const tvec3 operator-(const T& rhs) |
3097 | + // Subtract a scalar from a copy of this. Return the copy. |
3098 | + const tvec3 operator-(const T& rhs) const |
3099 | { |
3100 | return tvec3(*this) -= rhs; |
3101 | } |
3102 | |
3103 | + // Component-wise subtraction of another vector from this. |
3104 | + // Return a reference to this. |
3105 | tvec3& operator-=(const tvec3& rhs) |
3106 | { |
3107 | x_ -= rhs.x_; |
3108 | @@ -271,16 +394,20 @@ |
3109 | return *this; |
3110 | } |
3111 | |
3112 | - const tvec3 operator-(const tvec3& rhs) |
3113 | + // Component-wise subtraction of another vector from a copy of this. |
3114 | + // Return the copy. |
3115 | + const tvec3 operator-(const tvec3& rhs) const |
3116 | { |
3117 | return tvec3(*this) -= rhs; |
3118 | } |
3119 | |
3120 | + // Compute the length of this and return it. |
3121 | float length() const |
3122 | { |
3123 | return sqrt(dot(*this, *this)); |
3124 | } |
3125 | |
3126 | + // Make this a unit vector. |
3127 | void normalize() |
3128 | { |
3129 | float l = length(); |
3130 | @@ -289,11 +416,13 @@ |
3131 | z_ /= l; |
3132 | } |
3133 | |
3134 | + // Compute the dot product of two vectors. |
3135 | static T dot(const tvec3& v1, const tvec3& v2) |
3136 | { |
3137 | return (v1.x_ * v2.x_) + (v1.y_ * v2.y_) + (v1.z_ * v2.z_); |
3138 | } |
3139 | |
3140 | + // Compute the cross product of two vectors. |
3141 | static tvec3 cross(const tvec3& u, const tvec3& v) |
3142 | { |
3143 | return tvec3((u.y_ * v.z_) - (u.z_ * v.y_), |
3144 | @@ -307,6 +436,10 @@ |
3145 | T z_; |
3146 | }; |
3147 | |
3148 | +// A template class for creating, managing and operating on a 4-element vector |
3149 | +// of any type you like (intended for built-in types, but as long as it |
3150 | +// supports the basic arithmetic and assignment operators, any type should |
3151 | +// work). |
3152 | template<typename T> |
3153 | class tvec4 |
3154 | { |
3155 | @@ -316,12 +449,12 @@ |
3156 | y_(0), |
3157 | z_(0), |
3158 | w_(0) {} |
3159 | - tvec4(T t) : |
3160 | + tvec4(const T t) : |
3161 | x_(t), |
3162 | y_(t), |
3163 | z_(t), |
3164 | w_(t) {} |
3165 | - tvec4(T x, T y, T z, T w) : |
3166 | + tvec4(const T x, const T y, const T z, const T w) : |
3167 | x_(x), |
3168 | y_(y), |
3169 | z_(z), |
3170 | @@ -333,12 +466,19 @@ |
3171 | w_(v.w_) {} |
3172 | ~tvec4() {} |
3173 | |
3174 | + // Print the elements of the vector to standard out. |
3175 | + // Really only useful for debug and test. |
3176 | void print() const |
3177 | { |
3178 | std::cout << "| " << x_ << " " << y_ << " " << z_ << " " << w_ << " |" << std::endl; |
3179 | } |
3180 | + |
3181 | + // Allow raw data access for API calls and the like. |
3182 | + // For example, it is valid to pass a tvec4<float> into a call to |
3183 | + // the OpenGL command "glUniform4fv()". |
3184 | operator const T*() const { return &x_;} |
3185 | |
3186 | + // Get and set access members for the individual elements. |
3187 | const T x() const { return x_; } |
3188 | const T y() const { return y_; } |
3189 | const T z() const { return z_; } |
3190 | @@ -349,6 +489,7 @@ |
3191 | void z(const T& val) { z_ = val; } |
3192 | void w(const T& val) { w_ = val; } |
3193 | |
3194 | + // A direct assignment of 'rhs' to this. Return a reference to this. |
3195 | tvec4& operator=(const tvec4& rhs) |
3196 | { |
3197 | if (this != &rhs) |
3198 | @@ -361,6 +502,7 @@ |
3199 | return *this; |
3200 | } |
3201 | |
3202 | + // Divide this by a scalar. Return a reference to this. |
3203 | tvec4& operator/=(const T& rhs) |
3204 | { |
3205 | x_ /= rhs; |
3206 | @@ -370,11 +512,31 @@ |
3207 | return *this; |
3208 | } |
3209 | |
3210 | - const tvec4 operator/(const T& rhs) |
3211 | - { |
3212 | - return tvec4(*this) /= rhs; |
3213 | - } |
3214 | - |
3215 | + // Divide a copy of this by a scalar. Return the copy. |
3216 | + const tvec4 operator/(const T& rhs) const |
3217 | + { |
3218 | + return tvec4(*this) /= rhs; |
3219 | + } |
3220 | + |
3221 | + // Component-wise divide of this by another vector. |
3222 | + // Return a reference to this. |
3223 | + tvec4& operator/=(const tvec4& rhs) |
3224 | + { |
3225 | + x_ /= rhs.x_; |
3226 | + y_ /= rhs.y_; |
3227 | + z_ /= rhs.z_; |
3228 | + w_ /= rhs.w_; |
3229 | + return *this; |
3230 | + } |
3231 | + |
3232 | + // Component-wise divide of a copy of this by another vector. |
3233 | + // Return the copy. |
3234 | + const tvec4 operator/(const tvec4& rhs) const |
3235 | + { |
3236 | + return tvec4(*this) /= rhs; |
3237 | + } |
3238 | + |
3239 | + // Multiply this by a scalar. Return a reference to this. |
3240 | tvec4& operator*=(const T& rhs) |
3241 | { |
3242 | x_ *= rhs; |
3243 | @@ -384,11 +546,31 @@ |
3244 | return *this; |
3245 | } |
3246 | |
3247 | - const tvec4 operator*(const T& rhs) |
3248 | - { |
3249 | - return tvec4(*this) *= rhs; |
3250 | - } |
3251 | - |
3252 | + // Multiply a copy of this by a scalar. Return the copy. |
3253 | + const tvec4 operator*(const T& rhs) const |
3254 | + { |
3255 | + return tvec4(*this) *= rhs; |
3256 | + } |
3257 | + |
3258 | + // Component-wise multiply of this by another vector. |
3259 | + // Return a reference to this. |
3260 | + tvec4& operator*=(const tvec4& rhs) |
3261 | + { |
3262 | + x_ *= rhs.x_; |
3263 | + y_ *= rhs.y_; |
3264 | + z_ *= rhs.z_; |
3265 | + w_ *= rhs.w_; |
3266 | + return *this; |
3267 | + } |
3268 | + |
3269 | + // Component-wise multiply of a copy of this by another vector. |
3270 | + // Return the copy. |
3271 | + const tvec4 operator*(const tvec4& rhs) const |
3272 | + { |
3273 | + return tvec4(*this) *= rhs; |
3274 | + } |
3275 | + |
3276 | + // Add a scalar to this. Return a reference to this. |
3277 | tvec4& operator+=(const T& rhs) |
3278 | { |
3279 | x_ += rhs; |
3280 | @@ -398,11 +580,14 @@ |
3281 | return *this; |
3282 | } |
3283 | |
3284 | - const tvec4 operator+(const T& rhs) |
3285 | + // Add a scalar to a copy of this. Return the copy. |
3286 | + const tvec4 operator+(const T& rhs) const |
3287 | { |
3288 | return tvec4(*this) += rhs; |
3289 | } |
3290 | |
3291 | + // Component-wise addition of another vector to this. |
3292 | + // Return a reference to this. |
3293 | tvec4& operator+=(const tvec4& rhs) |
3294 | { |
3295 | x_ += rhs.x_; |
3296 | @@ -412,11 +597,14 @@ |
3297 | return *this; |
3298 | } |
3299 | |
3300 | - const tvec4 operator+(const tvec4& rhs) |
3301 | + // Component-wise addition of another vector to a copy of this. |
3302 | + // Return the copy. |
3303 | + const tvec4 operator+(const tvec4& rhs) const |
3304 | { |
3305 | return tvec4(*this) += rhs; |
3306 | } |
3307 | |
3308 | + // Subtract a scalar from this. Return a reference to this. |
3309 | tvec4& operator-=(const T& rhs) |
3310 | { |
3311 | x_ -= rhs; |
3312 | @@ -426,11 +614,14 @@ |
3313 | return *this; |
3314 | } |
3315 | |
3316 | - const tvec4 operator-(const T& rhs) |
3317 | + // Subtract a scalar from a copy of this. Return the copy. |
3318 | + const tvec4 operator-(const T& rhs) const |
3319 | { |
3320 | return tvec4(*this) -= rhs; |
3321 | } |
3322 | |
3323 | + // Component-wise subtraction of another vector from this. |
3324 | + // Return a reference to this. |
3325 | tvec4& operator-=(const tvec4& rhs) |
3326 | { |
3327 | x_ -= rhs.x_; |
3328 | @@ -440,16 +631,20 @@ |
3329 | return *this; |
3330 | } |
3331 | |
3332 | - const tvec4 operator-(const tvec4& rhs) |
3333 | + // Component-wise subtraction of another vector from a copy of this. |
3334 | + // Return the copy. |
3335 | + const tvec4 operator-(const tvec4& rhs) const |
3336 | { |
3337 | return tvec4(*this) -= rhs; |
3338 | } |
3339 | |
3340 | + // Compute the length of this and return it. |
3341 | float length() const |
3342 | { |
3343 | return sqrt(dot(*this, *this)); |
3344 | } |
3345 | |
3346 | + // Make this a unit vector. |
3347 | void normalize() |
3348 | { |
3349 | float l = length(); |
3350 | @@ -459,6 +654,7 @@ |
3351 | w_ /= l; |
3352 | } |
3353 | |
3354 | + // Compute the dot product of two vectors. |
3355 | static T dot(const tvec4& v1, const tvec4& v2) |
3356 | { |
3357 | return (v1.x_ * v2.x_) + (v1.y_ * v2.y_) + (v1.z_ * v2.z_) + (v1.w_ * v2.w_); |
3358 | @@ -497,4 +693,24 @@ |
3359 | |
3360 | } // namespace LibMatrix |
3361 | |
3362 | +// Global operators to allow for things like defining a new vector in terms of |
3363 | +// a product of a scalar and a vector |
3364 | +template<typename T> |
3365 | +const LibMatrix::tvec2<T> operator*(const T t, const LibMatrix::tvec2<T>& v) |
3366 | +{ |
3367 | + return v * t; |
3368 | +} |
3369 | + |
3370 | +template<typename T> |
3371 | +const LibMatrix::tvec3<T> operator*(const T t, const LibMatrix::tvec3<T>& v) |
3372 | +{ |
3373 | + return v * t; |
3374 | +} |
3375 | + |
3376 | +template<typename T> |
3377 | +const LibMatrix::tvec4<T> operator*(const T t, const LibMatrix::tvec4<T>& v) |
3378 | +{ |
3379 | + return v * t; |
3380 | +} |
3381 | + |
3382 | #endif // VEC_H_ |
3383 | |
3384 | === removed file 'src/log.cc' |
3385 | --- src/log.cc 2011-01-17 18:15:21 +0000 |
3386 | +++ src/log.cc 1970-01-01 00:00:00 +0000 |
3387 | @@ -1,58 +0,0 @@ |
3388 | -/* |
3389 | - * Copyright © 2011 Linaro Limited |
3390 | - * |
3391 | - * This file is part of glcompbench. |
3392 | - * |
3393 | - * glcompbench is free software: you can redistribute it and/or modify |
3394 | - * it under the terms of the GNU General Public License as published by |
3395 | - * the Free Software Foundation, either version 3 of the License, or |
3396 | - * (at your option) any later version. |
3397 | - * |
3398 | - * glcompbench is distributed in the hope that it will be useful, |
3399 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
3400 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3401 | - * GNU General Public License for more details. |
3402 | - * |
3403 | - * You should have received a copy of the GNU General Public License |
3404 | - * along with glcompbench. If not, see <http://www.gnu.org/licenses/>. |
3405 | - * |
3406 | - * Authors: |
3407 | - * Alexandros Frantzis <alexandros.frantzis@linaro.org> |
3408 | - * Jesse Barker <jesse.barker@linaro.org> |
3409 | - */ |
3410 | - |
3411 | -#include <cstdio> |
3412 | -#include <cstdarg> |
3413 | - |
3414 | -#include "options.h" |
3415 | -#include "log.h" |
3416 | - |
3417 | -void |
3418 | -Log::info(const char *fmt, ...) |
3419 | -{ |
3420 | - va_list ap; |
3421 | - va_start(ap, fmt); |
3422 | - vfprintf(stdout, fmt, ap); |
3423 | - va_end(ap); |
3424 | -} |
3425 | - |
3426 | -void |
3427 | -Log::debug(const char *fmt, ...) |
3428 | -{ |
3429 | - if (!Options::show_debug) |
3430 | - return; |
3431 | - va_list ap; |
3432 | - va_start(ap, fmt); |
3433 | - vfprintf(stdout, fmt, ap); |
3434 | - va_end(ap); |
3435 | -} |
3436 | - |
3437 | -void |
3438 | -Log::error(const char *fmt, ...) |
3439 | -{ |
3440 | - va_list ap; |
3441 | - va_start(ap, fmt); |
3442 | - vfprintf(stderr, fmt, ap); |
3443 | - va_end(ap); |
3444 | -} |
3445 | - |
3446 | |
3447 | === removed file 'src/log.h' |
3448 | --- src/log.h 2011-01-17 18:15:21 +0000 |
3449 | +++ src/log.h 1970-01-01 00:00:00 +0000 |
3450 | @@ -1,35 +0,0 @@ |
3451 | -/* |
3452 | - * Copyright © 2011 Linaro Limited |
3453 | - * |
3454 | - * This file is part of glcompbench. |
3455 | - * |
3456 | - * glcompbench is free software: you can redistribute it and/or modify |
3457 | - * it under the terms of the GNU General Public License as published by |
3458 | - * the Free Software Foundation, either version 3 of the License, or |
3459 | - * (at your option) any later version. |
3460 | - * |
3461 | - * glcompbench is distributed in the hope that it will be useful, |
3462 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
3463 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3464 | - * GNU General Public License for more details. |
3465 | - * |
3466 | - * You should have received a copy of the GNU General Public License |
3467 | - * along with glcompbench. If not, see <http://www.gnu.org/licenses/>. |
3468 | - * |
3469 | - * Authors: |
3470 | - * Alexandros Frantzis <alexandros.frantzis@linaro.org> |
3471 | - * Jesse Barker <jesse.barker@linaro.org> |
3472 | - */ |
3473 | - |
3474 | -#ifndef LOG_H_ |
3475 | -#define LOG_H_ |
3476 | - |
3477 | -class Log |
3478 | -{ |
3479 | -public: |
3480 | - static void info(const char *fmt, ...); |
3481 | - static void debug(const char *fmt, ...); |
3482 | - static void error(const char *fmt, ...); |
3483 | -}; |
3484 | - |
3485 | -#endif /* LOG_H_ */ |
3486 | |
3487 | === removed file 'src/shader-source.cc' |
3488 | --- src/shader-source.cc 2011-12-12 12:43:08 +0000 |
3489 | +++ src/shader-source.cc 1970-01-01 00:00:00 +0000 |
3490 | @@ -1,625 +0,0 @@ |
3491 | -/* |
3492 | - * Copyright © 2010-2011 Linaro Limited |
3493 | - * |
3494 | - * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. |
3495 | - * |
3496 | - * glmark2 is free software: you can redistribute it and/or modify it under the |
3497 | - * terms of the GNU General Public License as published by the Free Software |
3498 | - * Foundation, either version 3 of the License, or (at your option) any later |
3499 | - * version. |
3500 | - * |
3501 | - * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY |
3502 | - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
3503 | - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
3504 | - * details. |
3505 | - * |
3506 | - * You should have received a copy of the GNU General Public License along with |
3507 | - * glmark2. If not, see <http://www.gnu.org/licenses/>. |
3508 | - * |
3509 | - * Authors: |
3510 | - * Alexandros Frantzis (glmark2) |
3511 | - */ |
3512 | - |
3513 | -#include <istream> |
3514 | -#include <memory> |
3515 | - |
3516 | -#include "shader-source.h" |
3517 | -#include "log.h" |
3518 | -#include "vec.h" |
3519 | -#include "util.h" |
3520 | - |
3521 | -/** |
3522 | - * Holds default precision values for all shader types |
3523 | - * (even the unknown type, which is hardwired to default precision values) |
3524 | - */ |
3525 | -std::vector<ShaderSource::Precision> |
3526 | -ShaderSource::default_precision_(ShaderSource::ShaderTypeUnknown + 1); |
3527 | - |
3528 | -/** |
3529 | - * Loads the contents of a file into a string. |
3530 | - * |
3531 | - * @param filename the name of the file |
3532 | - * @param str the string to put the contents of the file into |
3533 | - */ |
3534 | -bool |
3535 | -ShaderSource::load_file(const std::string& filename, std::string& str) |
3536 | -{ |
3537 | - std::auto_ptr<std::istream> is_ptr(Util::get_resource(filename)); |
3538 | - std::istream& inputFile(*is_ptr); |
3539 | - |
3540 | - if (!inputFile) |
3541 | - { |
3542 | - Log::error("Failed to open \"%s\"\n", filename.c_str()); |
3543 | - return false; |
3544 | - } |
3545 | - |
3546 | - std::string curLine; |
3547 | - while (getline(inputFile, curLine)) |
3548 | - { |
3549 | - str += curLine; |
3550 | - str += '\n'; |
3551 | - } |
3552 | - |
3553 | - return true; |
3554 | -} |
3555 | - |
3556 | - |
3557 | -/** |
3558 | - * Appends a string to the shader source. |
3559 | - * |
3560 | - * @param str the string to append |
3561 | - */ |
3562 | -void |
3563 | -ShaderSource::append(const std::string &str) |
3564 | -{ |
3565 | - source_ << str; |
3566 | -} |
3567 | - |
3568 | -/** |
3569 | - * Appends the contents of a file to the shader source. |
3570 | - * |
3571 | - * @param filename the name of the file to append |
3572 | - */ |
3573 | -void |
3574 | -ShaderSource::append_file(const std::string &filename) |
3575 | -{ |
3576 | - std::string source; |
3577 | - if (load_file(filename, source)) |
3578 | - source_ << source; |
3579 | -} |
3580 | - |
3581 | -/** |
3582 | - * Replaces a string in the source with another string. |
3583 | - * |
3584 | - * @param remove the string to replace |
3585 | - * @param insert the string to replace with |
3586 | - */ |
3587 | -void |
3588 | -ShaderSource::replace(const std::string &remove, const std::string &insert) |
3589 | -{ |
3590 | - std::string::size_type pos = 0; |
3591 | - std::string str(source_.str()); |
3592 | - |
3593 | - while ((pos = str.find(remove, pos)) != std::string::npos) { |
3594 | - str.replace(pos, remove.size(), insert); |
3595 | - pos++; |
3596 | - } |
3597 | - |
3598 | - source_.clear(); |
3599 | - source_.str(str); |
3600 | -} |
3601 | - |
3602 | -/** |
3603 | - * Replaces a string in the source with the contents of a file. |
3604 | - * |
3605 | - * @param remove the string to replace |
3606 | - * @param filename the name of the file to read from |
3607 | - */ |
3608 | -void |
3609 | -ShaderSource::replace_with_file(const std::string &remove, const std::string &filename) |
3610 | -{ |
3611 | - std::string source; |
3612 | - if (load_file(filename, source)) |
3613 | - replace(remove, source); |
3614 | -} |
3615 | - |
3616 | -/** |
3617 | - * Adds a string (usually containing a constant definition) at |
3618 | - * global (per shader) scope. |
3619 | - * |
3620 | - * The string is placed after any default precision qualifiers. |
3621 | - * |
3622 | - * @param str the string to add |
3623 | - */ |
3624 | -void |
3625 | -ShaderSource::add_global(const std::string &str) |
3626 | -{ |
3627 | - std::string::size_type pos = 0; |
3628 | - std::string source(source_.str()); |
3629 | - |
3630 | - /* Find the last precision qualifier */ |
3631 | - pos = source.rfind("precision"); |
3632 | - |
3633 | - if (pos != std::string::npos) { |
3634 | - /* |
3635 | - * Find the next #endif line of a preprocessor block that contains |
3636 | - * the precision qualifier. |
3637 | - */ |
3638 | - std::string::size_type pos_if = source.find("#if", pos); |
3639 | - std::string::size_type pos_endif = source.find("#endif", pos); |
3640 | - |
3641 | - if (pos_endif != std::string::npos && pos_endif < pos_if) |
3642 | - pos = pos_endif; |
3643 | - |
3644 | - /* Go to the next line */ |
3645 | - pos = source.find("\n", pos); |
3646 | - if (pos != std::string::npos) |
3647 | - pos++; |
3648 | - } |
3649 | - else |
3650 | - pos = 0; |
3651 | - |
3652 | - source.insert(pos, str); |
3653 | - |
3654 | - source_.clear(); |
3655 | - source_.str(source); |
3656 | -} |
3657 | - |
3658 | -/** |
3659 | - * Adds a string (usually containing a constant definition) at |
3660 | - * global (per shader) scope. |
3661 | - * |
3662 | - * The string is placed after any default precision qualifiers. |
3663 | - * |
3664 | - * @param function the function to add the string into |
3665 | - * @param str the string to add |
3666 | - */ |
3667 | -void |
3668 | -ShaderSource::add_local(const std::string &str, const std::string &function) |
3669 | -{ |
3670 | - std::string::size_type pos = 0; |
3671 | - std::string source(source_.str()); |
3672 | - |
3673 | - /* Find the function */ |
3674 | - pos = source.find(function); |
3675 | - pos = source.find('{', pos); |
3676 | - |
3677 | - /* Go to the next line */ |
3678 | - pos = source.find("\n", pos); |
3679 | - if (pos != std::string::npos) |
3680 | - pos++; |
3681 | - |
3682 | - source.insert(pos, str); |
3683 | - |
3684 | - source_.clear(); |
3685 | - source_.str(source); |
3686 | -} |
3687 | - |
3688 | -/** |
3689 | - * Adds a string (usually containing a constant definition) to a shader source |
3690 | - * |
3691 | - * If the function parameter is empty, the string will be added to global |
3692 | - * scope, after any precision definitions. |
3693 | - * |
3694 | - * @param str the string to add |
3695 | - * @param function if not empty, the function to add the string into |
3696 | - */ |
3697 | -void |
3698 | -ShaderSource::add(const std::string &str, const std::string &function) |
3699 | -{ |
3700 | - if (!function.empty()) |
3701 | - add_local(str, function); |
3702 | - else |
3703 | - add_global(str); |
3704 | -} |
3705 | - |
3706 | -/** |
3707 | - * Adds a float constant definition. |
3708 | - * |
3709 | - * @param name the name of the constant |
3710 | - * @param f the value of the constant |
3711 | - * @param function if not empty, the function to put the definition in |
3712 | - */ |
3713 | -void |
3714 | -ShaderSource::add_const(const std::string &name, float f, |
3715 | - const std::string &function) |
3716 | -{ |
3717 | - std::stringstream ss; |
3718 | - |
3719 | - ss << "const float " << name << " = " << std::fixed << f << ";" << std::endl; |
3720 | - |
3721 | - add(ss.str(), function); |
3722 | -} |
3723 | - |
3724 | -/** |
3725 | - * Adds a float array constant definition. |
3726 | - * |
3727 | - * Note that various GLSL versions (including ES) don't support |
3728 | - * array constants. |
3729 | - * |
3730 | - * @param name the name of the constant |
3731 | - * @param v the value of the constant |
3732 | - * @param function if not empty, the function to put the definition in |
3733 | - */ |
3734 | -void |
3735 | -ShaderSource::add_const(const std::string &name, std::vector<float> &array, |
3736 | - const std::string &function) |
3737 | -{ |
3738 | - std::stringstream ss; |
3739 | - |
3740 | - ss << "const float " << name << "[" << array.size() << "] = {" << std::fixed; |
3741 | - for(std::vector<float>::const_iterator iter = array.begin(); |
3742 | - iter != array.end(); |
3743 | - iter++) |
3744 | - { |
3745 | - ss << *iter; |
3746 | - if (iter + 1 != array.end()) |
3747 | - ss << ", " << std::endl; |
3748 | - } |
3749 | - |
3750 | - ss << "};" << std::endl; |
3751 | - |
3752 | - add(ss.str(), function); |
3753 | -} |
3754 | - |
3755 | -/** |
3756 | - * Adds a vec2 constant definition. |
3757 | - * |
3758 | - * @param name the name of the constant |
3759 | - * @param v the value of the constant |
3760 | - * @param function if not empty, the function to put the definition in |
3761 | - */ |
3762 | -void |
3763 | -ShaderSource::add_const(const std::string &name, const LibMatrix::vec2 &v, |
3764 | - const std::string &function) |
3765 | -{ |
3766 | - std::stringstream ss; |
3767 | - |
3768 | - ss << "const vec2 " << name << " = vec2(" << std::fixed; |
3769 | - ss << v.x() << ", " << v.y() << ");" << std::endl; |
3770 | - |
3771 | - add(ss.str(), function); |
3772 | -} |
3773 | - |
3774 | -/** |
3775 | - * Adds a vec3 constant definition. |
3776 | - * |
3777 | - * @param name the name of the constant |
3778 | - * @param v the value of the constant |
3779 | - * @param function if not empty, the function to put the definition in |
3780 | - */ |
3781 | -void |
3782 | -ShaderSource::add_const(const std::string &name, const LibMatrix::vec3 &v, |
3783 | - const std::string &function) |
3784 | -{ |
3785 | - std::stringstream ss; |
3786 | - |
3787 | - ss << "const vec3 " << name << " = vec3(" << std::fixed; |
3788 | - ss << v.x() << ", " << v.y() << ", " << v.z() << ");" << std::endl; |
3789 | - |
3790 | - add(ss.str(), function); |
3791 | -} |
3792 | - |
3793 | -/** |
3794 | - * Adds a vec4 constant definition. |
3795 | - * |
3796 | - * @param name the name of the constant |
3797 | - * @param v the value of the constant |
3798 | - * @param function if not empty, the function to put the definition in |
3799 | - */ |
3800 | -void |
3801 | -ShaderSource::add_const(const std::string &name, const LibMatrix::vec4 &v, |
3802 | - const std::string &function) |
3803 | -{ |
3804 | - std::stringstream ss; |
3805 | - |
3806 | - ss << "const vec4 " << name << " = vec4(" << std::fixed; |
3807 | - ss << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ");" << std::endl; |
3808 | - |
3809 | - add(ss.str(), function); |
3810 | -} |
3811 | - |
3812 | -/** |
3813 | - * Adds a mat3 constant definition. |
3814 | - * |
3815 | - * @param name the name of the constant |
3816 | - * @param v the value of the constant |
3817 | - * @param function if not empty, the function to put the definition in |
3818 | - */ |
3819 | -void |
3820 | -ShaderSource::add_const(const std::string &name, const LibMatrix::mat3 &m, |
3821 | - const std::string &function) |
3822 | -{ |
3823 | - std::stringstream ss; |
3824 | - |
3825 | - ss << "const mat3 " << name << " = mat3(" << std::fixed; |
3826 | - ss << m[0][0] << ", " << m[1][0] << ", " << m[2][0] << "," << std::endl; |
3827 | - ss << m[0][1] << ", " << m[1][1] << ", " << m[2][1] << "," << std::endl; |
3828 | - ss << m[0][2] << ", " << m[1][2] << ", " << m[2][2] << std::endl; |
3829 | - ss << ");" << std::endl; |
3830 | - |
3831 | - add(ss.str(), function); |
3832 | -} |
3833 | - |
3834 | -/** |
3835 | - * Adds a float array declaration and initialization. |
3836 | - * |
3837 | - * @param name the name of the array |
3838 | - * @param array the array values |
3839 | - * @param init_function the function to put the initialization in |
3840 | - * @param decl_function if not empty, the function to put the declaration in |
3841 | - */ |
3842 | -void |
3843 | -ShaderSource::add_array(const std::string &name, std::vector<float> &array, |
3844 | - const std::string &init_function, |
3845 | - const std::string &decl_function) |
3846 | -{ |
3847 | - if (init_function.empty() || name.empty()) |
3848 | - return; |
3849 | - |
3850 | - std::stringstream ss; |
3851 | - ss << "float " << name << "[" << array.size() << "];" << std::endl; |
3852 | - |
3853 | - std::string decl(ss.str()); |
3854 | - |
3855 | - ss.clear(); |
3856 | - ss.str(""); |
3857 | - ss << std::fixed; |
3858 | - |
3859 | - for(std::vector<float>::const_iterator iter = array.begin(); |
3860 | - iter != array.end(); |
3861 | - iter++) |
3862 | - { |
3863 | - ss << name << "[" << iter - array.begin() << "] = " << *iter << ";" << std::endl; |
3864 | - } |
3865 | - |
3866 | - add(ss.str(), init_function); |
3867 | - |
3868 | - add(decl, decl_function); |
3869 | -} |
3870 | - |
3871 | -/** |
3872 | - * Gets the ShaderType for this ShaderSource. |
3873 | - * |
3874 | - * If the ShaderType is unknown, an attempt is made to infer |
3875 | - * the type from the shader source contents. |
3876 | - * |
3877 | - * @return the ShaderType |
3878 | - */ |
3879 | -ShaderSource::ShaderType |
3880 | -ShaderSource::type() |
3881 | -{ |
3882 | - /* Try to infer the type from the source contents */ |
3883 | - if (type_ == ShaderSource::ShaderTypeUnknown) { |
3884 | - std::string source(source_.str()); |
3885 | - |
3886 | - if (source.find("gl_FragColor") != std::string::npos) |
3887 | - type_ = ShaderSource::ShaderTypeFragment; |
3888 | - else if (source.find("gl_Position") != std::string::npos) |
3889 | - type_ = ShaderSource::ShaderTypeVertex; |
3890 | - else |
3891 | - Log::debug("Cannot infer shader type from contents. Leaving it Unknown.\n"); |
3892 | - } |
3893 | - |
3894 | - return type_; |
3895 | -} |
3896 | - |
3897 | -/** |
3898 | - * Helper function that emits a precision statement. |
3899 | - * |
3900 | - * @param ss the stringstream to add the statement to |
3901 | - * @param val the precision value |
3902 | - * @param type_str the variable type to apply the precision value to |
3903 | - */ |
3904 | -void |
3905 | -ShaderSource::emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, |
3906 | - const std::string& type_str) |
3907 | -{ |
3908 | - static const char *precision_map[] = { |
3909 | - "lowp", "mediump", "highp", NULL |
3910 | - }; |
3911 | - |
3912 | - if (val == ShaderSource::PrecisionValueHigh) { |
3913 | - if (type_ == ShaderSource::ShaderTypeFragment) |
3914 | - ss << "#ifdef GL_FRAGMENT_PRECISION_HIGH" << std::endl; |
3915 | - |
3916 | - ss << "precision highp " << type_str << ";" << std::endl; |
3917 | - |
3918 | - if (type_ == ShaderSource::ShaderTypeFragment) { |
3919 | - ss << "#else" << std::endl; |
3920 | - ss << "precision mediump " << type_str << ";" << std::endl; |
3921 | - ss << "#endif" << std::endl; |
3922 | - } |
3923 | - } |
3924 | - else if (val >= 0 && val < ShaderSource::PrecisionValueDefault) { |
3925 | - ss << "precision " << precision_map[val] << " "; |
3926 | - ss << type_str << ";" << std::endl; |
3927 | - } |
3928 | - |
3929 | - /* There is no default precision in the fragment shader, so set it to mediump */ |
3930 | - if (val == ShaderSource::PrecisionValueDefault |
3931 | - && type_str == "float" && type_ == ShaderSource::ShaderTypeFragment) |
3932 | - { |
3933 | - ss << "precision mediump float;" << std::endl; |
3934 | - } |
3935 | -} |
3936 | - |
3937 | -/** |
3938 | - * Gets a string containing the complete shader source. |
3939 | - * |
3940 | - * Precision statements are applied at this point. |
3941 | - * |
3942 | - * @return the shader source |
3943 | - */ |
3944 | -std::string |
3945 | -ShaderSource::str() |
3946 | -{ |
3947 | - /* Decide which precision values to use */ |
3948 | - ShaderSource::Precision precision; |
3949 | - |
3950 | - /* Ensure we have tried to infer the type from the contents */ |
3951 | - type(); |
3952 | - |
3953 | - if (precision_has_been_set_) |
3954 | - precision = precision_; |
3955 | - else |
3956 | - precision = default_precision(type_); |
3957 | - |
3958 | - /* Create the precision statements */ |
3959 | - std::stringstream ss; |
3960 | - |
3961 | - emit_precision(ss, precision.int_precision, "int"); |
3962 | - emit_precision(ss, precision.float_precision, "float"); |
3963 | - emit_precision(ss, precision.sampler2d_precision, "sampler2D"); |
3964 | - emit_precision(ss, precision.samplercube_precision, "samplerCube"); |
3965 | - |
3966 | - std::string precision_str(ss.str()); |
3967 | - if (!precision_str.empty()) { |
3968 | - precision_str.insert(0, "#ifdef GL_ES\n"); |
3969 | - precision_str.insert(precision_str.size(), "#endif\n"); |
3970 | - } |
3971 | - |
3972 | - return precision_str + source_.str(); |
3973 | -} |
3974 | - |
3975 | -/** |
3976 | - * Sets the precision that will be used for this shader. |
3977 | - * |
3978 | - * This overrides any default values set with ShaderSource::default_*_precision(). |
3979 | - * |
3980 | - * @param precision the precision to set |
3981 | - */ |
3982 | -void |
3983 | -ShaderSource::precision(const ShaderSource::Precision& precision) |
3984 | -{ |
3985 | - precision_ = precision; |
3986 | - precision_has_been_set_ = true; |
3987 | -} |
3988 | - |
3989 | -/** |
3990 | - * Gets the precision that will be used for this shader. |
3991 | - * |
3992 | - * @return the precision |
3993 | - */ |
3994 | -const ShaderSource::Precision& |
3995 | -ShaderSource::precision() |
3996 | -{ |
3997 | - return precision_; |
3998 | -} |
3999 | - |
4000 | -/** |
4001 | - * Sets the default precision that will be used for a shaders type. |
4002 | - * |
4003 | - * If type is ShaderTypeUnknown the supplied precision is used for all |
4004 | - * shader types. |
4005 | - * |
4006 | - * This can be overriden per ShaderSource object by using ::precision(). |
4007 | - * |
4008 | - * @param precision the default precision to set |
4009 | - * @param type the ShaderType to use the precision for |
4010 | - */ |
4011 | -void |
4012 | -ShaderSource::default_precision(const ShaderSource::Precision& precision, |
4013 | - ShaderSource::ShaderType type) |
4014 | -{ |
4015 | - if (type < 0 || type > ShaderSource::ShaderTypeUnknown) |
4016 | - type = ShaderSource::ShaderTypeUnknown; |
4017 | - |
4018 | - if (type == ShaderSource::ShaderTypeUnknown) { |
4019 | - for (size_t i = 0; i < ShaderSource::ShaderTypeUnknown; i++) |
4020 | - default_precision_[i] = precision; |
4021 | - } |
4022 | - else { |
4023 | - default_precision_[type] = precision; |
4024 | - } |
4025 | -} |
4026 | - |
4027 | -/** |
4028 | - * Gets the default precision that will be used for a shader type. |
4029 | - * |
4030 | - * It is valid to use a type of ShaderTypeUnknown. This will always |
4031 | - * return a Precision with default values. |
4032 | - * |
4033 | - * @param type the ShaderType to get the precision of |
4034 | - * |
4035 | - * @return the precision |
4036 | - */ |
4037 | -const ShaderSource::Precision& |
4038 | -ShaderSource::default_precision(ShaderSource::ShaderType type) |
4039 | -{ |
4040 | - if (type < 0 || type > ShaderSource::ShaderTypeUnknown) |
4041 | - type = ShaderSource::ShaderTypeUnknown; |
4042 | - |
4043 | - return default_precision_[type]; |
4044 | -} |
4045 | - |
4046 | -/**************************************** |
4047 | - * ShaderSource::Precision constructors * |
4048 | - ****************************************/ |
4049 | - |
4050 | -/** |
4051 | - * Creates a ShaderSource::Precision with default precision values. |
4052 | - */ |
4053 | -ShaderSource::Precision::Precision() : |
4054 | - int_precision(ShaderSource::PrecisionValueDefault), |
4055 | - float_precision(ShaderSource::PrecisionValueDefault), |
4056 | - sampler2d_precision(ShaderSource::PrecisionValueDefault), |
4057 | - samplercube_precision(ShaderSource::PrecisionValueDefault) |
4058 | -{ |
4059 | -} |
4060 | - |
4061 | -/** |
4062 | - * Creates a ShaderSource::Precision using the supplied precision values. |
4063 | - */ |
4064 | -ShaderSource::Precision::Precision(ShaderSource::PrecisionValue int_p, |
4065 | - ShaderSource::PrecisionValue float_p, |
4066 | - ShaderSource::PrecisionValue sampler2d_p, |
4067 | - ShaderSource::PrecisionValue samplercube_p) : |
4068 | - int_precision(int_p), float_precision(float_p), |
4069 | - sampler2d_precision(sampler2d_p), samplercube_precision(samplercube_p) |
4070 | -{ |
4071 | -} |
4072 | - |
4073 | -/** |
4074 | - * Creates a ShaderSource::Precision from a string representation of |
4075 | - * precision values. |
4076 | - * |
4077 | - * The string format is: |
4078 | - * "<int>,<float>,<sampler2d>,<samplercube>" |
4079 | - * |
4080 | - * Each precision value is one of "high", "medium", "low" or "default". |
4081 | - * |
4082 | - * @param precision_values the string representation of the precision values |
4083 | - */ |
4084 | -ShaderSource::Precision::Precision(const std::string& precision_values) : |
4085 | - int_precision(ShaderSource::PrecisionValueDefault), |
4086 | - float_precision(ShaderSource::PrecisionValueDefault), |
4087 | - sampler2d_precision(ShaderSource::PrecisionValueDefault), |
4088 | - samplercube_precision(ShaderSource::PrecisionValueDefault) |
4089 | -{ |
4090 | - std::vector<std::string> elems; |
4091 | - |
4092 | - Util::split(precision_values, ',', elems); |
4093 | - |
4094 | - for (size_t i = 0; i < elems.size() && i < 4; i++) { |
4095 | - const std::string& pstr(elems[i]); |
4096 | - ShaderSource::PrecisionValue pval; |
4097 | - |
4098 | - if (pstr == "high") |
4099 | - pval = ShaderSource::PrecisionValueHigh; |
4100 | - else if (pstr == "medium") |
4101 | - pval = ShaderSource::PrecisionValueMedium; |
4102 | - else if (pstr == "low") |
4103 | - pval = ShaderSource::PrecisionValueLow; |
4104 | - else |
4105 | - pval = ShaderSource::PrecisionValueDefault; |
4106 | - |
4107 | - switch(i) { |
4108 | - case 0: int_precision = pval; break; |
4109 | - case 1: float_precision = pval; break; |
4110 | - case 2: sampler2d_precision = pval; break; |
4111 | - case 3: samplercube_precision = pval; break; |
4112 | - default: break; |
4113 | - } |
4114 | - } |
4115 | -} |
4116 | |
4117 | === removed file 'src/shader-source.h' |
4118 | --- src/shader-source.h 2011-12-02 21:54:50 +0000 |
4119 | +++ src/shader-source.h 1970-01-01 00:00:00 +0000 |
4120 | @@ -1,113 +0,0 @@ |
4121 | -/* |
4122 | - * Copyright © 2010-2011 Linaro Limited |
4123 | - * |
4124 | - * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark. |
4125 | - * |
4126 | - * glmark2 is free software: you can redistribute it and/or modify it under the |
4127 | - * terms of the GNU General Public License as published by the Free Software |
4128 | - * Foundation, either version 3 of the License, or (at your option) any later |
4129 | - * version. |
4130 | - * |
4131 | - * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY |
4132 | - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
4133 | - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
4134 | - * details. |
4135 | - * |
4136 | - * You should have received a copy of the GNU General Public License along with |
4137 | - * glmark2. If not, see <http://www.gnu.org/licenses/>. |
4138 | - * |
4139 | - * Authors: |
4140 | - * Alexandros Frantzis (glmark2) |
4141 | - */ |
4142 | - |
4143 | -#include <string> |
4144 | -#include <sstream> |
4145 | -#include <vector> |
4146 | -#include "vec.h" |
4147 | -#include "mat.h" |
4148 | - |
4149 | -/** |
4150 | - * Helper class for loading and manipulating shader sources. |
4151 | - */ |
4152 | -class ShaderSource |
4153 | -{ |
4154 | -public: |
4155 | - enum ShaderType { |
4156 | - ShaderTypeVertex, |
4157 | - ShaderTypeFragment, |
4158 | - ShaderTypeUnknown |
4159 | - }; |
4160 | - |
4161 | - ShaderSource(ShaderType type = ShaderTypeUnknown) : |
4162 | - precision_has_been_set_(false), type_(type) {} |
4163 | - ShaderSource(const std::string &filename, ShaderType type = ShaderTypeUnknown) : |
4164 | - precision_has_been_set_(false), type_(type) { append_file(filename); } |
4165 | - |
4166 | - void append(const std::string &str); |
4167 | - void append_file(const std::string &filename); |
4168 | - |
4169 | - void replace(const std::string &remove, const std::string &insert); |
4170 | - void replace_with_file(const std::string &remove, const std::string &filename); |
4171 | - |
4172 | - void add(const std::string &str, const std::string &function = ""); |
4173 | - |
4174 | - void add_const(const std::string &name, float f, |
4175 | - const std::string &function = ""); |
4176 | - void add_const(const std::string &name, std::vector<float> &f, |
4177 | - const std::string &function = ""); |
4178 | - void add_const(const std::string &name, const LibMatrix::vec2 &v, |
4179 | - const std::string &function = ""); |
4180 | - void add_const(const std::string &name, const LibMatrix::vec3 &v, |
4181 | - const std::string &function = ""); |
4182 | - void add_const(const std::string &name, const LibMatrix::vec4 &v, |
4183 | - const std::string &function = ""); |
4184 | - void add_const(const std::string &name, const LibMatrix::mat3 &m, |
4185 | - const std::string &function = ""); |
4186 | - |
4187 | - void add_array(const std::string &name, std::vector<float> &array, |
4188 | - const std::string &init_function, |
4189 | - const std::string &decl_function = ""); |
4190 | - |
4191 | - ShaderType type(); |
4192 | - std::string str(); |
4193 | - |
4194 | - enum PrecisionValue { |
4195 | - PrecisionValueLow, |
4196 | - PrecisionValueMedium, |
4197 | - PrecisionValueHigh, |
4198 | - PrecisionValueDefault, |
4199 | - }; |
4200 | - |
4201 | - struct Precision { |
4202 | - Precision(); |
4203 | - Precision(PrecisionValue int_p, PrecisionValue float_p, |
4204 | - PrecisionValue sampler2d_p, PrecisionValue samplercube_p); |
4205 | - Precision(const std::string& list); |
4206 | - |
4207 | - PrecisionValue int_precision; |
4208 | - PrecisionValue float_precision; |
4209 | - PrecisionValue sampler2d_precision; |
4210 | - PrecisionValue samplercube_precision; |
4211 | - }; |
4212 | - |
4213 | - void precision(const Precision& precision); |
4214 | - const Precision& precision(); |
4215 | - |
4216 | - static void default_precision(const Precision& precision, |
4217 | - ShaderType type = ShaderTypeUnknown); |
4218 | - static const Precision& default_precision(ShaderType type); |
4219 | - |
4220 | -private: |
4221 | - void add_global(const std::string &str); |
4222 | - void add_local(const std::string &str, const std::string &function); |
4223 | - bool load_file(const std::string& filename, std::string& str); |
4224 | - void emit_precision(std::stringstream& ss, ShaderSource::PrecisionValue val, |
4225 | - const std::string& type_str); |
4226 | - |
4227 | - std::stringstream source_; |
4228 | - Precision precision_; |
4229 | - bool precision_has_been_set_; |
4230 | - ShaderType type_; |
4231 | - |
4232 | - static std::vector<Precision> default_precision_; |
4233 | -}; |
4234 | |
4235 | === removed file 'src/util.cc' |
4236 | --- src/util.cc 2011-12-02 21:54:50 +0000 |
4237 | +++ src/util.cc 1970-01-01 00:00:00 +0000 |
4238 | @@ -1,90 +0,0 @@ |
4239 | -/* |
4240 | - * Copyright © 2011 Linaro Limited |
4241 | - * |
4242 | - * This file is part of glcompbench. |
4243 | - * |
4244 | - * glcompbench is free software: you can redistribute it and/or modify |
4245 | - * it under the terms of the GNU General Public License as published by |
4246 | - * the Free Software Foundation, either version 3 of the License, or |
4247 | - * (at your option) any later version. |
4248 | - * |
4249 | - * glcompbench is distributed in the hope that it will be useful, |
4250 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4251 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4252 | - * GNU General Public License for more details. |
4253 | - * |
4254 | - * You should have received a copy of the GNU General Public License |
4255 | - * along with glcompbench. If not, see <http://www.gnu.org/licenses/>. |
4256 | - * |
4257 | - * Authors: |
4258 | - * Alexandros Frantzis <alexandros.frantzis@linaro.org> |
4259 | - * Jesse Barker <jesse.barker@linaro.org> |
4260 | - */ |
4261 | - |
4262 | -#include <sstream> |
4263 | -#include <fstream> |
4264 | -#include <sys/time.h> |
4265 | -#include <dirent.h> |
4266 | - |
4267 | -#include "log.h" |
4268 | -#include "util.h" |
4269 | - |
4270 | -/** |
4271 | - * Splits a string using a delimiter |
4272 | - * |
4273 | - * @param s the string to split |
4274 | - * @param delim the delimitir to use |
4275 | - * @param elems the string vector to populate |
4276 | - */ |
4277 | -void |
4278 | -Util::split(const std::string &s, char delim, std::vector<std::string> &elems) |
4279 | -{ |
4280 | - std::stringstream ss(s); |
4281 | - |
4282 | - std::string item; |
4283 | - while(std::getline(ss, item, delim)) |
4284 | - elems.push_back(item); |
4285 | -} |
4286 | - |
4287 | -uint64_t |
4288 | -Util::get_timestamp_us() |
4289 | -{ |
4290 | - struct timeval tv; |
4291 | - gettimeofday(&tv, NULL); |
4292 | - uint64_t now = static_cast<uint64_t>(tv.tv_sec) * 1000000 + |
4293 | - static_cast<double>(tv.tv_usec); |
4294 | - return now; |
4295 | -} |
4296 | - |
4297 | -std::istream * |
4298 | -Util::get_resource(const std::string &path) |
4299 | -{ |
4300 | - std::ifstream *ifs = new std::ifstream(path.c_str()); |
4301 | - |
4302 | - return static_cast<std::istream *>(ifs); |
4303 | -} |
4304 | - |
4305 | -void |
4306 | -Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec) |
4307 | -{ |
4308 | - DIR* dir = opendir(dirName.c_str()); |
4309 | - if (!dir) |
4310 | - { |
4311 | - Log::error("Failed to open models directory '%s'\n", dirName.c_str()); |
4312 | - return; |
4313 | - } |
4314 | - |
4315 | - struct dirent* entry = readdir(dir); |
4316 | - while (entry) |
4317 | - { |
4318 | - std::string pathname(dirName + "/"); |
4319 | - pathname += std::string(entry->d_name); |
4320 | - // Skip '.' and '..' |
4321 | - if (entry->d_name[0] != '.') |
4322 | - { |
4323 | - fileVec.push_back(pathname); |
4324 | - } |
4325 | - entry = readdir(dir); |
4326 | - } |
4327 | - closedir(dir); |
4328 | -} |
4329 | |
4330 | === removed file 'src/util.h' |
4331 | --- src/util.h 2011-12-02 21:54:50 +0000 |
4332 | +++ src/util.h 1970-01-01 00:00:00 +0000 |
4333 | @@ -1,69 +0,0 @@ |
4334 | -/* |
4335 | - * Copyright © 2011 Linaro Limited |
4336 | - * |
4337 | - * This file is part of glcompbench. |
4338 | - * |
4339 | - * glcompbench is free software: you can redistribute it and/or modify |
4340 | - * it under the terms of the GNU General Public License as published by |
4341 | - * the Free Software Foundation, either version 3 of the License, or |
4342 | - * (at your option) any later version. |
4343 | - * |
4344 | - * glcompbench is distributed in the hope that it will be useful, |
4345 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4346 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4347 | - * GNU General Public License for more details. |
4348 | - * |
4349 | - * You should have received a copy of the GNU General Public License |
4350 | - * along with glcompbench. If not, see <http://www.gnu.org/licenses/>. |
4351 | - * |
4352 | - * Authors: |
4353 | - * Alexandros Frantzis <alexandros.frantzis@linaro.org> |
4354 | - * Jesse Barker <jesse.barker@linaro.org> |
4355 | - */ |
4356 | - |
4357 | -#ifndef UTIL_H_ |
4358 | -#define UTIL_H_ |
4359 | - |
4360 | -#include <string> |
4361 | -#include <vector> |
4362 | -#include <istream> |
4363 | -#include <sstream> |
4364 | -#include <stdint.h> |
4365 | - |
4366 | -struct Util { |
4367 | - static void split(const std::string &s, char delim, std::vector<std::string> &elems); |
4368 | - static uint64_t get_timestamp_us(); |
4369 | - static std::istream *get_resource(const std::string &path); |
4370 | - static void list_files(const std::string& dirName, std::vector<std::string>& fileVec); |
4371 | - template <class T> static void dispose_pointer_vector(std::vector<T*> &vec) |
4372 | - { |
4373 | - for (typename std::vector<T*>::const_iterator iter = vec.begin(); |
4374 | - iter != vec.end(); |
4375 | - iter++) |
4376 | - { |
4377 | - delete *iter; |
4378 | - } |
4379 | - |
4380 | - vec.clear(); |
4381 | - } |
4382 | - template<typename T> |
4383 | - static T |
4384 | - fromString(const std::string& asString) |
4385 | - { |
4386 | - std::stringstream ss(asString); |
4387 | - T retVal; |
4388 | - ss >> retVal; |
4389 | - return retVal; |
4390 | - } |
4391 | - |
4392 | - template<typename T> |
4393 | - static std::string |
4394 | - toString(const T t) |
4395 | - { |
4396 | - std::stringstream ss; |
4397 | - ss << t; |
4398 | - return ss.str(); |
4399 | - } |
4400 | -}; |
4401 | - |
4402 | -#endif /* UTIL_H */ |