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