Add more structure constructor tests.
[piglit/hramrach.git] / tests / general / sync_api.c
blob32193c61fa9408800d1ae267545c1303a4119199
1 /*
2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Authors:
24 * Ian Romanick <ian.d.romanick@intel.com>
27 /**
28 * \file sync_api.c
29 * Simple test of the API for GL_ARB_sync.
32 #include "piglit-util.h"
34 #define FAIL_ON_ERROR(string) \
35 do { \
36 const GLenum err = glGetError(); \
37 if (err != GL_NO_ERROR) { \
38 fprintf(stderr, "%s generated error 0x%04x\n", \
39 string, err); \
40 pass = GL_FALSE; \
41 goto done; \
42 } \
43 } while (0)
45 #ifndef APIENTRY
46 #define APIENTRY
47 #endif
48 #ifndef APIENTRYP
49 #define APIENTRYP APIENTRY *
50 #endif
52 #ifndef GL_ARB_sync
53 #define GL_ARB_sync 1
55 #define GL_MAX_SERVER_WAIT_TIMEOUT 0x9111
56 #define GL_OBJECT_TYPE 0x9112
57 #define GL_SYNC_CONDITION 0x9113
58 #define GL_SYNC_STATUS 0x9114
59 #define GL_SYNC_FLAGS 0x9115
60 #define GL_SYNC_FENCE 0x9116
61 #define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117
62 #define GL_UNSIGNALED 0x9118
63 #define GL_SIGNALED 0x9119
64 #define GL_ALREADY_SIGNALED 0x911A
65 #define GL_TIMEOUT_EXPIRED 0x911B
66 #define GL_CONDITION_SATISFIED 0x911C
67 #define GL_WAIT_FAILED 0x911D
68 #define GL_SYNC_FLUSH_COMMANDS_BIT 0x00000001
69 #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
71 typedef int64_t GLint64;
72 typedef uint64_t GLuint64;
73 typedef struct __GLsync *GLsync;
75 typedef GLsync (APIENTRYP PFNGLFENCESYNCPROC) (GLenum condition, GLbitfield flags);
76 typedef GLboolean (APIENTRYP PFNGLISSYNCPROC) (GLsync sync);
77 typedef void (APIENTRYP PFNGLDELETESYNCPROC) (GLsync sync);
78 typedef GLenum (APIENTRYP PFNGLCLIENTWAITSYNCPROC) (GLsync sync, GLbitfield flags, GLuint64 timeout);
79 typedef void (APIENTRYP PFNGLWAITSYNCPROC) (GLsync sync, GLbitfield flags, GLuint64 timeout);
80 typedef void (APIENTRYP PFNGLGETINTEGER64VPROC) (GLenum pname, GLint64 *params);
81 typedef void (APIENTRYP PFNGLGETSYNCIVPROC) (GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length, GLint *values);
82 #endif
84 static PFNGLFENCESYNCPROC pglFenceSync = NULL;
85 static PFNGLISSYNCPROC pglIsSync = NULL;
86 static PFNGLDELETESYNCPROC pglDeleteSync = NULL;
87 static PFNGLCLIENTWAITSYNCPROC pglClientWaitSync = NULL;
88 static PFNGLWAITSYNCPROC pglWaitSync = NULL;
89 static PFNGLGETINTEGER64VPROC pglGetInteger64v = NULL;
90 static PFNGLGETSYNCIVPROC pglGetSynciv = NULL;
93 static GLboolean Automatic = GL_FALSE;
95 static void
96 init(void)
98 piglit_require_extension("GL_ARB_sync");
100 pglFenceSync = (PFNGLFENCESYNCPROC) piglit_get_proc_address("glFenceSync");
101 pglIsSync = (PFNGLISSYNCPROC) piglit_get_proc_address("glIsSync");
102 pglDeleteSync = (PFNGLDELETESYNCPROC) piglit_get_proc_address("glDeleteSync");
103 pglClientWaitSync = (PFNGLCLIENTWAITSYNCPROC) piglit_get_proc_address("glClientWaitSync");
104 pglWaitSync = (PFNGLWAITSYNCPROC) piglit_get_proc_address("glWaitSync");
105 pglGetInteger64v = (PFNGLGETINTEGER64VPROC) piglit_get_proc_address("glGetInteger64v");
106 pglGetSynciv = (PFNGLGETSYNCIVPROC) piglit_get_proc_address("glGetSynciv");
108 glClearColor(0.1, 0.1, 0.3, 0.0);
112 static void
113 reshape(int width, int height)
115 glViewport(0, 0, (GLint) width, (GLint) height);
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();
119 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
120 glMatrixMode(GL_MODELVIEW);
124 GLboolean
125 test_GetSynciv(GLsync sync, GLenum pname, GLint expect)
127 GLboolean pass = GL_TRUE;
128 GLint val;
129 GLsizei len;
131 (*pglGetSynciv)(sync, pname, 1, & len, & val);
132 FAIL_ON_ERROR("glGetSynciv");
133 if (len != 1) {
134 fprintf(stderr, "glGetSynciv length of 0x%04x was %d\n",
135 pname, len);
136 pass = GL_FALSE;
137 } else if (val != expect) {
138 fprintf(stderr, "glGetSynciv of 0x%04x expected 0x%08x, "
139 "got 0x%08x\n", pname, expect, val);
140 pass = GL_FALSE;
143 done:
144 return pass;
147 static void
148 display(void)
150 GLboolean pass = GL_TRUE;
151 GLenum wait_val;
152 GLsync sync;
154 glClear(GL_COLOR_BUFFER_BIT);
156 glBegin(GL_TRIANGLES);
157 glColor3f(.8,0,0);
158 glVertex3f(-0.9, -0.9, -30.0);
159 glColor3f(0,.9,0);
160 glVertex3f( 0.9, -0.9, -30.0);
161 glColor3f(0,0,.7);
162 glVertex3f( 0.0, 0.9, -30.0);
163 glEnd();
165 glGetError();
167 sync = (*pglFenceSync)(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
168 FAIL_ON_ERROR("glFenceSync");
170 if (! (*pglIsSync)(sync)) {
171 fprintf(stderr, "IsSync(%p) failed\n", sync);
172 pass = GL_FALSE;
173 goto done;
175 FAIL_ON_ERROR("glIsSync");
177 if (! test_GetSynciv(sync, GL_SYNC_CONDITION,
178 GL_SYNC_GPU_COMMANDS_COMPLETE)) {
179 pass = GL_FALSE;
180 goto done;
183 if (! test_GetSynciv(sync, GL_SYNC_FLAGS, 0)) {
184 pass = GL_FALSE;
185 goto done;
188 glFinish();
190 /* After the glFinish, the sync *must* be signaled!
192 if (! test_GetSynciv(sync, GL_SYNC_STATUS, GL_SIGNALED)) {
193 pass = GL_FALSE;
194 goto done;
198 /* Since the sync has already been signaled, the wait should return
199 * GL_ALREADY_SIGNALED.
201 wait_val = (*pglClientWaitSync)(sync, 0, 1);
202 FAIL_ON_ERROR("glClientWaitSync");
204 if (wait_val != GL_ALREADY_SIGNALED) {
205 fprintf(stderr, "glClientWaitSync expected 0x%08x, "
206 "got 0x%08x\n", GL_ALREADY_SIGNALED, wait_val);
207 pass = GL_FALSE;
210 (*pglDeleteSync)(sync);
211 FAIL_ON_ERROR("glDeleteSync");
213 done:
214 if (Automatic)
215 piglit_report_result(pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE);
219 int main(int argc, char **argv)
221 glutInit(&argc, argv);
223 if (argc == 2 && !strcmp(argv[1], "-auto"))
224 Automatic = GL_TRUE;
226 glutInitWindowSize(400, 300);
227 glutInitDisplayMode(GLUT_RGB);
228 glutCreateWindow("GL_ARB_sync API test");
229 glutReshapeFunc(reshape);
230 glutKeyboardFunc(piglit_escape_exit_key);
231 glutDisplayFunc(display);
233 init();
235 glutMainLoop();
236 return 0;