2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Shared-context state
33 #include "mfeatures.h"
36 #if FEATURE_ATI_fragment_shader
37 #include "atifragshader.h"
39 #include "bufferobj.h"
41 #include "program/program.h"
43 #include "shaderobj.h"
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.
52 * \return pointer to a gl_shared_state structure on success, or NULL on
55 struct gl_shared_state
*
56 _mesa_alloc_shared_state(struct gl_context
*ctx
)
58 struct gl_shared_state
*shared
;
61 shared
= CALLOC_STRUCT(gl_shared_state
);
65 _glthread_INIT_MUTEX(shared
->Mutex
);
67 shared
->DisplayList
= _mesa_NewHashTable();
68 shared
->TexObjects
= _mesa_NewHashTable();
69 shared
->Programs
= _mesa_NewHashTable();
71 #if FEATURE_ARB_vertex_program
72 shared
->DefaultVertexProgram
= (struct gl_vertex_program
*)
73 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
76 #if FEATURE_ARB_fragment_program
77 shared
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
78 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
81 #if FEATURE_ATI_fragment_shader
82 shared
->ATIShaders
= _mesa_NewHashTable();
83 shared
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
86 #if FEATURE_ARB_shader_objects
87 shared
->ShaderObjects
= _mesa_NewHashTable();
90 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
91 shared
->BufferObjects
= _mesa_NewHashTable();
94 /* Allocate the default buffer object */
95 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0, 0);
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
] = {
102 GL_TEXTURE_2D_ARRAY_EXT
,
103 GL_TEXTURE_1D_ARRAY_EXT
,
106 GL_TEXTURE_RECTANGLE_NV
,
110 assert(Elements(targets
) == NUM_TEXTURE_TARGETS
);
111 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
115 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
117 /* Mutex and timestamp for texobj state validation */
118 _glthread_INIT_MUTEX(shared
->TexMutex
);
119 shared
->TextureStateStamp
= 0;
121 #if FEATURE_EXT_framebuffer_object
122 shared
->FrameBuffers
= _mesa_NewHashTable();
123 shared
->RenderBuffers
= _mesa_NewHashTable();
126 make_empty_list(& shared
->SyncObjects
);
133 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
136 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
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
);
145 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
148 delete_texture_cb(GLuint id
, void *data
, void *userData
)
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
);
157 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
160 delete_program_cb(GLuint id
, void *data
, void *userData
)
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
);
172 #if FEATURE_ATI_fragment_shader
174 * Callback for deleting an ATI fragment shader object.
175 * Called by _mesa_HashDeleteAll().
178 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
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
);
188 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
191 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
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
;
199 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
204 * Callback for freeing shader program data. Call it before delete_shader_cb
205 * to avoid memory access error.
208 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
210 struct gl_context
*ctx
= (struct gl_context
*) userData
;
211 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
213 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
214 _mesa_free_shader_program_data(ctx
, shProg
);
220 * Callback for deleting shader and shader programs objects.
221 * Called by _mesa_HashDeleteAll().
224 delete_shader_cb(GLuint id
, void *data
, void *userData
)
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
);
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
);
240 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
243 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
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.
249 /*assert(fb->RefCount == 1);*/
252 /* NOTE: Delete should always be defined but there are two reports
253 * of it being NULL (bugs 13507, 14293). Work-around for now.
261 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
264 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
266 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
267 rb
->RefCount
= 0; /* see comment for FBOs above */
274 * Deallocate a shared state object and all children structures.
276 * \param ctx GL context.
277 * \param shared shared state pointer.
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.
283 * \sa alloc_shared_state().
286 free_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
290 /* Free the dummy/fallback texture object */
291 if (shared
->FallbackTex
)
292 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
);
297 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
298 _mesa_DeleteHashTable(shared
->DisplayList
);
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
);
306 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
307 _mesa_DeleteHashTable(shared
->Programs
);
309 #if FEATURE_ARB_vertex_program
310 _mesa_reference_vertprog(ctx
, &shared
->DefaultVertexProgram
, NULL
);
313 #if FEATURE_ARB_fragment_program
314 _mesa_reference_fragprog(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
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
);
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
);
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
);
335 #if FEATURE_ARB_vertex_buffer_object
336 _mesa_reference_buffer_object(ctx
, &shared
->NullBufferObj
, NULL
);
340 struct simple_node
*node
;
341 struct simple_node
*temp
;
343 foreach_s(node
, temp
, & shared
->SyncObjects
) {
344 _mesa_unref_sync_object(ctx
, (struct gl_sync_object
*) node
);
349 * Free texture objects (after FBOs since some textures might have
350 * been bound to FBOs).
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
]);
358 /* all other textures */
359 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
360 _mesa_DeleteHashTable(shared
->TexObjects
);
362 _glthread_DESTROY_MUTEX(shared
->Mutex
);
363 _glthread_DESTROY_MUTEX(shared
->TexMutex
);
370 * Decrement shared state object reference count and potentially free it
371 * and all children structures.
373 * \param ctx GL context.
374 * \param shared shared state pointer.
376 * \sa free_shared_state().
379 _mesa_release_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
383 _glthread_LOCK_MUTEX(shared
->Mutex
);
384 RefCount
= --shared
->RefCount
;
385 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
387 assert(RefCount
>= 0);
390 /* free shared state */
391 free_shared_state( ctx
, shared
);