Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / ttexrect.cpp
blobccc5ba3dce2f5a8ee5e006579c30f6903da23af6
1 // BEGIN_COPYRIGHT -*- glean -*-
3 /*
4 * Copyright © 2006 Intel Corporation
5 * Copyright © 1999 Allen Akin
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Authors:
27 * Eric Anholt <eric@anholt.net>
31 /* ttexrect.cpp: Test the ARB_texture_rectangle extension
32 * Author: Eric Anholt <eric@anholt.net>
34 * Test procedure:
35 * Create a 255x127 texture of varying colors and bind it as a
36 * GL_ARB_texture_recangle target. Draw that rectangle to the window, and
37 * check that the texture was drawn correctly. The common failure to be
38 * caught with this test is not adjusting the non-normalized coordinates on
39 * hardware that expects normalized coordinates.
42 #include "ttexrect.h"
43 #include <cassert>
44 #include <stdio.h>
45 #include <cmath>
47 namespace GLEAN {
49 /**
50 * Test if two colors are close enough to be considered the same.
52 bool
53 TexRectTest::TestColor(const GLfloat c1[3], const GLfloat c2[3])
55 if (fabs(c1[0] - c2[0]) <= mTolerance[0] &&
56 fabs(c1[1] - c2[1]) <= mTolerance[1] &&
57 fabs(c1[2] - c2[2]) <= mTolerance[2])
58 return true;
59 else
60 return false;
63 void
64 TexRectTest::CalculateTolerance()
66 GLint rBits, gBits, bBits;
67 GLint rTexBits, gTexBits, bTexBits;
69 // Get fb resolution
70 glGetIntegerv(GL_RED_BITS, &rBits);
71 glGetIntegerv(GL_GREEN_BITS, &gBits);
72 glGetIntegerv(GL_BLUE_BITS, &bBits);
74 // Get tex resolution
75 glGetTexLevelParameteriv(GL_TEXTURE_RECTANGLE_ARB,
76 0, GL_TEXTURE_RED_SIZE, &rTexBits);
77 glGetTexLevelParameteriv(GL_TEXTURE_RECTANGLE_ARB,
78 0, GL_TEXTURE_GREEN_SIZE, &gTexBits);
79 glGetTexLevelParameteriv(GL_TEXTURE_RECTANGLE_ARB,
80 0, GL_TEXTURE_BLUE_SIZE, &bTexBits);
82 // Find smaller of frame buffer and texture bits
83 rBits = (rBits < rTexBits) ? rBits : rTexBits;
84 gBits = (gBits < gTexBits) ? gBits : gTexBits;
85 bBits = (bBits < bTexBits) ? bBits : bTexBits;
87 // If these fail, something's seriously wrong.
88 assert(rBits > 0);
89 assert(gBits > 0);
90 assert(bBits > 0);
91 mTolerance[0] = 3.0 / (1 << rBits);
92 mTolerance[1] = 3.0 / (1 << gBits);
93 mTolerance[2] = 3.0 / (1 << bBits);
96 #define TEXTURE_WIDTH 255
97 #define TEXTURE_HEIGHT 127
99 /**
100 * Creates a TEXTURE_WIDTH * TEXTURE_HEIGHT rectangular texture and draws it to
101 * the window. It then reads the output back to verify that the texture stayed
102 * intact.
104 void
105 TexRectTest::runOne(BasicResult& r, Window& w)
107 float image[TEXTURE_WIDTH * TEXTURE_HEIGHT * 3];
108 float actual[TEXTURE_WIDTH * TEXTURE_HEIGHT * 3];
109 (void) w;
111 /* Set up a texture that is color ramps with red to black top to
112 * bottom and green to black left to right.
114 for (int y = 0; y < TEXTURE_HEIGHT; y++) {
115 for (int x = 0; x < TEXTURE_WIDTH; x++) {
116 int i = (y * TEXTURE_WIDTH + x) * 3;
118 image[i + 0] = (float)x / (TEXTURE_WIDTH - 1);
119 image[i + 1] = 1.0 - ((float) y / (TEXTURE_HEIGHT - 1));
120 image[i + 2] = 0.0;
124 glClearColor(0.0, 0.0, 0.0, 0.0);
125 glClear(GL_COLOR_BUFFER_BIT);
127 glShadeModel(GL_FLAT);
129 glMatrixMode(GL_PROJECTION);
130 glLoadIdentity();
131 glOrtho(0, 256, 0, 256, -1, 1);
132 glMatrixMode(GL_MODELVIEW);
133 glLoadIdentity();
134 glViewport(0, 0, windowSize, windowSize);
136 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, 255, 127, 0,
137 GL_RGB, GL_FLOAT, image);
138 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
139 GL_NEAREST);
140 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
141 GL_NEAREST);
142 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
143 glEnable(GL_TEXTURE_RECTANGLE_ARB);
145 glDrawBuffer(GL_BACK);
146 glReadBuffer(GL_BACK);
148 r.pass = true;
150 /* Draw our texture to the window such that each texel should map
151 * to the corresponding pixel of the window.
153 glBegin(GL_POLYGON);
154 glTexCoord2f(0, 0);
155 glVertex2f(0, 0);
157 glTexCoord2f(TEXTURE_WIDTH, 0);
158 glVertex2f(TEXTURE_WIDTH, 0);
160 glTexCoord2f(TEXTURE_WIDTH, TEXTURE_HEIGHT);
161 glVertex2f(TEXTURE_WIDTH, TEXTURE_HEIGHT);
163 glTexCoord2f(0, TEXTURE_HEIGHT);
164 glVertex2f(0, TEXTURE_HEIGHT);
165 glEnd();
167 /* Read back the output */
168 glReadPixels(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT,
169 GL_RGB, GL_FLOAT, actual);
171 w.swap(); // lets us watch the progress
173 CalculateTolerance();
175 /* Verify the output */
176 for (int y = 0; y < TEXTURE_HEIGHT; y++) {
177 for (int x = 0; x < TEXTURE_WIDTH; x++) {
178 int i = (y * TEXTURE_WIDTH + x) * 3;
180 if (!TestColor(&image[i], &actual[i])) {
181 // Report the error
182 env->log << name
183 << ": FAIL at (" << x << "," << y
184 << "):\n"
185 << " Expected=("
186 << image[i + 0] << ", "
187 << image[i + 1] << ", "
188 << image[i + 2] << ")\n"
189 << " Measured=("
190 << actual[i + 0] << ", "
191 << actual[i + 1] << ", "
192 << actual[i + 2] << ")\n";
193 r.pass = false;
197 } // TexRectTest::runOne
200 void
201 TexRectTest::logOne(BasicResult& r) {
202 logPassFail(r);
203 logConcise(r);
204 } // TexRectTest::logOne
207 ///////////////////////////////////////////////////////////////////////////////
208 // The test object itself:
209 ///////////////////////////////////////////////////////////////////////////////
210 TexRectTest texRectTest("texRect", "window, rgb",
211 "GL_ARB_texture_rectangle",
212 "Test basic texture rectangle functionality.\n");
215 } // namespace GLEAN