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
35 #if FEATURE_ATI_fragment_shader
36 #include "atifragshader.h"
38 #include "bufferobj.h"
40 #include "program/program.h"
42 #include "shaderobj.h"
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.
53 * \return pointer to a gl_shared_state structure on success, or NULL on
56 struct gl_shared_state
*
57 _mesa_alloc_shared_state(GLcontext
*ctx
)
59 struct gl_shared_state
*shared
;
62 shared
= CALLOC_STRUCT(gl_shared_state
);
66 _glthread_INIT_MUTEX(shared
->Mutex
);
68 shared
->DisplayList
= _mesa_NewHashTable();
69 shared
->TexObjects
= _mesa_NewHashTable();
70 shared
->Programs
= _mesa_NewHashTable();
72 #if FEATURE_ARB_vertex_program
73 shared
->DefaultVertexProgram
= (struct gl_vertex_program
*)
74 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
77 #if FEATURE_ARB_fragment_program
78 shared
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
79 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
82 #if FEATURE_ATI_fragment_shader
83 shared
->ATIShaders
= _mesa_NewHashTable();
84 shared
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
87 #if FEATURE_ARB_shader_objects
88 shared
->ShaderObjects
= _mesa_NewHashTable();
91 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
92 shared
->BufferObjects
= _mesa_NewHashTable();
95 /* Allocate the default buffer object */
96 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0, 0);
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
,
106 GL_TEXTURE_RECTANGLE_NV
,
110 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
114 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
116 /* Mutex and timestamp for texobj state validation */
117 _glthread_INIT_MUTEX(shared
->TexMutex
);
118 shared
->TextureStateStamp
= 0;
120 #if FEATURE_EXT_framebuffer_object
121 shared
->FrameBuffers
= _mesa_NewHashTable();
122 shared
->RenderBuffers
= _mesa_NewHashTable();
126 make_empty_list(& shared
->SyncObjects
);
134 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
137 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
139 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
140 GLcontext
*ctx
= (GLcontext
*) userData
;
141 _mesa_delete_list(ctx
, list
);
146 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
149 delete_texture_cb(GLuint id
, void *data
, void *userData
)
151 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
152 GLcontext
*ctx
= (GLcontext
*) userData
;
153 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
158 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
161 delete_program_cb(GLuint id
, void *data
, void *userData
)
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
);
173 #if FEATURE_ATI_fragment_shader
175 * Callback for deleting an ATI fragment shader object.
176 * Called by _mesa_HashDeleteAll().
179 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
181 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
182 GLcontext
*ctx
= (GLcontext
*) userData
;
183 _mesa_delete_ati_fragment_shader(ctx
, shader
);
189 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
192 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
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
;
200 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
205 * Callback for freeing shader program data. Call it before delete_shader_cb
206 * to avoid memory access error.
209 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
211 GLcontext
*ctx
= (GLcontext
*) userData
;
212 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
214 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
215 _mesa_free_shader_program_data(ctx
, shProg
);
221 * Callback for deleting shader and shader programs objects.
222 * Called by _mesa_HashDeleteAll().
225 delete_shader_cb(GLuint id
, void *data
, void *userData
)
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
);
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
);
241 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
244 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
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.
250 /*assert(fb->RefCount == 1);*/
253 /* NOTE: Delete should always be defined but there are two reports
254 * of it being NULL (bugs 13507, 14293). Work-around for now.
262 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
265 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
267 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
268 rb
->RefCount
= 0; /* see comment for FBOs above */
275 * Deallocate a shared state object and all children structures.
277 * \param ctx GL context.
278 * \param shared shared state pointer.
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.
284 * \sa alloc_shared_state().
287 free_shared_state(GLcontext
*ctx
, struct gl_shared_state
*shared
)
291 /* Free the dummy/fallback texture object */
292 if (shared
->FallbackTex
)
293 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
);
298 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
299 _mesa_DeleteHashTable(shared
->DisplayList
);
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
);
307 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
308 _mesa_DeleteHashTable(shared
->Programs
);
310 #if FEATURE_ARB_vertex_program
311 _mesa_reference_vertprog(ctx
, &shared
->DefaultVertexProgram
, NULL
);
314 #if FEATURE_ARB_fragment_program
315 _mesa_reference_fragprog(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
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
);
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
);
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
);
336 #if FEATURE_ARB_vertex_buffer_object
337 _mesa_reference_buffer_object(ctx
, &shared
->NullBufferObj
, NULL
);
342 struct simple_node
*node
;
343 struct simple_node
*temp
;
345 foreach_s(node
, temp
, & shared
->SyncObjects
) {
346 _mesa_unref_sync_object(ctx
, (struct gl_sync_object
*) node
);
352 * Free texture objects (after FBOs since some textures might have
353 * been bound to FBOs).
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
]);
361 /* all other textures */
362 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
363 _mesa_DeleteHashTable(shared
->TexObjects
);
365 _glthread_DESTROY_MUTEX(shared
->Mutex
);
366 _glthread_DESTROY_MUTEX(shared
->TexMutex
);
373 * Decrement shared state object reference count and potentially free it
374 * and all children structures.
376 * \param ctx GL context.
377 * \param shared shared state pointer.
379 * \sa free_shared_state().
382 _mesa_release_shared_state(GLcontext
*ctx
, struct gl_shared_state
*shared
)
386 _glthread_LOCK_MUTEX(shared
->Mutex
);
387 RefCount
= --shared
->RefCount
;
388 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
390 assert(RefCount
>= 0);
393 /* free shared state */
394 free_shared_state( ctx
, shared
);