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