aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_transform_feedback/alignment.c
blob: ac05bc18902cf141acb9fc11316afd8a4d2ee963 (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
/*
 * 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.
 */

/**
 * \file alignment.c
 *
 * Verify that transform feedback outputs are generated correctly
 * regardless of how the buffers (and the data) are aligned in memory.
 *
 * The test requires a single integer argument, which specifies the
 * number of bytes of offset that should be specified when calling
 * glBindBufferRange().  This value may be 0, 4, 8, or 12.
 */

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

#define BUFFER_SIZE 0x40

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;

	config.window_width = 10;
	config.window_height = 10;
	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_ALPHA;

PIGLIT_GL_TEST_CONFIG_END

/* Test parameters */
static unsigned long additional_offset;

/* Other globals */
static GLuint prog;
static GLuint xfb_buf;

/**
 * Input data for the vertex shader.
 */
static GLuint verts[4] = { 0, 1, 2, 3 };

/**
 * Vertex shader.  This is designed so that its transform feedback
 * outputs appear at all possible alignments, and so that the correct
 * output will consist of the following pattern of uints:
 *
 * 0x00010203
 * 0x04050607
 * 0x08090a0b
 *  ...
 * 0xacadaeaf
 *
 * (a total of 44 uints)
 */
static const char *vstext =
	"#version 130\n"
	"in uint input_uint;\n"
	"flat out uint  out_a;\n"
	"flat out uvec2 out_b;\n"
	"flat out uvec3 out_c;\n"
	"flat out uvec4 out_d;\n"
	"flat out uint  out_e;\n"
	"\n"
	"void main()\n"
	"{\n"
	"  gl_Position = vec4(0.0);\n"
	"  uint offset = input_uint * 0x2c2c2c2cu;\n"
	"  out_a = 0x00010203u + offset;\n"
	"  out_b = uvec2(0x04050607, 0x08090a0b) + offset;\n"
	"  out_c = uvec3(0x0c0d0e0f, 0x10111213, 0x14151617) + offset;\n"
	"  out_d = uvec4(0x18191a1b, 0x1c1d1e1f, 0x20212223, 0x24252627) + offset;\n"
	"  out_e = 0x28292a2bu + offset;\n"
	"}\n";

#define EXPECTED_NUM_OUTPUTS 44

static const char *varyings[] = {
	"out_a", "out_b", "out_c", "out_d", "out_e"
};

static const char *fstext =
	"#version 130\n"
	"void main()\n"
	"{\n"
	"  gl_FragColor = vec4(0.0);\n"
	"}\n";

static void
print_usage_and_exit(char *prog_name)
{
	printf("Usage: %s <additional_offset>\n"
	       "  where <additional_offset> is one of the values\n"
	       "  0, 4, 8, or 12.\n", prog_name);
	exit(1);
}

void piglit_init(int argc, char **argv)
{
	char *endptr;
	GLuint vs, fs;

	/* Interpret command line args */
	if (argc != 2)
		print_usage_and_exit(argv[0]);
	endptr = argv[1];
	additional_offset = strtoul(argv[1], &endptr, 0);
	if (*endptr != '\0')
		print_usage_and_exit(argv[0]);
	if (additional_offset > 12 || additional_offset % 4 != 0)
		print_usage_and_exit(argv[0]);

	piglit_require_GLSL_version(130);
	piglit_require_gl_version(30);
	piglit_require_transform_feedback();
	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vstext);
	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fstext);
	prog = glCreateProgram();
	glAttachShader(prog, vs);
	glAttachShader(prog, fs);
	glTransformFeedbackVaryings(prog, 5, varyings, GL_INTERLEAVED_ATTRIBS);
	glLinkProgram(prog);
	if (!piglit_link_check_status(prog))
		piglit_report_result(PIGLIT_FAIL);
	glGenBuffers(1, &xfb_buf);
	if (!piglit_check_gl_error(0))
		piglit_report_result(PIGLIT_FAIL);
}

enum piglit_result piglit_display(void)
{
	GLint input_index = glGetAttribLocation(prog, "input_uint");
	GLuint *readback;
	GLuint buffer[BUFFER_SIZE];
	GLuint expected[BUFFER_SIZE];
	int i;
	GLboolean pass = GL_TRUE;

	glUseProgram(prog);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glVertexAttribIPointer(input_index, 1, GL_UNSIGNED_INT,
			       sizeof(GLuint), &verts);
	glEnableVertexAttribArray(input_index);
	pass = piglit_check_gl_error(0) && pass;

	glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, xfb_buf);
	memset(buffer, 0xffffffff, sizeof(buffer));
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(buffer), buffer,
		     GL_STREAM_READ);
	glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb_buf,
			  additional_offset,
			  sizeof(buffer) - additional_offset);
	glBeginTransformFeedback(GL_POINTS);
	glDrawArrays(GL_POINTS, 0, 4);
	glEndTransformFeedback();
	pass = piglit_check_gl_error(0) && pass;

	readback = glMapBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, GL_READ_ONLY);
	pass = piglit_check_gl_error(0) && pass;

	/* Figure out expected output */
	memset(expected, 0xffffffff, sizeof(expected));
	for (i = 0; i < EXPECTED_NUM_OUTPUTS; ++i) {
		expected[i + additional_offset / 4] =
			0x00010203 + 0x04040404 * i;
	}

	/* Check output */
	for (i = 0; i < BUFFER_SIZE; ++i) {
		if (expected[i] != readback[i]) {
			printf("readback[%u]: %u, expected: %u\n", i,
			       readback[i], expected[i]);
			pass = GL_FALSE;
		}
	}

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}