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