1 /* Copyright © 2018 Danylo Piliaiev
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice (including the next
11 * paragraph) shall be included in all copies or substantial portions of the
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Tests GL_ARB_texture_view interaction with ARB_shader_image_load_store.
25 * Creates texture maps with different solid colors for each layer,
26 * reads the framebuffer to ensure the rendered color is correct
27 * and verifies that image has expected layers count.
30 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config
.supports_gl_core_version
= 32;
37 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
38 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
40 PIGLIT_GL_TEST_CONFIG_END
45 const char* uniform_type
;
46 const char* img_layers_dimension
;
47 const char* img_access
;
51 struct test_info tests
[] = {
52 {GL_TEXTURE_1D_ARRAY
, "image1DArray", "y", "ivec2(0, tex_layer)", -1},
53 {GL_TEXTURE_2D_ARRAY
, "image2DArray", "z", "ivec3(0, 0, tex_layer)", -1},
54 {GL_TEXTURE_CUBE_MAP_ARRAY
, "imageCubeArray", "z * 6", "ivec3(0, 0, tex_layer)", -1},
58 test_render_layers(const struct test_info
*test
)
61 const GLint width
= 16, height
= 16, layers
= 12;
62 const GLint num_layers
[] = {7, 11, 2, 4};
65 glUseProgram(test
->program
);
67 const GLint expected_layers_uniform
= glGetUniformLocation(test
->program
, "expected_layers");
69 glGenTextures(1, &tex
);
70 glActiveTexture(GL_TEXTURE0
);
71 glBindTexture(test
->target
, tex
);
73 if (test
->target
== GL_TEXTURE_1D_ARRAY
) {
74 glTexStorage2D(test
->target
, 1, GL_RGBA8
, width
, layers
);
76 glTexStorage3D(test
->target
, 1, GL_RGBA8
, width
, height
, layers
);
79 /* Load each array layer with a different color texture */
80 for (GLint l
= 0; l
< layers
; l
++) {
81 GLubyte
*buf
= create_solid_image(width
, height
, 1, 4, l
);
84 if (test
->target
== GL_TEXTURE_1D_ARRAY
) {
85 glTexSubImage2D(test
->target
, 0, 0, l
,
87 GL_UNSIGNED_BYTE
, buf
);
89 glTexSubImage3D(test
->target
, 0, 0, 0, l
,
90 width
, height
, 1, GL_RGBA
,
91 GL_UNSIGNED_BYTE
, buf
);
98 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
100 /* Create a view of texture with restricted layers, bind it as image and draw a quad
101 * using a single layer in the view range which varies every loop, check image's layers
104 for (GLint first_layer
= 0; first_layer
< ARRAY_SIZE(num_layers
); first_layer
++) {
105 const int total_layers
= test
->target
== GL_TEXTURE_CUBE_MAP_ARRAY
? 6 : num_layers
[first_layer
];
108 glGenTextures(1, &view_tex
);
109 glTextureView(view_tex
, test
->target
, tex
, GL_RGBA8
,
110 0, 1, first_layer
, total_layers
);
112 glActiveTexture(GL_TEXTURE0
);
113 glBindImageTexture(0, view_tex
, 0, GL_TRUE
, 0, GL_READ_ONLY
, GL_RGBA8
);
115 glUniform1i(expected_layers_uniform
, total_layers
);
117 glClear(GL_COLOR_BUFFER_BIT
);
119 draw_3d_depth(-1.0, -1.0, 2.0, 2.0, total_layers
- 1);
121 const int expected_layer
= first_layer
+ total_layers
- 1;
124 expected
[0] = Colors
[expected_layer
][0] / 255.0;
125 expected
[1] = Colors
[expected_layer
][1] / 255.0;
126 expected
[2] = Colors
[expected_layer
][2] / 255.0;
129 const int p
= piglit_probe_pixel_rgba(piglit_width
/2, piglit_height
/2, expected
);
131 piglit_present_results();
134 printf("Wrong color for view min layer %d, expected layer %d\n",
135 first_layer
, expected_layer
);
138 glDeleteTextures(1, &view_tex
);
141 glDeleteTextures(1, &tex
);
149 for (int test_idx
= 0; test_idx
< ARRAY_SIZE(tests
); test_idx
++) {
150 const struct test_info
*test
= &tests
[test_idx
];
152 const bool subtest_pass
= test_render_layers(test
);
154 piglit_report_subtest_result(subtest_pass
? PIGLIT_PASS
: PIGLIT_FAIL
,
155 "layers rendering of %s", test
->uniform_type
);
156 pass
= pass
&& subtest_pass
;
158 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
159 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
162 static const char *vs
=
164 "in vec4 piglit_vertex;\n"
165 "in vec2 piglit_texcoord;\n"
166 "flat out int tex_layer;\n"
168 " gl_Position = vec4(piglit_vertex.xy, 0.0, 1.0);\n"
169 " tex_layer = int(piglit_vertex.z);\n"
172 static const char *fs_template
=
174 "#extension GL_ARB_shader_image_size : enable\n"
175 "#extension GL_ARB_shading_language_420pack : enable\n"
176 "#extension GL_ARB_shader_image_load_store : enable\n"
177 "flat in int tex_layer;\n"
178 "layout(binding = 0, rgba8) uniform %s img;\n"
179 "uniform int expected_layers;\n"
182 " if (imageSize(img).%s == expected_layers)\n"
183 " color = vec4(imageLoad(img, %s).rgb, 1.0);\n"
185 " color = vec4(0.0);\n"
189 piglit_init(int argc
, char **argv
)
191 piglit_require_extension("GL_ARB_texture_storage");
192 piglit_require_extension("GL_ARB_texture_view");
193 piglit_require_extension("GL_ARB_shader_image_load_store");
194 piglit_require_extension("GL_ARB_shader_image_size");
196 for (int test_idx
= 0; test_idx
< ARRAY_SIZE(tests
); test_idx
++) {
197 struct test_info
*test
= &tests
[test_idx
];
200 snprintf(fs
, sizeof(fs
), fs_template
, test
->uniform_type
,
201 test
->img_layers_dimension
, test
->img_access
);
202 test
->program
= piglit_build_simple_program(vs
, fs
);