summaryrefslogtreecommitdiff
path: root/tests/spec/arb_gpu_shader5/execution/xfb-streams-without-invocations.c
blob: 1c6f76c0cb631e4c743761a474471ec2894f081d (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (c) 2014 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-gl.h"
#include "piglit-shader-test.h"

/**
 * @file xfb-streams-without-invocations.c
 *
 * This test uses geometry shader multiple stream support from
 * GL_ARB_gpu_shader5 and GL_ARB_transform_feedback3 to capture
 * transform feedback from 3 streams into 2 buffers.
 *
 * Based on the work of Jordan's work in xfb-streams.c.
 */

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 32;
	config.supports_gl_core_version = 32;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

int stream_float_counts[] = { 1, 2, 5, 0 };
static bool use_spirv = false;

#define STREAMS 4

#define SHADER_SOURCE_FILE_NAME "xfb_streams_without_invocations.shader_source"
char shader_source_filename[4096];

static const char *varyings[] = {
	"stream0_0_out", "gl_NextBuffer",
	"stream1_0_out", "gl_NextBuffer",
	"stream2_0_out", "stream2_1_out"
};


static GLuint
assemble_spirv_shader(GLenum shader_type)
{
	char *shader_asm;
	unsigned shader_asm_size;
	GLuint shader;

	if (!piglit_load_source_from_shader_test(shader_source_filename,
						 shader_type, true,
						 &shader_asm, &shader_asm_size)) {
		piglit_report_result(PIGLIT_FAIL);
	}

	shader = piglit_assemble_spirv(shader_type,
				       shader_asm_size,
				       shader_asm);
	free(shader_asm);

	glSpecializeShader(shader,
			   "main",
			   0, /* numSpecializationConstants */
			   NULL /* pConstantIndex */,
			   NULL /* pConstantValue */);

	return shader;
}

static GLuint
build_spirv_program(void)
{
	GLuint prog, shader;

	prog = glCreateProgram();

	shader = assemble_spirv_shader(GL_VERTEX_SHADER);
	glAttachShader(prog, shader);
	glDeleteShader(shader);

	shader = assemble_spirv_shader(GL_GEOMETRY_SHADER);
	glAttachShader(prog, shader);
	glDeleteShader(shader);

	return prog;
}

static GLuint
build_glsl_program()
{
	GLuint prog;
	char *gs_text;
	char *vs_pass_thru_text;

	if (!piglit_load_source_from_shader_test(shader_source_filename,
						 GL_GEOMETRY_SHADER, false,
						 &gs_text, NULL))
		return 0;


	if (!piglit_load_source_from_shader_test(shader_source_filename,
						 GL_VERTEX_SHADER, false,
						 &vs_pass_thru_text, NULL)) {
		free(gs_text);
		return 0;
	}


	prog = piglit_build_simple_program_multiple_shaders(
			GL_VERTEX_SHADER, vs_pass_thru_text,
			GL_GEOMETRY_SHADER, gs_text, 0);

	glTransformFeedbackVaryings(prog, ARRAY_SIZE(varyings), varyings,
			GL_INTERLEAVED_ATTRIBS);

	free(gs_text);
	free(vs_pass_thru_text);

	return prog;
}

static void
build_and_use_program()
{
	GLuint prog;

	if (use_spirv)
		prog = build_spirv_program();
	else
		prog = build_glsl_program();

	glLinkProgram(prog);
	if (!piglit_link_check_status(prog))
		piglit_report_result(PIGLIT_FAIL);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	glUseProgram(prog);
}

static bool
probe_buffers(const GLuint *xfb, const GLuint *queries, unsigned primitive_n)
{
	bool pass;
	unsigned i;
	GLuint query_result;
	float *expected[STREAMS];
	int expected_n[STREAMS];

	for (i = 0; i < STREAMS; i++) {
		expected_n[i] = stream_float_counts[i] * primitive_n;
	}
	/* Skip Stream = 3 as it has no transform feedback primitives written
	 * nor primitives generated
	 */
	for (i = 0; i < STREAMS-1; i++) {
		glGetQueryObjectuiv(queries[i], GL_QUERY_RESULT, &query_result);
		if (query_result != primitive_n) {
			printf("Stream = %d: Expected %u primitives generated, got %u\n",
					i, primitive_n, query_result);
			piglit_report_result(PIGLIT_FAIL);
		}
		glGetQueryObjectuiv(queries[STREAMS+i], GL_QUERY_RESULT, &query_result);
		if (query_result != primitive_n) {
			printf("Stream = %d: Expected %u TF primitives written, got %u\n",
					i, primitive_n, query_result);
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	glGetQueryObjectuiv(queries[3], GL_QUERY_RESULT, &query_result);
	if (query_result != 0) {
		printf("Stream = 3: Expected 0 primitives generated, got %u\n",
				query_result);
		piglit_report_result(PIGLIT_FAIL);
	}
	glGetQueryObjectuiv(queries[STREAMS+3], GL_QUERY_RESULT, &query_result);
	if (query_result != 0) {
		printf("Stream = 3: Expected 0 primitives written, got %u\n",
				query_result);
		piglit_report_result(PIGLIT_FAIL);
	}

	for (i = 0; i < STREAMS; i++) {
		expected[i] = malloc(expected_n[i] * sizeof(float));
		memset(expected[i], 0, expected_n[i] * sizeof(float));
	}

	for (i = 0; i < primitive_n; ++i) {
		expected[0][i * stream_float_counts[1] + 0] = 0.0; /* stream0_0[0] */

		expected[1][i * stream_float_counts[1] + 0] = 0.0; /* stream1_0[0] */
		expected[1][i * stream_float_counts[1] + 1] = 1.0; /* stream1_0[1] */

		expected[2][i * stream_float_counts[2] + 0] = 0.0; /* stream2_0 */
		expected[2][i * stream_float_counts[2] + 1] = 1.0; /* stream2_1[0] */
		expected[2][i * stream_float_counts[2] + 2] = 2.0; /* stream2_1[1] */
		expected[2][i * stream_float_counts[2] + 3] = 3.0; /* stream2_1[2] */
		expected[2][i * stream_float_counts[2] + 4] = 4.0; /* stream2_1[3] */
	}

	/* Skip Stream = 3 as it has no primitives written nor generated */
	for (i = 0; i < STREAMS-1; ++i) {
		char *name;
		(void)!asprintf(&name, "stream%d", i);
		pass = piglit_probe_buffer(xfb[i], GL_TRANSFORM_FEEDBACK_BUFFER,
				name, 1, expected_n[i], expected[i]);
		free(name);
	}

	for (i = 0; i < STREAMS; i++) {
		free(expected[i]);
	}

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	bool pass;
	unsigned primitive_n = 1;
	GLuint queries[2*STREAMS];
	GLuint xfb[STREAMS];
	GLuint vao;
	unsigned i;

	piglit_require_extension("GL_ARB_gpu_shader5");
	piglit_require_extension("GL_ARB_transform_feedback3");

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "spirv")) {
			use_spirv = true;
			printf("Running on SPIR-V mode\n");
		}
	}

	if (use_spirv) {
		piglit_require_extension("GL_ARB_gl_spirv");
	}

	piglit_join_paths(shader_source_filename,
			  sizeof(shader_source_filename),
			  7, /* num parts */
			  piglit_source_dir(),
			  "tests",
			  "spec",
			  "arb_gpu_shader5",
			  "execution",
			  "shader_source",
			  SHADER_SOURCE_FILE_NAME);

	build_and_use_program();

	/* Set up the transform feedback buffers. */
	glGenBuffers(ARRAY_SIZE(xfb), xfb);
	for (i = 0; i < ARRAY_SIZE(xfb); i++) {
		unsigned float_n = primitive_n * stream_float_counts[i];
		glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, i, xfb[i]);
		glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
				float_n * sizeof(float), NULL,
				GL_STREAM_READ);
	}

	/* Test only records using transform feedback. */
	glEnable(GL_RASTERIZER_DISCARD);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	glGenQueries(ARRAY_SIZE(queries), queries);
	for (i = 0; i < STREAMS; i++) {
		glBeginQueryIndexed(GL_PRIMITIVES_GENERATED, i, queries[i]);
		glBeginQueryIndexed(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
				i, queries[STREAMS + i]);
	}

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	/* Draw and record */
	glBeginTransformFeedback(GL_POINTS);
	glDrawArrays(GL_POINTS, 0, 1);
	for (i = 0; i < STREAMS; i++) {
		glEndQueryIndexed(GL_PRIMITIVES_GENERATED, i);
		glEndQueryIndexed(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
				i);
	}
	glEndTransformFeedback();
	glDeleteVertexArrays(1, &vao);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	pass = probe_buffers(xfb, queries, primitive_n);

	glDeleteBuffers(ARRAY_SIZE(xfb), xfb);
	glDeleteQueries(ARRAY_SIZE(queries), queries);

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

enum piglit_result
piglit_display(void)
{
	/* Should never be reached */
	return PIGLIT_FAIL;
}