acc6a02b920b6e14662429a489d6fa895b4d4fce
[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 #include "marshal_generated.h"
37
38 struct marshal_cmd_base
39 {
40 /**
41 * Type of command. See enum marshal_dispatch_cmd_id.
42 */
43 uint16_t cmd_id;
44
45 /**
46 * Size of command, in multiples of 4 bytes, including cmd_base.
47 */
48 uint16_t cmd_size;
49 };
50
51 typedef void (*_mesa_unmarshal_func)(struct gl_context *ctx, const void *cmd);
52 extern const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD];
53
54 static inline void *
55 _mesa_glthread_allocate_command(struct gl_context *ctx,
56 uint16_t cmd_id,
57 int size)
58 {
59 struct glthread_state *glthread = ctx->GLThread;
60 struct glthread_batch *next = &glthread->batches[glthread->next];
61 struct marshal_cmd_base *cmd_base;
62 const int aligned_size = ALIGN(size, 8);
63
64 if (unlikely(next->used + size > MARSHAL_MAX_CMD_SIZE)) {
65 _mesa_glthread_flush_batch(ctx);
66 next = &glthread->batches[glthread->next];
67 }
68
69 cmd_base = (struct marshal_cmd_base *)&next->buffer[next->used];
70 next->used += aligned_size;
71 cmd_base->cmd_id = cmd_id;
72 cmd_base->cmd_size = aligned_size;
73 return cmd_base;
74 }
75
76 /**
77 * Instead of conditionally handling marshaling immediate index data in draw
78 * calls (deprecated and removed in GL core), we just disable threading.
79 */
80 static inline bool
81 _mesa_glthread_is_non_vbo_draw_elements(const struct gl_context *ctx)
82 {
83 struct glthread_state *glthread = ctx->GLThread;
84
85 return ctx->API != API_OPENGL_CORE &&
86 (glthread->CurrentVAO->IndexBufferIsUserPointer ||
87 glthread->CurrentVAO->HasUserPointer);
88 }
89
90 static inline bool
91 _mesa_glthread_is_non_vbo_draw_arrays(const struct gl_context *ctx)
92 {
93 struct glthread_state *glthread = ctx->GLThread;
94
95 return ctx->API != API_OPENGL_CORE && glthread->CurrentVAO->HasUserPointer;
96 }
97
98 static inline bool
99 _mesa_glthread_is_non_vbo_draw_arrays_indirect(const struct gl_context *ctx)
100 {
101 struct glthread_state *glthread = ctx->GLThread;
102
103 return ctx->API != API_OPENGL_CORE &&
104 (!glthread->draw_indirect_buffer_is_vbo ||
105 glthread->CurrentVAO->HasUserPointer );
106 }
107
108 static inline bool
109 _mesa_glthread_is_non_vbo_draw_elements_indirect(const struct gl_context *ctx)
110 {
111 struct glthread_state *glthread = ctx->GLThread;
112
113 return ctx->API != API_OPENGL_CORE &&
114 (!glthread->draw_indirect_buffer_is_vbo ||
115 glthread->CurrentVAO->IndexBufferIsUserPointer ||
116 glthread->CurrentVAO->HasUserPointer);
117 }
118
119 #define DEBUG_MARSHAL_PRINT_CALLS 0
120
121 /**
122 * This is printed when we have fallen back to a sync. This can happen when
123 * MARSHAL_MAX_CMD_SIZE is exceeded.
124 */
125 static inline void
126 debug_print_sync_fallback(const char *func)
127 {
128 #if DEBUG_MARSHAL_PRINT_CALLS
129 printf("fallback to sync: %s\n", func);
130 #endif
131 }
132
133
134 static inline void
135 debug_print_sync(const char *func)
136 {
137 #if DEBUG_MARSHAL_PRINT_CALLS
138 printf("sync: %s\n", func);
139 #endif
140 }
141
142 static inline void
143 debug_print_marshal(const char *func)
144 {
145 #if DEBUG_MARSHAL_PRINT_CALLS
146 printf("marshal: %s\n", func);
147 #endif
148 }
149
150 struct _glapi_table *
151 _mesa_create_marshal_table(const struct gl_context *ctx);
152
153 struct marshal_cmd_ShaderSource;
154 struct marshal_cmd_BufferData;
155 struct marshal_cmd_BufferSubData;
156
157 void GLAPIENTRY
158 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
159 const GLchar * const *string, const GLint *length);
160
161 void
162 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
163 const struct marshal_cmd_ShaderSource *cmd);
164
165 void
166 _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target, GLuint buffer);
167
168 void
169 _mesa_unmarshal_BufferData(struct gl_context *ctx,
170 const struct marshal_cmd_BufferData *cmd);
171
172 void
173 _mesa_unmarshal_NamedBufferData(struct gl_context *ctx,
174 const struct marshal_cmd_BufferData *cmd);
175
176 void
177 _mesa_unmarshal_NamedBufferDataEXT(struct gl_context *ctx,
178 const struct marshal_cmd_BufferData *cmd);
179
180 void GLAPIENTRY
181 _mesa_marshal_BufferData(GLenum target, GLsizeiptr size, const GLvoid * data,
182 GLenum usage);
183
184 void GLAPIENTRY
185 _mesa_marshal_NamedBufferData(GLuint buffer, GLsizeiptr size,
186 const GLvoid * data, GLenum usage);
187
188 void GLAPIENTRY
189 _mesa_marshal_NamedBufferDataEXT(GLuint buffer, GLsizeiptr size,
190 const GLvoid *data, GLenum usage);
191
192 void
193 _mesa_unmarshal_BufferSubData(struct gl_context *ctx,
194 const struct marshal_cmd_BufferSubData *cmd);
195
196 void
197 _mesa_unmarshal_NamedBufferSubData(struct gl_context *ctx,
198 const struct marshal_cmd_BufferSubData *cmd);
199
200 void
201 _mesa_unmarshal_NamedBufferSubDataEXT(struct gl_context *ctx,
202 const struct marshal_cmd_BufferSubData *cmd);
203
204 void GLAPIENTRY
205 _mesa_marshal_BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
206 const GLvoid * data);
207
208 void GLAPIENTRY
209 _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size,
210 const GLvoid * data);
211
212 void GLAPIENTRY
213 _mesa_marshal_NamedBufferSubDataEXT(GLuint buffer, GLintptr offset,
214 GLsizeiptr size, const GLvoid * data);
215
216 static inline unsigned
217 _mesa_buffer_enum_to_count(GLenum buffer)
218 {
219 switch (buffer) {
220 case GL_COLOR:
221 return 4;
222 case GL_DEPTH_STENCIL:
223 return 2;
224 case GL_STENCIL:
225 case GL_DEPTH:
226 return 1;
227 default:
228 return 0;
229 }
230 }
231
232 static inline unsigned
233 _mesa_tex_param_enum_to_count(GLenum pname)
234 {
235 switch (pname) {
236 case GL_TEXTURE_MIN_FILTER:
237 case GL_TEXTURE_MAG_FILTER:
238 case GL_TEXTURE_WRAP_S:
239 case GL_TEXTURE_WRAP_T:
240 case GL_TEXTURE_WRAP_R:
241 case GL_TEXTURE_BASE_LEVEL:
242 case GL_TEXTURE_MAX_LEVEL:
243 case GL_GENERATE_MIPMAP_SGIS:
244 case GL_TEXTURE_COMPARE_MODE_ARB:
245 case GL_TEXTURE_COMPARE_FUNC_ARB:
246 case GL_DEPTH_TEXTURE_MODE_ARB:
247 case GL_DEPTH_STENCIL_TEXTURE_MODE:
248 case GL_TEXTURE_SRGB_DECODE_EXT:
249 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
250 case GL_TEXTURE_SWIZZLE_R:
251 case GL_TEXTURE_SWIZZLE_G:
252 case GL_TEXTURE_SWIZZLE_B:
253 case GL_TEXTURE_SWIZZLE_A:
254 case GL_TEXTURE_MIN_LOD:
255 case GL_TEXTURE_MAX_LOD:
256 case GL_TEXTURE_PRIORITY:
257 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
258 case GL_TEXTURE_LOD_BIAS:
259 case GL_TEXTURE_TILING_EXT:
260 return 1;
261 case GL_TEXTURE_CROP_RECT_OES:
262 case GL_TEXTURE_SWIZZLE_RGBA:
263 case GL_TEXTURE_BORDER_COLOR:
264 return 4;
265 default:
266 return 0;
267 }
268 }
269
270 static inline unsigned
271 _mesa_fog_enum_to_count(GLenum pname)
272 {
273 switch (pname) {
274 case GL_FOG_MODE:
275 case GL_FOG_DENSITY:
276 case GL_FOG_START:
277 case GL_FOG_END:
278 case GL_FOG_INDEX:
279 case GL_FOG_COORDINATE_SOURCE_EXT:
280 case GL_FOG_DISTANCE_MODE_NV:
281 return 1;
282 case GL_FOG_COLOR:
283 return 4;
284 default:
285 return 0;
286 }
287 }
288
289 static inline unsigned
290 _mesa_light_enum_to_count(GLenum pname)
291 {
292 switch (pname) {
293 case GL_AMBIENT:
294 case GL_DIFFUSE:
295 case GL_SPECULAR:
296 case GL_POSITION:
297 return 4;
298 case GL_SPOT_DIRECTION:
299 return 3;
300 case GL_SPOT_EXPONENT:
301 case GL_SPOT_CUTOFF:
302 case GL_CONSTANT_ATTENUATION:
303 case GL_LINEAR_ATTENUATION:
304 case GL_QUADRATIC_ATTENUATION:
305 return 1;
306 default:
307 return 0;
308 }
309 }
310
311 static inline unsigned
312 _mesa_light_model_enum_to_count(GLenum pname)
313 {
314 switch (pname) {
315 case GL_LIGHT_MODEL_AMBIENT:
316 return 4;
317 case GL_LIGHT_MODEL_LOCAL_VIEWER:
318 case GL_LIGHT_MODEL_TWO_SIDE:
319 case GL_LIGHT_MODEL_COLOR_CONTROL:
320 return 1;
321 default:
322 return 0;
323 }
324 }
325
326 static inline unsigned
327 _mesa_texenv_enum_to_count(GLenum pname)
328 {
329 switch (pname) {
330 case GL_TEXTURE_ENV_MODE:
331 case GL_COMBINE_RGB:
332 case GL_COMBINE_ALPHA:
333 case GL_SOURCE0_RGB:
334 case GL_SOURCE1_RGB:
335 case GL_SOURCE2_RGB:
336 case GL_SOURCE3_RGB_NV:
337 case GL_SOURCE0_ALPHA:
338 case GL_SOURCE1_ALPHA:
339 case GL_SOURCE2_ALPHA:
340 case GL_SOURCE3_ALPHA_NV:
341 case GL_OPERAND0_RGB:
342 case GL_OPERAND1_RGB:
343 case GL_OPERAND2_RGB:
344 case GL_OPERAND3_RGB_NV:
345 case GL_OPERAND0_ALPHA:
346 case GL_OPERAND1_ALPHA:
347 case GL_OPERAND2_ALPHA:
348 case GL_OPERAND3_ALPHA_NV:
349 case GL_RGB_SCALE:
350 case GL_ALPHA_SCALE:
351 case GL_TEXTURE_LOD_BIAS_EXT:
352 case GL_COORD_REPLACE_NV:
353 return 1;
354 case GL_TEXTURE_ENV_COLOR:
355 return 4;
356 default:
357 return 0;
358 }
359 }
360
361 static inline unsigned
362 _mesa_texgen_enum_to_count(GLenum pname)
363 {
364 switch (pname) {
365 case GL_TEXTURE_GEN_MODE:
366 return 1;
367 case GL_OBJECT_PLANE:
368 case GL_EYE_PLANE:
369 return 4;
370 default:
371 return 0;
372 }
373 }
374
375 static inline unsigned
376 _mesa_material_enum_to_count(GLenum pname)
377 {
378 switch (pname) {
379 case GL_EMISSION:
380 case GL_AMBIENT:
381 case GL_DIFFUSE:
382 case GL_SPECULAR:
383 case GL_AMBIENT_AND_DIFFUSE:
384 return 4;
385 case GL_COLOR_INDEXES:
386 return 3;
387 case GL_SHININESS:
388 return 1;
389 default:
390 return 0;
391 }
392 }
393
394 static inline unsigned
395 _mesa_point_param_enum_to_count(GLenum pname)
396 {
397 switch (pname) {
398 case GL_DISTANCE_ATTENUATION_EXT:
399 return 3;
400 case GL_POINT_SIZE_MIN_EXT:
401 case GL_POINT_SIZE_MAX_EXT:
402 case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
403 case GL_POINT_SPRITE_R_MODE_NV:
404 case GL_POINT_SPRITE_COORD_ORIGIN:
405 return 1;
406 default:
407 return 0;
408 }
409 }
410
411 static inline unsigned
412 _mesa_calllists_enum_to_count(GLenum type)
413 {
414 switch (type) {
415 case GL_BYTE:
416 case GL_UNSIGNED_BYTE:
417 return 1;
418 case GL_SHORT:
419 case GL_UNSIGNED_SHORT:
420 case GL_2_BYTES:
421 return 2;
422 case GL_3_BYTES:
423 return 3;
424 case GL_INT:
425 case GL_UNSIGNED_INT:
426 case GL_FLOAT:
427 case GL_4_BYTES:
428 return 4;
429 default:
430 return 0;
431 }
432 }
433
434 static inline unsigned
435 _mesa_patch_param_enum_to_count(GLenum pname)
436 {
437 switch (pname) {
438 case GL_PATCH_DEFAULT_OUTER_LEVEL:
439 return 4;
440 case GL_PATCH_DEFAULT_INNER_LEVEL:
441 return 2;
442 default:
443 return 0;
444 }
445 }
446
447 static inline unsigned
448 _mesa_memobj_enum_to_count(GLenum pname)
449 {
450 switch (pname) {
451 case GL_DEDICATED_MEMORY_OBJECT_EXT:
452 return 1;
453 default:
454 return 0;
455 }
456 }
457
458 static inline unsigned
459 _mesa_semaphore_enum_to_count(GLenum pname)
460 {
461 switch (pname) {
462 /* EXT_semaphore and EXT_semaphore_fd define no parameters */
463 default:
464 return 0;
465 }
466 }
467
468 #endif /* MARSHAL_H */