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
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
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
)
53 piglit_draw_rect(rect
[0], rect
[1], rect
[2], rect
[3]);
55 glBlendFunc(blendsrc
, blenddst
);
57 glBlendColor(blendcol
[0], blendcol
[1], blendcol
[2], blendcol
[3]);
59 piglit_draw_rect(rect
[0], rect
[1], rect
[2], rect
[3]);
65 get_texture_bits(GLenum target
, GLenum size_enum
, GLenum type_enum
)
69 glGetTexLevelParameteriv(target
, 0, size_enum
, &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 */
86 calc_blend_factor(float src
, float dst
, float blendcol
, GLenum factor
)
95 case GL_ONE_MINUS_SRC_COLOR
:
99 case GL_ONE_MINUS_DST_COLOR
:
101 case GL_CONSTANT_COLOR
:
103 case GL_ONE_MINUS_CONSTANT_COLOR
:
104 return 1.0f
- blendcol
;
112 * Calculate add blend func result. Pretty simplified, no separate a/rgb factors.
115 blend_func_add(const float *src
, const float *dst
, const float *blendcol
,
116 GLenum src_factor
, GLenum dst_factor
, float *res
)
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
;
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
;
142 struct blend_config
{
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
};
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
};
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
};
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
};
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
};
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
,
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
);
231 /* When there are no bits for the alpha channel, we
232 * always expect to read an alpha value of 1.0.
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).
260 /* Clamp the bits for the framebuffer, except we aren't checking
261 * the actual framebuffer bits.
272 piglit_set_tolerance_for_bits(r
, g
, b
, a
);
274 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
275 GL_COLOR_ATTACHMENT0_EXT
,
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");
291 glClearColor(0.0, 0.0, 0.0, 0.0);
292 glClear(GL_COLOR_BUFFER_BIT
);
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");
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");
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");
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");
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");
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");
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
);