glthread: track pointers and strides for Pointer & EXT_dsa attrib functions
[mesa.git] / src / mesa / main / glthread_varray.c
1 /*
2 * Copyright © 2020 Advanced Micro Devices, Inc.
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 /* This implements vertex array state tracking for glthread. It's separate
25 * from the rest of Mesa. Only minimum functionality is implemented here
26 * to serve glthread.
27 */
28
29 #include "main/glthread.h"
30 #include "main/glformats.h"
31 #include "main/mtypes.h"
32 #include "main/hash.h"
33 #include "main/dispatch.h"
34
35 /* TODO:
36 * - Handle GL_ARB_instanced_arrays (incl. EXT_dsa)
37 * - Handle ARB_vertex_attrib_binding (incl. EXT_dsa and ARB_dsa)
38 */
39
40 static struct glthread_vao *
41 lookup_vao(struct gl_context *ctx, GLuint id)
42 {
43 struct glthread_state *glthread = &ctx->GLThread;
44 struct glthread_vao *vao;
45
46 assert(id != 0);
47
48 if (glthread->LastLookedUpVAO &&
49 glthread->LastLookedUpVAO->Name == id) {
50 vao = glthread->LastLookedUpVAO;
51 } else {
52 vao = _mesa_HashLookupLocked(glthread->VAOs, id);
53 if (!vao)
54 return NULL;
55
56 glthread->LastLookedUpVAO = vao;
57 }
58
59 return vao;
60 }
61
62 void
63 _mesa_glthread_BindVertexArray(struct gl_context *ctx, GLuint id)
64 {
65 struct glthread_state *glthread = &ctx->GLThread;
66
67 if (id == 0) {
68 glthread->CurrentVAO = &glthread->DefaultVAO;
69 } else {
70 struct glthread_vao *vao = lookup_vao(ctx, id);
71
72 if (vao)
73 glthread->CurrentVAO = vao;
74 }
75 }
76
77 void
78 _mesa_glthread_DeleteVertexArrays(struct gl_context *ctx,
79 GLsizei n, const GLuint *ids)
80 {
81 struct glthread_state *glthread = &ctx->GLThread;
82
83 if (!ids)
84 return;
85
86 for (int i = 0; i < n; i++) {
87 /* IDs equal to 0 should be silently ignored. */
88 if (!ids[i])
89 continue;
90
91 struct glthread_vao *vao = lookup_vao(ctx, ids[i]);
92 if (!vao)
93 continue;
94
95 /* If the array object is currently bound, the spec says "the binding
96 * for that object reverts to zero and the default vertex array
97 * becomes current."
98 */
99 if (glthread->CurrentVAO == vao)
100 glthread->CurrentVAO = &glthread->DefaultVAO;
101
102 if (glthread->LastLookedUpVAO == vao)
103 glthread->LastLookedUpVAO = NULL;
104
105 /* The ID is immediately freed for re-use */
106 _mesa_HashRemoveLocked(glthread->VAOs, vao->Name);
107 free(vao);
108 }
109 }
110
111 void
112 _mesa_glthread_GenVertexArrays(struct gl_context *ctx,
113 GLsizei n, GLuint *arrays)
114 {
115 struct glthread_state *glthread = &ctx->GLThread;
116
117 if (!arrays)
118 return;
119
120 /* The IDs have been generated at this point. Create VAOs for glthread. */
121 for (int i = 0; i < n; i++) {
122 GLuint id = arrays[i];
123 struct glthread_vao *vao;
124
125 vao = calloc(1, sizeof(*vao));
126 if (!vao)
127 continue; /* Is that all we can do? */
128
129 vao->Name = id;
130 _mesa_HashInsertLocked(glthread->VAOs, id, vao);
131 }
132 }
133
134 void
135 _mesa_glthread_ClientState(struct gl_context *ctx, GLuint *vaobj,
136 gl_vert_attrib attrib, bool enable)
137 {
138 struct glthread_state *glthread = &ctx->GLThread;
139 struct glthread_vao *vao;
140
141 if (attrib >= VERT_ATTRIB_MAX)
142 return;
143
144 if (vaobj) {
145 vao = lookup_vao(ctx, *vaobj);
146 if (!vao)
147 return;
148 } else {
149 vao = glthread->CurrentVAO;
150 }
151
152 if (enable)
153 vao->Enabled |= 1u << attrib;
154 else
155 vao->Enabled &= ~(1u << attrib);
156 }
157
158 static void
159 attrib_pointer(struct glthread_state *glthread, struct glthread_vao *vao,
160 GLuint buffer, gl_vert_attrib attrib,
161 GLint size, GLenum type, GLsizei stride,
162 const void *pointer)
163 {
164 if (attrib >= VERT_ATTRIB_MAX)
165 return;
166
167 unsigned elem_size = _mesa_bytes_per_vertex_attrib(size, type);
168
169 vao->Attrib[attrib].ElementSize = elem_size;
170 vao->Attrib[attrib].Stride = stride ? stride : elem_size;
171 vao->Attrib[attrib].Pointer = pointer;
172
173 if (buffer != 0)
174 vao->UserPointerMask &= ~(1u << attrib);
175 else
176 vao->UserPointerMask |= 1u << attrib;
177 }
178
179 void
180 _mesa_glthread_AttribPointer(struct gl_context *ctx, gl_vert_attrib attrib,
181 GLint size, GLenum type, GLsizei stride,
182 const void *pointer)
183 {
184 struct glthread_state *glthread = &ctx->GLThread;
185
186 attrib_pointer(glthread, glthread->CurrentVAO,
187 glthread->CurrentArrayBufferName,
188 attrib, size, type, stride, pointer);
189 }
190
191 void
192 _mesa_glthread_DSAAttribPointer(struct gl_context *ctx, GLuint vaobj,
193 GLuint buffer, gl_vert_attrib attrib,
194 GLint size, GLenum type, GLsizei stride,
195 GLintptr offset)
196 {
197 struct glthread_state *glthread = &ctx->GLThread;
198 struct glthread_vao *vao;
199
200 vao = lookup_vao(ctx, vaobj);
201 if (!vao)
202 return;
203
204 attrib_pointer(glthread, vao, buffer, attrib, size, type, stride,
205 (const void*)offset);
206 }