glsl-array-bounds: set out-of-bounds array index inside shader
[piglit.git] / tests / fbo / fbo-blending-snorm.c
blob5ea033a5186f57fc15735f3e4567cfe2e5e6e6a4
1 /*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
4 * Copyright (c) 2017 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 * Marek Olšák <maraeo@gmail.com>
28 * Roland Scheidegger <sroland@vmware.com>
32 #include "piglit-util-gl.h"
34 PIGLIT_GL_TEST_CONFIG_BEGIN
36 config.supports_gl_compat_version = 30;
38 /* Drivers that do not support GL_ARB_texture_non_power_of_two require
39 * window dimensions that are powers of two for this test.
41 config.window_width = next_power_of_two(config.window_width);
42 config.window_height = next_power_of_two(config.window_height);
44 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
45 config.khr_no_error_support = PIGLIT_NO_ERRORS;
47 PIGLIT_GL_TEST_CONFIG_END
49 static void blend(const float *rect, const float *src, const float *dst, const float *blendcol,
50 GLenum blendsrc, GLenum blenddst)
52 glColor4fv(dst);
53 piglit_draw_rect(rect[0], rect[1], rect[2], rect[3]);
54 glEnable(GL_BLEND);
55 glBlendFunc(blendsrc, blenddst);
56 if (blendcol)
57 glBlendColor(blendcol[0], blendcol[1], blendcol[2], blendcol[3]);
58 glColor4fv(src);
59 piglit_draw_rect(rect[0], rect[1], rect[2], rect[3]);
60 glDisable(GL_BLEND);
64 static int
65 get_texture_bits(GLenum target, GLenum size_enum, GLenum type_enum)
67 GLint size = 0;
68 GLint type = GL_NONE;
69 glGetTexLevelParameteriv(target, 0, size_enum, &size);
70 if (!size) {
71 return size;
73 if (piglit_is_extension_supported("GL_EXT_texture_snorm") ||
74 piglit_get_gl_version() >= 31) {
75 glGetTexLevelParameteriv(target, 0, type_enum, &type);
76 if (type == GL_SIGNED_NORMALIZED) {
77 /* One bit is lost for the sign */
78 size -= 1;
81 return size;
85 float
86 calc_blend_factor(float src, float dst, float blendcol, GLenum factor)
88 switch (factor) {
89 case GL_ZERO:
90 return 0.0f;
91 case GL_ONE:
92 return 1.0f;
93 case GL_SRC_COLOR:
94 return src;
95 case GL_ONE_MINUS_SRC_COLOR:
96 return 1.0f - src;
97 case GL_DST_COLOR:
98 return dst;
99 case GL_ONE_MINUS_DST_COLOR:
100 return 1.0f - dst;
101 case GL_CONSTANT_COLOR:
102 return blendcol;
103 case GL_ONE_MINUS_CONSTANT_COLOR:
104 return 1.0f - blendcol;
105 default:
106 assert(0);
108 return 0.0f;
112 * Calculate add blend func result. Pretty simplified, no separate a/rgb factors.
114 void
115 blend_func_add(const float *src, const float *dst, const float *blendcol,
116 GLenum src_factor, GLenum dst_factor, float *res)
118 int i;
119 for (i = 0; i < 4; i++) {
120 float src_clamped = CLAMP(src[i], -1.0f, 1.0f);
121 float dst_clamped = CLAMP(dst[i], -1.0f, 1.0f);
122 float blendcol_clamped = 0.0f;
123 float res_unclamped, s_factor, d_factor;
124 if (blendcol)
125 blendcol_clamped = CLAMP(blendcol[i], -1.0f, 1.0f);
126 s_factor = calc_blend_factor(src_clamped, dst_clamped,
127 blendcol_clamped, src_factor);
128 d_factor = calc_blend_factor(src_clamped, dst_clamped,
129 blendcol_clamped, dst_factor);
130 res_unclamped = s_factor * src_clamped + d_factor * dst_clamped;
131 res[i] = CLAMP(res_unclamped, -1.0f, 1.0f);
136 enum piglit_result piglit_display(void)
138 GLboolean pass = GL_TRUE;
139 GLuint tex, fb;
140 GLenum status;
141 int r, g, b, a;
142 struct blend_config {
143 GLenum src_factor;
144 GLenum dst_factor;
147 float res0[] = { 0.3, -0.3, 0.3, 0.0};
149 float pos1[] = {-0.66, -1.0, 0.33, 2.0};
150 float dst1[] = { 0.5, 0.4, -0.6, 0.2};
151 float src1[] = { -0.2, 1.9, 0.8, -0.7};
152 struct blend_config cnf1 = {GL_ONE_MINUS_SRC_COLOR, GL_ONE};
153 float res1[4];
155 float pos2[] = {-0.33, -1.0, 0.33, 2.0};
156 float dst2[] = {1.9, -0.4, 0.7, 0.5};
157 float src2[] = {-1.8, 0.3, 0.5, 0.9};
158 struct blend_config cnf2 = {GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR};
159 float res2[4];
161 float pos3[] = {0.0, -1.0, 0.33, 2.0};
162 float dst3[] = {-0.6, 0.4, 0.8, 0.5};
163 float src3[] = {0.8, 0.9, -0.7, 0.8};
164 struct blend_config cnf3 = {GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR};
165 float res3[4];
167 float pos4[] = {0.33, -1.0, 0.33, 2.0};
168 float dst4[] = {0.9, 0.4, 0.7, 0.5};
169 float src4[] = {0.8, 0.3, 0.5, -0.9};
170 struct blend_config cnf4 = {GL_SRC_COLOR, GL_SRC_COLOR};
171 float res4[4];
173 float pos5[] = {0.66, -1.0, 0.33, 2.0};
174 float dst5[] = {0.6, -0.3, 0.8, 0.5};
175 float src5[] = {0.8, 0.1, 0.7, 0.8};
176 float con5[] = {1.2, -1.8, 0.4, 0.6};
177 struct blend_config cnf5 = {GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_DST_COLOR};
178 float res5[4];
180 glGenFramebuffersEXT(1, &fb);
181 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
182 glViewport(0, 0, piglit_width, piglit_height);
184 glGenTextures(1, &tex);
185 glBindTexture(GL_TEXTURE_2D, tex);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM,
189 piglit_width, piglit_height, 0,
190 GL_RGBA, GL_FLOAT, NULL);
192 a = get_texture_bits(GL_TEXTURE_2D,
193 GL_TEXTURE_ALPHA_SIZE,
194 GL_TEXTURE_ALPHA_TYPE);
195 r = get_texture_bits(GL_TEXTURE_2D,
196 GL_TEXTURE_RED_SIZE,
197 GL_TEXTURE_RED_TYPE);
198 g = get_texture_bits(GL_TEXTURE_2D,
199 GL_TEXTURE_GREEN_SIZE,
200 GL_TEXTURE_GREEN_TYPE);
201 b = get_texture_bits(GL_TEXTURE_2D,
202 GL_TEXTURE_BLUE_SIZE,
203 GL_TEXTURE_BLUE_TYPE);
206 if (!r) {
207 res0[0] = 0;
208 res1[0] = 0;
209 res2[0] = 0;
210 res3[0] = 0;
211 res4[0] = 0;
212 res5[0] = 0;
214 if (!g) {
215 res0[1] = 0;
216 res1[1] = 0;
217 res2[1] = 0;
218 res3[1] = 0;
219 res4[1] = 0;
220 res5[1] = 0;
222 if (!b) {
223 res0[2] = 0;
224 res1[2] = 0;
225 res2[2] = 0;
226 res3[2] = 0;
227 res4[2] = 0;
228 res5[2] = 0;
230 if (!a) {
231 /* When there are no bits for the alpha channel, we
232 * always expect to read an alpha value of 1.0.
234 res0[3] = 1;
235 res1[3] = 1;
236 res2[3] = 1;
237 res3[3] = 1;
238 res4[3] = 1;
239 res5[3] = 1;
241 /* Also blending with
242 * DST_ALPHA/ONE_MINUS_DST_ALPHA (as in case
243 * 4) with an implicit destination alpha value
244 * of 1.0 means that the result color should
245 * be identical to the source color, (if there
246 * are any bits to store that color that is).
248 if (r) {
249 res4[0] = src4[0];
251 if (g) {
252 res4[1] = src4[1];
254 if (b) {
255 res4[2] = src4[2];
260 /* Clamp the bits for the framebuffer, except we aren't checking
261 * the actual framebuffer bits.
263 if (r > 8)
264 r = 8;
265 if (g > 8)
266 g = 8;
267 if (b > 8)
268 b = 8;
269 if (a > 8)
270 a = 8;
272 piglit_set_tolerance_for_bits(r, g, b, a);
274 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
275 GL_COLOR_ATTACHMENT0_EXT,
276 GL_TEXTURE_2D,
277 tex,
279 if (!piglit_check_gl_error(GL_NO_ERROR))
280 piglit_report_result(PIGLIT_FAIL);
282 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
283 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
284 printf(" - fbo incomplete (status = %s)\n",
285 piglit_get_gl_enum_name(status));
286 piglit_report_subtest_result(PIGLIT_SKIP, "%s", "GL_RGBA8_SNORM");
287 return PIGLIT_SKIP;
289 printf("\n");
291 glClearColor(0.0, 0.0, 0.0, 0.0);
292 glClear(GL_COLOR_BUFFER_BIT);
294 glColor4fv(res0);
295 piglit_draw_rect(-1.0, -1.0, 0.33, 2.0);
297 blend(pos1, src1, dst1, NULL, cnf1.src_factor, cnf1.dst_factor);
298 blend(pos2, src2, dst2, NULL, cnf2.src_factor, cnf2.dst_factor);
299 blend(pos3, src3, dst3, NULL, cnf3.src_factor, cnf3.dst_factor);
300 blend(pos4, src4, dst4, NULL, cnf4.src_factor, cnf4.dst_factor);
301 blend(pos5, src5, dst5, con5, cnf5.src_factor, cnf5.dst_factor);
303 if (!piglit_probe_pixel_rgba(piglit_width * 1 / 12, 0, res0)) {
304 printf(" when testing FBO result, simple.\n");
305 pass = GL_FALSE;
307 blend_func_add(src1, dst1, NULL, cnf1.src_factor, cnf1.dst_factor, res1);
308 if (!piglit_probe_pixel_rgba(piglit_width * 3 / 12, 0, res1)) {
309 printf(" when testing FBO result, blending with inv_src/one.\n");
310 pass = GL_FALSE;
312 blend_func_add(src2, dst2, NULL, cnf2.src_factor, cnf2.dst_factor, res2);
313 if (!piglit_probe_pixel_rgba(piglit_width * 5 / 12, 0, res2)) {
314 printf(" when testing FBO result, blending with dst/inv_dst.\n");
315 pass = GL_FALSE;
317 blend_func_add(src3, dst3, NULL, cnf3.src_factor, cnf3.dst_factor, res3);
318 if (!piglit_probe_pixel_rgba(piglit_width * 7 / 12, 0, res3)) {
319 printf(" when testing FBO result, blending with src/inv_src.\n");
320 pass = GL_FALSE;
322 blend_func_add(src4, dst4, NULL, cnf4.src_factor, cnf4.dst_factor, res4);
323 if (!piglit_probe_pixel_rgba(piglit_width * 9 / 12, 0, res4)) {
324 printf(" when testing FBO result, blending with src/src.\n");
325 pass = GL_FALSE;
327 blend_func_add(src5, dst5, con5, cnf5.src_factor, cnf5.dst_factor, res5);
328 if (!piglit_probe_pixel_rgba(piglit_width * 11 / 12, 0, res5)) {
329 printf(" when testing FBO result, blending with inv_constant/dst.\n");
330 pass = GL_FALSE;
333 piglit_present_results();
335 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
338 void piglit_init(int argc, char **argv)
341 * Either need GL_EXT_texture_snorm or GL 3.1 (which introduced
342 * snorm formats, but only the non-legacy ones).
343 * Note neither guarantees it's renderable (in fact GL 3.1 lists
344 * it explicitly as "texture only" but later versions just say
345 * not required for rendering). That would need
346 * GL_ARB_internalformat_query2.
348 if (!piglit_is_extension_supported("GL_EXT_texture_snorm"))
349 piglit_require_gl_version(31);
351 glDisable(GL_DITHER);
353 * Note that all values entering blend will still be clamped
354 * implicitly to [-1,1] for snorm formats.
356 glClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
357 glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
358 glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);