lodbias.c 8.91 KB
Newer Older
Nicolai Hähnle's avatar
Nicolai Hähnle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright (c) The Piglit project 2008
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file
 * Test the GL_EXT_texture_lod_bias extension.
 *
 * Only test LOD bias with a granularity of 1.0 with a nearest mip filter.
 * This leaves room for somewhat inaccurate hardware implementations.
 * The point of this test is that the implementation has to get the big
 * picture issues right:
 *
 *  1. LOD bias is per texture stage, not per texture object.
 *  2. LOD bias is applied *before* clamping.
 *  3. The supported bias range must be reported correctly.
 *
37 38 39 40
 * @todo
 * Check per-texture object LOD bias (support for this was added in OpenGL 1.4).
 * In particular, check interaction of per-texture and per-TexUnit bias.
 * Check clamping behaviour.
Nicolai Hähnle's avatar
Nicolai Hähnle committed
41 42
 */

43
#include "piglit-framework.h"
Nicolai Hähnle's avatar
Nicolai Hähnle committed
44 45 46 47
#include "piglit-util.h"

#define SquareSize 32

48 49 50
int piglit_width = 3*SquareSize;
int piglit_height = 3*SquareSize;
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static int CurrentTest = 0;
static int CurrentBias = 0;
static int CurrentBias2 = 0;
static int MaxTextureLodBias;
static GLuint Textures[2];

#define NrTests 2

/**
 * The test uses two 4x4 clamped, mipmapped textures (i.e. 3 mip levels)
 * with the following RGB colors on each level.
 *
 * Note: Black is used as a background color, so don't use black for the textures.
 */
static GLfloat TextureData[2][3][3] = {
	{ { 0.5, 0.5, 0.5 }, { 0.5, 0.0, 0.0 }, { 0.0, 0.5, 0.0 } },
	{ { 0.0, 0.0, 0.5 }, { 0.5, 0.5, 0.0 }, { 0.0, 0.5, 0.5 } }
};

70 71
static GLboolean
probe_cell(const char* testname, int cellx, int celly, const float* expected)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
72 73 74 75 76 77 78 79 80
{
	int x, y;

	for(y = 0; y < 4; ++y) {
		for(x = 0; x < 4; ++x) {
			int pixx = (5*cellx+x+1)*SquareSize/5;
			int pixy = (5*celly+y+1)*SquareSize/5;
			if (!piglit_probe_pixel_rgb(pixx, pixy, expected)) {
				fprintf(stderr, "%s: %i,%i failed\n", testname, cellx, celly);
81
				return GL_FALSE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
82 83 84
			}
		}
	}
85 86

	return GL_TRUE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
87 88 89 90 91 92 93 94 95 96 97
}

static float scale_for_miplevel(int bias, int level)
{
	float base = SquareSize/(4>>level);
	if (bias >= 0)
		return base/(float)(1<<bias);
	else
		return base*(float)(1<<(-bias));
}

98 99
static GLboolean
test_simple_texture(int tex, int bias)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
100
{
101
	GLboolean pass = GL_TRUE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	int level;

	glBindTexture(GL_TEXTURE_2D, Textures[tex]);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);

	for(level = 0; level < 3; ++level) {
		float scale = scale_for_miplevel(bias, level);

		glPushMatrix();
		glScalef(SquareSize, SquareSize, 1.0);
		glTranslatef(level, tex, 0.0);
		glBegin(GL_QUADS);
			glTexCoord2f(0.0,   0.0);   glVertex2f(0, 0);
			glTexCoord2f(scale, 0.0);   glVertex2f(1, 0);
			glTexCoord2f(scale, scale); glVertex2f(1, 1);
			glTexCoord2f(0.0,   scale); glVertex2f(0, 1);
		glEnd();
		glPopMatrix();

122 123
		pass = probe_cell("test_simple", level, tex,
				  TextureData[tex][level]) && pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
124
	}
125 126

	return pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
127 128 129 130 131 132
}

/**
 * Simple test: Attempt to draw all LOD levels of both textures
 * at the given LOD bias.
 */
133 134
static GLboolean
test_simple(int bias1, int bias2)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
135
{
136 137
	GLboolean pass = GL_TRUE;

Nicolai Hähnle's avatar
Nicolai Hähnle committed
138 139 140 141
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);
142 143
	pass = test_simple_texture(0, bias1) && pass;
	pass = test_simple_texture(1, bias2) && pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
144 145
	glDisable(GL_TEXTURE_2D);

146 147
	if (!piglit_automatic)
		glutSwapBuffers();
148 149

	return pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
150 151 152
}


153 154
static GLboolean
test_multitex_combo(int bias1, int level1, int bias2, int level2)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
	float scale1 = scale_for_miplevel(bias1, level1);
	float scale2 = scale_for_miplevel(bias2, level2);
	GLfloat expected[3];
	int i;

	for(i = 0; i < 3; ++i)
		expected[i] = TextureData[0][level1][i] + TextureData[1][level2][i];

	glPushMatrix();
	glScalef(SquareSize, SquareSize, 1.0);
	glTranslatef(level1, level2, 0.0);
	glBegin(GL_QUADS);
		glMultiTexCoord2f(GL_TEXTURE0, 0.0,    0.0);
		glMultiTexCoord2f(GL_TEXTURE1, 0.0,    0.0);
		glVertex2f(0, 0);

		glMultiTexCoord2f(GL_TEXTURE0, scale1, 0.0);
		glMultiTexCoord2f(GL_TEXTURE1, scale2, 0.0);
		glVertex2f(1, 0);

		glMultiTexCoord2f(GL_TEXTURE0, scale1, scale1);
		glMultiTexCoord2f(GL_TEXTURE1, scale2, scale2);
		glVertex2f(1, 1);

		glMultiTexCoord2f(GL_TEXTURE0, 0.0,    scale1);
		glMultiTexCoord2f(GL_TEXTURE1, 0.0,    scale2);
		glVertex2f(0, 1);
	glEnd();
	glPopMatrix();

