mesa: add _mesa_InternalBind{ElementBuffer,VertexBuffers} for glthread
[mesa.git] / src / mesa / main / glthread.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 #ifndef _GLTHREAD_H
25 #define _GLTHREAD_H
26
27 /* The size of one batch and the maximum size of one call.
28 *
29 * This should be as low as possible, so that:
30 * - multiple synchronizations within a frame don't slow us down much
31 * - a smaller number of calls per frame can still get decent parallelism
32 * - the memory footprint of the queue is low, and with that comes a lower
33 * chance of experiencing CPU cache thrashing
34 * but it should be high enough so that u_queue overhead remains negligible.
35 */
36 #define MARSHAL_MAX_CMD_SIZE (8 * 1024)
37
38 /* The number of batch slots in memory.
39 *
40 * One batch is being executed, one batch is being filled, the rest are
41 * waiting batches. There must be at least 1 slot for a waiting batch,
42 * so the minimum number of batches is 3.
43 */
44 #define MARSHAL_MAX_BATCHES 8
45
46 #include <inttypes.h>
47 #include <stdbool.h>
48 #include "util/u_queue.h"
49 #include "GL/gl.h"
50 #include "compiler/shader_enums.h"
51
52 struct gl_context;
53 struct _mesa_HashTable;
54
55 struct glthread_attrib_binding {
56 struct gl_buffer_object *buffer; /**< where non-VBO data was uploaded */
57 int offset; /**< offset to uploaded non-VBO data */
58 const void *original_pointer; /**< restore this pointer after the draw */
59 };
60
61 struct glthread_vao {
62 GLuint Name;
63 GLuint CurrentElementBufferName;
64 GLbitfield Enabled;
65 GLbitfield UserPointerMask;
66 };
67
68 /** A single batch of commands queued up for execution. */
69 struct glthread_batch
70 {
71 /** Batch fence for waiting for the execution to finish. */
72 struct util_queue_fence fence;
73
74 /** The worker thread will access the context with this. */
75 struct gl_context *ctx;
76
77 /** Amount of data used by batch commands, in bytes. */
78 int used;
79
80 /** Data contained in the command buffer. */
81 #ifdef _MSC_VER
82 __declspec(align(8))
83 #else
84 __attribute__((aligned(8)))
85 #endif
86 uint8_t buffer[MARSHAL_MAX_CMD_SIZE];
87 };
88
89 struct glthread_state
90 {
91 /** Multithreaded queue. */
92 struct util_queue queue;
93
94 /** This is sent to the driver for framebuffer overlay / HUD. */
95 struct util_queue_monitoring stats;
96
97 /** Whether GLThread is enabled. */
98 bool enabled;
99
100 /** The ring of batches in memory. */
101 struct glthread_batch batches[MARSHAL_MAX_BATCHES];
102
103 /** Pointer to the batch currently being filled. */
104 struct glthread_batch *next_batch;
105
106 /** Index of the last submitted batch. */
107 unsigned last;
108
109 /** Index of the batch being filled and about to be submitted. */
110 unsigned next;
111
112 /** Vertex Array objects tracked by glthread independently of Mesa. */
113 struct _mesa_HashTable *VAOs;
114 struct glthread_vao *CurrentVAO;
115 struct glthread_vao *LastLookedUpVAO;
116 struct glthread_vao DefaultVAO;
117 int ClientActiveTexture;
118
119 /** Currently-bound buffer object IDs. */
120 GLuint CurrentArrayBufferName;
121 GLuint CurrentDrawIndirectBufferName;
122 };
123
124 void _mesa_glthread_init(struct gl_context *ctx);
125 void _mesa_glthread_destroy(struct gl_context *ctx);
126
127 void _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func);
128 void _mesa_glthread_disable(struct gl_context *ctx, const char *func);
129 void _mesa_glthread_flush_batch(struct gl_context *ctx);
130 void _mesa_glthread_finish(struct gl_context *ctx);
131 void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
132
133 void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target,
134 GLuint buffer);
135 void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
136 const GLuint *buffers);
137
138 void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
139 void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
140 GLsizei n, const GLuint *ids);
141 void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
142 GLsizei n, GLuint *arrays);
143 void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
144 gl_vert_attrib attrib, bool enable);
145 void _mesa_glthread_AttribPointer(struct gl_context *ctx,
146 gl_vert_attrib attrib);
147
148 #endif /* _GLTHREAD_H*/