ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / nv_image_formats / copy-image-formats.c
blobc15f23eae9ffd0e392cc938d057a7a4b04dee99e
1 /*
2 * Copyright (C) 2016 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 DEALINGS
21 * IN THE SOFTWARE.
24 /** @file copy-image-formats.c
26 * A simple test verifying we can make use of the additional OpenGL ES 3.1
27 * image formats provided by the GL_NV_image_formats extension. The
28 * normalized 16 bits format provided by this extension are subject to the
29 * condition that GL_EXT_texture_norm16 or equivalent is available.
32 #include "piglit-util-gl.h"
34 const struct image_format {
35 /** Format name as specified by GLSL. */
36 const char *name;
38 /** Format enum. */
39 GLenum format;
41 /** Pixel transfer format (e.g. as specified for glGetTexImage()). */
42 GLenum pixel_format;
44 /** Pixel transfer type (e.g. as specified for glGetTexImage()). */
45 GLenum pixel_type;
46 } image_formats[] = {
47 { "rg32f", GL_RG32F, GL_RG, GL_FLOAT },
48 { "rg16f", GL_RG16F, GL_RG, GL_HALF_FLOAT },
49 { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV },
50 { "r16f", GL_R16F, GL_RED, GL_HALF_FLOAT },
51 { "rgb10_a2ui", GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV },
52 { "rg32ui", GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT },
53 { "rg16ui", GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT },
54 { "rg8ui", GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE },
55 { "r16ui", GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT },
56 { "r8ui", GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE },
57 { "rg32i", GL_RG32I, GL_RG_INTEGER, GL_INT },
58 { "rg16i", GL_RG16I, GL_RG_INTEGER, GL_SHORT },
59 { "rg8i", GL_RG8I, GL_RG_INTEGER, GL_BYTE },
60 { "r16i", GL_R16I, GL_RED_INTEGER, GL_SHORT },
61 { "r8i", GL_R8I, GL_RED_INTEGER, GL_BYTE },
62 { "rgba16", GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT },
63 { "rgb10_a2", GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV },
64 { "rg16", GL_RG16, GL_RG, GL_UNSIGNED_SHORT },
65 { "rg8", GL_RG8, GL_RG, GL_UNSIGNED_BYTE },
66 { "r16", GL_R16, GL_RED, GL_UNSIGNED_SHORT },
67 { "r8", GL_R8, GL_RED, GL_UNSIGNED_BYTE },
68 { "rgba16_snorm", GL_RGBA16_SNORM, GL_RGBA, GL_SHORT },
69 { "rg16_snorm", GL_RG16_SNORM, GL_RG, GL_SHORT },
70 { "rg8_snorm", GL_RG8_SNORM, GL_RG, GL_BYTE },
71 { "r16_snorm", GL_R16_SNORM, GL_RED, GL_SHORT },
72 { "r8_snorm", GL_R8_SNORM, GL_RED, GL_BYTE },
75 static struct piglit_subtest tests[ARRAY_SIZE(image_formats) + 1];
76 static struct piglit_gl_test_config * piglit_config;
77 static enum piglit_result run_test(void *);
79 PIGLIT_GL_TEST_CONFIG_BEGIN
81 piglit_config = &config;
83 for (unsigned i = 0; i < ARRAY_SIZE(image_formats); ++i) {
85 char * name[64];
86 asprintf(name, "copy-%s", image_formats[i].name);
87 tests[i].name = *name;
88 tests[i].option = image_formats[i].name;
89 tests[i].subtest_func = run_test;
90 tests[i].data = (void *)&image_formats[i];
92 config.subtests = tests;
94 config.supports_gl_es_version = 31;
96 PIGLIT_GL_TEST_CONFIG_END
98 #define WIDTH 16
99 #define HEIGHT 16
101 static const char *
102 glsl_image_type_name(GLenum format)
104 switch (format) {
105 case GL_RG32F:
106 case GL_RG16F:
107 case GL_R11F_G11F_B10F:
108 case GL_R16F:
109 /* Fallthrough */
111 case GL_RGBA16_EXT:
112 case GL_RGB10_A2:
113 case GL_RG16_EXT:
114 case GL_RG8:
115 case GL_R16_EXT:
116 case GL_R8:
117 /* Fallthrough */
119 case GL_RGBA16_SNORM_EXT:
120 case GL_RG16_SNORM_EXT:
121 case GL_RG8_SNORM:
122 case GL_R16_SNORM_EXT:
123 case GL_R8_SNORM:
124 return "image";
126 case GL_RGB10_A2UI:
127 case GL_RG32UI:
128 case GL_RG16UI:
129 case GL_RG8UI:
130 case GL_R16UI:
131 case GL_R8UI:
132 return "uimage";
134 case GL_RG32I:
135 case GL_RG16I:
136 case GL_RG8I:
137 case GL_R16I:
138 case GL_R8I:
139 return "iimage";
141 default:
142 assert("Unsupported format");
143 return "";
147 static const char *
148 glsl_type_name(GLenum format)
150 switch (format) {
151 case GL_RG32F:
152 case GL_RG16F:
153 case GL_R11F_G11F_B10F:
154 case GL_R16F:
155 /* Fallthrough */
157 case GL_RGBA16_EXT:
158 case GL_RGB10_A2:
159 case GL_RG16_EXT:
160 case GL_RG8:
161 case GL_R16_EXT:
162 case GL_R8:
163 /* Fallthrough */
165 case GL_RGBA16_SNORM_EXT:
166 case GL_RG16_SNORM_EXT:
167 case GL_RG8_SNORM:
168 case GL_R16_SNORM_EXT:
169 case GL_R8_SNORM:
170 return "highp vec4";
172 case GL_RGB10_A2UI:
173 case GL_RG32UI:
174 case GL_RG16UI:
175 case GL_RG8UI:
176 case GL_R16UI:
177 case GL_R8UI:
178 return "highp uvec4";
180 case GL_RG32I:
181 case GL_RG16I:
182 case GL_RG8I:
183 case GL_R16I:
184 case GL_R8I:
185 return "highp ivec4";
187 default:
188 assert("Unsupported format");
189 return "";
193 static bool
194 format_is_norm16(GLenum format)
196 switch (format) {
197 case GL_RGBA16_EXT:
198 case GL_RG16_EXT:
199 case GL_R16_EXT:
200 case GL_RGBA16_SNORM_EXT:
201 case GL_RG16_SNORM_EXT:
202 case GL_R16_SNORM_EXT:
203 return true;
205 default:
206 return false;
210 static enum piglit_result
211 run_test(void * data)
213 struct image_format * image_format = (struct image_format *)data;
214 GLuint src, dst, prog;
215 char *fs_source;
217 glGenTextures(1, &src);
218 glBindTexture(GL_TEXTURE_2D, src);
219 glTexStorage2D(GL_TEXTURE_2D, 1, image_format->format, WIDTH, HEIGHT);
220 glBindImageTexture(0, src, 0, GL_FALSE, 0,
221 GL_READ_ONLY, image_format->format);
223 if (format_is_norm16(image_format->format)) {
224 if (!piglit_is_extension_supported("GL_EXT_texture_norm16")) {
225 piglit_check_gl_error(GL_INVALID_VALUE);
226 return PIGLIT_PASS;
229 piglit_check_gl_error(GL_NO_ERROR);
231 glGenTextures(1, &dst);
232 glBindTexture(GL_TEXTURE_2D, dst);
233 glTexStorage2D(GL_TEXTURE_2D, 1, image_format->format, WIDTH, HEIGHT);
234 glBindImageTexture(0, dst, 0, GL_FALSE, 0,
235 GL_WRITE_ONLY, image_format->format);
236 piglit_check_gl_error(GL_NO_ERROR);
238 fs_source = NULL;
239 if (asprintf(&fs_source,
240 "#version 310 es\n"
241 "#extension GL_NV_image_formats : require\n"
242 "\n"
243 "layout(%s) readonly uniform highp %s2D img_src;\n"
244 "layout(%s) writeonly uniform highp %s2D img_dst;\n"
245 "\n"
246 "void main() {\n"
247 " %s v = imageLoad(img_src, ivec2(gl_FragCoord));\n"
248 " imageStore(img_dst, ivec2(gl_FragCoord), v);\n"
249 "}",
250 image_format->name,
251 glsl_image_type_name(image_format->format),
252 image_format->name,
253 glsl_image_type_name(image_format->format),
254 glsl_type_name(image_format->format)) < 0)
255 return PIGLIT_FAIL;
257 prog = piglit_build_simple_program(
258 "#version 310 es\n"
259 "\n"
260 "in vec4 piglit_vertex;\n"
261 "void main() {\n"
262 " gl_Position = piglit_vertex;\n"
263 "}",
264 fs_source);
265 free(fs_source);
267 glUseProgram(prog);
269 piglit_draw_rect(-1, -1, 1, 1);
271 return PIGLIT_PASS;
274 void
275 piglit_init(int argc, char **argv)
277 enum piglit_result status = PIGLIT_PASS;
279 piglit_require_extension("GL_NV_image_formats");
281 status = piglit_run_selected_subtests(
282 tests,
283 piglit_config->selected_subtests,
284 piglit_config->num_selected_subtests,
285 status);
287 piglit_report_result(status);
290 enum piglit_result
291 piglit_display(void)
293 return PIGLIT_FAIL;