6604ed07d53c11e666ef9e6546b5b80a69350db7
[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 "mfeatures.h"
34 #include "mtypes.h"
35 #include "hash.h"
36 #if FEATURE_ATI_fragment_shader
37 #include "atifragshader.h"
38 #endif
39 #include "bufferobj.h"
40 #include "shared.h"
41 #include "program/program.h"
42 #include "dlist.h"
43 #include "shaderobj.h"
44 #include "syncobj.h"
45
46 /**
47 * Allocate and initialize a shared context state structure.
48 * Initializes the display list, texture objects and vertex programs hash
49 * tables, allocates the texture objects. If it runs out of memory, frees
50 * everything already allocated before returning NULL.
51 *
52 * \return pointer to a gl_shared_state structure on success, or NULL on
53 * failure.
54 */
55 struct gl_shared_state *
56 _mesa_alloc_shared_state(struct gl_context *ctx)
57 {
58 struct gl_shared_state *shared;
59 GLuint i;
60
61 shared = CALLOC_STRUCT(gl_shared_state);
62 if (!shared)
63 return NULL;
64
65 _glthread_INIT_MUTEX(shared->Mutex);
66
67 shared->DisplayList = _mesa_NewHashTable();
68 shared->TexObjects = _mesa_NewHashTable();
69 shared->Programs = _mesa_NewHashTable();
70
71 #if FEATURE_ARB_vertex_program
72 shared->DefaultVertexProgram = (struct gl_vertex_program *)
73 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
74 #endif
75
76 #if FEATURE_ARB_fragment_program
77 shared->DefaultFragmentProgram = (struct gl_fragment_program *)
78 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
79 #endif
80
81 #if FEATURE_ATI_fragment_shader
82 shared->ATIShaders = _mesa_NewHashTable();
83 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
84 #endif
85
86 #if FEATURE_ARB_shader_objects
87 shared->ShaderObjects = _mesa_NewHashTable();
88 #endif
89
90 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
91 shared->BufferObjects = _mesa_NewHashTable();
92 #endif
93
94 /* Allocate the default buffer object */
95 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
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_BUFFER,
102 GL_TEXTURE_2D_ARRAY_EXT,
103 GL_TEXTURE_1D_ARRAY_EXT,
104 GL_TEXTURE_CUBE_MAP,
105 GL_TEXTURE_3D,
106 GL_TEXTURE_RECTANGLE_NV,
107 GL_TEXTURE_2D,
108 GL_TEXTURE_1D
109 };
110 assert(Elements(targets) == NUM_TEXTURE_TARGETS);
111 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
112 }
113
114 /* sanity check */
115 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
116
117 /* Mutex and timestamp for texobj state validation */
118 _glthread_INIT_MUTEX(shared->TexMutex);
119 shared->TextureStateStamp = 0;
120
121 #if FEATURE_EXT_framebuffer_object
122 shared->FrameBuffers = _mesa_NewHashTable();
123 shared->RenderBuffers = _mesa_NewHashTable();
124 #endif
125
126 make_empty_list(& shared->SyncObjects);
127
128 return shared;
129 }
130
131
132 /**
133 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
134 */
135 static void
136 delete_displaylist_cb(GLuint id, void *data, void *userData)
137 {
138 struct gl_display_list *list = (struct gl_display_list *) data;
139 struct gl_context *ctx = (struct gl_context *) userData;
140 _mesa_delete_list(ctx, list);
141 }
142
143
144 /**
145 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
146 */
147 static void
148 delete_texture_cb(GLuint id, void *data, void *userData)
149 {
150 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
151 struct gl_context *ctx = (struct gl_context *) userData;
152 ctx->Driver.DeleteTexture(ctx, texObj);
153 }
154
155
156 /**
157 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
158 */
159 static void
160 delete_program_cb(GLuint id, void *data, void *userData)
161 {
162 struct gl_program *prog = (struct gl_program *) data;
163 struct gl_context *ctx = (struct gl_context *) userData;
164 if(prog != &_mesa_DummyProgram) {
165 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
166 prog->RefCount = 0; /* now going away */
167 ctx->Driver.DeleteProgram(ctx, prog);
168 }
169 }
170
171
172 #if FEATURE_ATI_fragment_shader
173 /**
174 * Callback for deleting an ATI fragment shader object.
175 * Called by _mesa_HashDeleteAll().
176 */
177 static void
178 delete_fragshader_cb(GLuint id, void *data, void *userData)
179 {
180 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
181 struct gl_context *ctx = (struct gl_context *) userData;
182 _mesa_delete_ati_fragment_shader(ctx, shader);
183 }
184 #endif
185
186
187 /**
188 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
189 */
190 static void
191 delete_bufferobj_cb(GLuint id, void *data, void *userData)
192 {
193 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
194 struct gl_context *ctx = (struct gl_context *) userData;
195 if (_mesa_bufferobj_mapped(bufObj)) {
196 ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
197 bufObj->Pointer = NULL;
198 }
199 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
200 }
201
202
203 /**
204 * Callback for freeing shader program data. Call it before delete_shader_cb
205 * to avoid memory access error.
206 */
207 static void
208 free_shader_program_data_cb(GLuint id, void *data, void *userData)
209 {
210 struct gl_context *ctx = (struct gl_context *) userData;
211 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
212
213 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
214 _mesa_free_shader_program_data(ctx, shProg);
215 }
216 }
217
218
219 /**
220 * Callback for deleting shader and shader programs objects.
221 * Called by _mesa_HashDeleteAll().
222 */
223 static void
224 delete_shader_cb(GLuint id, void *data, void *userData)
225 {
226 struct gl_context *ctx = (struct gl_context *) userData;
227 struct gl_shader *sh = (struct gl_shader *) data;
228 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
229 ctx->Driver.DeleteShader(ctx, sh);
230 }
231 else {
232 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
233 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
234 ctx->Driver.DeleteShaderProgram(ctx, shProg);
235 }
236 }
237
238
239 /**
240 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
241 */
242 static void
243 delete_framebuffer_cb(GLuint id, void *data, void *userData)
244 {
245 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
246 /* The fact that the framebuffer is in the hashtable means its refcount
247 * is one, but we're removing from the hashtable now. So clear refcount.
248 */
249 /*assert(fb->RefCount == 1);*/
250 fb->RefCount = 0;
251
252 /* NOTE: Delete should always be defined but there are two reports
253 * of it being NULL (bugs 13507, 14293). Work-around for now.
254 */
255 if (fb->Delete)
256 fb->Delete(fb);
257 }
258
259
260 /**
261 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
262 */
263 static void
264 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
265 {
266 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
267 rb->RefCount = 0; /* see comment for FBOs above */
268 if (rb->Delete)
269 rb->Delete(rb);
270 }
271
272
273 /**
274 * Deallocate a shared state object and all children structures.
275 *
276 * \param ctx GL context.
277 * \param shared shared state pointer.
278 *
279 * Frees the display lists, the texture objects (calling the driver texture
280 * deletion callback to free its private data) and the vertex programs, as well
281 * as their hash tables.
282 *
283 * \sa alloc_shared_state().
284 */
285 static void
286 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
287 {
288 GLuint i;
289
290 /* Free the dummy/fallback texture object */
291 if (shared->FallbackTex)
292 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex);
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 #if FEATURE_ARB_vertex_program
310 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
311 #endif
312
313 #if FEATURE_ARB_fragment_program
314 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
315 #endif
316
317 #if FEATURE_ATI_fragment_shader
318 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
319 _mesa_DeleteHashTable(shared->ATIShaders);
320 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
321 #endif
322
323 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
324 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
325 _mesa_DeleteHashTable(shared->BufferObjects);
326 #endif
327
328 #if FEATURE_EXT_framebuffer_object
329 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
330 _mesa_DeleteHashTable(shared->FrameBuffers);
331 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
332 _mesa_DeleteHashTable(shared->RenderBuffers);
333 #endif
334
335 #if FEATURE_ARB_vertex_buffer_object
336 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
337 #endif
338
339 {
340 struct simple_node *node;
341 struct simple_node *temp;
342
343 foreach_s(node, temp, & shared->SyncObjects) {
344 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node);
345 }
346 }
347
348 /*
349 * Free texture objects (after FBOs since some textures might have
350 * been bound to FBOs).
351 */
352 ASSERT(ctx->Driver.DeleteTexture);
353 /* the default textures */
354 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
355 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
356 }
357
358 /* all other textures */
359 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
360 _mesa_DeleteHashTable(shared->TexObjects);
361
362 _glthread_DESTROY_MUTEX(shared->Mutex);
363 _glthread_DESTROY_MUTEX(shared->TexMutex);
364
365 free(shared);
366 }
367
368
369 /**
370 * Decrement shared state object reference count and potentially free it
371 * and all children structures.
372 *
373 * \param ctx GL context.
374 * \param shared shared state pointer.
375 *
376 * \sa free_shared_state().
377 */
378 void
379 _mesa_release_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
380 {
381 GLint RefCount;
382
383 _glthread_LOCK_MUTEX(shared->Mutex);
384 RefCount = --shared->RefCount;
385 _glthread_UNLOCK_MUTEX(shared->Mutex);
386
387 assert(RefCount >= 0);
388
389 if (RefCount == 0) {
390 /* free shared state */
391 free_shared_state( ctx, shared );
392 }
393 }