File: ClientWaitSync-timeout.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 (204 lines) | stat: -rw-r--r-- 5,588 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright © 2014 Advanced Micro Devices, Inc.
 * All rights reserved.
 * Copyright 2015 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.
 *
 * Authors:
 *    Marek Olšák <marek.olsak@amd.com>
 *    Laura Ekstrand <laura@jlekstrand.net>
 *
 * Adapted to demonstrate a bug in ClientWaitSync by Laura Ekstrand
 * <laura@jlekstrand.net>, January 2015
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

#define BUF_SIZE (12 * 4 * sizeof(float))

void
piglit_init(int argc, char **argv)
{
	piglit_require_gl_version(15);
	piglit_require_extension("GL_ARB_buffer_storage");
	piglit_require_extension("GL_ARB_map_buffer_range");
	piglit_require_extension("GL_ARB_copy_buffer");
	piglit_require_extension("GL_ARB_sync");

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
}

bool
create_mapped_buffer(GLuint *buffer, GLfloat **map, GLboolean coherent,
		     GLboolean client_storage)
{
	glGenBuffers(1, buffer);
	glBindBuffer(GL_ARRAY_BUFFER, *buffer);
	glBufferStorage(GL_ARRAY_BUFFER, BUF_SIZE, NULL,
			GL_MAP_WRITE_BIT |
			GL_MAP_PERSISTENT_BIT |
			(coherent ? GL_MAP_COHERENT_BIT : 0) |
			GL_DYNAMIC_STORAGE_BIT |
			(client_storage ? GL_CLIENT_STORAGE_BIT : 0));

	piglit_check_gl_error(GL_NO_ERROR);

	*map = glMapBufferRange(GL_ARRAY_BUFFER, 0, BUF_SIZE,
			        GL_MAP_WRITE_BIT |
			        GL_MAP_PERSISTENT_BIT |
			        (coherent ? GL_MAP_COHERENT_BIT : 0));

	piglit_check_gl_error(GL_NO_ERROR);

	if (!*map)
		return false;

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	return true;
}

static enum piglit_result result = PIGLIT_PASS;
static float array[] = {
	17, 13, 0,
	17, 18, 0,
	12, 13, 0,
	12, 18, 0,
	27, 13, 0,
	27, 18, 0,
	22, 13, 0,
	22, 18, 0,
	37, 13, 0,
	37, 18, 0,
	32, 13, 0,
	32, 18, 0,
	47, 13, 0,
	47, 18, 0,
	42, 13, 0,
	42, 18, 0
};

void
read_subtest(GLboolean coherent, GLboolean client_storage)
{
	GLuint buffer;
	GLfloat *map;
	bool pass = true;

	int i;
	GLuint srcbuf;
	GLsync fence;
	GLenum wait_cond = GL_TIMEOUT_EXPIRED;
	int try_counter = 0;

	if (!create_mapped_buffer(&buffer, &map, coherent, client_storage)) {
		piglit_report_subtest_result(PIGLIT_FAIL,
			"read%s%s", coherent ? " coherent" : "",
			client_storage ? " client-storage" : "");
		result = PIGLIT_FAIL;
		return;
	}

	glClear(GL_COLOR_BUFFER_BIT);
	glGenBuffers(1, &srcbuf);
	glBindBuffer(GL_COPY_READ_BUFFER, srcbuf);
	glBufferData(GL_COPY_READ_BUFFER, BUF_SIZE, array, GL_STATIC_DRAW);

	/* Copy some data to the mapped buffer and check if the CPU can see it. */
	glBindBuffer(GL_COPY_WRITE_BUFFER, buffer);
	glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
			    0, 0, BUF_SIZE);

	glBindBuffer(GL_COPY_READ_BUFFER, 0);
	glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
	glDeleteBuffers(1, &srcbuf);

	if (!coherent)
		glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);

	/*
	 * Wait for the GPU to flush.
	 * This should only take one try because ClientWaitSync with
	 * GL_TIMEOUT_IGNORED should wait until the signal happens and never
	 * return GL_TIMEOUT_EXPIRED.
	 */
	fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
	while ((wait_cond == GL_TIMEOUT_EXPIRED) && (try_counter < 100)){
		wait_cond = glClientWaitSync(fence,
					     GL_SYNC_FLUSH_COMMANDS_BIT,
					     GL_TIMEOUT_IGNORED);
		printf("glClientWaitSync returned %s.\n",
		       piglit_get_gl_enum_name(wait_cond));
		try_counter++;

		if (wait_cond == GL_WAIT_FAILED) {
			/* Give up */
			pass = false;
			break;
		}
	}

	for (i = 0; i < ARRAY_SIZE(array); i++) {
		if (map[i] != array[i]) {
			printf("Probe [%i] failed. Expected: %f  Observed: %f\n",
			       i, array[i], map[i]);
			pass = false;
		}
	}

	if (try_counter > 1) {
		printf("glClientWaitSync called more than once"
		       " (%d total times).\n", try_counter);
		pass = false;
	}

	piglit_report_subtest_result(pass ? PIGLIT_PASS : PIGLIT_FAIL,
		"read%s%s", coherent ? " coherent" : "",
		client_storage ? " client-storage" : "");

	if (!pass)
		result = PIGLIT_FAIL;
}

enum piglit_result
piglit_display(void)
{
	/* !Coherent read subtests: require MemoryBarrier */
	if (piglit_is_extension_supported(
	       "GL_ARB_shader_image_load_store")) {
		read_subtest(false, false);
		read_subtest(false, true);
	}

	/* Coherent read subtests */
	read_subtest(true, false);
	read_subtest(true, true);

	return result;
}