summaryrefslogtreecommitdiff
path: root/tests/spec/arb_multisample/pushpop.c
blob: ba1b8c34a1605d9afa759169cc8ec3f79e481972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright © 2011 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "piglit-util.h"

/**
 * @file pushpop.c
 *
 * From the GL_ARB_multisample spec:
 *
 *     "An additional group of state variables, MULTISAMPLE_BIT_ARB,
 *      is defined by this extension.  When PushAttrib is called with
 *      bit MULTISAMPLE_BIT_ARB set, the multisample group of state
 *      variables is pushed onto the attribute stack.  When PopAttrib
 *      is called, these state variables are restored to their
 *      previous values if they were pushed.  Some multisample state
 *      is included in the ENABLE_BIT group as well. In order to avoid
 *      incompatibility with GL implementations that do not support
 *      SGIS_multisample, ALL_ATTRIB_BITS does not include
 *      MULTISAMPLE_BIT_ARB."
 *
 *      Get Value                       Get Command    Type    Initial Value    Attribute
 *      ---------                       -----------    ----    -------------    ---------
 *      MULTISAMPLE_ARB                 IsEnabled      B       TRUE             multisample/enable
 *      SAMPLE_ALPHA_TO_COVERAGE_ARB    IsEnabled      B       FALSE            multisample/enable
 *      SAMPLE_ALPHA_TO_ONE_ARB         IsEnabled      B       FALSE            multisample/enable
 *      SAMPLE_COVERAGE_ARB             IsEnabled      B       FALSE            multisample/enable
 *
 *      SAMPLE_COVERAGE_VALUE_ARB       GetFloatv      R+      1                multisample
 *      SAMPLE_COVERAGE_INVERT_ARB      GetBooleanv    B       FALSE            multisample
 *
 */

PIGLIT_GL_TEST_MAIN(
    32 /*window_width*/,
    32 /*window_height*/,
    GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA)

enum piglit_result
piglit_display(void)
{
	/* UNREACHED */
	return PIGLIT_FAIL;
}

static bool
test_bool(GLenum e, const char *name, bool val)
{
	GLboolean ret = glIsEnabled(e);

	if (ret != val) {
		fprintf(stderr, "  %s %d doesn't match expected %d\n",
			name, ret, val);
		return false;
	} else {
		return true;
	}
}

static bool
test_enable_bits(bool val)
{
	bool pass = true;

	pass = test_bool(GL_MULTISAMPLE, "multisample", val) && pass;
	pass = test_bool(GL_SAMPLE_ALPHA_TO_COVERAGE, "alpha to coverage", val) && pass;
	pass = test_bool(GL_SAMPLE_ALPHA_TO_ONE, "alpha to one", val) && pass;
	pass = test_bool(GL_SAMPLE_COVERAGE, "sample coverage", val) && pass;

	return pass;
}

static void
set_enable_bits(bool val)
{
	if (val) {
		glEnable(GL_MULTISAMPLE);
		glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
		glEnable(GL_SAMPLE_ALPHA_TO_ONE);
		glEnable(GL_SAMPLE_COVERAGE);
	} else {
		glDisable(GL_MULTISAMPLE);
		glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
		glDisable(GL_SAMPLE_ALPHA_TO_ONE);
		glDisable(GL_SAMPLE_COVERAGE);
	}
}

static bool
test_coverage(bool mode)
{
	bool pass = true;
	GLboolean invert;

	float expected = 0.25 + (mode ? 0.5 : 0.0), coverage;

	glGetFloatv(GL_SAMPLE_COVERAGE_VALUE, &coverage);
	if (coverage != expected) {
		fprintf(stderr,
			"  coverage value %f doesn't match expected %f\n",
			expected, coverage);
		pass = false;
	}

	glGetBooleanv(GL_SAMPLE_COVERAGE_INVERT, &invert);
	if (invert != mode) {
		fprintf(stderr,
			"  coverage invert value %d doesn't match expected %d\n",
			invert, mode);
		pass = false;
	}

	return pass;
}

static bool
test_state(bool enable_on, bool coverage_mode)
{
	return test_enable_bits(enable_on) & test_coverage(coverage_mode);
}

static void
set_coverage(bool mode)
{
	float coverage_val = 0.25 + (mode ? 0.5 : 0.0);

	glSampleCoverageARB(coverage_val, mode);
}

static bool
pushpop(GLuint bit, const char *test,
	bool affects_enabled, bool affects_other)
{
	printf("%s test:\n", test);

	set_enable_bits(true);
	set_coverage(true);

	if (bit != 0) {
		glPushAttrib(bit);
		set_enable_bits(false);
		set_coverage(false);
		glPopAttrib();
	}

	if (!test_state(affects_enabled, affects_other))
		return false;

	/* Now, test the bits the other direction.  Caught a bug in my
	 * first pass of fixing Mesa.
	 */
	set_enable_bits(false);
	set_coverage(false);

	if (bit != 0) {
		glPushAttrib(bit);
		set_enable_bits(true);
		set_coverage(true);
		glPopAttrib();
	}

	return test_state(!affects_enabled, !affects_other);
}

void
piglit_init(int argc, char **argv)
{
	bool pass = true;

	piglit_require_extension("GL_ARB_multisample");

	pass = pushpop(0, "sanity test", true, true);
	if (!pass)
		piglit_report_result(PIGLIT_FAIL);
	pass = pushpop(GL_MULTISAMPLE_BIT, "GL_MULTISAMPLE_BIT",
		       true, true) && pass;
	pass = pushpop(GL_ENABLE_BIT, "GL_ENABLE_BIT",
		       true, false) && pass;
	pass = pushpop(GL_ALL_ATTRIB_BITS, "GL_ALL_ATTRIB_BITS",
		       true, false) && pass;

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}