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
36 #include "bufferobj.h"
38 #include "shader/program.h"
39 #include "shader/shader_api.h"
43 #if FEATURE_ATI_fragment_shader
44 #include "shader/atifragshader.h"
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.
54 * \return pointer to a gl_shared_state structure on success, or NULL on
57 struct gl_shared_state
*
58 _mesa_alloc_shared_state(GLcontext
*ctx
)
60 struct gl_shared_state
*shared
;
63 shared
= CALLOC_STRUCT(gl_shared_state
);
67 _glthread_INIT_MUTEX(shared
->Mutex
);
69 shared
->DisplayList
= _mesa_NewHashTable();
70 shared
->TexObjects
= _mesa_NewHashTable();
71 shared
->Programs
= _mesa_NewHashTable();
73 #if FEATURE_ARB_vertex_program
74 shared
->DefaultVertexProgram
= (struct gl_vertex_program
*)
75 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
78 #if FEATURE_ARB_fragment_program
79 shared
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
80 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
83 #if FEATURE_ATI_fragment_shader
84 shared
->ATIShaders
= _mesa_NewHashTable();
85 shared
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
88 #if FEATURE_ARB_shader_objects
89 shared
->ShaderObjects
= _mesa_NewHashTable();
92 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
93 shared
->BufferObjects
= _mesa_NewHashTable();
96 /* Allocate the default buffer object and set refcount so high that
97 * it never gets deleted.
98 * XXX with recent/improved refcounting this may not longer be needed.
100 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0, 0);
101 shared
->NullBufferObj
->RefCount
= 1000 * 1000 * 1000;
103 /* Create default texture objects */
104 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
105 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
106 static const GLenum targets
[NUM_TEXTURE_TARGETS
] = {
107 GL_TEXTURE_2D_ARRAY_EXT
,
108 GL_TEXTURE_1D_ARRAY_EXT
,
111 GL_TEXTURE_RECTANGLE_NV
,
115 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
119 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
121 /* Mutex and timestamp for texobj state validation */
122 _glthread_INIT_MUTEX(shared
->TexMutex
);
123 shared
->TextureStateStamp
= 0;
125 #if FEATURE_EXT_framebuffer_object
126 shared
->FrameBuffers
= _mesa_NewHashTable();
127 shared
->RenderBuffers
= _mesa_NewHashTable();
135 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
138 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
141 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
142 GLcontext
*ctx
= (GLcontext
*) userData
;
143 _mesa_delete_list(ctx
, list
);
149 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
152 delete_texture_cb(GLuint id
, void *data
, void *userData
)
154 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
155 GLcontext
*ctx
= (GLcontext
*) userData
;
156 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
161 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
164 delete_program_cb(GLuint id
, void *data
, void *userData
)
166 struct gl_program
*prog
= (struct gl_program
*) data
;
167 GLcontext
*ctx
= (GLcontext
*) userData
;
168 if(prog
!= &_mesa_DummyProgram
) {
169 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
170 prog
->RefCount
= 0; /* now going away */
171 ctx
->Driver
.DeleteProgram(ctx
, prog
);
176 #if FEATURE_ATI_fragment_shader
178 * Callback for deleting an ATI fragment shader object.
179 * Called by _mesa_HashDeleteAll().
182 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
184 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
185 GLcontext
*ctx
= (GLcontext
*) userData
;
186 _mesa_delete_ati_fragment_shader(ctx
, shader
);
192 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
195 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
197 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
198 GLcontext
*ctx
= (GLcontext
*) userData
;
199 if (bufObj
->Pointer
) {
200 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
201 bufObj
->Pointer
= NULL
;
203 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
208 * Callback for freeing shader program data. Call it before delete_shader_cb
209 * to avoid memory access error.
212 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
214 GLcontext
*ctx
= (GLcontext
*) userData
;
215 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
217 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
218 _mesa_free_shader_program_data(ctx
, shProg
);
224 * Callback for deleting shader and shader programs objects.
225 * Called by _mesa_HashDeleteAll().
228 delete_shader_cb(GLuint id
, void *data
, void *userData
)
230 GLcontext
*ctx
= (GLcontext
*) userData
;
231 struct gl_shader
*sh
= (struct gl_shader
*) data
;
232 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
233 _mesa_free_shader(ctx
, sh
);
236 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
237 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
238 _mesa_free_shader_program(ctx
, shProg
);
244 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
247 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
249 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
250 /* The fact that the framebuffer is in the hashtable means its refcount
251 * is one, but we're removing from the hashtable now. So clear refcount.
253 /*assert(fb->RefCount == 1);*/
256 /* NOTE: Delete should always be defined but there are two reports
257 * of it being NULL (bugs 13507, 14293). Work-around for now.
265 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
268 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
270 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
271 rb
->RefCount
= 0; /* see comment for FBOs above */
278 * Deallocate a shared state object and all children structures.
280 * \param ctx GL context.
281 * \param shared shared state pointer.
283 * Frees the display lists, the texture objects (calling the driver texture
284 * deletion callback to free its private data) and the vertex programs, as well
285 * as their hash tables.
287 * \sa alloc_shared_state().
290 _mesa_free_shared_state(GLcontext
*ctx
, struct gl_shared_state
*shared
)
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 ctx
->Driver
.DeleteBuffer(ctx
, shared
->NullBufferObj
);
340 * Free texture objects (after FBOs since some textures might have
341 * been bound to FBOs).
343 ASSERT(ctx
->Driver
.DeleteTexture
);
344 /* the default textures */
345 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
346 ctx
->Driver
.DeleteTexture(ctx
, shared
->DefaultTex
[i
]);
349 /* all other textures */
350 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
351 _mesa_DeleteHashTable(shared
->TexObjects
);
353 _glthread_DESTROY_MUTEX(shared
->Mutex
);
354 _glthread_DESTROY_MUTEX(shared
->TexMutex
);