File: drawarrays.c

package info (click to toggle)
piglit 0~git20200212-f4710c51b-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 106,972 kB
  • sloc: ansic: 263,763; xml: 48,941; python: 29,918; lisp: 19,789; cpp: 12,142; sh: 22; makefile: 20; pascal: 5
file content (276 lines) | stat: -rw-r--r-- 7,259 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2014 VMware, 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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
 * Tests GL_ARB_base_instance.  This test also requires GL_ARB_draw_instanced
 * and GL_ARB_instanced_arrays
 */

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

PIGLIT_GL_TEST_CONFIG_BEGIN
#ifdef PIGLIT_USE_OPENGL
	config.supports_gl_core_version = 31;
#else
	config.supports_gl_es_version = 30;
#endif
	config.window_width = 400;
	config.window_height = 400;
	config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;
PIGLIT_GL_TEST_CONFIG_END

static const char *TestName = "arb_base_instance-drawarrays";

static GLint MVPUniform, PosUniform;
static GLint VertexAttrib, ColorAttrib;

static float modelview[16], projection[16], modelviewproj[16];

#define PRIMS 8

/* Ortho projection width, height */
#define W 10.0
#define H 10.0


/**
 * Vertex position comes from Pos[instance].  Color comes from an
 * instanced array.
 */
static const char *VertShaderText =
#ifdef PIGLIT_USE_OPENGL
	"#version 130 \n"
	"#extension GL_ARB_draw_instanced: enable \n"
#else
	"#version 300 es\n"
	"#define gl_InstanceIDARB gl_InstanceID\n"
#endif
	"in vec4 Vertex, Color; \n"
	"uniform vec2 Pos[8]; \n"
	"uniform mat4 MVP; \n"
	"out vec4 ColorVarying; \n"
	"void main() \n"
	"{ \n"
	"	vec4 p = Vertex; \n"
	"	vec2 pos = Pos[gl_InstanceIDARB]; \n"
	"	p.xy += pos; \n"
	"	gl_Position = MVP * p; \n"
	"	ColorVarying = Color; \n"
	"} \n";

/* Simple color pass-through */
static const char *FragShaderText =
#ifdef PIGLIT_USE_OPENGL
	"#version 130 \n"
#else
	"#version 300 es \n"
	"precision highp float;\n"
#endif
	"in vec4 ColorVarying; \n"
	"out vec4 FragColor; \n"
	"void main() \n"
	"{ \n"
	"	FragColor = ColorVarying; \n"
	"} \n";


static GLuint VertShader, FragShader, Program;

/* Instance positions in uniform array */
static const GLfloat Positions[PRIMS][2] = {
	{ -6, 6 },
	{ -4, 4 },
	{ -2, 2 },
	{  0, 0 },
	{  2, -2 },
	{  4, -4 },
	{  6, -6 },
	{  8, -8 }
};

/* Instance colors in vertex array */
static const GLfloat Colors[PRIMS][4] = {
	{1, 0, 0, 1},
	{0, 1, 0, 1},
	{0, 0, 1, 1},
	{1, 1, 0, 1},
	{0, 1, 1, 1},
	{1, 0, 1, 1},
	{1, 1, 1, 1},
	{0.5, 0.5, 0.5, 1},
};


/** Convert object position to window position */
static void
objpos_to_winpos(const float obj[2], int win[2])
{
	float winpos[4];
	float objpos[4] = { obj[0], obj[1], 0.0, 1.0 };

	piglit_project_to_window(winpos, objpos, modelview, projection,
				 0, 0, piglit_width, piglit_height);
	win[0] = (int) winpos[0];
	win[1] = (int) winpos[1];
}


