2 * Copyright (c) 2013 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 DEALINGS
26 * Test 2D depth array texture rendering with gl_Layer (AMD_vertex_shader_layer)
28 * This test uses layered rendering (gl_Layer) within the vertex shader.
29 * Support for gl_Layer in VS is added by the AMD_vertex_shader_layer
32 * This test first renders to a depth array texture which is attached to
33 * a framebuffer. The texture has 5 layers and 7 LODs.
35 * Once depths have been rendered to each array slice & LOD, the test
36 * then verifies the depth value in each array slice & LOD.
39 #include "piglit-util-gl.h"
45 #define DRAW_COUNT LAYERS * LOD
46 #define STRINGIFY(x) #x
47 #define EXP_STRINGIFY(x) STRINGIFY(x)
49 PIGLIT_GL_TEST_CONFIG_BEGIN
51 config
.supports_gl_core_version
= 31;
52 config
.supports_gl_compat_version
= 31;
54 config
.window_width
= (((SIZE
+PAD
)*LAYERS
)+PAD
);
55 config
.window_height
= (((SIZE
+PAD
)*2)+PAD
);
56 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGB
;
58 PIGLIT_GL_TEST_CONFIG_END
60 static GLuint rectangle_vertices_bo
;
62 /* VS and FS to fill the 2D array texture */
63 static const char *fill_tex_vs
=
65 "#extension GL_AMD_vertex_shader_layer: enable\n"
66 "uniform int drawing_level;\n"
69 "int num_layers = " EXP_STRINGIFY(LAYERS
) ";\n"
70 "int draw_count = " EXP_STRINGIFY(DRAW_COUNT
) ";\n"
73 " return float((drawing_level * num_layers) + gl_InstanceID) / draw_count;\n"
77 " gl_Position = vec4(vertex, get_z(), 1.0);\n"
78 " gl_Layer = gl_InstanceID;\n"
81 static GLuint fill_tex_program
;
84 render_tex_layers(GLuint tex
)
87 GLint drawing_level_loc
, vertex_loc
;
92 glUseProgram(fill_tex_program
);
94 drawing_level_loc
= glGetUniformLocation(fill_tex_program
, "drawing_level");
96 glBindBuffer(GL_ARRAY_BUFFER
, rectangle_vertices_bo
);
97 vertex_loc
= glGetAttribLocation(fill_tex_program
, "vertex");
98 glVertexAttribPointer(vertex_loc
, 2, GL_FLOAT
, 0, 0, 0);
99 glEnableVertexAttribArray(vertex_loc
);
100 for (lod
= 0; lod
< LOD
; lod
++) {
102 glFramebufferTexture(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, tex
, lod
);
103 status
= glCheckFramebufferStatus(GL_FRAMEBUFFER
);
104 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
105 fprintf(stderr
, "fbo incomplete (status = %s)\n",
106 piglit_get_gl_enum_name(status
));
110 /* Clear background to gray */
112 glClear(GL_DEPTH_BUFFER_BIT
);
114 glViewport(0, 0, size
, size
);
115 glUniform1i(drawing_level_loc
, lod
);
116 glDrawArraysInstanced(GL_TRIANGLE_STRIP
, 0, 4, LAYERS
);
117 color_index
+= LAYERS
;
119 glFramebufferTexture(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, 0, 0);
120 glDisableVertexAttribArray(vertex_loc
);
132 glGenTextures(1, &tex
);
133 glBindTexture(GL_TEXTURE_2D_ARRAY
, tex
);
134 for (lod
= 0; lod
< LOD
; lod
++) {
136 glTexImage3D(GL_TEXTURE_2D_ARRAY
, lod
, GL_DEPTH_COMPONENT
,
137 size
, size
, LAYERS
, 0, GL_DEPTH_COMPONENT
, GL_FLOAT
, NULL
);
140 render_tex_layers(tex
);
145 /* Attach the texture layer/lod to the read framebuffer
148 set_up_read_framebuffer(GLuint tex
, int level
, int layer
)
152 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER
,
156 status
= glCheckFramebufferStatus(GL_FRAMEBUFFER
);
157 if (status
== GL_FRAMEBUFFER_UNSUPPORTED
&& level
== 0) {
158 printf("This buffer combination is unsupported\n");
159 piglit_report_result(PIGLIT_SKIP
);
160 } else if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
161 printf("FBO incomplete at miplevel %d\n", level
);
162 piglit_report_result(PIGLIT_FAIL
);
167 test_texture(GLuint tex
)
170 GLboolean retval
= GL_TRUE
;
172 float draw_count
= LAYERS
* LOD
;
175 for (lod
= 0; lod
< LOD
; lod
++) {
176 for (layer
= 0; layer
< LAYERS
; layer
++) {
178 set_up_read_framebuffer(tex
, lod
, layer
);
179 expected
= ((float)(lod
* LAYERS
) + layer
) / draw_count
;
180 expected
= (expected
/ 2.0) + 0.5;
181 pass
= piglit_probe_rect_depth(0, 0, dim
, dim
, expected
);
182 retval
= retval
&& pass
;
196 static const GLfloat verts
[4][2] = {
203 glEnable(GL_DEPTH_TEST
);
205 glGenFramebuffers(1, &fbo
);
206 glBindFramebuffer(GL_FRAMEBUFFER
, fbo
);
207 glGenVertexArrays(1, &vao
);
208 glBindVertexArray(vao
);
209 glGenBuffers(1, &rectangle_vertices_bo
);
210 glBindBuffer(GL_ARRAY_BUFFER
, rectangle_vertices_bo
);
211 glBufferData(GL_ARRAY_BUFFER
, sizeof(verts
), verts
, GL_STATIC_DRAW
);
213 tex
= build_texture();
214 pass
= test_texture(tex
);
215 glDeleteTextures(1, &tex
);
217 glDeleteBuffers(1, &rectangle_vertices_bo
);
218 glDeleteFramebuffers(1, &fbo
);
219 glDeleteVertexArrays(1, &vao
);
221 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
225 piglit_init(int argc
, char **argv
)
227 /* For glFramebufferTexture we need either GL 3.2 or
228 * GL_ARB_geometry_shader4.
230 if (piglit_get_gl_version() < 32) {
231 piglit_require_extension("GL_ARB_geometry_shader4");
234 piglit_require_extension("GL_AMD_vertex_shader_layer");
236 fill_tex_program
= piglit_build_simple_program(fill_tex_vs
, NULL
);
237 piglit_check_gl_error(GL_NO_ERROR
);