mesa: Track the current vertex/element array buffers for glthread.
[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
36 struct marshal_cmd_base
37 {
38 /**
39 * Type of command. See enum marshal_dispatch_cmd_id.
40 */
41 uint16_t cmd_id;
42
43 /**
44 * Size of command, in multiples of 4 bytes, including cmd_base.
45 */
46 uint16_t cmd_size;
47 };
48
49
50 static inline void *
51 _mesa_glthread_allocate_command(struct gl_context *ctx,
52 uint16_t cmd_id,
53 size_t size)
54 {
55 struct glthread_state *glthread = ctx->GLThread;
56 struct marshal_cmd_base *cmd_base;
57
58 if (unlikely(glthread->batch->used + size > MARSHAL_MAX_CMD_SIZE))
59 _mesa_glthread_flush_batch(ctx);
60
61 cmd_base = (struct marshal_cmd_base *)
62 &glthread->batch->buffer[glthread->batch->used];
63 glthread->batch->used += size;
64 cmd_base->cmd_id = cmd_id;
65 cmd_base->cmd_size = size;
66 return cmd_base;
67 }
68
69 #define DEBUG_MARSHAL_PRINT_CALLS 0
70
71 static inline void
72 debug_print_sync(const char *func)
73 {
74 #if DEBUG_MARSHAL_PRINT_CALLS
75 printf("sync: %s\n", func);
76 #endif
77 }
78
79 static inline void
80 debug_print_marshal(const char *func)
81 {
82 #if DEBUG_MARSHAL_PRINT_CALLS
83 printf("marshal: %s\n", func);
84 #endif
85 }
86
87 static inline void
88 debug_print_unmarshal(const char *func)
89 {
90 #if DEBUG_MARSHAL_PRINT_CALLS
91 printf("unmarshal: %s\n", func);
92 #endif
93 }
94
95 struct _glapi_table *
96 _mesa_create_marshal_table(const struct gl_context *ctx);
97
98 size_t
99 _mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, const void *cmd);
100
101 static inline void
102 _mesa_post_marshal_hook(struct gl_context *ctx)
103 {
104 /* This can be enabled for debugging whether a failure is a synchronization
105 * problem between the main thread and the worker thread, or a failure in
106 * how we actually marshal.
107 */
108 if (false)
109 _mesa_glthread_finish(ctx);
110 }
111
112
113 /**
114 * Checks whether we're on a compat context for code-generated
115 * glBindVertexArray().
116 *
117 * In order to decide whether a draw call uses only VBOs for vertex and index
118 * buffers, we track the current vertex and index buffer bindings by
119 * glBindBuffer(). However, the index buffer binding is stored in the vertex
120 * array as opposed to the context. If we were to accurately track whether
121 * the index buffer was a user pointer ot not, we'd have to track it per
122 * vertex array, which would mean synchronizing with the client thread and
123 * looking into the hash table to find the actual vertex array object. That's
124 * more tracking than we'd like to do in the main thread, if possible.
125 *
126 * Instead, just punt for now and disable threading on apps using vertex
127 * arrays and compat contexts. Apps using vertex arrays can probably use a
128 * core context.
129 */
130 static inline bool
131 _mesa_glthread_is_compat_bind_vertex_array(const struct gl_context *ctx)
132 {
133 return ctx->API != API_OPENGL_CORE;
134 }
135
136 /**
137 * Instead of conditionally handling marshaling previously-bound user vertex
138 * array data in draw calls (deprecated and removed in GL core), we just
139 * disable threading at the point where the user sets a user vertex array.
140 */
141 static inline bool
142 _mesa_glthread_is_non_vbo_vertex_attrib_pointer(const struct gl_context *ctx)
143 {
144 struct glthread_state *glthread = ctx->GLThread;
145
146 return ctx->API != API_OPENGL_CORE && !glthread->vertex_array_is_vbo;
147 }
148
149 /**
150 * Instead of conditionally handling marshaling immediate index data in draw
151 * calls (deprecated and removed in GL core), we just disable threading.
152 */
153 static inline bool
154 _mesa_glthread_is_non_vbo_draw_elements(const struct gl_context *ctx)
155 {
156 struct glthread_state *glthread = ctx->GLThread;
157
158 return ctx->API != API_OPENGL_CORE && !glthread->element_array_is_vbo;
159 }
160
161 struct marshal_cmd_ShaderSource;
162 struct marshal_cmd_Flush;
163 struct marshal_cmd_BindBuffer;
164
165 void GLAPIENTRY
166 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
167 const GLchar * const *string, const GLint *length);
168
169 void
170 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
171 const struct marshal_cmd_ShaderSource *cmd);
172
173 void GLAPIENTRY
174 _mesa_marshal_Flush(void);
175
176 void
177 _mesa_unmarshal_Flush(struct gl_context *ctx,
178 const struct marshal_cmd_Flush *cmd);
179
180 void GLAPIENTRY
181 _mesa_marshal_BindBuffer(GLenum target, GLuint buffer);
182
183 void
184 _mesa_unmarshal_BindBuffer(struct gl_context *ctx,
185 const struct marshal_cmd_BindBuffer *cmd);
186
187 #endif /* MARSHAL_H */