1[require]
2GL >= 3.3
3GLSL >= 3.30
4GL_ARB_shader_storage_buffer_object
5GL_ARB_shader_atomic_counters
6GL_ARB_shader_atomic_counter_ops
7GL_ARB_gpu_shader5
8GL_INTEL_shader_atomic_float_minmax
9
10[vertex shader passthrough]
11
12[fragment shader]
13#extension GL_ARB_shader_storage_buffer_object: require
14#extension GL_ARB_shader_atomic_counters: require
15#extension GL_ARB_shader_atomic_counter_ops: require
16#extension GL_ARB_gpu_shader5: require
17#extension GL_INTEL_shader_atomic_float_minmax: require
18
19layout(binding = 0, std430) buffer bufblock {
20	float value;
21};
22
23/* GL_ARB_shader_atomic_counters requires at least 8 total counters. */
24layout(binding = 0) uniform atomic_uint mask[7];
25layout(binding = 0) uniform atomic_uint fail;
26
27out vec4 color;
28
29void main()
30{
31	/* According to issue #22 of the GL_ARB_shader_image_load_store, the
32	 * return result of atomic operations in helper invocations is
33	 * undefined.  To avoid a possible infinite loop (below) in a helper
34	 * invocation, bail out now.
35	 */
36	if (gl_SampleMaskIn[0] == 0)
37		return;
38
39	/* Each of 32 * N fragments should see a unique value.  Each value
40	 * observed is tracked in "mask."  The test automatically fails if a
41	 * duplicate value is observed.  After the shaders are done running,
42	 * the mask values will be probed to ensure that all possible values
43	 * were observed.
44	 */
45	float f;
46
47	/* This is an open-coded atomicAdd. */
48	do {
49		f = value;
50	} while (f != atomicCompSwap(value, f, f + .5));
51
52	uint i = uint(f * 2.0);
53	uint bit = i % 32u;
54	int c = int(i / 32u);
55	uint m = 1u << bit;
56
57	if (c < mask.length()) {
58		/* If the bit was already set, the test fails. */
59		if ((atomicCounterOrARB(mask[c], m) & m) != 0u)
60			atomicCounterIncrement(fail);
61
62		color = vec4(0.0, 1.0, 0.0, 1.0);
63	} else {
64		color = vec4(0.0, 0.0, 1.0, 1.0);
65	}
66}
67
68[test]
69atomic counters 8
70
71ssbo 0 32
72ssbo 0 subdata float 0 0.0
73
74clear color 0.5 0.5 0.5 0.5
75clear
76
77draw rect -1 -1 2 2
78
79probe atomic counter 0 == 4294967295
80probe atomic counter 1 == 4294967295
81probe atomic counter 2 == 4294967295
82probe atomic counter 3 == 4294967295
83probe atomic counter 4 == 4294967295
84probe atomic counter 5 == 4294967295
85probe atomic counter 6 == 4294967295
86probe atomic counter 7 == 0
87