aboutsummaryrefslogtreecommitdiff
path: root/tests/shaders/getuniform-01.c
blob: 1aa7b4f1b96d4fa4f4410c98b86105f6fd3c0b2b (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
/*
 * Copyright © 2010 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.
 */

/**
 * \file getuniform-01.c
 * Verify that glGetUniformfv fetches uniform arrays correctly
 *
 * This test reproduces the failure reported in bugzilla #29823.
 * https://bugs.freedesktop.org/show_bug.cgi?id=29823
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.window_width = 20;
	config.window_height = 20;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const char vs_text[] =
	"uniform float c[4];\n"
	"varying vec4 color;\n"
	"\n"
	"void main()\n"
	"{\n"
	"  gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
	"  color = vec4(c[3], c[2], c[1], c[0]);\n"
	"}\n"
	;

static const char fs_text[] =
	"varying vec4 color;\n"
	"void main() { gl_FragColor = color; }\n"
	;

static GLint prog;
static GLint base_location;
static GLint array_location[4];

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

union data_blob {
	float f;
	uint32_t u;
};

void
validate_buffer(const union data_blob *buffer, unsigned size, float expected)
{
	unsigned i;

	if (buffer[0].f != expected) {
		printf("index 0: got %f, expected %f\n",
		       buffer[0].f, expected);
		piglit_report_result(PIGLIT_FAIL);
	} else if (!piglit_automatic) {
		printf("index 0: got %f, expected %f (good)\n",
		       buffer[0].f, expected);
	}

	for (i = 1; i < size; i++) {
		if (buffer[i].u != 0xdeadbeef) {
			printf("glGetUniformfv overrun at index %u!\n", i);
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	if (!piglit_automatic) {
		printf("No buffer overrun.\n");
	}
}

void
piglit_init(int argc, char **argv)
{
	static const float uniform_data[4] = {
		12.0, 0.5, 3.14169, 42.0
	};
	GLint vs;
	GLint fs;
	unsigned i;
	union data_blob buffer[16];

	piglit_require_vertex_shader();
	piglit_require_fragment_shader();

	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text);
	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_text);

	prog = piglit_link_simple_program(vs, fs);

	piglit_UseProgram(prog);

	base_location = piglit_GetUniformLocation(prog, "c");
	if (base_location < 0) {
		printf("Could not get location of `c'.\n");
		piglit_report_result(PIGLIT_FAIL);
	}

	for (i = 0; i < 4; i++) {
		char name[5];

		name[0] = 'c';
		name[1] = '[';
		name[2] = '0' + i;
		name[3] = ']';
		name[4] = '\0';
		array_location[i] = piglit_GetUniformLocation(prog, name);
		if (array_location[i] < 0) {
			printf("Could not get location of `%s'.\n", name);
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	/* From page 80 of the OpenGL 2.1 spec:
	 *
	 *     The first element of a uniform array is identified using the
	 *     name of the uniform array appended with "[0]". Except if the
	 *     last part of the string name indicates a uniform array, then
	 *     the location of the first element of that array can be
	 *     retrieved by either using the name of the uniform array, or the
	 *     name of the uniform array appended with "[0]".
	 */
	if (base_location != array_location[0]) {
		printf("Locations of `c' = %d and `c[0]' = %d, but they "
		       "should be the same.\n",
		       base_location, array_location[0]);
		piglit_report_result(PIGLIT_FAIL);
	}

	piglit_Uniform1fv(base_location, 4, uniform_data);

	/* From page 264 of the OpenGL 2.1 spec:
	 *
	 *     In order to query the values of an array of uniforms, a
	 *     GetUniform* command needs to be issued for each array element.
	 *
	 * This means that querying using the location of 'array' is the same
	 * as 'array[0]'.
	 */
	printf("Getting array element 0 from base location...\n");
	for (i = 0; i < ARRAY_SIZE(buffer); i++) {
		buffer[i].u = 0xdeadbeef;
	}

	piglit_GetUniformfv(prog, base_location, (GLfloat *) buffer);
	validate_buffer(buffer, ARRAY_SIZE(buffer), uniform_data[0]);

	printf("Getting one array element at a time...\n");
	for (i = 0; i < 4; i++) {
		unsigned j;
		for (j = 0; j < ARRAY_SIZE(buffer); j++) {
			buffer[j].u = 0xdeadbeef;
		}

		piglit_GetUniformfv(prog, array_location[i],
				    (GLfloat *) buffer);
		validate_buffer(buffer, ARRAY_SIZE(buffer), uniform_data[i]);
	}

	piglit_report_result(PIGLIT_PASS);
}