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
31 #include "mfeatures.h"
34 #include "atifragshader.h"
35 #include "bufferobj.h"
37 #include "program/program.h"
39 #include "samplerobj.h"
40 #include "shaderobj.h"
45 * Allocate and initialize a shared context state structure.
46 * Initializes the display list, texture objects and vertex programs hash
47 * tables, allocates the texture objects. If it runs out of memory, frees
48 * everything already allocated before returning NULL.
50 * \return pointer to a gl_shared_state structure on success, or NULL on
53 struct gl_shared_state
*
54 _mesa_alloc_shared_state(struct gl_context
*ctx
)
56 struct gl_shared_state
*shared
;
59 shared
= CALLOC_STRUCT(gl_shared_state
);
63 _glthread_INIT_MUTEX(shared
->Mutex
);
65 shared
->DisplayList
= _mesa_NewHashTable();
66 shared
->TexObjects
= _mesa_NewHashTable();
67 shared
->Programs
= _mesa_NewHashTable();
69 shared
->DefaultVertexProgram
=
70 gl_vertex_program(ctx
->Driver
.NewProgram(ctx
,
71 GL_VERTEX_PROGRAM_ARB
, 0));
72 shared
->DefaultFragmentProgram
=
73 gl_fragment_program(ctx
->Driver
.NewProgram(ctx
,
74 GL_FRAGMENT_PROGRAM_ARB
, 0));
76 shared
->ATIShaders
= _mesa_NewHashTable();
77 shared
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
79 shared
->ShaderObjects
= _mesa_NewHashTable();
81 shared
->BufferObjects
= _mesa_NewHashTable();
83 /* GL_ARB_sampler_objects */
84 shared
->SamplerObjects
= _mesa_NewHashTable();
86 /* Allocate the default buffer object */
87 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0, 0);
89 /* Create default texture objects */
90 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
91 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
92 static const GLenum targets
[] = {
94 GL_TEXTURE_2D_ARRAY_EXT
,
95 GL_TEXTURE_1D_ARRAY_EXT
,
96 GL_TEXTURE_EXTERNAL_OES
,
99 GL_TEXTURE_RECTANGLE_NV
,
103 STATIC_ASSERT(Elements(targets
) == NUM_TEXTURE_TARGETS
);
104 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
108 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
110 /* Mutex and timestamp for texobj state validation */
111 _glthread_INIT_MUTEX(shared
->TexMutex
);
112 shared
->TextureStateStamp
= 0;
114 shared
->FrameBuffers
= _mesa_NewHashTable();
115 shared
->RenderBuffers
= _mesa_NewHashTable();
117 make_empty_list(& shared
->SyncObjects
);
124 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
127 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
129 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
130 struct gl_context
*ctx
= (struct gl_context
*) userData
;
131 _mesa_delete_list(ctx
, list
);
136 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
139 delete_texture_cb(GLuint id
, void *data
, void *userData
)
141 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
142 struct gl_context
*ctx
= (struct gl_context
*) userData
;
143 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
148 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
151 delete_program_cb(GLuint id
, void *data
, void *userData
)
153 struct gl_program
*prog
= (struct gl_program
*) data
;
154 struct gl_context
*ctx
= (struct gl_context
*) userData
;
155 if(prog
!= &_mesa_DummyProgram
) {
156 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
157 prog
->RefCount
= 0; /* now going away */
158 ctx
->Driver
.DeleteProgram(ctx
, prog
);
164 * Callback for deleting an ATI fragment shader object.
165 * Called by _mesa_HashDeleteAll().
168 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
170 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
171 struct gl_context
*ctx
= (struct gl_context
*) userData
;
172 _mesa_delete_ati_fragment_shader(ctx
, shader
);
177 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
180 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
182 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
183 struct gl_context
*ctx
= (struct gl_context
*) userData
;
184 if (_mesa_bufferobj_mapped(bufObj
)) {
185 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
186 bufObj
->Pointer
= NULL
;
188 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
193 * Callback for freeing shader program data. Call it before delete_shader_cb
194 * to avoid memory access error.
197 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
199 struct gl_context
*ctx
= (struct gl_context
*) userData
;
200 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
202 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
203 _mesa_free_shader_program_data(ctx
, shProg
);
209 * Callback for deleting shader and shader programs objects.
210 * Called by _mesa_HashDeleteAll().
213 delete_shader_cb(GLuint id
, void *data
, void *userData
)
215 struct gl_context
*ctx
= (struct gl_context
*) userData
;
216 struct gl_shader
*sh
= (struct gl_shader
*) data
;
217 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
218 ctx
->Driver
.DeleteShader(ctx
, sh
);
221 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
222 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
223 ctx
->Driver
.DeleteShaderProgram(ctx
, shProg
);
229 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
232 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
234 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
235 /* The fact that the framebuffer is in the hashtable means its refcount
236 * is one, but we're removing from the hashtable now. So clear refcount.
238 /*assert(fb->RefCount == 1);*/
241 /* NOTE: Delete should always be defined but there are two reports
242 * of it being NULL (bugs 13507, 14293). Work-around for now.
250 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
253 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
255 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
256 rb
->RefCount
= 0; /* see comment for FBOs above */
263 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
266 delete_sampler_object_cb(GLuint id
, void *data
, void *userData
)
268 struct gl_context
*ctx
= (struct gl_context
*) userData
;
269 struct gl_sampler_object
*sampObj
= (struct gl_sampler_object
*) data
;
270 _mesa_reference_sampler_object(ctx
, &sampObj
, NULL
);
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(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
291 /* Free the dummy/fallback texture objects */
292 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
293 if (shared
->FallbackTex
[i
])
294 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
[i
]);
300 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
301 _mesa_DeleteHashTable(shared
->DisplayList
);
303 _mesa_HashWalk(shared
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
304 _mesa_HashDeleteAll(shared
->ShaderObjects
, delete_shader_cb
, ctx
);
305 _mesa_DeleteHashTable(shared
->ShaderObjects
);
307 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
308 _mesa_DeleteHashTable(shared
->Programs
);
310 _mesa_reference_vertprog(ctx
, &shared
->DefaultVertexProgram
, NULL
);
311 _mesa_reference_fragprog(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
313 _mesa_HashDeleteAll(shared
->ATIShaders
, delete_fragshader_cb
, ctx
);
314 _mesa_DeleteHashTable(shared
->ATIShaders
);
315 _mesa_delete_ati_fragment_shader(ctx
, shared
->DefaultFragmentShader
);
317 _mesa_HashDeleteAll(shared
->BufferObjects
, delete_bufferobj_cb
, ctx
);
318 _mesa_DeleteHashTable(shared
->BufferObjects
);
320 _mesa_HashDeleteAll(shared
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
321 _mesa_DeleteHashTable(shared
->FrameBuffers
);
322 _mesa_HashDeleteAll(shared
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
323 _mesa_DeleteHashTable(shared
->RenderBuffers
);
325 _mesa_reference_buffer_object(ctx
, &shared
->NullBufferObj
, NULL
);
328 struct simple_node
*node
;
329 struct simple_node
*temp
;
331 foreach_s(node
, temp
, & shared
->SyncObjects
) {
332 _mesa_unref_sync_object(ctx
, (struct gl_sync_object
*) node
);
336 _mesa_HashDeleteAll(shared
->SamplerObjects
, delete_sampler_object_cb
, ctx
);
337 _mesa_DeleteHashTable(shared
->SamplerObjects
);
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
);
361 * gl_shared_state objects are ref counted.
362 * If ptr's refcount goes to zero, free the shared state.
365 _mesa_reference_shared_state(struct gl_context
*ctx
,
366 struct gl_shared_state
**ptr
,
367 struct gl_shared_state
*state
)
373 /* unref old state */
374 struct gl_shared_state
*old
= *ptr
;
377 _glthread_LOCK_MUTEX(old
->Mutex
);
378 assert(old
->RefCount
>= 1);
380 delete = (old
->RefCount
== 0);
381 _glthread_UNLOCK_MUTEX(old
->Mutex
);
384 free_shared_state(ctx
, old
);
391 /* reference new state */
392 _glthread_LOCK_MUTEX(state
->Mutex
);
395 _glthread_UNLOCK_MUTEX(state
->Mutex
);