glsl_to_tgsi: lower small branches based on the CAP
[mesa.git] / src / mesa / state_tracker / st_cb_syncobj.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Marek Olšák <maraeo@gmail.com>
31 */
32
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "st_context.h"
38 #include "st_cb_syncobj.h"
39
40 struct st_sync_object {
41 struct gl_sync_object b;
42
43 struct pipe_fence_handle *fence;
44 mtx_t mutex; /**< protects "fence" */
45 };
46
47
48 static struct gl_sync_object * st_new_sync_object(struct gl_context *ctx,
49 GLenum type)
50 {
51 if (type == GL_SYNC_FENCE) {
52 struct st_sync_object *so = CALLOC_STRUCT(st_sync_object);
53
54 mtx_init(&so->mutex, mtx_plain);
55 return &so->b;
56 } else {
57 return NULL;
58 }
59 }
60
61 static void st_delete_sync_object(struct gl_context *ctx,
62 struct gl_sync_object *obj)
63 {
64 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
65 struct st_sync_object *so = (struct st_sync_object*)obj;
66
67 screen->fence_reference(screen, &so->fence, NULL);
68 mtx_destroy(&so->mutex);
69 free(so->b.Label);
70 free(so);
71 }
72
73 static void st_fence_sync(struct gl_context *ctx, struct gl_sync_object *obj,
74 GLenum condition, GLbitfield flags)
75 {
76 struct pipe_context *pipe = st_context(ctx)->pipe;
77 struct st_sync_object *so = (struct st_sync_object*)obj;
78
79 assert(condition == GL_SYNC_GPU_COMMANDS_COMPLETE && flags == 0);
80 assert(so->fence == NULL);
81
82 pipe->flush(pipe, &so->fence, PIPE_FLUSH_DEFERRED);
83 }
84
85 static void st_client_wait_sync(struct gl_context *ctx,
86 struct gl_sync_object *obj,
87 GLbitfield flags, GLuint64 timeout)
88 {
89 struct pipe_context *pipe = st_context(ctx)->pipe;
90 struct pipe_screen *screen = pipe->screen;
91 struct st_sync_object *so = (struct st_sync_object*)obj;
92 struct pipe_fence_handle *fence = NULL;
93
94 /* If the fence doesn't exist, assume it's signalled. */
95 mtx_lock(&so->mutex);
96 if (!so->fence) {
97 mtx_unlock(&so->mutex);
98 so->b.StatusFlag = GL_TRUE;
99 return;
100 }
101
102 /* We need a local copy of the fence pointer, so that we can call
103 * fence_finish unlocked.
104 */
105 screen->fence_reference(screen, &fence, so->fence);
106 mtx_unlock(&so->mutex);
107
108 /* Section 4.1.2 of OpenGL 4.5 (Compatibility Profile) says:
109 * [...] if ClientWaitSync is called and all of the following are true:
110 * - the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
111 * - sync is unsignaled when ClientWaitSync is called,
112 * - and the calls to ClientWaitSync and FenceSync were issued from
113 * the same context,
114 * then the GL will behave as if the equivalent of Flush were inserted
115 * immediately after the creation of sync.
116 *
117 * Assume GL_SYNC_FLUSH_COMMANDS_BIT is always set, because applications
118 * forget to set it.
119 */
120 if (screen->fence_finish(screen, pipe, fence, timeout)) {
121 mtx_lock(&so->mutex);
122 screen->fence_reference(screen, &so->fence, NULL);
123 mtx_unlock(&so->mutex);
124 so->b.StatusFlag = GL_TRUE;
125 }
126 screen->fence_reference(screen, &fence, NULL);
127 }
128
129 static void st_check_sync(struct gl_context *ctx, struct gl_sync_object *obj)
130 {
131 st_client_wait_sync(ctx, obj, 0, 0);
132 }
133
134 static void st_server_wait_sync(struct gl_context *ctx,
135 struct gl_sync_object *obj,
136 GLbitfield flags, GLuint64 timeout)
137 {
138 /* NO-OP.
139 * Neither Gallium nor DRM interfaces support blocking on the GPU. */
140 }
141
142 void st_init_syncobj_functions(struct dd_function_table *functions)
143 {
144 functions->NewSyncObject = st_new_sync_object;
145 functions->FenceSync = st_fence_sync;
146 functions->DeleteSyncObject = st_delete_sync_object;
147 functions->CheckSync = st_check_sync;
148 functions->ClientWaitSync = st_client_wait_sync;
149 functions->ServerWaitSync = st_server_wait_sync;
150 }