Merge remote branch 'origin/master' into nv50-compiler
[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 #if FEATURE_ATI_fragment_shader
36 #include "atifragshader.h"
37 #endif
38 #include "bufferobj.h"
39 #include "shared.h"
40 #include "program/program.h"
41 #include "dlist.h"
42 #include "shaderobj.h"
43 #if FEATURE_ARB_sync
44 #include "syncobj.h"
45 #endif
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 /* Allocate the default buffer object */
96 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
97
98 /* Create default texture objects */
99 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
100 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
101 static const GLenum targets[NUM_TEXTURE_TARGETS] = {
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 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
111 }
112
113 /* sanity check */
114 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
115
116 /* Mutex and timestamp for texobj state validation */
117 _glthread_INIT_MUTEX(shared->TexMutex);
118 shared->TextureStateStamp = 0;
119
120 #if FEATURE_EXT_framebuffer_object
121 shared->FrameBuffers = _mesa_NewHashTable();
122 shared->RenderBuffers = _mesa_NewHashTable();
123 #endif
124
125 #if FEATURE_ARB_sync
126 make_empty_list(& shared->SyncObjects);
127 #endif
128
129 return shared;
130 }
131
132
133 /**
134 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
135 */
136 static void
137 delete_displaylist_cb(GLuint id, void *data, void *userData)
138 {
139 struct gl_display_list *list = (struct gl_display_list *) data;
140 GLcontext *ctx = (GLcontext *) userData;
141 _mesa_delete_list(ctx, list);
142 }
143
144
145 /**
146 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
147 */
148 static void
149 delete_texture_cb(GLuint id, void *data, void *userData)
150 {
151 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
152 GLcontext *ctx = (GLcontext *) userData;
153 ctx->Driver.DeleteTexture(ctx, texObj);
154 }
155
156
157 /**
158 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
159 */
160 static void
161 delete_program_cb(GLuint id, void *data, void *userData)
162 {
163 struct gl_program *prog = (struct gl_program *) data;
164 GLcontext *ctx = (GLcontext *) userData;
165 if(prog != &_mesa_DummyProgram) {
166 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
167 prog->RefCount = 0; /* now going away */
168 ctx->Driver.DeleteProgram(ctx, prog);
169 }
170 }
171
172
173 #if FEATURE_ATI_fragment_shader
174 /**
175 * Callback for deleting an ATI fragment shader object.
176 * Called by _mesa_HashDeleteAll().
177 */
178 static void
179 delete_fragshader_cb(GLuint id, void *data, void *userData)
180 {
181 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
182 GLcontext *ctx = (GLcontext *) userData;
183 _mesa_delete_ati_fragment_shader(ctx, shader);
184 }
185 #endif
186
187
188 /**
189 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
190 */
191 static void
192 delete_bufferobj_cb(GLuint id, void *data, void *userData)
193 {
194 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
195 GLcontext *ctx = (GLcontext *) userData;
196 if (_mesa_bufferobj_mapped(bufObj)) {
197 ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
198 bufObj->Pointer = NULL;
199 }
200 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
201 }
202
203
204 /**
205 * Callback for freeing shader program data. Call it before delete_shader_cb
206 * to avoid memory access error.
207 */
208 static void
209 free_shader_program_data_cb(GLuint id, void *data, void *userData)
210 {
211 GLcontext *ctx = (GLcontext *) userData;
212 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
213
214 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
215 _mesa_free_shader_program_data(ctx, shProg);
216 }
217 }
218
219
220 /**
221 * Callback for deleting shader and shader programs objects.
222 * Called by _mesa_HashDeleteAll().
223 */
224 static void
225 delete_shader_cb(GLuint id, void *data, void *userData)
226 {
227 GLcontext *ctx = (GLcontext *) userData;
228 struct gl_shader *sh = (struct gl_shader *) data;
229 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
230 ctx->Driver.DeleteShader(ctx, sh);
231 }
232 else {
233 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
234 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
235 ctx->Driver.DeleteShaderProgram(ctx, shProg);
236 }
237 }
238
239
240 /**
241 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
242 */
243 static void
244 delete_framebuffer_cb(GLuint id, void *data, void *userData)
245 {
246 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
247 /* The fact that the framebuffer is in the hashtable means its refcount
248 * is one, but we're removing from the hashtable now. So clear refcount.
249 */
250 /*assert(fb->RefCount == 1);*/
251 fb->RefCount = 0;
252
253 /* NOTE: Delete should always be defined but there are two reports
254 * of it being NULL (bugs 13507, 14293). Work-around for now.
255 */
256 if (fb->Delete)
257 fb->Delete(fb);
258 }
259
260
261 /**
262 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
263 */
264 static void
265 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
266 {
267 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
268 rb->RefCount = 0; /* see comment for FBOs above */
269 if (rb->Delete)
270 rb->Delete(rb);
271 }
272
273
274 /**
275 * Deallocate a shared state object and all children structures.
276 *
277 * \param ctx GL context.
278 * \param shared shared state pointer.
279 *
280 * Frees the display lists, the texture objects (calling the driver texture
281 * deletion callback to free its private data) and the vertex programs, as well
282 * as their hash tables.
283 *
284 * \sa alloc_shared_state().
285 */
286 static void
287 free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
288 {
289 GLuint i;
290
291 /* Free the dummy/fallback texture object */
292 if (shared->FallbackTex)
293 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex);
294
295 /*
296 * Free display lists
297 */
298 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
299 _mesa_DeleteHashTable(shared->DisplayList);
300
301 #if FEATURE_ARB_shader_objects
302 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
303 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
304 _mesa_DeleteHashTable(shared->ShaderObjects);
305 #endif
306
307 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
308 _mesa_DeleteHashTable(shared->Programs);
309
310 #if FEATURE_ARB_vertex_program
311 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
312 #endif
313
314 #if FEATURE_ARB_fragment_program
315 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
316 #endif
317
318 #if FEATURE_ATI_fragment_shader
319 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
320 _mesa_DeleteHashTable(shared->ATIShaders);
321 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
322 #endif
323
324 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
325 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
326 _mesa_DeleteHashTable(shared->BufferObjects);
327 #endif
328
329 #if FEATURE_EXT_framebuffer_object
330 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
331 _mesa_DeleteHashTable(shared->FrameBuffers);
332 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
333 _mesa_DeleteHashTable(shared->RenderBuffers);
334 #endif
335
336 #if FEATURE_ARB_vertex_buffer_object
337 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
338 #endif
339
340 #if FEATURE_ARB_sync
341 {
342 struct simple_node *node;
343 struct simple_node *temp;
344
345 foreach_s(node, temp, & shared->SyncObjects) {
346 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node);
347 }
348 }
349 #endif
350
351 /*
352 * Free texture objects (after FBOs since some textures might have
353 * been bound to FBOs).
354 */
355 ASSERT(ctx->Driver.DeleteTexture);
356 /* the default textures */
357 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
358 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
359 }
360
361 /* all other textures */
362 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
363 _mesa_DeleteHashTable(shared->TexObjects);
364
365 _glthread_DESTROY_MUTEX(shared->Mutex);
366 _glthread_DESTROY_MUTEX(shared->TexMutex);
367
368 free(shared);
369 }
370
371
372 /**
373 * Decrement shared state object reference count and potentially free it
374 * and all children structures.
375 *
376 * \param ctx GL context.
377 * \param shared shared state pointer.
378 *
379 * \sa free_shared_state().
380 */
381 void
382 _mesa_release_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
383 {
384 GLint RefCount;
385
386 _glthread_LOCK_MUTEX(shared->Mutex);
387 RefCount = --shared->RefCount;
388 _glthread_UNLOCK_MUTEX(shared->Mutex);
389
390 assert(RefCount >= 0);
391
392 if (RefCount == 0) {
393 /* free shared state */
394 free_shared_state( ctx, shared );
395 }
396 }