Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / glsl-lod-bias.c
blobed07322b8d8d3c86060180c3cb2cf72bc0eaf14d
1 /*
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
13 * Software.
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.
24 // author: Ben Holmes
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
37 * texture is 1x1.
39 #define TEST_COLS 5
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;
46 static GLuint tex[1];
47 static GLint prog;
48 static GLint vs;
49 static GLint fs;
50 static GLint tex_loc;
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"
59 "void main()\n"
60 "{ \n"
61 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
62 " texCoords = gl_MultiTexCoord0.st;\n"
63 "} \n";
65 static const char *fragShaderText =
66 "uniform sampler2D tex2d;\n"
67 "uniform float lodBias;\n"
68 "varying vec2 texCoords;\n"
69 "void main()\n"
70 "{ \n"
71 " gl_FragColor = texture2D(tex2d, texCoords, lodBias);\n"
72 "} \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 */
79 void
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);
87 loadTex();
88 compileLinkProg();
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]);
96 static void
97 compileLinkProg(void)
99 GLint stat;
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);
107 glCompileShader(vs);
108 glGetShaderiv(vs, GL_COMPILE_STATUS, &stat);
109 if (!stat) {
110 printf("error compiling vertex shader!\n");
111 exit(1);
114 glCompileShader(fs);
115 glGetShaderiv(fs, GL_COMPILE_STATUS, &stat);
116 if (!stat) {
117 printf("error compiling fragment shader!\n");
118 exit(1);
122 prog = glCreateProgram();
123 glAttachShader(prog, vs);
124 glAttachShader(prog, fs);
125 glLinkProgram(prog);
126 glUseProgram(prog);
128 tex_loc = glGetUniformLocation(prog, "tex2d");
129 bias_loc = glGetUniformLocation(prog, "lodBias");
131 glUniform1i(tex_loc, 0);
134 static void
135 loadTex(void)
137 #define height BOX_SIZE
138 #define width BOX_SIZE
139 int i, j;
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));
147 else {
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,
160 GL_NEAREST);
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);
166 glEnable(GL_BLEND);
167 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 #undef height
169 #undef width
173 enum piglit_result
174 piglit_display(void)
176 const int tile_size = BOX_SIZE / 4;
177 GLboolean pass = GL_TRUE;
178 unsigned i;
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,
191 BOX_SIZE, BOX_SIZE,
192 0.0, 0.0, 1.0, 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)) {
199 pass = GL_FALSE;
203 glutSwapBuffers();
205 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;