perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / tests / spec / ext_texture_storage_compression / tex-storage.c
blob81c8a7b89ac9c8e39658734f023b0b6921c6f973
1 /*
2 * Copyright © 2024 Collabora Ltd.
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
21 * DEALINGS IN THE SOFTWARE.
24 #include "piglit-util-gl.h"
26 PIGLIT_GL_TEST_CONFIG_BEGIN
28 config.supports_gl_es_version = 30;
30 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
32 config.khr_no_error_support = PIGLIT_HAS_ERRORS;
34 PIGLIT_GL_TEST_CONFIG_END
36 static int
37 enum_to_rate(GLint value)
39 switch (value) {
40 case GL_SURFACE_COMPRESSION_FIXED_RATE_NONE_EXT: return 0;
41 case GL_SURFACE_COMPRESSION_FIXED_RATE_1BPC_EXT: return 1;
42 case GL_SURFACE_COMPRESSION_FIXED_RATE_2BPC_EXT: return 2;
43 case GL_SURFACE_COMPRESSION_FIXED_RATE_3BPC_EXT: return 3;
44 case GL_SURFACE_COMPRESSION_FIXED_RATE_4BPC_EXT: return 4;
45 case GL_SURFACE_COMPRESSION_FIXED_RATE_5BPC_EXT: return 5;
46 case GL_SURFACE_COMPRESSION_FIXED_RATE_6BPC_EXT: return 6;
47 case GL_SURFACE_COMPRESSION_FIXED_RATE_7BPC_EXT: return 7;
48 case GL_SURFACE_COMPRESSION_FIXED_RATE_8BPC_EXT: return 8;
49 case GL_SURFACE_COMPRESSION_FIXED_RATE_9BPC_EXT: return 9;
50 case GL_SURFACE_COMPRESSION_FIXED_RATE_10BPC_EXT: return 10;
51 case GL_SURFACE_COMPRESSION_FIXED_RATE_11BPC_EXT: return 11;
52 case GL_SURFACE_COMPRESSION_FIXED_RATE_12BPC_EXT: return 12;
53 default: return 0;
57 enum piglit_result
58 piglit_display(void)
60 return PIGLIT_FAIL;
63 static void
64 check_zero_texture(void)
66 /* attempting to call TexStorageAttribs*EXT on the zero texture
67 * must fail with INVALID_OPERATION
69 const GLint attribs[] = { GL_NONE };
71 glBindTexture(GL_TEXTURE_2D, 0);
72 glTexStorageAttribs2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, 64, 64, attribs);
74 piglit_report_subtest_result(
75 piglit_check_gl_error(GL_INVALID_OPERATION) ? PIGLIT_PASS : PIGLIT_FAIL,
76 "zero-texture");
79 static void
80 check_unsized_format(void)
82 /* attempting to call TexStorageAttribs*EXT with an unsized internalformat
83 * must fail with INVALID_ENUM
86 const GLint attribs[] = { GL_NONE };
87 GLuint tex;
88 glGenTextures(1, &tex);
89 glBindTexture(GL_TEXTURE_2D, tex);
91 glTexStorageAttribs2DEXT(GL_TEXTURE_2D, 1, GL_RGBA, 64, 64, attribs);
93 /* unsized formats may not be used with TexStorage* */
94 piglit_report_subtest_result(
95 piglit_check_gl_error(GL_INVALID_ENUM) ? PIGLIT_PASS : PIGLIT_FAIL,
96 "unsized-format");
99 static void
100 check_immutable(void)
102 GLuint tex;
103 GLint param;
104 const GLint attribs[] = { GL_NONE };
106 glGenTextures(1, &tex);
107 glBindTexture(GL_TEXTURE_2D, tex);
109 /* specify storage for the texture, it will be set as immutable */
110 glTexStorageAttribs2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, 64, 64, attribs);
112 if (!piglit_check_gl_error(GL_NO_ERROR)) {
113 piglit_report_subtest_result(PIGLIT_FAIL, "immutable");
114 return;
117 /* should now have TEXTURE_IMMUTABLE_FORMAT */
118 glGetTexParameteriv(GL_TEXTURE_2D,
119 GL_TEXTURE_IMMUTABLE_FORMAT, &param);
121 if (!piglit_check_gl_error(GL_NO_ERROR)) {
122 printf("failed to fetch texture parameter TEXTURE_IMMUTABLE_FORMAT\n");
123 piglit_report_subtest_result(PIGLIT_FAIL, "immutable");
124 return;
127 if (param != GL_TRUE) {
128 printf("expected TEXTURE_IMMUTABLE_FORMAT to be true, got %d\n", param);
129 piglit_report_subtest_result(PIGLIT_FAIL, "immutable");
130 return;
133 /* calling TexStorageAttribs2D again on the same texture should fail */
134 glTexStorageAttribs2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, 64, 64, attribs);
136 if (!piglit_check_gl_error(GL_INVALID_OPERATION)) {
137 printf("expected respecifying an immutable-format texture (with TexStorageAttribs*) to fail\n");
138 piglit_report_subtest_result(PIGLIT_FAIL, "immutable");
139 return;
142 piglit_report_subtest_result(PIGLIT_PASS, "immutable");
145 static void
146 check_compression(void)
148 GLuint tex, fb;
149 GLint rates[16], num_rates = 0, actual_rate;
150 GLint attribs[3] = {GL_SURFACE_COMPRESSION_EXT, GL_SURFACE_COMPRESSION_FIXED_RATE_DEFAULT_EXT,
151 GL_NONE};
153 glGenTextures(1, &tex);
154 glActiveTexture(GL_TEXTURE0);
155 glBindTexture(GL_TEXTURE_2D, tex);
157 glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8,
158 GL_NUM_SURFACE_COMPRESSION_FIXED_RATES_EXT, 1, &num_rates);
159 glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8,
160 GL_SURFACE_COMPRESSION_EXT, num_rates, rates);
162 /* Test default and none rates as well */
163 rates[num_rates++] = GL_SURFACE_COMPRESSION_FIXED_RATE_DEFAULT_EXT;
164 rates[num_rates++] = GL_SURFACE_COMPRESSION_FIXED_RATE_NONE_EXT;
166 for (GLint i = 0; i < num_rates; i++) {
167 glGenTextures(1, &tex);
168 glBindTexture(GL_TEXTURE_2D, tex);
170 /* specify storage for the texture, it will be set as immutable */
171 attribs[1] = rates[i];
172 glTexStorageAttribs2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, 64, 64, attribs);
174 if (!piglit_check_gl_error(GL_NO_ERROR)) {
175 piglit_report_subtest_result(PIGLIT_FAIL, "compression");
176 return;
179 /* should now have TEXTURE_IMMUTABLE_FORMAT */
180 glGetTexParameteriv(GL_TEXTURE_2D,
181 GL_SURFACE_COMPRESSION_EXT, &actual_rate);
182 if (rates[i] == GL_SURFACE_COMPRESSION_FIXED_RATE_DEFAULT_EXT) {
183 piglit_logd("actual default rate is %i bpc", enum_to_rate(actual_rate));
184 } else if (rates[i] != actual_rate) {
185 piglit_loge("actual rate (%i bpc) differs from expected rate (%i bpc)",
186 enum_to_rate(actual_rate), enum_to_rate(rates[i]));
187 piglit_report_subtest_result(PIGLIT_FAIL, "compression");
188 return;
191 GLubyte *data = piglit_rgbw_image_ubyte(64, 64, GL_FALSE);
192 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, data);
193 free(data);
195 if (!piglit_check_gl_error(GL_NO_ERROR)) {
196 piglit_loge("failed to upload texture data");
197 piglit_report_subtest_result(PIGLIT_FAIL, "compression");
198 return;
201 glGenFramebuffers(1, &fb);
202 glBindFramebuffer(GL_FRAMEBUFFER, fb);
203 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
204 glClearColor(1.0, 1.0, 0.0, 1.0);
205 glEnable(GL_SCISSOR_TEST);
206 glScissor(0, 0, 32, 32);
207 glClear(GL_COLOR_BUFFER_BIT);
208 glDisable(GL_SCISSOR_TEST);
210 const float yellow[] = {1.0, 1.0, 0.0, 1.0};
211 const float green[] = {0.0, 1.0, 0.0, 1.0};
212 const float white[] = {1.0, 1.0, 1.0, 1.0};
213 glReadBuffer(GL_COLOR_ATTACHMENT0);
214 if (!piglit_probe_pixel_rgba(16, 16, yellow) || !piglit_probe_pixel_rgba(48, 48, white) ||
215 !piglit_probe_pixel_rgba(48, 16, green)) {
216 piglit_loge("pixels are not acturate");
217 piglit_report_subtest_result(PIGLIT_FAIL, "compression");
218 return;
221 glBindFramebuffer(GL_FRAMEBUFFER, 0);
222 glReadBuffer(GL_BACK);
224 if (!piglit_check_gl_error(GL_NO_ERROR)) {
225 piglit_report_subtest_result(PIGLIT_FAIL, "compression");
226 return;
230 piglit_report_subtest_result(PIGLIT_PASS, "compression");
234 void
235 piglit_init(int argc, char **argv)
237 piglit_require_extension("GL_EXT_texture_storage_compression");
239 check_zero_texture();
240 check_immutable();
241 check_unsized_format();
242 check_compression();
244 piglit_report_result(PIGLIT_SKIP);