fc69f2704b8ae1f149e3235622d239a7e41eecdc
[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 previously-bound user vertex
78 * array data in draw calls (deprecated and removed in GL core), we just
79 * disable threading at the point where the user sets a user vertex array.
80 */
81 static inline bool
82 _mesa_glthread_is_non_vbo_vertex_attrib_pointer(const struct gl_context *ctx)
83 {
84 struct glthread_state *glthread = ctx->GLThread;
85
86 return ctx->API != API_OPENGL_CORE && !glthread->vertex_array_is_vbo;
87 }
88
89 /**
90 * Instead of conditionally handling marshaling immediate index data in draw
91 * calls (deprecated and removed in GL core), we just disable threading.
92 */
93 static inline bool
94 _mesa_glthread_is_non_vbo_draw_elements(const struct gl_context *ctx)
95 {
96 struct glthread_state *glthread = ctx->GLThread;
97
98 return ctx->API != API_OPENGL_CORE && !glthread->element_array_is_vbo;
99 }
100
101 static inline bool
102 _mesa_glthread_is_non_vbo_draw_arrays_indirect(const struct gl_context *ctx)
103 {
104 struct glthread_state *glthread = ctx->GLThread;
105
106 return ctx->API != API_OPENGL_CORE &&
107 !glthread->draw_indirect_buffer_is_vbo;
108 }
109
110 static inline bool
111 _mesa_glthread_is_non_vbo_draw_elements_indirect(const struct gl_context *ctx)
112 {
113 struct glthread_state *glthread = ctx->GLThread;
114
115 return ctx->API != API_OPENGL_CORE &&
116 (!glthread->draw_indirect_buffer_is_vbo ||
117 !glthread->element_array_is_vbo);
118 }
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 struct _glapi_table *
152 _mesa_create_marshal_table(const struct gl_context *ctx);
153
154
155 /**
156 * Checks whether we're on a compat context for code-generated
157 * glBindVertexArray().
158 *
159 * In order to decide whether a draw call uses only VBOs for vertex and index
160 * buffers, we track the current vertex and index buffer bindings by
161 * glBindBuffer(). However, the index buffer binding is stored in the vertex
162 * array as opposed to the context. If we were to accurately track whether
163 * the index buffer was a user pointer ot not, we'd have to track it per
164 * vertex array, which would mean synchronizing with the client thread and
165 * looking into the hash table to find the actual vertex array object. That's
166 * more tracking than we'd like to do in the main thread, if possible.
167 *
168 * Instead, just punt for now and disable threading on apps using vertex
169 * arrays and compat contexts. Apps using vertex arrays can probably use a
170 * core context.
171 */
172 static inline bool
173 _mesa_glthread_is_compat_bind_vertex_array(const struct gl_context *ctx)
174 {
175 return ctx->API != API_OPENGL_CORE;
176 }
177
178 struct marshal_cmd_Enable;
179 struct marshal_cmd_ShaderSource;
180 struct marshal_cmd_Flush;
181 struct marshal_cmd_BindBuffer;
182 struct marshal_cmd_BufferData;
183 struct marshal_cmd_BufferSubData;
184 struct marshal_cmd_NamedBufferData;
185 struct marshal_cmd_NamedBufferSubData;
186
187 void
188 _mesa_unmarshal_Enable(struct gl_context *ctx,
189 const struct marshal_cmd_Enable *cmd);
190
191 void GLAPIENTRY
192 _mesa_marshal_Enable(GLenum cap);
193
194 void GLAPIENTRY
195 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
196 const GLchar * const *string, const GLint *length);
197
198 void
199 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
200 const struct marshal_cmd_ShaderSource *cmd);
201
202 void GLAPIENTRY
203 _mesa_marshal_Flush(void);
204
205 void
206 _mesa_unmarshal_Flush(struct gl_context *ctx,
207 const struct marshal_cmd_Flush *cmd);
208
209 void GLAPIENTRY
210 _mesa_marshal_BindBuffer(GLenum target, GLuint buffer);
211
212 void
213 _mesa_unmarshal_BindBuffer(struct gl_context *ctx,
214 const struct marshal_cmd_BindBuffer *cmd);
215
216 void
217 _mesa_unmarshal_BufferData(struct gl_context *ctx,
218 const struct marshal_cmd_BufferData *cmd);
219
220 void GLAPIENTRY
221 _mesa_marshal_BufferData(GLenum target, GLsizeiptr size, const GLvoid * data,
222 GLenum usage);
223
224 void
225 _mesa_unmarshal_BufferSubData(struct gl_context *ctx,
226 const struct marshal_cmd_BufferSubData *cmd);
227
228 void GLAPIENTRY
229 _mesa_marshal_BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
230 const GLvoid * data);
231
232 void
233 _mesa_unmarshal_NamedBufferData(struct gl_context *ctx,
234 const struct marshal_cmd_NamedBufferData *cmd);
235
236 void GLAPIENTRY
237 _mesa_marshal_NamedBufferData(GLuint buffer, GLsizeiptr size,
238 const GLvoid * data, GLenum usage);
239
240 void
241 _mesa_unmarshal_NamedBufferSubData(struct gl_context *ctx,
242 const struct marshal_cmd_NamedBufferSubData *cmd);
243
244 void GLAPIENTRY
245 _mesa_marshal_NamedBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size,
246 const GLvoid * data);
247
248 static inline unsigned
249 _mesa_buffer_enum_to_count(GLenum buffer)
250 {
251 switch (buffer) {
252 case GL_COLOR:
253 return 4;
254 case GL_DEPTH_STENCIL:
255 return 2;
256 case GL_STENCIL:
257 case GL_DEPTH:
258 return 1;
259 default:
260 return 0;
261 }
262 }
263
264 static inline unsigned
265 _mesa_tex_param_enum_to_count(GLenum pname)
266 {
267 switch (pname) {
268 case GL_TEXTURE_MIN_FILTER:
269 case GL_TEXTURE_MAG_FILTER:
270 case GL_TEXTURE_WRAP_S:
271 case GL_TEXTURE_WRAP_T:
272 case GL_TEXTURE_WRAP_R:
273 case GL_TEXTURE_BASE_LEVEL:
274 case GL_TEXTURE_MAX_LEVEL:
275 case GL_GENERATE_MIPMAP_SGIS:
276 case GL_TEXTURE_COMPARE_MODE_ARB:
277 case GL_TEXTURE_COMPARE_FUNC_ARB:
278 case GL_DEPTH_TEXTURE_MODE_ARB:
279 case GL_DEPTH_STENCIL_TEXTURE_MODE:
280 case GL_TEXTURE_SRGB_DECODE_EXT:
281 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
282 case GL_TEXTURE_SWIZZLE_R:
283 case GL_TEXTURE_SWIZZLE_G:
284 case GL_TEXTURE_SWIZZLE_B:
285 case GL_TEXTURE_SWIZZLE_A:
286 case GL_TEXTURE_MIN_LOD:
287 case GL_TEXTURE_MAX_LOD:
288 case GL_TEXTURE_PRIORITY:
289 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
290 case GL_TEXTURE_LOD_BIAS:
291 case GL_TEXTURE_TILING_EXT:
292 return 1;
293 case GL_TEXTURE_CROP_RECT_OES:
294 case GL_TEXTURE_SWIZZLE_RGBA:
295 case GL_TEXTURE_BORDER_COLOR:
296 return 4;
297 default:
298 return 0;
299 }
300 }
301
302 static inline unsigned
303 _mesa_fog_enum_to_count(GLenum pname)
304 {
305 switch (pname) {
306 case GL_FOG_MODE:
307 case GL_FOG_DENSITY:
308 case GL_FOG_START:
309 case GL_FOG_END:
310 case GL_FOG_INDEX:
311 case GL_FOG_COORDINATE_SOURCE_EXT:
312 case GL_FOG_DISTANCE_MODE_NV:
313 return 1;
314 case GL_FOG_COLOR:
315 return 4;
316 default:
317 return 0;
318 }
319 }
320
321 static inline unsigned
322 _mesa_light_enum_to_count(GLenum pname)
323 {
324 switch (pname) {
325 case GL_AMBIENT:
326 case GL_DIFFUSE:
327 case GL_SPECULAR:
328 case GL_POSITION:
329 return 4;
330 case GL_SPOT_DIRECTION:
331 return 3;
332 case GL_SPOT_EXPONENT:
333 case GL_SPOT_CUTOFF:
334 case GL_CONSTANT_ATTENUATION:
335 case GL_LINEAR_ATTENUATION:
336 case GL_QUADRATIC_ATTENUATION:
337 return 1;
338 default:
339 return 0;
340 }
341 }
342
343 static inline unsigned
344 _mesa_light_model_enum_to_count(GLenum pname)
345 {
346 switch (pname) {
347 case GL_LIGHT_MODEL_AMBIENT:
348 return 4;
349 case GL_LIGHT_MODEL_LOCAL_VIEWER:
350 case GL_LIGHT_MODEL_TWO_SIDE:
351 case GL_LIGHT_MODEL_COLOR_CONTROL:
352 return 1;
353 default:
354 return 0;
355 }
356 }
357
358 static inline unsigned
359 _mesa_texenv_enum_to_count(GLenum pname)
360 {
361 switch (pname) {
362 case GL_TEXTURE_ENV_MODE:
363 case GL_COMBINE_RGB:
364 case GL_COMBINE_ALPHA:
365 case GL_SOURCE0_RGB:
366 case GL_SOURCE1_RGB:
367 case GL_SOURCE2_RGB:
368 case GL_SOURCE3_RGB_NV:
369 case GL_SOURCE0_ALPHA:
370 case GL_SOURCE1_ALPHA:
371 case GL_SOURCE2_ALPHA:
372 case GL_SOURCE3_ALPHA_NV:
373 case GL_OPERAND0_RGB:
374 case GL_OPERAND1_RGB:
375 case GL_OPERAND2_RGB:
376 case GL_OPERAND3_RGB_NV:
377 case GL_OPERAND0_ALPHA:
378 case GL_OPERAND1_ALPHA:
379 case GL_OPERAND2_ALPHA:
380 case GL_OPERAND3_ALPHA_NV:
381 case GL_RGB_SCALE:
382 case GL_ALPHA_SCALE:
383 case GL_TEXTURE_LOD_BIAS_EXT:
384 case GL_COORD_REPLACE_NV:
385 return 1;
386 case GL_TEXTURE_ENV_COLOR:
387 return 4;
388 default:
389 return 0;
390 }
391 }
392
393 static inline unsigned
394 _mesa_texgen_enum_to_count(GLenum pname)
395 {
396 switch (pname) {
397 case GL_TEXTURE_GEN_MODE:
398 return 1;
399 case GL_OBJECT_PLANE:
400 case GL_EYE_PLANE:
401 return 4;
402 default:
403 return 0;
404 }
405 }
406
407 #endif /* MARSHAL_H */