2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 /* This test utilizes a texture sampling function in GLSL that specifies a
27 * LOD bias. Create a texture with a 4x4 checkerboard pattern. Draw that
28 * texture with all of the positive LOD biases that will result in a mipmap
29 * level greater than or equal to 4x4 (single texel tiles) being used. Verify
30 * that all the image are the same.
33 #include "piglit-util.h"
35 /* Pick the number of LODs to examine and the size of the texture so that the
36 * smallest LOD is the one where each of the 4x4 tiles in the checkerboard
40 #define BOX_SIZE (1 << (TEST_COLS + 1))
42 int piglit_width
= (BOX_SIZE
+ 2) * TEST_COLS
+ 1;
43 int piglit_height
= (BOX_SIZE
+ 1) + 1;
44 int piglit_window_mode
= GLUT_RGB
| GLUT_DOUBLE
;
51 static GLint bias_loc
;
54 static void loadTex(void);
55 static void compileLinkProg(void);
57 static const char *vertShaderText
=
58 "varying vec2 texCoords;\n"
61 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
62 " texCoords = gl_MultiTexCoord0.st;\n"
65 static const char *fragShaderText
=
66 "uniform sampler2D tex2d;\n"
67 "uniform float lodBias;\n"
68 "varying vec2 texCoords;\n"
71 " gl_FragColor = texture2D(tex2d, texCoords, lodBias);\n"
75 static const float clear_color
[4] = {0.6, 0.6, 0.6, 1.0};
76 static const float green
[4] = {0.0, 1.0, 0.0, 1.0};
77 static const float pink
[4] = {1.0, 0.0, 1.0, 0.0}; /* Note: 0.0 alpha */
80 piglit_init(int argc
, char **argv
)
82 if (!GLEW_VERSION_2_0
) {
83 printf("Requires OpenGL 2.0\n");
84 piglit_report_result(PIGLIT_SKIP
);
90 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
91 glEnable(GL_TEXTURE_2D
);
92 glClearColor(clear_color
[0], clear_color
[1],
93 clear_color
[2], clear_color
[3]);
101 vs
= glCreateShader(GL_VERTEX_SHADER
);
102 fs
= glCreateShader(GL_FRAGMENT_SHADER
);
104 glShaderSource(vs
, 1, (const GLchar
**) &vertShaderText
, NULL
);
105 glShaderSource(fs
, 1, (const GLchar
**) &fragShaderText
, NULL
);
108 glGetShaderiv(vs
, GL_COMPILE_STATUS
, &stat
);
110 printf("error compiling vertex shader!\n");
115 glGetShaderiv(fs
, GL_COMPILE_STATUS
, &stat
);
117 printf("error compiling fragment shader!\n");
122 prog
= glCreateProgram();
123 glAttachShader(prog
, vs
);
124 glAttachShader(prog
, fs
);
128 tex_loc
= glGetUniformLocation(prog
, "tex2d");
129 bias_loc
= glGetUniformLocation(prog
, "lodBias");
131 glUniform1i(tex_loc
, 0);
137 #define height BOX_SIZE
138 #define width BOX_SIZE
141 GLfloat texData
[width
][height
][4];
142 for (i
=0; i
< width
; ++i
) {
143 for (j
=0; j
< height
; ++j
) {
144 if ((i
^ j
) & (BOX_SIZE
/ 4)) {
145 memcpy(texData
[i
][j
], pink
, sizeof(pink
));
148 memcpy(texData
[i
][j
], green
, sizeof(green
));
153 glGenTextures(1, tex
);
154 glActiveTexture(GL_TEXTURE0
);
155 glBindTexture(GL_TEXTURE_2D
, tex
[0]);
156 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP
, GL_TRUE
);
157 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
158 GL_NEAREST_MIPMAP_NEAREST
);
159 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
,
161 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
162 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
163 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, width
, height
, 0,
164 GL_RGBA
, GL_FLOAT
, texData
);
167 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
176 const int tile_size
= BOX_SIZE
/ 4;
177 GLboolean pass
= GL_TRUE
;
180 glClear(GL_COLOR_BUFFER_BIT
);
182 for (i
= 0; i
< TEST_COLS
; i
++) {
183 const int x
= 1 + ((BOX_SIZE
+ 2) * i
);
185 glUniform1f(bias_loc
, (float) i
);
187 /* Draw the rectangle the same size as the texture. This
188 * guarantees that the unbiased LOD will be 0.0.
190 piglit_draw_rect_tex((float) x
, 1.0,
194 /* The middle of the lower left tile should be green, and the
195 * middle of the tile next to it should be the clear color.
197 if (!piglit_probe_pixel_rgb(x
+ (3 * tile_size
/ 2), 2, clear_color
)
198 || !piglit_probe_pixel_rgb(x
+ (tile_size
/ 2), 2, green
)) {
205 return pass
? PIGLIT_SUCCESS
: PIGLIT_FAILURE
;