186
	return probe_cell("multitex", level1, level2, expected);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
187 188 189 190 191 192
}


/**
 * Test combinations of LOD bias when multitexturing.
 */
193 194
static GLboolean
test_multitex(int bias1, int bias2)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
195
{
196
	GLboolean pass = GL_TRUE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	int x, y;

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, Textures[0]);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias1);

	glActiveTexture(GL_TEXTURE1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, Textures[1]);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
	glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias2);

	for(y = 0; y < 3; ++y) {
		for(x = 0; x < 3; ++x)
215
			pass = test_multitex_combo(bias1, x, bias2, y) && pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
216 217 218 219 220 221
	}

	glDisable(GL_TEXTURE_2D);
	glActiveTexture(GL_TEXTURE0);
	glDisable(GL_TEXTURE_2D);

222 223
	if (!piglit_automatic)
		glutSwapBuffers();
224 225

	return pass;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
226 227
}

228 229 230 231
enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
232

233
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
234

235
	if (piglit_automatic) {
Nicolai Hähnle's avatar
Nicolai Hähnle committed
236 237 238
		int bias1, bias2;
		for(bias1 = -MaxTextureLodBias; bias1 <= MaxTextureLodBias; bias1++) {
			for(bias2 = -MaxTextureLodBias; bias2 <= MaxTextureLodBias; bias2++) {
239 240
				pass = pass && test_simple(bias1, bias2);
				pass = pass && test_multitex(bias1, bias2);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
241 242 243 244
			}
		}
	} else {
		if (CurrentTest == 0) {
245
			pass = test_simple(CurrentBias, CurrentBias2);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
246
		} else {
247
			pass = test_multitex(CurrentBias, CurrentBias2);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
248 249 250
		}
	}

251
	return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

static void Key(unsigned char key, int x, int y)
{
	(void) x;
	(void) y;
	switch (key) {
	case 't':
		CurrentTest++;
		if (CurrentTest >= NrTests)
			CurrentTest = 0;
		printf("Test: %s\n", CurrentTest ? "multitexturing" : "simple");
		break;
	case 'b':
		CurrentBias--;
		if (CurrentBias < -MaxTextureLodBias)
			CurrentBias = -MaxTextureLodBias;
		break;
	case 'B':
		CurrentBias++;
		if (CurrentBias > MaxTextureLodBias)
			CurrentBias = MaxTextureLodBias;
		break;
	case 'n':
		CurrentBias2--;
		if (CurrentBias2 < -MaxTextureLodBias)
			CurrentBias2 = -MaxTextureLodBias;
		break;
	case 'N':
		CurrentBias2++;
		if (CurrentBias2 > MaxTextureLodBias)
			CurrentBias2 = MaxTextureLodBias;
		break;
	case 27:
		exit(0);
		break;
	}
	printf("Current LOD bias: 1st tex: %i  2nd tex: %i\n", CurrentBias, CurrentBias2);
	glutPostRedisplay();
}

293 294
void
piglit_init(int argc, char **argv)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
295
{
296 297 298 299 300 301 302 303 304 305 306 307 308 309
	int i;

	if (!GLEW_VERSION_1_3) {
		printf("Requires OpenGL 1.3\n");
		piglit_report_result(PIGLIT_SKIP);
	}

	piglit_require_extension("GL_EXT_texture_lod_bias");

	glGetIntegerv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &MaxTextureLodBias);
	if (!piglit_automatic)
		printf("MAX_TEXTURE_LOD_BIAS_EXT = %i\n", MaxTextureLodBias);

	if (!piglit_automatic) {
Nicolai Hähnle's avatar
Nicolai Hähnle committed
310 311 312 313 314 315 316
		printf(
			"Press 't' to switch tests\n"
			"Press 'b'/'B' to change primary LOD bias\n"
			"Press 'n'/'N' to change secondary LOD bias\n"
			"Press 'Escape' to quit\n");
		glutKeyboardFunc(Key);
	}
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	glGenTextures(2, Textures);

	for(i = 0; i < 2; ++i) {
		int level;

		glBindTexture(GL_TEXTURE_2D, Textures[i]);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

		for(level = 0; level < 3; ++level) {
			GLfloat texdata[16][3];
			int dim = 4 >> level;
			int j;

			for(j = 0; j < dim*dim; ++j) {
				texdata[j][0] = TextureData[i][level][0];
				texdata[j][1] = TextureData[i][level][1];
				texdata[j][2] = TextureData[i][level][2];
			}

			glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, dim, dim, 0, GL_RGB, GL_FLOAT, texdata);
		}
341 342
	}

343
	glReadBuffer(GL_BACK);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
344
}