Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / ttexunits.cpp
blob8ac63d8d90d761f22bcbc99d59f00d3e38f38f28
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 2008 VMWare, Inc. All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
29 // Test texture unit things
30 // We're generally just testing API-related things, not rendering.
31 // Brian Paul 31 Dec 2008
33 #define GL_GLEXT_PROTOTYPES
35 #include <cstring>
36 #include <cassert>
37 #include <math.h>
38 #include "ttexunits.h"
41 namespace GLEAN {
43 static PFNGLACTIVETEXTUREPROC glActiveTexture_func = NULL;
44 static PFNGLCLIENTACTIVETEXTUREPROC glClientActiveTexture_func = NULL;
47 void
48 TexUnitsTest::reportFailure(const char *msg) const
50 env->log << "FAILURE:\n";
51 env->log << "\t" << msg << "\n";
55 void
56 TexUnitsTest::reportFailure(const char *msg, GLint unit) const
58 char s[100];
59 #if defined(_MSC_VER)
60 _snprintf(s, sizeof(s), msg, unit);
61 #else
62 snprintf(s, sizeof(s), msg, unit);
63 #endif
64 env->log << "FAILURE:\n";
65 env->log << "\t" << s << "\n";
69 bool
70 TexUnitsTest::setup(void)
72 // check that we have OpenGL 2.x or 3.x
73 const char *verString = (const char *) glGetString(GL_VERSION);
75 if (verString[0] != '2' && verString[0] != '3') {
76 env->log << "OpenGL 2.x or 3.x not supported\n";
77 return false;
80 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedUnits);
81 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxImageUnits);
82 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxCoordUnits);
83 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxUnits);
85 glActiveTexture_func = (PFNGLACTIVETEXTUREPROC) GLUtils::getProcAddress("glActiveTexture");
86 assert(glActiveTexture_func);
87 glClientActiveTexture_func = (PFNGLCLIENTACTIVETEXTUREPROC) GLUtils::getProcAddress("glClientActiveTexture");
88 assert(glClientActiveTexture_func);
90 return true;
94 bool
95 TexUnitsTest::testLimits(void)
97 if (maxImageUnits < maxUnits) {
98 reportFailure("GL_MAX_TEXTURE_IMAGE_UNITS < GL_MAX_TEXTURE_UNITS");
99 return false;
101 if (maxCoordUnits < maxUnits) {
102 reportFailure("GL_MAX_TEXTURE_COORD_UNITS < GL_MAX_TEXTURE_UNITS");
103 return false;
105 return true;
109 bool
110 TexUnitsTest::testActiveTexture(void)
112 GLint i;
114 // clear any error state
115 while (glGetError())
118 // test glActiveTexture()
119 for (i = 0; i < maxCombinedUnits; i++) {
120 glActiveTexture_func(GL_TEXTURE0 + i);
121 if (glGetError()) {
122 reportFailure("glActiveTexture(GL_TEXTURE%d) failed", i);
123 return false;
126 GLint unit;
127 glGetIntegerv(GL_ACTIVE_TEXTURE, &unit);
128 if (unit != GL_TEXTURE0 + i || glGetError()) {
129 reportFailure("glGetIntegerv(GL_ACTIVE_TEXTURE) failed");
130 return false;
134 // this should fail:
135 glActiveTexture_func(GL_TEXTURE0 + maxCombinedUnits);
136 if (glGetError() != GL_INVALID_ENUM) {
137 reportFailure("glActiveTexture(GL_TEXTURE%d) failed to generate an error",
138 maxCombinedUnits);
139 return false;
143 // test glClientActiveTexture()
144 for (i = 0; i < maxCoordUnits; i++) {
145 glClientActiveTexture_func(GL_TEXTURE0 + i);
146 if (glGetError()) {
147 reportFailure("glClientActiveTexture(GL_TEXTURE%d) failed", i);
148 return false;
151 GLint unit;
152 glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &unit);
153 if (unit != GL_TEXTURE0 + i || glGetError()) {
154 reportFailure("glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE) failed");
155 return false;
159 // this should fail:
160 glClientActiveTexture_func(GL_TEXTURE0 + maxCoordUnits);
161 if (glGetError() != GL_INVALID_ENUM) {
162 reportFailure("glClientActiveTexture(GL_TEXTURE%d) failed to generate an error", maxCoordUnits);
163 return false;
166 return true;
170 bool
171 TexUnitsTest::testTextureMatrices(void)
173 GLint i;
175 glActiveTexture_func(GL_TEXTURE0);
176 glMatrixMode(GL_TEXTURE);
178 // set texture matrices
179 for (i = 0; i < maxCoordUnits; i++) {
180 glActiveTexture_func(GL_TEXTURE0 + i);
182 // generate matrix
183 GLfloat m[16];
184 for (int j = 0; j < 16; j++) {
185 m[j] = float(i * 100 + j);
188 glLoadMatrixf(m);
191 // query texture matrices
192 for (i = 0; i < maxCoordUnits; i++) {
193 glActiveTexture_func(GL_TEXTURE0 + i);
195 // get matrix and check it
196 GLfloat m[16];
197 memset(m, 0, sizeof(m));
198 glGetFloatv(GL_TEXTURE_MATRIX, m);
200 if (glGetError()) {
201 reportFailure("Query of texture matrix %d raised an error", i);
202 return false;
205 for (int j = 0; j < 16; j++) {
206 if (m[j] != float(i * 100 + j)) {
207 reportFailure("Query of texture matrix %d failed", i);
208 return false;
213 if (glGetError()) {
214 reportFailure("GL error was generated while testing texture matrices");
215 return false;
218 return true;
222 bool
223 TexUnitsTest::testTextureCoordGen(void)
225 GLint i;
227 glActiveTexture_func(GL_TEXTURE0);
228 glMatrixMode(GL_TEXTURE);
230 // test texgen enable/disable
231 for (i = 0; i < maxCombinedUnits; i++) {
232 glActiveTexture_func(GL_TEXTURE0 + i);
234 glEnable(GL_TEXTURE_GEN_S);
235 glEnable(GL_TEXTURE_GEN_T);
236 glEnable(GL_TEXTURE_GEN_R);
237 glEnable(GL_TEXTURE_GEN_Q);
238 if (i < maxCoordUnits) {
239 // should be no error
240 if (glGetError()) {
241 reportFailure("GL error was generated by enabling GL_TEXTURE_GEN_x, unit %d", i);
242 return false;
244 glDisable(GL_TEXTURE_GEN_S);
245 glDisable(GL_TEXTURE_GEN_T);
246 glDisable(GL_TEXTURE_GEN_R);
247 glDisable(GL_TEXTURE_GEN_Q);
249 else {
250 // should be an error
251 if (glGetError() != GL_INVALID_OPERATION) {
252 reportFailure("GL error not generated by invalid enable of GL_TEXTURE_GEN_x, unit %d", i);
253 return false;
258 return true;
262 bool
263 TexUnitsTest::testTexcoordArrays(void)
265 GLint i;
267 for (i = 0; i < maxCoordUnits; i++) {
268 glClientActiveTexture_func(GL_TEXTURE0 + i);
270 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
271 if (glGetError()) {
272 reportFailure("GL error was generated by glEnableClientState for unit %d", i);
273 return false;
275 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
279 return true;
283 void
284 TexUnitsTest::runOne(MultiTestResult &r, Window &w)
286 (void) w;
288 if (!setup()) {
289 r.pass = false;
290 return;
293 if (testLimits())
294 r.numPassed++;
295 else
296 r.numFailed++;
298 if (testActiveTexture())
299 r.numPassed++;
300 else
301 r.numFailed++;
303 if (testTextureMatrices())
304 r.numPassed++;
305 else
306 r.numFailed++;
308 if (testTextureCoordGen())
309 r.numPassed++;
310 else
311 r.numFailed++;
313 if (testTexcoordArrays())
314 r.numPassed++;
315 else
316 r.numFailed++;
318 r.pass = (r.numFailed == 0);
322 // The test object itself:
323 TexUnitsTest texUnitTest("texUnits", "window, rgb",
324 "", // no extension filter
325 "texUnits: test texture units.\n"
330 } // namespace GLEAN