1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
10  *
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.
14  *
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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /** \file array-depth-roundtrip.c
25  *
26  * Test that an array texture containing depth data works properly
27  * when making a full "roundtrip" through both the GPU's rendering
28  * pipeline and texturing operations.
29  *
30  * The test performs the following steps:
31  *
32  * - Create an array texture containing depth data.
33  *
34  * - Bind each slice of the array texture to a framebuffer, clear it,
35  *   and render a quad to it.  A different depth value is used for
36  *   each slice of the array.
37  *
38  * - Use a shader to read from each slice of the array texture and
39  *   render to the window system framebuffer.
40  *
41  * - Verify that correct data was rendered to the window system
42  *   framebuffer.
43  */
44 
45 #include "piglit-util-gl.h"
46 
47 
48 #define TEX_WIDTH 56
49 #define TEX_HEIGHT 56
50 #define NUM_TILES_ACROSS 4
51 #define NUM_TILES_DOWN 4
52 #define TEX_DEPTH (NUM_TILES_ACROSS * NUM_TILES_DOWN)
53 
54 
55 PIGLIT_GL_TEST_CONFIG_BEGIN
56 
57 	config.supports_gl_compat_version = 10;
58 
59 	config.window_width = TEX_WIDTH*NUM_TILES_ACROSS;
60 	config.window_height = TEX_HEIGHT*NUM_TILES_DOWN;
61 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB;
62 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
63 
64 PIGLIT_GL_TEST_CONFIG_END
65 
66 
67 GLuint tex;
68 GLuint fb;
69 GLuint prog;
70 GLint samp_loc;
71 GLint proj_loc;
72 GLint tex_depth_loc;
73 
74 
75 const char *vs_text = \
76 	"#version 130\n"
77 	"uniform mat4 proj;\n"
78 	"uniform float tex_depth;\n"
79 	"out vec3 tex_coord;\n"
80 	"void main()\n"
81 	"{\n"
82 	"  gl_Position = proj * gl_Vertex;\n"
83 	"  tex_coord = vec3(gl_Vertex.xy, tex_depth);\n"
84 	"}\n";
85 
86 
87 const char *fs_text = \
88 	"#version 130\n"
89 	"uniform sampler2DArray samp;\n"
90 	"in vec3 tex_coord;\n"
91 	"void main()\n"
92 	"{\n"
93 	"  gl_FragColor = texture(samp, tex_coord);\n"
94 	"}\n";
95 
96 
97 void
piglit_init(int argc,char ** argv)98 piglit_init(int argc, char **argv)
99 {
100 	GLuint vs, fs;
101 
102 	piglit_require_gl_version(30);
103 
104 	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
105 	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_text);
106 	prog = piglit_link_simple_program(vs, fs);
107 	samp_loc = glGetUniformLocation(prog, "samp");
108 	proj_loc = glGetUniformLocation(prog, "proj");
109 	tex_depth_loc = glGetUniformLocation(prog, "tex_depth");
110 
111 	/* Create the array texture */
112 	glGenTextures(1, &tex);
113 	glActiveTexture(GL_TEXTURE0);
114 	glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
115 	glTexImage3D(GL_TEXTURE_2D_ARRAY, 0 /* level */,
116 		     GL_DEPTH_COMPONENT, TEX_WIDTH, TEX_HEIGHT, TEX_DEPTH,
117 		     0 /* border */, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
118 	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER,
119 			GL_NEAREST);
120 	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER,
121 			GL_NEAREST);
122 	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE,
123 			GL_NONE);
124 
125 	glGenFramebuffers(1, &fb);
126 
127 	if (!piglit_check_gl_error(GL_NO_ERROR))
128 		piglit_report_result(PIGLIT_FAIL);
129 }
130 
131 
132 enum piglit_result
piglit_display()133 piglit_display()
134 {
135 	int zoffset, x_tile, y_tile, i;
136 	float depth_value;
137 	float expected[3];
138 	bool pass = true;
139 
140 	/* Bind each level of the array texture to the framebuffer,
141 	 * clear it, and render a quad to it, using a depth value that
142 	 * is different in each array slice.
143 	 */
144 	glBindFramebuffer(GL_FRAMEBUFFER, fb);
145 	glViewport(0, 0, TEX_WIDTH, TEX_HEIGHT);
146 	glUseProgram(0);
147 	glEnable(GL_DEPTH_TEST);
148 	glDepthFunc(GL_ALWAYS);
149 	for (zoffset = 0; zoffset < TEX_DEPTH; ++zoffset) {
150 		glFramebufferTextureLayer(GL_FRAMEBUFFER,
151 					  GL_DEPTH_ATTACHMENT,
152 					  tex, 0 /* level */, zoffset);
153 		glClear(GL_DEPTH_BUFFER_BIT);
154 		depth_value = zoffset / (float) (TEX_DEPTH - 1);
155 		/* Adjust depth_value to [-1, 1] range to account for
156 		 * the fact that the pipeline translates from [-1, 1]
157 		 * to [0, 1].
158 		 */
159 		depth_value = depth_value * 2.0 - 1.0;
160 		piglit_draw_rect_z(depth_value,
161 				   -1, -1, 2, 2);
162 	}
163 
164 	/* Use a shader to read from each slice of the array texture
165 	 * and render to the window system framebuffer.
166 	 */
167 	glBindFramebuffer(GL_FRAMEBUFFER, piglit_winsys_fbo);
168 	glViewport(0, 0, piglit_width, piglit_height);
169 	glClear(GL_COLOR_BUFFER_BIT);
170 	glUseProgram(prog);
171 	glUniform1i(samp_loc, 0);
172 	for (y_tile = 0; y_tile < NUM_TILES_DOWN; ++y_tile) {
173 		for (x_tile = 0; x_tile < NUM_TILES_ACROSS; ++x_tile) {
174 			float xscale = 2.0 / NUM_TILES_ACROSS;
175 			float yscale = 2.0 / NUM_TILES_DOWN;
176 			float proj[4][4] = {
177 				{ xscale, 0, 0, xscale * x_tile - 1 },
178 				{ 0, yscale, 0, yscale * y_tile - 1 },
179 				{ 0, 0, 0, 0 },
180 				{ 0, 0, 0, 1 }
181 			};
182 			zoffset = NUM_TILES_ACROSS * y_tile + x_tile;
183 			glUniformMatrix4fv(proj_loc, 1, GL_TRUE, &proj[0][0]);
184 			glUniform1f(tex_depth_loc, zoffset);
185 			piglit_draw_rect(0, 0, 1, 1);
186 		}
187 	}
188 
189 	/* Verify that correct data was rendered. */
190 	for (y_tile = 0; y_tile < NUM_TILES_DOWN; ++y_tile) {
191 		for (x_tile = 0; x_tile < NUM_TILES_ACROSS; ++x_tile) {
192 			zoffset = NUM_TILES_ACROSS * y_tile + x_tile;
193 			printf("Probing array slice %d\n", zoffset);
194 			depth_value = zoffset / (float) (TEX_DEPTH - 1);
195 			for (i = 0; i < 3; ++i)
196 				expected[i] = depth_value;
197 			pass = piglit_probe_rect_rgb(x_tile * TEX_WIDTH,
198 						     y_tile * TEX_HEIGHT,
199 						     TEX_WIDTH, TEX_HEIGHT,
200 						     expected) && pass;
201 		}
202 	}
203 
204 	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
205 
206 	piglit_present_results();
207 
208 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
209 }
210