glthread: track instance divisor changes
[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 gl_buffer_object;
54 struct _mesa_HashTable;
55
56 struct glthread_attrib_binding {
57 struct gl_buffer_object *buffer; /**< where non-VBO data was uploaded */
58 int offset; /**< offset to uploaded non-VBO data */
59 const void *original_pointer; /**< restore this pointer after the draw */
60 };
61
62 struct glthread_vao {
63 GLuint Name;
64 GLuint CurrentElementBufferName;
65 GLbitfield Enabled;
66 GLbitfield UserPointerMask;
67 GLbitfield NonZeroDivisorMask;
68
69 struct {
70 GLuint ElementSize;
71 GLsizei Stride;
72 GLuint Divisor;
73 const void *Pointer;
74 } Attrib[VERT_ATTRIB_MAX];
75 };
76
77 /** A single batch of commands queued up for execution. */
78 struct glthread_batch
79 {
80 /** Batch fence for waiting for the execution to finish. */
81 struct util_queue_fence fence;
82
83 /** The worker thread will access the context with this. */
84 struct gl_context *ctx;
85
86 /** Amount of data used by batch commands, in bytes. */
87 int used;
88
89 /** Data contained in the command buffer. */
90 #ifdef _MSC_VER
91 __declspec(align(8))
92 #else
93 __attribute__((aligned(8)))
94 #endif
95 uint8_t buffer[MARSHAL_MAX_CMD_SIZE];
96 };
97
98 struct glthread_state
99 {
100 /** Multithreaded queue. */
101 struct util_queue queue;
102
103 /** This is sent to the driver for framebuffer overlay / HUD. */
104 struct util_queue_monitoring stats;
105
106 /** Whether GLThread is enabled. */
107 bool enabled;
108
109 /** The ring of batches in memory. */
110 struct glthread_batch batches[MARSHAL_MAX_BATCHES];
111
112 /** Pointer to the batch currently being filled. */
113 struct glthread_batch *next_batch;
114
115 /** Index of the last submitted batch. */
116 unsigned last;
117
118 /** Index of the batch being filled and about to be submitted. */
119 unsigned next;
120
121 /** Upload buffer. */
122 struct gl_buffer_object *upload_buffer;
123 uint8_t *upload_ptr;
124 unsigned upload_offset;
125 int upload_buffer_private_refcount;
126
127 /** Caps. */
128 GLboolean SupportsBufferUploads;
129
130 /** Vertex Array objects tracked by glthread independently of Mesa. */
131 struct _mesa_HashTable *VAOs;
132 struct glthread_vao *CurrentVAO;
133 struct glthread_vao *LastLookedUpVAO;
134 struct glthread_vao DefaultVAO;
135 int ClientActiveTexture;
136
137 /** Currently-bound buffer object IDs. */
138 GLuint CurrentArrayBufferName;
139 GLuint CurrentDrawIndirectBufferName;
140 };
141
142 void _mesa_glthread_init(struct gl_context *ctx);
143 void _mesa_glthread_destroy(struct gl_context *ctx);
144
145 void _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func);
146 void _mesa_glthread_disable(struct gl_context *ctx, const char *func);
147 void _mesa_glthread_flush_batch(struct gl_context *ctx);
148 void _mesa_glthread_finish(struct gl_context *ctx);
149 void _mesa_glthread_finish_before(struct gl_context *ctx, const char *func);
150 void _mesa_glthread_upload(struct gl_context *ctx, const void *data,
151 GLsizeiptr size, unsigned *out_offset,
152 struct gl_buffer_object **out_buffer,
153 uint8_t **out_ptr);
154
155 void _mesa_glthread_BindBuffer(struct gl_context *ctx, GLenum target,
156 GLuint buffer);
157 void _mesa_glthread_DeleteBuffers(struct gl_context *ctx, GLsizei n,
158 const GLuint *buffers);
159
160 void _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id);
161 void _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
162 GLsizei n, const GLuint *ids);
163 void _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
164 GLsizei n, GLuint *arrays);
165 void _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
166 gl_vert_attrib attrib, bool enable);
167 void _mesa_glthread_AttribDivisor(struct gl_context *ctx, const GLuint *vaobj,
168 gl_vert_attrib attrib, GLuint divisor);
169 void _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
170 GLint size, GLenum type, GLsizei stride,
171 const void *pointer);
172 void _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vao,
173 GLuint buffer, gl_vert_attrib attrib,
174 GLint size, GLenum type, GLsizei stride,
175 GLintptr offset);
176
177 #endif /* _GLTHREAD_H*/