Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / mesa / main / shared.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shared.c
27 * Shared-context state
28 */
29
30
31
32 #include "imports.h"
33 #include "mtypes.h"
34 #include "hash.h"
35 #include "arrayobj.h"
36 #include "shared.h"
37 #include "shader/program.h"
38 #include "shader/shader_api.h"
39 #if FEATURE_dlist
40 #include "dlist.h"
41 #endif
42 #if FEATURE_ATI_fragment_shader
43 #include "shader/atifragshader.h"
44 #endif
45
46
47 /**
48 * Allocate and initialize a shared context state structure.
49 * Initializes the display list, texture objects and vertex programs hash
50 * tables, allocates the texture objects. If it runs out of memory, frees
51 * everything already allocated before returning NULL.
52 *
53 * \return pointer to a gl_shared_state structure on success, or NULL on
54 * failure.
55 */
56 struct gl_shared_state *
57 _mesa_alloc_shared_state(GLcontext *ctx)
58 {
59 struct gl_shared_state *shared;
60 GLuint i;
61
62 shared = CALLOC_STRUCT(gl_shared_state);
63 if (!shared)
64 return NULL;
65
66 _glthread_INIT_MUTEX(shared->Mutex);
67
68 shared->DisplayList = _mesa_NewHashTable();
69 shared->TexObjects = _mesa_NewHashTable();
70 shared->Programs = _mesa_NewHashTable();
71
72 #if FEATURE_ARB_vertex_program
73 shared->DefaultVertexProgram = (struct gl_vertex_program *)
74 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
75 #endif
76
77 #if FEATURE_ARB_fragment_program
78 shared->DefaultFragmentProgram = (struct gl_fragment_program *)
79 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
80 #endif
81
82 #if FEATURE_ATI_fragment_shader
83 shared->ATIShaders = _mesa_NewHashTable();
84 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
85 #endif
86
87 #if FEATURE_ARB_shader_objects
88 shared->ShaderObjects = _mesa_NewHashTable();
89 #endif
90
91 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
92 shared->BufferObjects = _mesa_NewHashTable();
93 #endif
94
95 shared->ArrayObjects = _mesa_NewHashTable();
96
97 /* Create default texture objects */
98 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
99 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
100 static const GLenum targets[NUM_TEXTURE_TARGETS] = {
101 GL_TEXTURE_2D_ARRAY_EXT,
102 GL_TEXTURE_1D_ARRAY_EXT,
103 GL_TEXTURE_CUBE_MAP,
104 GL_TEXTURE_3D,
105 GL_TEXTURE_RECTANGLE_NV,
106 GL_TEXTURE_2D,
107 GL_TEXTURE_1D
108 };
109 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
110 }
111
112 /* sanity check */
113 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
114
115 /* Mutex and timestamp for texobj state validation */
116 _glthread_INIT_MUTEX(shared->TexMutex);
117 shared->TextureStateStamp = 0;
118
119 #if FEATURE_EXT_framebuffer_object
120 shared->FrameBuffers = _mesa_NewHashTable();
121 shared->RenderBuffers = _mesa_NewHashTable();
122 #endif
123
124 return shared;
125 }
126
127
128 /**
129 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
130 */
131 static void
132 delete_displaylist_cb(GLuint id, void *data, void *userData)
133 {
134 #if FEATURE_dlist
135 struct gl_display_list *list = (struct gl_display_list *) data;
136 GLcontext *ctx = (GLcontext *) userData;
137 _mesa_delete_list(ctx, list);
138 #endif
139 }
140
141
142 /**
143 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
144 */
145 static void
146 delete_texture_cb(GLuint id, void *data, void *userData)
147 {
148 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
149 GLcontext *ctx = (GLcontext *) userData;
150 ctx->Driver.DeleteTexture(ctx, texObj);
151 }
152
153
154 /**
155 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
156 */
157 static void
158 delete_program_cb(GLuint id, void *data, void *userData)
159 {
160 struct gl_program *prog = (struct gl_program *) data;
161 GLcontext *ctx = (GLcontext *) userData;
162 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
163 prog->RefCount = 0; /* now going away */
164 ctx->Driver.DeleteProgram(ctx, prog);
165 }
166
167
168 #if FEATURE_ATI_fragment_shader
169 /**
170 * Callback for deleting an ATI fragment shader object.
171 * Called by _mesa_HashDeleteAll().
172 */
173 static void
174 delete_fragshader_cb(GLuint id, void *data, void *userData)
175 {
176 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
177 GLcontext *ctx = (GLcontext *) userData;
178 _mesa_delete_ati_fragment_shader(ctx, shader);
179 }
180 #endif
181
182
183 /**
184 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
185 */
186 static void
187 delete_bufferobj_cb(GLuint id, void *data, void *userData)
188 {
189 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
190 GLcontext *ctx = (GLcontext *) userData;
191 ctx->Driver.DeleteBuffer(ctx, bufObj);
192 }
193
194
195 /**
196 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
197 */
198 static void
199 delete_arrayobj_cb(GLuint id, void *data, void *userData)
200 {
201 struct gl_array_object *arrayObj = (struct gl_array_object *) data;
202 GLcontext *ctx = (GLcontext *) userData;
203 _mesa_delete_array_object(ctx, arrayObj);
204 }
205
206
207 /**
208 * Callback for freeing shader program data. Call it before delete_shader_cb
209 * to avoid memory access error.
210 */
211 static void
212 free_shader_program_data_cb(GLuint id, void *data, void *userData)
213 {
214 GLcontext *ctx = (GLcontext *) userData;
215 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
216
217 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
218 _mesa_free_shader_program_data(ctx, shProg);
219 }
220 }
221
222
223 /**
224 * Callback for deleting shader and shader programs objects.
225 * Called by _mesa_HashDeleteAll().
226 */
227 static void
228 delete_shader_cb(GLuint id, void *data, void *userData)
229 {
230 GLcontext *ctx = (GLcontext *) userData;
231 struct gl_shader *sh = (struct gl_shader *) data;
232 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
233 _mesa_free_shader(ctx, sh);
234 }
235 else {
236 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
237 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
238 _mesa_free_shader_program(ctx, shProg);
239 }
240 }
241
242
243 /**
244 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
245 */
246 static void
247 delete_framebuffer_cb(GLuint id, void *data, void *userData)
248 {
249 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
250 /* The fact that the framebuffer is in the hashtable means its refcount
251 * is one, but we're removing from the hashtable now. So clear refcount.
252 */
253 /*assert(fb->RefCount == 1);*/
254 fb->RefCount = 0;
255
256 /* NOTE: Delete should always be defined but there are two reports
257 * of it being NULL (bugs 13507, 14293). Work-around for now.
258 */
259 if (fb->Delete)
260 fb->Delete(fb);
261 }
262
263
264 /**
265 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
266 */
267 static void
268 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
269 {
270 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
271 rb->RefCount = 0; /* see comment for FBOs above */
272 if (rb->Delete)
273 rb->Delete(rb);
274 }
275
276
277 /**
278 * Deallocate a shared state object and all children structures.
279 *
280 * \param ctx GL context.
281 * \param shared shared state pointer.
282 *
283 * Frees the display lists, the texture objects (calling the driver texture
284 * deletion callback to free its private data) and the vertex programs, as well
285 * as their hash tables.
286 *
287 * \sa alloc_shared_state().
288 */
289 void
290 _mesa_free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
291 {
292 GLuint i;
293
294 /*
295 * Free display lists
296 */
297 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
298 _mesa_DeleteHashTable(shared->DisplayList);
299
300 #if FEATURE_ARB_shader_objects
301 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
302 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
303 _mesa_DeleteHashTable(shared->ShaderObjects);
304 #endif
305
306 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
307 _mesa_DeleteHashTable(shared->Programs);
308
309 _mesa_HashDeleteAll(shared->ArrayObjects, delete_arrayobj_cb, ctx);
310 _mesa_DeleteHashTable(shared->ArrayObjects);
311
312 #if FEATURE_ARB_vertex_program
313 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
314 #endif
315
316 #if FEATURE_ARB_fragment_program
317 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
318 #endif
319
320 #if FEATURE_ATI_fragment_shader
321 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
322 _mesa_DeleteHashTable(shared->ATIShaders);
323 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
324 #endif
325
326 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
327 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
328 _mesa_DeleteHashTable(shared->BufferObjects);
329 #endif
330
331 #if FEATURE_EXT_framebuffer_object
332 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
333 _mesa_DeleteHashTable(shared->FrameBuffers);
334 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
335 _mesa_DeleteHashTable(shared->RenderBuffers);
336 #endif
337
338 /*
339 * Free texture objects (after FBOs since some textures might have
340 * been bound to FBOs).
341 */
342 ASSERT(ctx->Driver.DeleteTexture);
343 /* the default textures */
344 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
345 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
346 }
347
348 /* all other textures */
349 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
350 _mesa_DeleteHashTable(shared->TexObjects);
351
352 _glthread_DESTROY_MUTEX(shared->Mutex);
353 _glthread_DESTROY_MUTEX(shared->TexMutex);
354
355 _mesa_free(shared);
356 }