glthread: inline _mesa_unmarshal_dispatch_cmd and convert the switch to a table
[mesa.git] / src / mesa / main / marshal.h
1 /*
2 * Copyright © 2012 Intel 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** \file marshal.h
25 *
26 * Declarations of functions related to marshalling GL calls from a client
27 * thread to a server thread.
28 */
29
30 #ifndef MARSHAL_H
31 #define MARSHAL_H
32
33 #include "main/glthread.h"
34 #include "main/context.h"
35 #include "main/macros.h"
36 #include "marshal_generated.h"
37
38 struct marshal_cmd_base
39 {
40 /**
41 * Type of command. See enum marshal_dispatch_cmd_id.
42 */
43 uint16_t cmd_id;
44
45 /**
46 * Size of command, in multiples of 4 bytes, including cmd_base.
47 */
48 uint16_t cmd_size;
49 };
50
51 typedef void (*_mesa_unmarshal_func)(struct gl_context *ctx, const void *cmd);
52 extern const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD];
53
54 static inline void *
55 _mesa_glthread_allocate_command(struct gl_context *ctx,
56 uint16_t cmd_id,
57 size_t size)
58 {
59 struct glthread_state *glthread = ctx->GLThread;
60 struct glthread_batch *next = &glthread->batches[glthread->next];
61 struct marshal_cmd_base *cmd_base;
62 const size_t aligned_size = ALIGN(size, 8);
63
64 if (unlikely(next->used + size > MARSHAL_MAX_CMD_SIZE)) {
65 _mesa_glthread_flush_batch(ctx);
66 next = &glthread->batches[glthread->next];
67 }
68
69 cmd_base = (struct marshal_cmd_base *)&next->buffer[next->used];
70 next->used += aligned_size;
71 cmd_base->cmd_id = cmd_id;
72 cmd_base->cmd_size = aligned_size;
73 return cmd_base;
74 }
75
76 /**
77 * Instead of conditionally handling marshaling previously-bound user vertex
78 * array data in draw calls (deprecated and removed in GL core), we just
79 * disable threading at the point where the user sets a user vertex array.
80 */
81 static inline bool
82 _mesa_glthread_is_non_vbo_vertex_attrib_pointer(const struct gl_context *ctx)
83 {
84 struct glthread_state *glthread = ctx->GLThread;
85
86 return ctx->API != API_OPENGL_CORE && !glthread->vertex_array_is_vbo;
87 }
88
89 /**
90 * Instead of conditionally handling marshaling immediate index data in draw
91 * calls (deprecated and removed in GL core), we just disable threading.
92 */
93 static inline bool
94 _mesa_glthread_is_non_vbo_draw_elements(const struct gl_context *ctx)
95 {
96 struct glthread_state *glthread = ctx->GLThread;
97
98 return ctx->API != API_OPENGL_CORE && !glthread->element_array_is_vbo;
99 }
100
101 #define DEBUG_MARSHAL_PRINT_CALLS 0
102
103 /**
104 * This is printed when we have fallen back to a sync. This can happen when
105 * MARSHAL_MAX_CMD_SIZE is exceeded.
106 */
107 static inline void
108 debug_print_sync_fallback(const char *func)
109 {
110 #if DEBUG_MARSHAL_PRINT_CALLS
111 printf("fallback to sync: %s\n", func);
112 #endif
113 }
114
115
116 static inline void
117 debug_print_sync(const char *func)
118 {
119 #if DEBUG_MARSHAL_PRINT_CALLS
120 printf("sync: %s\n", func);
121 #endif
122 }
123
124 static inline void
125 debug_print_marshal(const char *func)
126 {
127 #if DEBUG_MARSHAL_PRINT_CALLS
128 printf("marshal: %s\n", func);
129 #endif
130 }
131
132 struct _glapi_table *
133 _mesa_create_marshal_table(const struct gl_context *ctx);
134
135 static inline void
136 _mesa_post_marshal_hook(struct gl_context *ctx)
137 {
138 /* This can be enabled for debugging whether a failure is a synchronization
139 * problem between the main thread and the worker thread, or a failure in
140 * how we actually marshal.
141 */
142 if (false)
143 _mesa_glthread_finish(ctx);
144 }
145
146
147 /**
148 * Checks whether we're on a compat context for code-generated
149 * glBindVertexArray().
150 *
151 * In order to decide whether a draw call uses only VBOs for vertex and index
152 * buffers, we track the current vertex and index buffer bindings by
153 * glBindBuffer(). However, the index buffer binding is stored in the vertex
154 * array as opposed to the context. If we were to accurately track whether
155 * the index buffer was a user pointer ot not, we'd have to track it per
156 * vertex array, which would mean synchronizing with the client thread and
157 * looking into the hash table to find the actual vertex array object. That's
158 * more tracking than we'd like to do in the main thread, if possible.
159 *
160 * Instead, just punt for now and disable threading on apps using vertex
161 * arrays and compat contexts. Apps using vertex arrays can probably use a
162 * core context.
163 */
164 static inline bool
165 _mesa_glthread_is_compat_bind_vertex_array(const struct gl_context *ctx)
166 {
167 return ctx->API != API_OPENGL_CORE;
168 }
169
170 struct marshal_cmd_Enable;
171 struct marshal_cmd_ShaderSource;
172 struct marshal_cmd_Flush;
173 struct marshal_cmd_BindBuffer;
174 struct marshal_cmd_BufferData;
175 struct marshal_cmd_BufferSubData;
176 struct marshal_cmd_NamedBufferData;
177 struct marshal_cmd_NamedBufferSubData;
178 struct marshal_cmd_ClearBuffer;
179 #define marshal_cmd_ClearBufferfv marshal_cmd_ClearBuffer
180 #define marshal_cmd_ClearBufferiv marshal_cmd_ClearBuffer
181 #define marshal_cmd_ClearBufferuiv marshal_cmd_ClearBuffer
182 #define marshal_cmd_ClearBufferfi marshal_cmd_ClearBuffer
183
184 void
185 _mesa_unmarshal_Enable(struct gl_context *ctx,
186 const struct marshal_cmd_Enable *cmd);
187
188 void GLAPIENTRY
189 _mesa_marshal_Enable(GLenum cap);
190
191 void GLAPIENTRY
192 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
193 const GLchar * const *string, const GLint *length);
194
195 void
196 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
197 const struct marshal_cmd_ShaderSource *cmd);
198
199 void GLAPIENTRY
200 _mesa_marshal_Flush(void);
201
202 void
203 _mesa_unmarshal_Flush(struct gl_context *ctx,
204 const struct marshal_cmd_Flush *cmd);
205
206 void GLAPIENTRY
207 _mesa_marshal_BindBuffer(GLenum target, GLuint buffer);
208
209 void
210 _mesa_unmarshal_BindBuffer(struct gl_context *ctx,
211 const struct marshal_cmd_BindBuffer *cmd);
212
213 void
214 _mesa_unmarshal_BufferData(struct gl_context *ctx,
215 const struct marshal_cmd_BufferData *cmd);
216
217 void GLAPIENTRY
218 _mesa_marshal_BufferData(GLenum target, GLsizeiptr size, const GLvoid * data,
219 GLenum usage);
220
221 void
222 _mesa_unmarshal_BufferSubData(struct gl_context *ctx,
223 const struct marshal_cmd_BufferSubData *cmd);
224
225 void GLAPIENTRY
226 _mesa_marshal_BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
227 const GLvoid * data);
228
229 void
230 _mesa_unmarshal_NamedBufferData(struct gl_context *ctx,
231 const struct marshal_cmd_NamedBufferData *cmd);
232
233 void GLAPIENTRY
234 _mesa_marshal_NamedBufferData(GLuint buffer, GLsizeiptr size,
235 const GLvoid * data, GLenum usage);
236
237 void
238 _mesa_unmarshal_NamedBufferSubData(struct gl_context *ctx,
239 const struct marshal_cmd_NamedBufferSubData *cmd);
240
241 void GLAPIENTRY
242 _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size,
243 const GLvoid * data);
244
245 void
246 _mesa_unmarshal_ClearBufferfv(struct gl_context *ctx,
247 const struct marshal_cmd_ClearBuffer *cmd);
248
249 void GLAPIENTRY
250 _mesa_marshal_ClearBufferfv(GLenum buffer, GLint drawbuffer,
251 const GLfloat *value);
252
253 void
254 _mesa_unmarshal_ClearBufferiv(struct gl_context *ctx,
255 const struct marshal_cmd_ClearBuffer *cmd);
256
257 void GLAPIENTRY
258 _mesa_marshal_ClearBufferiv(GLenum buffer, GLint drawbuffer,
259 const GLint *value);
260
261 void
262 _mesa_unmarshal_ClearBufferuiv(struct gl_context *ctx,
263 const struct marshal_cmd_ClearBuffer *cmd);
264
265 void GLAPIENTRY
266 _mesa_marshal_ClearBufferuiv(GLenum buffer, GLint drawbuffer,
267 const GLuint *value);
268
269 void
270 _mesa_unmarshal_ClearBufferfi(struct gl_context *ctx,
271 const struct marshal_cmd_ClearBuffer *cmd);
272
273 void GLAPIENTRY
274 _mesa_marshal_ClearBufferfi(GLenum buffer, GLint drawbuffer,
275 const GLfloat depth, const GLint stencil);
276
277 #endif /* MARSHAL_H */