glthread: merge glBufferData and glNamedBufferData into 1 set of functions
[mesa.git] / src / mesa / main / marshal.c
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.c
25 *
26 * Custom functions for marshalling GL calls from the main thread to a worker
27 * thread when automatic code generation isn't appropriate.
28 */
29
30 #include "main/enums.h"
31 #include "main/macros.h"
32 #include "marshal.h"
33 #include "dispatch.h"
34
35 static inline void
36 _mesa_post_marshal_hook(struct gl_context *ctx)
37 {
38 /* This can be enabled for debugging whether a failure is a synchronization
39 * problem between the main thread and the worker thread, or a failure in
40 * how we actually marshal.
41 */
42 if (false)
43 _mesa_glthread_finish(ctx);
44 }
45
46
47 struct marshal_cmd_ShaderSource
48 {
49 struct marshal_cmd_base cmd_base;
50 GLuint shader;
51 GLsizei count;
52 /* Followed by GLint length[count], then the contents of all strings,
53 * concatenated.
54 */
55 };
56
57
58 void
59 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
60 const struct marshal_cmd_ShaderSource *cmd)
61 {
62 const GLint *cmd_length = (const GLint *) (cmd + 1);
63 const GLchar *cmd_strings = (const GLchar *) (cmd_length + cmd->count);
64 /* TODO: how to deal with malloc failure? */
65 const GLchar * *string = malloc(cmd->count * sizeof(const GLchar *));
66 int i;
67
68 for (i = 0; i < cmd->count; ++i) {
69 string[i] = cmd_strings;
70 cmd_strings += cmd_length[i];
71 }
72 CALL_ShaderSource(ctx->CurrentServerDispatch,
73 (cmd->shader, cmd->count, string, cmd_length));
74 free((void *)string);
75 }
76
77
78 static size_t
79 measure_ShaderSource_strings(GLsizei count, const GLchar * const *string,
80 const GLint *length_in, GLint *length_out)
81 {
82 int i;
83 size_t total_string_length = 0;
84
85 for (i = 0; i < count; ++i) {
86 if (length_in == NULL || length_in[i] < 0) {
87 if (string[i])
88 length_out[i] = strlen(string[i]);
89 } else {
90 length_out[i] = length_in[i];
91 }
92 total_string_length += length_out[i];
93 }
94 return total_string_length;
95 }
96
97
98 void GLAPIENTRY
99 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
100 const GLchar * const *string, const GLint *length)
101 {
102 /* TODO: how to report an error if count < 0? */
103
104 GET_CURRENT_CONTEXT(ctx);
105 /* TODO: how to deal with malloc failure? */
106 const size_t fixed_cmd_size = sizeof(struct marshal_cmd_ShaderSource);
107 STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource) % sizeof(GLint) == 0);
108 size_t length_size = count * sizeof(GLint);
109 GLint *length_tmp = malloc(length_size);
110 size_t total_string_length =
111 measure_ShaderSource_strings(count, string, length, length_tmp);
112 size_t total_cmd_size = fixed_cmd_size + length_size + total_string_length;
113
114 if (total_cmd_size <= MARSHAL_MAX_CMD_SIZE) {
115 struct marshal_cmd_ShaderSource *cmd =
116 _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_ShaderSource,
117 total_cmd_size);
118 GLint *cmd_length = (GLint *) (cmd + 1);
119 GLchar *cmd_strings = (GLchar *) (cmd_length + count);
120 int i;
121
122 cmd->shader = shader;
123 cmd->count = count;
124 memcpy(cmd_length, length_tmp, length_size);
125 for (i = 0; i < count; ++i) {
126 memcpy(cmd_strings, string[i], cmd_length[i]);
127 cmd_strings += cmd_length[i];
128 }
129 _mesa_post_marshal_hook(ctx);
130 } else {
131 _mesa_glthread_finish(ctx);
132 CALL_ShaderSource(ctx->CurrentServerDispatch,
133 (shader, count, string, length_tmp));
134 }
135 free(length_tmp);
136 }
137
138
139 /** Tracks the current bindings for the vertex array and index array buffers.
140 *
141 * This is part of what we need to enable glthread on compat-GL contexts that
142 * happen to use VBOs, without also supporting the full tracking of VBO vs
143 * user vertex array bindings per attribute on each vertex array for
144 * determining what to upload at draw call time.
145 *
146 * Note that GL core makes it so that a buffer binding with an invalid handle
147 * in the "buffer" parameter will throw an error, and then a
148 * glVertexAttribPointer() that followsmight not end up pointing at a VBO.
149 * However, in GL core the draw call would throw an error as well, so we don't
150 * really care if our tracking is wrong for this case -- we never need to
151 * marshal user data for draw calls, and the unmarshal will just generate an
152 * error or not as appropriate.
153 *
154 * For compatibility GL, we do need to accurately know whether the draw call
155 * on the unmarshal side will dereference a user pointer or load data from a
156 * VBO per vertex. That would make it seem like we need to track whether a
157 * "buffer" is valid, so that we can know when an error will be generated
158 * instead of updating the binding. However, compat GL has the ridiculous
159 * feature that if you pass a bad name, it just gens a buffer object for you,
160 * so we escape without having to know if things are valid or not.
161 */
162 void
163 _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target, GLuint buffer)
164 {
165 struct glthread_state *glthread = ctx->GLThread;
166
167 switch (target) {
168 case GL_ARRAY_BUFFER:
169 glthread->vertex_array_is_vbo = (buffer != 0);
170 break;
171 case GL_ELEMENT_ARRAY_BUFFER:
172 /* The current element array buffer binding is actually tracked in the
173 * vertex array object instead of the context, so this would need to
174 * change on vertex array object updates.
175 */
176 glthread->CurrentVAO->IndexBufferIsUserPointer = buffer != 0;
177 break;
178 case GL_DRAW_INDIRECT_BUFFER:
179 glthread->draw_indirect_buffer_is_vbo = buffer != 0;
180 break;
181 }
182 }
183
184
185 /* BufferData: marshalled asynchronously */
186 struct marshal_cmd_BufferData
187 {
188 struct marshal_cmd_base cmd_base;
189 GLuint target_or_name;
190 GLsizeiptr size;
191 GLenum usage;
192 const GLvoid *data_external_mem;
193 bool data_null; /* If set, no data follows for "data" */
194 bool named;
195 /* Next size bytes are GLubyte data[size] */
196 };
197
198 void
199 _mesa_unmarshal_BufferData(struct gl_context *ctx,
200 const struct marshal_cmd_BufferData *cmd)
201 {
202 const GLuint target_or_name = cmd->target_or_name;
203 const GLsizei size = cmd->size;
204 const GLenum usage = cmd->usage;
205 const void *data;
206
207 if (cmd->data_null)
208 data = NULL;
209 else if (!cmd->named && target_or_name == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD)
210 data = cmd->data_external_mem;
211 else
212 data = (const void *) (cmd + 1);
213
214 if (cmd->named) {
215 CALL_NamedBufferData(ctx->CurrentServerDispatch,
216 (target_or_name, size, data, usage));
217 } else {
218 CALL_BufferData(ctx->CurrentServerDispatch,
219 (target_or_name, size, data, usage));
220 }
221 }
222
223 void
224 _mesa_unmarshal_NamedBufferData(struct gl_context *ctx,
225 const struct marshal_cmd_BufferData *cmd)
226 {
227 unreachable("never used - all BufferData variants use DISPATCH_CMD_BufferData");
228 }
229
230 static void
231 _mesa_marshal_BufferData_merged(GLuint target_or_name, GLsizeiptr size,
232 const GLvoid *data, GLenum usage, bool named,
233 const char *func)
234 {
235 GET_CURRENT_CONTEXT(ctx);
236 bool external_mem = !named &&
237 target_or_name == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD;
238 bool copy_data = data && !external_mem;
239 int cmd_size = sizeof(struct marshal_cmd_BufferData) + (copy_data ? size : 0);
240 debug_print_marshal("BufferData");
241
242 if (unlikely(size < 0 || size > INT_MAX || cmd_size < 0 ||
243 cmd_size > MARSHAL_MAX_CMD_SIZE ||
244 (named && target_or_name == 0))) {
245 _mesa_glthread_finish_before(ctx, func);
246 if (named) {
247 CALL_NamedBufferData(ctx->CurrentServerDispatch,
248 (target_or_name, size, data, usage));
249 } else {
250 CALL_BufferData(ctx->CurrentServerDispatch,
251 (target_or_name, size, data, usage));
252 }
253 return;
254 }
255
256 struct marshal_cmd_BufferData *cmd =
257 _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_BufferData,
258 cmd_size);
259
260 cmd->target_or_name = target_or_name;
261 cmd->size = size;
262 cmd->usage = usage;
263 cmd->data_null = !data;
264 cmd->named = named;
265 cmd->data_external_mem = data;
266
267 if (copy_data) {
268 char *variable_data = (char *) (cmd + 1);
269 memcpy(variable_data, data, size);
270 }
271 _mesa_post_marshal_hook(ctx);
272 }
273
274 void GLAPIENTRY
275 _mesa_marshal_BufferData(GLenum target, GLsizeiptr size, const GLvoid * data,
276 GLenum usage)
277 {
278 _mesa_marshal_BufferData_merged(target, size, data, usage, false,
279 "BufferData");
280 }
281
282 void GLAPIENTRY
283 _mesa_marshal_NamedBufferData(GLuint buffer, GLsizeiptr size,
284 const GLvoid * data, GLenum usage)
285 {
286 _mesa_marshal_BufferData_merged(buffer, size, data, usage, true,
287 "NamedBufferData");
288 }
289
290
291 /* BufferSubData: marshalled asynchronously */
292 struct marshal_cmd_BufferSubData
293 {
294 struct marshal_cmd_base cmd_base;
295 GLenum target;
296 GLintptr offset;
297 GLsizeiptr size;
298 /* Next size bytes are GLubyte data[size] */
299 };
300
301 void
302 _mesa_unmarshal_BufferSubData(struct gl_context *ctx,
303 const struct marshal_cmd_BufferSubData *cmd)
304 {
305 const GLenum target = cmd->target;
306 const GLintptr offset = cmd->offset;
307 const GLsizeiptr size = cmd->size;
308 const void *data = (const void *) (cmd + 1);
309
310 CALL_BufferSubData(ctx->CurrentServerDispatch,
311 (target, offset, size, data));
312 }
313
314 void GLAPIENTRY
315 _mesa_marshal_BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
316 const GLvoid * data)
317 {
318 GET_CURRENT_CONTEXT(ctx);
319 size_t cmd_size = sizeof(struct marshal_cmd_BufferSubData) + size;
320
321 debug_print_marshal("BufferSubData");
322 if (unlikely(size < 0)) {
323 _mesa_glthread_finish(ctx);
324 _mesa_error(ctx, GL_INVALID_VALUE, "BufferSubData(size < 0)");
325 return;
326 }
327
328 if (target != GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD &&
329 cmd_size <= MARSHAL_MAX_CMD_SIZE) {
330 struct marshal_cmd_BufferSubData *cmd =
331 _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_BufferSubData,
332 cmd_size);
333 cmd->target = target;
334 cmd->offset = offset;
335 cmd->size = size;
336 char *variable_data = (char *) (cmd + 1);
337 memcpy(variable_data, data, size);
338 _mesa_post_marshal_hook(ctx);
339 } else {
340 _mesa_glthread_finish(ctx);
341 CALL_BufferSubData(ctx->CurrentServerDispatch,
342 (target, offset, size, data));
343 }
344 }
345
346
347 /* NamedBufferSubData: marshalled asynchronously */
348 struct marshal_cmd_NamedBufferSubData
349 {
350 struct marshal_cmd_base cmd_base;
351 GLuint name;
352 GLintptr offset;
353 GLsizei size;
354 /* Next size bytes are GLubyte data[size] */
355 };
356
357 void
358 _mesa_unmarshal_NamedBufferSubData(struct gl_context *ctx,
359 const struct marshal_cmd_NamedBufferSubData *cmd)
360 {
361 const GLuint name = cmd->name;
362 const GLintptr offset = cmd->offset;
363 const GLsizei size = cmd->size;
364 const void *data = (const void *) (cmd + 1);
365
366 CALL_NamedBufferSubData(ctx->CurrentServerDispatch,
367 (name, offset, size, data));
368 }
369
370 void GLAPIENTRY
371 _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset,
372 GLsizeiptr size, const GLvoid * data)
373 {
374 GET_CURRENT_CONTEXT(ctx);
375 size_t cmd_size = sizeof(struct marshal_cmd_NamedBufferSubData) + size;
376
377 debug_print_marshal("NamedBufferSubData");
378 if (unlikely(size < 0)) {
379 _mesa_glthread_finish(ctx);
380 _mesa_error(ctx, GL_INVALID_VALUE, "NamedBufferSubData(size < 0)");
381 return;
382 }
383
384 if (buffer > 0 && cmd_size <= MARSHAL_MAX_CMD_SIZE) {
385 struct marshal_cmd_NamedBufferSubData *cmd =
386 _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_NamedBufferSubData,
387 cmd_size);
388 cmd->name = buffer;
389 cmd->offset = offset;
390 cmd->size = size;
391 char *variable_data = (char *) (cmd + 1);
392 memcpy(variable_data, data, size);
393 _mesa_post_marshal_hook(ctx);
394 } else {
395 _mesa_glthread_finish(ctx);
396 CALL_NamedBufferSubData(ctx->CurrentServerDispatch,
397 (buffer, offset, size, data));
398 }
399 }