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