2 * Copyright © 2012 Intel Corporation
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:
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
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
26 * Custom functions for marshalling GL calls from the main thread to a worker
27 * thread when automatic code generation isn't appropriate.
30 #include "main/enums.h"
31 #include "main/macros.h"
34 #include "marshal_generated.h"
38 struct marshal_cmd_Flush
40 struct marshal_cmd_base cmd_base
;
45 _mesa_unmarshal_Flush(struct gl_context
*ctx
,
46 const struct marshal_cmd_Flush
*cmd
)
48 CALL_Flush(ctx
->CurrentServerDispatch
, ());
53 _mesa_marshal_Flush(void)
55 GET_CURRENT_CONTEXT(ctx
);
56 struct marshal_cmd_Flush
*cmd
=
57 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_Flush
,
58 sizeof(struct marshal_cmd_Flush
));
60 _mesa_post_marshal_hook(ctx
);
62 /* Flush() needs to be handled specially. In addition to telling the
63 * background thread to flush, we need to ensure that our own buffer is
64 * submitted to the background thread so that it will complete in a finite
67 _mesa_glthread_flush_batch(ctx
);
71 struct marshal_cmd_ShaderSource
73 struct marshal_cmd_base cmd_base
;
76 /* Followed by GLint length[count], then the contents of all strings,
83 _mesa_unmarshal_ShaderSource(struct gl_context
*ctx
,
84 const struct marshal_cmd_ShaderSource
*cmd
)
86 const GLint
*cmd_length
= (const GLint
*) (cmd
+ 1);
87 const GLchar
*cmd_strings
= (const GLchar
*) (cmd_length
+ cmd
->count
);
88 /* TODO: how to deal with malloc failure? */
89 const GLchar
* *string
= malloc(cmd
->count
* sizeof(const GLchar
*));
92 for (i
= 0; i
< cmd
->count
; ++i
) {
93 string
[i
] = cmd_strings
;
94 cmd_strings
+= cmd_length
[i
];
96 CALL_ShaderSource(ctx
->CurrentServerDispatch
,
97 (cmd
->shader
, cmd
->count
, string
, cmd_length
));
103 measure_ShaderSource_strings(GLsizei count
, const GLchar
* const *string
,
104 const GLint
*length_in
, GLint
*length_out
)
107 size_t total_string_length
= 0;
109 for (i
= 0; i
< count
; ++i
) {
110 if (length_in
== NULL
|| length_in
[i
] < 0) {
112 length_out
[i
] = strlen(string
[i
]);
114 length_out
[i
] = length_in
[i
];
116 total_string_length
+= length_out
[i
];
118 return total_string_length
;
123 _mesa_marshal_ShaderSource(GLuint shader
, GLsizei count
,
124 const GLchar
* const *string
, const GLint
*length
)
126 /* TODO: how to report an error if count < 0? */
128 GET_CURRENT_CONTEXT(ctx
);
129 /* TODO: how to deal with malloc failure? */
130 const size_t fixed_cmd_size
= sizeof(struct marshal_cmd_ShaderSource
);
131 STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource
) % sizeof(GLint
) == 0);
132 size_t length_size
= count
* sizeof(GLint
);
133 GLint
*length_tmp
= malloc(length_size
);
134 size_t total_string_length
=
135 measure_ShaderSource_strings(count
, string
, length
, length_tmp
);
136 size_t total_cmd_size
= fixed_cmd_size
+ length_size
+ total_string_length
;
138 if (total_cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
139 struct marshal_cmd_ShaderSource
*cmd
=
140 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_ShaderSource
,
142 GLint
*cmd_length
= (GLint
*) (cmd
+ 1);
143 GLchar
*cmd_strings
= (GLchar
*) (cmd_length
+ count
);
146 cmd
->shader
= shader
;
148 memcpy(cmd_length
, length_tmp
, length_size
);
149 for (i
= 0; i
< count
; ++i
) {
150 memcpy(cmd_strings
, string
[i
], cmd_length
[i
]);
151 cmd_strings
+= cmd_length
[i
];
153 _mesa_post_marshal_hook(ctx
);
155 _mesa_glthread_finish(ctx
);
156 CALL_ShaderSource(ctx
->CurrentServerDispatch
,
157 (shader
, count
, string
, length_tmp
));
163 /* BindBufferBase: marshalled asynchronously */
164 struct marshal_cmd_BindBufferBase
166 struct marshal_cmd_base cmd_base
;
172 _mesa_unmarshal_BindBufferBase(struct gl_context
*ctx
, const struct marshal_cmd_BindBufferBase
*cmd
)
174 const GLenum target
= cmd
->target
;
175 const GLuint index
= cmd
->index
;
176 const GLuint buffer
= cmd
->buffer
;
177 CALL_BindBufferBase(ctx
->CurrentServerDispatch
, (target
, index
, buffer
));
180 /** Tracks the current bindings for the vertex array and index array buffers.
182 * This is part of what we need to enable glthread on compat-GL contexts that
183 * happen to use VBOs, without also supporting the full tracking of VBO vs
184 * user vertex array bindings per attribute on each vertex array for
185 * determining what to upload at draw call time.
187 * Note that GL core makes it so that a buffer binding with an invalid handle
188 * in the "buffer" parameter will throw an error, and then a
189 * glVertexAttribPointer() that followsmight not end up pointing at a VBO.
190 * However, in GL core the draw call would throw an error as well, so we don't
191 * really care if our tracking is wrong for this case -- we never need to
192 * marshal user data for draw calls, and the unmarshal will just generate an
193 * error or not as appropriate.
195 * For compatibility GL, we do need to accurately know whether the draw call
196 * on the unmarshal side will dereference a user pointer or load data from a
197 * VBO per vertex. That would make it seem like we need to track whether a
198 * "buffer" is valid, so that we can know when an error will be generated
199 * instead of updating the binding. However, compat GL has the ridiculous
200 * feature that if you pass a bad name, it just gens a buffer object for you,
201 * so we escape without having to know if things are valid or not.
204 track_vbo_binding(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
206 struct glthread_state
*glthread
= ctx
->GLThread
;
209 case GL_ARRAY_BUFFER
:
210 glthread
->vertex_array_is_vbo
= (buffer
!= 0);
212 case GL_ELEMENT_ARRAY_BUFFER
:
213 /* The current element array buffer binding is actually tracked in the
214 * vertex array object instead of the context, so this would need to
215 * change on vertex array object updates.
217 glthread
->element_array_is_vbo
= (buffer
!= 0);
223 struct marshal_cmd_BindBuffer
225 struct marshal_cmd_base cmd_base
;
231 * This is just like the code-generated glBindBuffer() support, except that we
232 * call track_vbo_binding().
235 _mesa_unmarshal_BindBuffer(struct gl_context
*ctx
,
236 const struct marshal_cmd_BindBuffer
*cmd
)
238 const GLenum target
= cmd
->target
;
239 const GLuint buffer
= cmd
->buffer
;
240 CALL_BindBuffer(ctx
->CurrentServerDispatch
, (target
, buffer
));
243 _mesa_marshal_BindBuffer(GLenum target
, GLuint buffer
)
245 GET_CURRENT_CONTEXT(ctx
);
246 size_t cmd_size
= sizeof(struct marshal_cmd_BindBuffer
);
247 struct marshal_cmd_BindBuffer
*cmd
;
248 debug_print_marshal("BindBuffer");
250 track_vbo_binding(ctx
, target
, buffer
);
252 if (cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
253 cmd
= _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_BindBuffer
,
255 cmd
->target
= target
;
256 cmd
->buffer
= buffer
;
257 _mesa_post_marshal_hook(ctx
);
259 _mesa_glthread_finish(ctx
);
260 CALL_BindBuffer(ctx
->CurrentServerDispatch
, (target
, buffer
));
264 /* BufferData: marshalled asynchronously */
265 struct marshal_cmd_BufferData
267 struct marshal_cmd_base cmd_base
;
271 bool data_null
; /* If set, no data follows for "data" */
272 /* Next size bytes are GLvoid data[size] */
276 _mesa_unmarshal_BufferData(struct gl_context
*ctx
,
277 const struct marshal_cmd_BufferData
*cmd
)
279 const GLenum target
= cmd
->target
;
280 const GLsizeiptr size
= cmd
->size
;
281 const GLenum usage
= cmd
->usage
;
282 const char *variable_data
= (const char *) (cmd
+ 1);
283 const GLvoid
*data
= (const GLvoid
*) variable_data
;
288 variable_data
+= size
;
290 CALL_BufferData(ctx
->CurrentServerDispatch
, (target
, size
, data
, usage
));
294 _mesa_marshal_BufferData(GLenum target
, GLsizeiptr size
, const GLvoid
* data
,
297 GET_CURRENT_CONTEXT(ctx
);
299 sizeof(struct marshal_cmd_BufferData
) + (data
? size
: 0);
300 debug_print_marshal("BufferData");
302 if (unlikely(size
< 0)) {
303 _mesa_glthread_finish(ctx
);
304 _mesa_error(ctx
, GL_INVALID_VALUE
, "BufferData(size < 0)");
308 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
309 cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
310 struct marshal_cmd_BufferData
*cmd
=
311 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_BufferData
,
314 cmd
->target
= target
;
317 char *variable_data
= (char *) (cmd
+ 1);
318 cmd
->data_null
= !data
;
319 if (!cmd
->data_null
) {
320 memcpy(variable_data
, data
, size
);
321 variable_data
+= size
;
323 _mesa_post_marshal_hook(ctx
);
325 _mesa_glthread_finish(ctx
);
326 CALL_BufferData(ctx
->CurrentServerDispatch
,
327 (target
, size
, data
, usage
));
331 /* BufferSubData: marshalled asynchronously */
332 struct marshal_cmd_BufferSubData
334 struct marshal_cmd_base cmd_base
;
338 /* Next size bytes are GLvoid data[size] */
342 _mesa_unmarshal_BufferSubData(struct gl_context
*ctx
,
343 const struct marshal_cmd_BufferSubData
*cmd
)
345 const GLenum target
= cmd
->target
;
346 const GLintptr offset
= cmd
->offset
;
347 const GLsizeiptr size
= cmd
->size
;
348 const char *variable_data
= (const char *) (cmd
+ 1);
349 const GLvoid
*data
= (const GLvoid
*) variable_data
;
351 variable_data
+= size
;
352 CALL_BufferSubData(ctx
->CurrentServerDispatch
,
353 (target
, offset
, size
, data
));
357 _mesa_marshal_BufferSubData(GLenum target
, GLintptr offset
, GLsizeiptr size
,
360 GET_CURRENT_CONTEXT(ctx
);
361 size_t cmd_size
= sizeof(struct marshal_cmd_BufferSubData
) + size
;
363 debug_print_marshal("BufferSubData");
364 if (unlikely(size
< 0)) {
365 _mesa_glthread_finish(ctx
);
366 _mesa_error(ctx
, GL_INVALID_VALUE
, "BufferSubData(size < 0)");
370 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
371 cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
372 struct marshal_cmd_BufferSubData
*cmd
=
373 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_BufferSubData
,
375 cmd
->target
= target
;
376 cmd
->offset
= offset
;
378 char *variable_data
= (char *) (cmd
+ 1);
379 memcpy(variable_data
, data
, size
);
380 variable_data
+= size
;
381 _mesa_post_marshal_hook(ctx
);
383 _mesa_glthread_finish(ctx
);
384 CALL_BufferSubData(ctx
->CurrentServerDispatch
,
385 (target
, offset
, size
, data
));
389 /* ClearBufferfv: marshalled asynchronously */
390 struct marshal_cmd_ClearBufferfv
392 struct marshal_cmd_base cmd_base
;
398 _mesa_unmarshal_ClearBufferfv(struct gl_context
*ctx
,
399 const struct marshal_cmd_ClearBufferfv
*cmd
)
401 const GLenum buffer
= cmd
->buffer
;
402 const GLint drawbuffer
= cmd
->drawbuffer
;
403 const char *variable_data
= (const char *) (cmd
+ 1);
404 const GLfloat
*value
= (const GLfloat
*) variable_data
;
406 CALL_ClearBufferfv(ctx
->CurrentServerDispatch
,
407 (buffer
, drawbuffer
, value
));
411 _mesa_marshal_ClearBufferfv(GLenum buffer
, GLint drawbuffer
,
412 const GLfloat
*value
)
414 GET_CURRENT_CONTEXT(ctx
);
415 debug_print_marshal("ClearBufferfv");
420 size
= sizeof(GLfloat
);
423 size
= sizeof(GLfloat
) * 4;
426 _mesa_glthread_finish(ctx
);
428 /* Page 498 of the PDF, section '17.4.3.1 Clearing Individual Buffers'
429 * of the OpenGL 4.5 spec states:
431 * "An INVALID_ENUM error is generated by ClearBufferfv and
432 * ClearNamedFramebufferfv if buffer is not COLOR or DEPTH."
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClearBufferfv(buffer=%s)",
435 _mesa_enum_to_string(buffer
));
439 size_t cmd_size
= sizeof(struct marshal_cmd_ClearBufferfv
) + size
;
440 if (cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
441 struct marshal_cmd_ClearBufferfv
*cmd
=
442 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_ClearBufferfv
,
444 cmd
->buffer
= buffer
;
445 cmd
->drawbuffer
= drawbuffer
;
446 GLfloat
*variable_data
= (GLfloat
*) (cmd
+ 1);
447 if (buffer
== GL_COLOR
)
448 COPY_4V(variable_data
, value
);
450 *variable_data
= *value
;
452 _mesa_post_marshal_hook(ctx
);
454 debug_print_sync("ClearBufferfv");
455 _mesa_glthread_finish(ctx
);
456 CALL_ClearBufferfv(ctx
->CurrentServerDispatch
,
457 (buffer
, drawbuffer
, value
));