summaryrefslogtreecommitdiff
path: root/tests/spec/arb_enhanced_layouts/gs-stream-location-aliasing.c
blob: 765431e9866598a6a78e9ee2fbd3ab1857fd85c7 (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
/*
 * Copyright (c) 2016 Advanced Micro Devices, Inc.
 *
 * 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 gs-stream-location-aliasing.c
 *
 * Tests two vertex streams produced by a geometry shader, with location
 * aliasing between the outputs of the different streams.
 *
 * Loosely based on transform_feedback_layout_qualifiers.c
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 32;
	config.supports_gl_core_version = 32;

PIGLIT_GL_TEST_CONFIG_END

static const char vs_text[] =
	"#version 150\n"
	"out int vertexid;\n"
	"void main() {\n"
	"  vertexid = gl_VertexID;\n"
	"}\n";

static const char gs_text[] =
	"#version 150\n"
	"#extension GL_ARB_gpu_shader5: require\n"
	"#extension GL_ARB_separate_shader_objects: require\n"
	"#extension GL_ARB_enhanced_layouts: require\n"
	"\n"
	"layout(points, invocations = 2) in;\n"
	"layout(points, max_vertices = 2) out;\n"
	"\n"
	"in int vertexid[];\n"
	"\n"
	"layout(xfb_offset = 0, xfb_buffer = 0, location = 0, component = 0, stream = 0) out float x1_out;\n"
	"layout(xfb_offset = 0, xfb_buffer = 1, location = 0, component = 1, stream = 1) out float x2_out;\n"
	"\n"
	"void main() {\n"
	"  x1_out = 100 + 10 * vertexid[0] + gl_InvocationID;\n"
	"  EmitStreamVertex(0);\n"
	"  EndStreamPrimitive(0);\n"
	"  x2_out = 200 + 10 * vertexid[0] + gl_InvocationID;\n"
	"  EmitStreamVertex(1);\n"
	"  EndStreamPrimitive(1);\n"
	"}";

static const int num_in_vertices = 2;
static const int num_xfb_results_per_stream = 2 * 2;

static void
build_and_use_program()
{
	GLuint prog;

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

	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)
{
	bool pass = true;
	float *first;
	float *second;

	first = malloc(num_xfb_results_per_stream * sizeof(float));
	second = malloc(num_xfb_results_per_stream * sizeof(float));

	unsigned i = 0;
	for (unsigned vertex = 0; vertex < 2; ++vertex) {
		for (unsigned invocation = 0; invocation < 2; ++invocation) {
			first[i] = 100 + 10 * vertex + invocation;
			second[i] = 200 + 10 * vertex + invocation;
			i++;
		}
	}
	assert(i == num_xfb_results_per_stream);

	pass = piglit_probe_buffer(xfb[0], GL_TRANSFORM_FEEDBACK_BUFFER,
			"first", 1, num_xfb_results_per_stream, first) && pass;
	pass = piglit_probe_buffer(xfb[1], GL_TRANSFORM_FEEDBACK_BUFFER,
			"second", 1, num_xfb_results_per_stream, second) &&
			pass;

	free(first);
	free(second);

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	bool pass = true;
	GLuint xfb[2];
	GLuint vao;

	piglit_require_GLSL_version(150);
	piglit_require_extension("GL_ARB_transform_feedback3");
	piglit_require_extension("GL_ARB_enhanced_layouts");

	build_and_use_program();
	glEnable(GL_RASTERIZER_DISCARD);

	/* Set up the transform feedback buffers. */
	glGenBuffers(2, xfb);
	glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfb[0]);
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
		     num_xfb_results_per_stream * 4, NULL, GL_STREAM_READ);
	glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 1, xfb[1]);
	glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
		     num_xfb_results_per_stream * 4, NULL, GL_STREAM_READ);

	/* Test is run under desktop OpenGL 3.2 -> use of VAOs is required */
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	/* Draw and record */
	glBeginTransformFeedback(GL_POINTS);
	glDrawArrays(GL_POINTS, 0, num_in_vertices);
	glEndTransformFeedback();
	glDeleteVertexArrays(1, &vao);

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

	pass = probe_buffers(xfb);

	glDeleteBuffers(2, xfb);

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}

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