st/mesa: use PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY
[mesa.git] / src / mesa / state_tracker / st_cb_semaphoreobjects.c
1 /*
2 * Copyright © 2017 Valve Corporation.
3 *
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:
10 *
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.
14 *
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.
22 */
23
24 #include "main/imports.h"
25 #include "main/mtypes.h"
26 #include "main/context.h"
27
28 #include "main/externalobjects.h"
29
30 #include "st_context.h"
31 #include "st_texture.h"
32 #include "st_cb_bitmap.h"
33 #include "st_cb_bufferobjects.h"
34 #include "st_cb_semaphoreobjects.h"
35
36 #include "state_tracker/drm_driver.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39
40 static struct gl_semaphore_object *
41 st_semaphoreobj_alloc(struct gl_context *ctx, GLuint name)
42 {
43 struct st_semaphore_object *st_obj = ST_CALLOC_STRUCT(st_semaphore_object);
44 if (!st_obj)
45 return NULL;
46
47 _mesa_initialize_semaphore_object(ctx, &st_obj->Base, name);
48 return &st_obj->Base;
49 }
50
51 static void
52 st_semaphoreobj_free(struct gl_context *ctx,
53 struct gl_semaphore_object *semObj)
54 {
55 _mesa_delete_semaphore_object(ctx, semObj);
56 }
57
58
59 static void
60 st_import_semaphoreobj_fd(struct gl_context *ctx,
61 struct gl_semaphore_object *semObj,
62 int fd)
63 {
64 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
65 struct st_context *st = st_context(ctx);
66 struct pipe_context *pipe = st->pipe;
67
68 pipe->create_fence_fd(pipe, &st_obj->fence, fd, PIPE_FD_TYPE_SYNCOBJ);
69
70 #if !defined(_WIN32)
71 /* We own fd, but we no longer need it. So get rid of it */
72 close(fd);
73 #endif
74 }
75
76 static void
77 st_server_wait_semaphore(struct gl_context *ctx,
78 struct gl_semaphore_object *semObj,
79 GLuint numBufferBarriers,
80 struct gl_buffer_object **bufObjs,
81 GLuint numTextureBarriers,
82 struct gl_texture_object **texObjs,
83 const GLenum *srcLayouts)
84 {
85 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
86 struct st_context *st = st_context(ctx);
87 struct pipe_context *pipe = st->pipe;
88 struct st_buffer_object *bufObj;
89 struct st_texture_object *texObj;
90
91 /* The driver is allowed to flush during fence_server_sync, be prepared */
92 st_flush_bitmap_cache(st);
93 pipe->fence_server_sync(pipe, st_obj->fence);
94
95 /**
96 * According to the EXT_external_objects spec, the memory operations must
97 * follow the wait. This is to make sure the flush is executed after the
98 * other party is done modifying the memory.
99 *
100 * Relevant excerpt from section "4.2.3 Waiting for Semaphores":
101 *
102 * Following completion of the semaphore wait operation, memory will also be
103 * made visible in the specified buffer and texture objects.
104 *
105 */
106 for (unsigned i = 0; i < numBufferBarriers; i++) {
107 if (!bufObjs[i])
108 continue;
109
110 bufObj = st_buffer_object(bufObjs[i]);
111 pipe->flush_resource(pipe, bufObj->buffer);
112 }
113
114 for (unsigned i = 0; i < numTextureBarriers; i++) {
115 if (!texObjs[i])
116 continue;
117
118 texObj = st_texture_object(texObjs[i]);
119 pipe->flush_resource(pipe, texObj->pt);
120 }
121 }
122
123 static void
124 st_server_signal_semaphore(struct gl_context *ctx,
125 struct gl_semaphore_object *semObj,
126 GLuint numBufferBarriers,
127 struct gl_buffer_object **bufObjs,
128 GLuint numTextureBarriers,
129 struct gl_texture_object **texObjs,
130 const GLenum *dstLayouts)
131 {
132 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
133 struct st_context *st = st_context(ctx);
134 struct pipe_context *pipe = st->pipe;
135 struct st_buffer_object *bufObj;
136 struct st_texture_object *texObj;
137
138 for (unsigned i = 0; i < numBufferBarriers; i++) {
139 if (!bufObjs[i])
140 continue;
141
142 bufObj = st_buffer_object(bufObjs[i]);
143 pipe->flush_resource(pipe, bufObj->buffer);
144 }
145
146 for (unsigned i = 0; i < numTextureBarriers; i++) {
147 if (!texObjs[i])
148 continue;
149
150 texObj = st_texture_object(texObjs[i]);
151 pipe->flush_resource(pipe, texObj->pt);
152 }
153
154 /* The driver is allowed to flush during fence_server_signal, be prepared */
155 st_flush_bitmap_cache(st);
156 pipe->fence_server_signal(pipe, st_obj->fence);
157 }
158
159 void
160 st_init_semaphoreobject_functions(struct dd_function_table *functions)
161 {
162 functions->NewSemaphoreObject = st_semaphoreobj_alloc;
163 functions->DeleteSemaphoreObject = st_semaphoreobj_free;
164 functions->ImportSemaphoreFd = st_import_semaphoreobj_fd;
165 functions->ServerWaitSemaphoreObject = st_server_wait_semaphore;
166 functions->ServerSignalSemaphoreObject = st_server_signal_semaphore;
167 }