static bool
test_instancing(GLuint divisor, GLuint baseInstance, GLuint vao)
{
	GLuint verts_bo, colors_bo;

	static const GLfloat verts[4][2] = {
		{-1, -1}, {1, -1}, {1, 1}, {-1, 1}
	};
	const GLuint numPrims = PRIMS - baseInstance;
        GLint i, pos[2];

	glGenBuffers(1, &verts_bo);
	glBindBuffer(GL_ARRAY_BUFFER, verts_bo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

	glVertexAttribPointer(VertexAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(VertexAttrib);

	glGenBuffers(1, &colors_bo);
	glBindBuffer(GL_ARRAY_BUFFER, colors_bo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);

	glVertexAttribPointer(ColorAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(ColorAttrib);
	/* advance color once every 'n' instances */

	if (vao) {
		glBindVertexArray(0);
		glVertexArrayVertexAttribDivisorEXT(vao, ColorAttrib, divisor);
		glBindVertexArray(vao);
	} else {
		glVertexAttribDivisorARB(ColorAttrib, divisor);
	}

	glViewport(0, 0, piglit_width, piglit_height);
	glClear(GL_COLOR_BUFFER_BIT);

	glUseProgram(Program);

	glDrawArraysInstancedBaseInstance(GL_TRIANGLE_FAN, 0, 4,
					  numPrims, baseInstance);

	/* check rendering */
	for (i = 0; i < numPrims; i++) {
		GLuint elem = i / divisor + baseInstance;

		/* compute probe location */
		objpos_to_winpos(Positions[i], pos);

		if (!piglit_probe_pixel_rgba(pos[0], pos[1], Colors[elem])) {
			fprintf(stderr, "%s: instance %d failed to draw correctly\n",
				TestName, i);
			fprintf(stderr, "%s: color instance divisor = %u  base = %u\n",
				TestName, divisor, baseInstance);
			piglit_present_results();
			return false;
		}
	}

	glDisableVertexAttribArray(VertexAttrib);
	glDisableVertexAttribArray(ColorAttrib);

	glDeleteBuffers(1, &verts_bo);
	glDeleteBuffers(1, &colors_bo);

	piglit_present_results();

	return true;
}


enum piglit_result
piglit_display(void)
{
	GLuint div, baseInst;
	bool has_ext_dsa = piglit_is_extension_supported("GL_EXT_direct_state_access");
	GLint vao;

	glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vao);

	for (div = 1; div <= PRIMS; div++) {
		for (baseInst = 0; baseInst < PRIMS -1; baseInst++) {
			if (!test_instancing(div, baseInst, 0))
				return PIGLIT_FAIL;
			if (has_ext_dsa && !test_instancing(div, baseInst, vao))
				return PIGLIT_FAIL;
		}
	}

	return PIGLIT_PASS;
}


void
piglit_init(int argc, char **argv)
{
	GLuint vao;

#ifdef PIGLIT_USE_OPENGL
	piglit_require_extension("GL_ARB_draw_instanced");
	piglit_require_extension("GL_ARB_instanced_arrays");
	piglit_require_extension("GL_ARB_base_instance");
#else
	piglit_require_extension("GL_EXT_base_instance");
#endif

	VertShader = piglit_compile_shader_text(GL_VERTEX_SHADER,
						VertShaderText);
	assert(VertShader);

	FragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER,
						FragShaderText);
	assert(FragShader);

	Program = piglit_link_simple_program(VertShader, FragShader);

	glUseProgram(Program);

	MVPUniform = glGetUniformLocation(Program, "MVP");
	PosUniform = glGetUniformLocation(Program, "Pos");
	VertexAttrib = glGetAttribLocation(Program, "Vertex");
	ColorAttrib = glGetAttribLocation(Program, "Color");

	glUniform2fv(PosUniform, PRIMS, (GLfloat *) Positions);

	/* Setup coordinate transformation */
	piglit_scale_matrix(modelview, 0.5, 0.5, 1.0);
	piglit_ortho_matrix(projection, -0.5 * W, 0.5 * W,
			    -0.5 * H, 0.5 * H, -1.0, 1.0);
	piglit_matrix_mul_matrix(modelviewproj, modelview, projection);

	glUniformMatrix4fv(MVPUniform, 1, GL_FALSE, modelviewproj);

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