1 /*
2 * Copyright © 2011 Intel Corporation
3 * Copyright © 2012 Red Hat
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /* Author: Dave Airlie */
26 /*
27 * Test to verify samplerCubeArrayShadow support
28 *
29 * samplerCubeArrayShadow takes the compare value in an extra vertex attrib.
30 * This test works like sampler-cube-shadow except it uses the cube
31 * map array interfaces.
32 */
33
34 #include "piglit-util-gl.h"
35
36 PIGLIT_GL_TEST_CONFIG_BEGIN
37
38 config.supports_gl_compat_version = 10;
39
40 config.window_width = 400;
41 config.window_height = 300;
42 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
43 config.khr_no_error_support = PIGLIT_NO_ERRORS;
44
45 PIGLIT_GL_TEST_CONFIG_END
46
47 static GLuint tex;
48 static GLint prog;
49
50 /* grab the coordinates from the main definition, and grab the
51 compvals from here */
52 static GLfloat cube_shadow_attributes[6][4][9];
53
54 static GLfloat verts[6][2] = { {100, 125}, {175, 125}, {250, 125},
55 {100, 200}, {175, 200}, {250, 200} };
56 static GLfloat compvals[6][4] = { { -0.50, 0.00, 0.50, 0.00 },
57 { 0.90, 0.20, -0.50, 0.20 },
58 { 0.35, 1.20, 0.35, -0.50 },
59 { 0.50, -0.50, 0.50, 1.50 },
60 { 0.85, 0.85, 0.85, 0.85 },
61 { 0.90, 0.90, 0.90, 0.90 } };
62
63 #define STRIDE (9 * sizeof(GLfloat))
64 /* Setup interleaved vertex attributes for 6 * 4 vertices:
65 * 4 float vertex coordinates for drawing 6 quads aligned in a 3x2 grid with
66 * some space inbetween.
67 * 4 float texture coordinates for sampling one cube map face per quad.
68 * 1 float compare value for shadow texture fetch.
69 */
setup_attributes(float layer_sample)70 void setup_attributes(float layer_sample)
71 {
72 int i, j;
73 for (i = 0; i < 6; i++) {
74 for (j = 0; j < 4; j++) {
75 cube_shadow_attributes[i][j][0] = verts[i][0];
76 cube_shadow_attributes[i][j][1] = verts[i][1];
77 cube_shadow_attributes[i][j][2] = 0.0;
78 cube_shadow_attributes[i][j][3] = 1.0;
79 if (j == 1 || j == 2)
80 cube_shadow_attributes[i][j][0] += 50.0;
81 if (j == 2 || j == 3)
82 cube_shadow_attributes[i][j][1] += 50.0;
83 memcpy(&cube_shadow_attributes[i][j][4], cube_face_texcoords[i][j], 3 * sizeof(GLfloat));
84 cube_shadow_attributes[i][j][7] = layer_sample;
85 cube_shadow_attributes[i][j][8] = compvals[i][j];
86 }
87 }
88 }
89
90 static const char *vertShaderText =
91 "#version 130\n"
92 "in vec4 vertex;\n"
93 "in vec4 texCoord;\n"
94 "in float compf;\n"
95 "out float compval;\n"
96 "void main()\n"
97 "{\n"
98 " gl_Position = gl_ModelViewProjectionMatrix * vertex;\n"
99 " gl_TexCoord[0] = texCoord;\n"
100 " compval = compf;\n"
101 "}\n";
102
103 static const char *fragShaderText =
104 "#version 130\n"
105 "#extension GL_ARB_texture_cube_map_array : enable\n"
106 "uniform samplerCubeArrayShadow cubeArrayShadow;\n"
107 "in float compval;\n"
108 "void main()\n"
109 "{\n"
110 " float shadow = texture(cubeArrayShadow, gl_TexCoord[0], compval);\n"
111 " gl_FragColor = vec4(shadow, shadow, shadow, 1.0);\n"
112 "}\n";
113
114 static void
shaderSetup(void)115 shaderSetup(void)
116 {
117 prog = piglit_build_simple_program(vertShaderText, fragShaderText);
118 glUseProgram(prog);
119 }
120
121
122 static void
loadTex(void)123 loadTex(void)
124 {
125 #define height 2
126 #define width 2
127 int i, j;
128 GLfloat tex_vals[12][width][height];
129
130 /* Set the cubemap depth values for each face */
131 for (i=0; i < height; ++i) {
132 for (j=0; j < width; ++j) {
133 /* fill base layer with wrong stuff - checks
134 we don't accidentally sample layer 0 */
135 tex_vals[0][i][j] = 1.0;
136 tex_vals[1][i][j] = 0.75;
137 tex_vals[2][i][j] = 0.50;
138 tex_vals[3][i][j] = 0.35;
139 tex_vals[4][i][j] = 0.2;
140 tex_vals[5][i][j] = 0.0;
141
142 tex_vals[6][i][j] = 0.0;
143 tex_vals[7][i][j] = 0.2;
144 tex_vals[8][i][j] = 0.35;
145 tex_vals[9][i][j] = 0.50;
146 tex_vals[10][i][j] = 0.75;
147 tex_vals[11][i][j] = 1.0;
148 }
149 }
150
151 /* Render the epth cube texture using LUMINANCE */
152 glGenTextures(1, &tex);
153 glActiveTexture(GL_TEXTURE0);
154 glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, tex);
155 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_GENERATE_MIPMAP, GL_FALSE);
156 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
157 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
158 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S,
159 GL_CLAMP_TO_EDGE);
160 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY,
161 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
162 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY,
163 GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
164 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_DEPTH_TEXTURE_MODE,
165 GL_LUMINANCE);
166 glTexParameterf(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_COMPARE_MODE,
167 GL_COMPARE_REF_TO_TEXTURE );
168 glTexParameterf(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_COMPARE_FUNC,
169 GL_LEQUAL);
170
171 glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_DEPTH_COMPONENT,
172 width, height, 12, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (void*)tex_vals);
173 #undef height
174 #undef width
175 }
176
177 void
piglit_init(int argc,char ** argv)178 piglit_init(int argc, char **argv)
179 {
180 piglit_require_extension("GL_ARB_texture_cube_map_array");
181 loadTex();
182 glMatrixMode(GL_PROJECTION);
183 glPushMatrix();
184 glLoadIdentity();
185 glOrtho(0, piglit_width, 0, piglit_height, -1, 1);
186 glMatrixMode(GL_MODELVIEW);
187 glPushMatrix();
188 glLoadIdentity();
189 glClearColor(0.1, 0.1, 0.1, 1.0);
190 shaderSetup();
191 setup_attributes(1.0);
192 }
193
194 enum piglit_result
piglit_display(void)195 piglit_display(void)
196 {
197 GLint cubeArrayShadow_loc, vertex_loc, texCoord_loc, compf_loc;
198 GLboolean pass = GL_TRUE;
199 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
200 GLfloat black[4] = {0.0, 0.0, 0.0, 1.0};
201 int i;
202
203 cubeArrayShadow_loc = glGetUniformLocation(prog, "cubeArrayShadow");
204 vertex_loc = glGetAttribLocation(prog, "vertex");
205 texCoord_loc = glGetAttribLocation(prog, "texCoord");
206 compf_loc = glGetAttribLocation(prog, "compf");
207
208 glClear(GL_COLOR_BUFFER_BIT);
209 glMatrixMode(GL_MODELVIEW);
210
211 glUniform1i(cubeArrayShadow_loc, 0);
212 glEnableVertexAttribArray(vertex_loc);
213 glEnableVertexAttribArray(texCoord_loc);
214 glEnableVertexAttribArray(compf_loc);
215
216 /* Apply each face of cubemap as texture to a polygon */
217 for (i = 0; i < 6; ++i) {
218 glVertexAttribPointer(vertex_loc, 4, GL_FLOAT, GL_FALSE,
219 STRIDE, &cube_shadow_attributes[i][0][0]);
220 glVertexAttribPointer(texCoord_loc, 4, GL_FLOAT, GL_FALSE,
221 STRIDE, &cube_shadow_attributes[i][0][4]);
222 glVertexAttribPointer(compf_loc, 1, GL_FLOAT, GL_FALSE,
223 STRIDE, &cube_shadow_attributes[i][0][8]);
224 glDrawArrays(GL_QUADS, 0, 4);
225 }
226
227 /* Test the pixel color of polygons against the expected output */
228 /* Polygon 1 */
229 pass = piglit_probe_pixel_rgb(101, 170, white);
230 pass = pass && piglit_probe_pixel_rgb(105, 130, white);
231 pass = pass && piglit_probe_pixel_rgb(120, 145, white);
232 pass = pass && piglit_probe_pixel_rgb(145, 126, white);
233 pass = pass && piglit_probe_pixel_rgb(105, 174, black);
234 pass = pass && piglit_probe_pixel_rgb(130, 155, black);
235 pass = pass && piglit_probe_pixel_rgb(145, 170, black);
236 pass = pass && piglit_probe_pixel_rgb(149, 130, black);
237
238 /* Polygon 2 */
239 pass = pass && piglit_probe_pixel_rgb(176, 170, black);
240 pass = pass && piglit_probe_pixel_rgb(180, 130, black);
241 pass = pass && piglit_probe_pixel_rgb(195, 145, black);
242 pass = pass && piglit_probe_pixel_rgb(220, 126, black);
243 pass = pass && piglit_probe_pixel_rgb(224, 130, white);
244 pass = pass && piglit_probe_pixel_rgb(205, 155, white);
245 pass = pass && piglit_probe_pixel_rgb(220, 170, white);
246 pass = pass && piglit_probe_pixel_rgb(180, 174, white);
247
248 /* Polygon 3 */
249 pass = pass && piglit_probe_pixel_rgb(251, 130, white);
250 pass = pass && piglit_probe_pixel_rgb(255, 170, white);
251 pass = pass && piglit_probe_pixel_rgb(270, 155, white);
252 pass = pass && piglit_probe_pixel_rgb(290, 174, white);
253 pass = pass && piglit_probe_pixel_rgb(255, 126, black);
254 pass = pass && piglit_probe_pixel_rgb(280, 145, black);
255 pass = pass && piglit_probe_pixel_rgb(295, 130, black);
256 pass = pass && piglit_probe_pixel_rgb(299, 170, black);
257
258 /* Polygon 4 */
259 pass = pass && piglit_probe_pixel_rgb(101, 205, black);
260 pass = pass && piglit_probe_pixel_rgb(105, 245, black);
261 pass = pass && piglit_probe_pixel_rgb(120, 230, black);
262 pass = pass && piglit_probe_pixel_rgb(145, 249, black);
263 pass = pass && piglit_probe_pixel_rgb(105, 201, white);
264 pass = pass && piglit_probe_pixel_rgb(130, 220, white);
265 pass = pass && piglit_probe_pixel_rgb(145, 205, white);
266 pass = pass && piglit_probe_pixel_rgb(149, 245, white);
267
268 /* Polygon 5 & 6 are filled with a flat color. So probe using
269 * piglit_probe_rect_rgb
270 */
271 pass = pass && piglit_probe_rect_rgba(175, 200, 50, 50, black);
272 pass = pass && piglit_probe_rect_rgba(250, 200, 50, 50, white);
273
274 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
275 if (piglit_automatic)
276 piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
277 piglit_present_results();
278 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
279 }
280