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