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