2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Shared-context state
30 #include "util/imports.h"
33 #include "atifragshader.h"
34 #include "bufferobj.h"
36 #include "program/program.h"
38 #include "samplerobj.h"
39 #include "shaderapi.h"
40 #include "shaderobj.h"
42 #include "texturebindless.h"
44 #include "util/hash_table.h"
48 free_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
);
51 * Allocate and initialize a shared context state structure.
52 * Initializes the display list, texture objects and vertex programs hash
53 * tables, allocates the texture objects. If it runs out of memory, frees
54 * everything already allocated before returning NULL.
56 * \return pointer to a gl_shared_state structure on success, or NULL on
59 struct gl_shared_state
*
60 _mesa_alloc_shared_state(struct gl_context
*ctx
)
62 struct gl_shared_state
*shared
;
65 shared
= CALLOC_STRUCT(gl_shared_state
);
69 simple_mtx_init(&shared
->Mutex
, mtx_plain
);
71 shared
->DisplayList
= _mesa_NewHashTable();
72 shared
->BitmapAtlas
= _mesa_NewHashTable();
73 shared
->TexObjects
= _mesa_NewHashTable();
74 shared
->Programs
= _mesa_NewHashTable();
76 shared
->DefaultVertexProgram
=
77 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0, true);
78 shared
->DefaultFragmentProgram
=
79 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0, true);
81 shared
->ATIShaders
= _mesa_NewHashTable();
82 shared
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
84 shared
->ShaderObjects
= _mesa_NewHashTable();
86 shared
->BufferObjects
= _mesa_NewHashTable();
88 /* GL_ARB_sampler_objects */
89 shared
->SamplerObjects
= _mesa_NewHashTable();
91 /* GL_ARB_bindless_texture */
92 _mesa_init_shared_handles(shared
);
94 /* ARB_shading_language_include */
95 _mesa_init_shader_includes(shared
);
96 mtx_init(&shared
->ShaderIncludeMutex
, mtx_plain
);
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
[] = {
102 GL_TEXTURE_2D_MULTISAMPLE
,
103 GL_TEXTURE_2D_MULTISAMPLE_ARRAY
,
104 GL_TEXTURE_CUBE_MAP_ARRAY
,
106 GL_TEXTURE_2D_ARRAY_EXT
,
107 GL_TEXTURE_1D_ARRAY_EXT
,
108 GL_TEXTURE_EXTERNAL_OES
,
111 GL_TEXTURE_RECTANGLE_NV
,
115 STATIC_ASSERT(ARRAY_SIZE(targets
) == NUM_TEXTURE_TARGETS
);
116 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
117 /* Need to explicitly set/overwrite the TargetIndex field here since
118 * the call to _mesa_tex_target_to_index() in NewTextureObject() may
119 * fail if the texture target is not supported.
121 shared
->DefaultTex
[i
]->TargetIndex
= i
;
125 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
127 /* Mutex and timestamp for texobj state validation */
128 mtx_init(&shared
->TexMutex
, mtx_recursive
);
129 shared
->TextureStateStamp
= 0;
131 shared
->FrameBuffers
= _mesa_NewHashTable();
132 shared
->RenderBuffers
= _mesa_NewHashTable();
134 shared
->SyncObjects
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
135 _mesa_key_pointer_equal
);
137 shared
->MemoryObjects
= _mesa_NewHashTable();
138 shared
->SemaphoreObjects
= _mesa_NewHashTable();
145 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
148 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
150 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
151 struct gl_context
*ctx
= (struct gl_context
*) userData
;
152 _mesa_delete_list(ctx
, list
);
157 * Callback for deleting a bitmap atlas. Called by _mesa_HashDeleteAll().
160 delete_bitmap_atlas_cb(GLuint id
, void *data
, void *userData
)
162 struct gl_bitmap_atlas
*atlas
= (struct gl_bitmap_atlas
*) data
;
163 struct gl_context
*ctx
= (struct gl_context
*) userData
;
164 _mesa_delete_bitmap_atlas(ctx
, atlas
);
169 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
172 delete_texture_cb(GLuint id
, void *data
, void *userData
)
174 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
175 struct gl_context
*ctx
= (struct gl_context
*) userData
;
176 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
181 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
184 delete_program_cb(GLuint id
, void *data
, void *userData
)
186 struct gl_program
*prog
= (struct gl_program
*) data
;
187 struct gl_context
*ctx
= (struct gl_context
*) userData
;
188 if(prog
!= &_mesa_DummyProgram
) {
189 assert(prog
->RefCount
== 1); /* should only be referenced by hash table */
190 prog
->RefCount
= 0; /* now going away */
191 ctx
->Driver
.DeleteProgram(ctx
, prog
);
197 * Callback for deleting an ATI fragment shader object.
198 * Called by _mesa_HashDeleteAll().
201 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
203 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
204 struct gl_context
*ctx
= (struct gl_context
*) userData
;
205 _mesa_delete_ati_fragment_shader(ctx
, shader
);
210 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
213 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
215 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
216 struct gl_context
*ctx
= (struct gl_context
*) userData
;
218 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
219 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
224 * Callback for freeing shader program data. Call it before delete_shader_cb
225 * to avoid memory access error.
228 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
230 struct gl_context
*ctx
= (struct gl_context
*) userData
;
231 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
233 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
234 _mesa_free_shader_program_data(ctx
, shProg
);
240 * Callback for deleting shader and shader programs objects.
241 * Called by _mesa_HashDeleteAll().
244 delete_shader_cb(GLuint id
, void *data
, void *userData
)
246 struct gl_context
*ctx
= (struct gl_context
*) userData
;
247 struct gl_shader
*sh
= (struct gl_shader
*) data
;
248 if (_mesa_validate_shader_target(ctx
, sh
->Type
)) {
249 _mesa_delete_shader(ctx
, sh
);
252 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
253 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
254 _mesa_delete_shader_program(ctx
, shProg
);
260 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
263 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
265 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
266 /* The fact that the framebuffer is in the hashtable means its refcount
267 * is one, but we're removing from the hashtable now. So clear refcount.
269 /*assert(fb->RefCount == 1);*/
272 /* NOTE: Delete should always be defined but there are two reports
273 * of it being NULL (bugs 13507, 14293). Work-around for now.
281 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
284 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
286 struct gl_context
*ctx
= (struct gl_context
*) userData
;
287 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
288 rb
->RefCount
= 0; /* see comment for FBOs above */
295 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
298 delete_sampler_object_cb(GLuint id
, void *data
, void *userData
)
300 struct gl_context
*ctx
= (struct gl_context
*) userData
;
301 struct gl_sampler_object
*sampObj
= (struct gl_sampler_object
*) data
;
302 _mesa_reference_sampler_object(ctx
, &sampObj
, NULL
);
306 * Callback for deleting a memory object. Called by _mesa_HashDeleteAll().
309 delete_memory_object_cb(GLuint id
, void *data
, void *userData
)
311 struct gl_memory_object
*memObj
= (struct gl_memory_object
*) data
;
312 struct gl_context
*ctx
= (struct gl_context
*) userData
;
313 ctx
->Driver
.DeleteMemoryObject(ctx
, memObj
);
317 * Callback for deleting a memory object. Called by _mesa_HashDeleteAll().
320 delete_semaphore_object_cb(GLuint id
, void *data
, void *userData
)
322 struct gl_semaphore_object
*semObj
= (struct gl_semaphore_object
*) data
;
323 struct gl_context
*ctx
= (struct gl_context
*) userData
;
324 ctx
->Driver
.DeleteSemaphoreObject(ctx
, semObj
);
328 * Deallocate a shared state object and all children structures.
330 * \param ctx GL context.
331 * \param shared shared state pointer.
333 * Frees the display lists, the texture objects (calling the driver texture
334 * deletion callback to free its private data) and the vertex programs, as well
335 * as their hash tables.
337 * \sa alloc_shared_state().
340 free_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
344 /* Free the dummy/fallback texture objects */
345 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
346 if (shared
->FallbackTex
[i
])
347 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
[i
]);
353 if (shared
->DisplayList
) {
354 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
355 _mesa_DeleteHashTable(shared
->DisplayList
);
358 if (shared
->BitmapAtlas
) {
359 _mesa_HashDeleteAll(shared
->BitmapAtlas
, delete_bitmap_atlas_cb
, ctx
);
360 _mesa_DeleteHashTable(shared
->BitmapAtlas
);
363 if (shared
->ShaderObjects
) {
364 _mesa_HashWalk(shared
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
365 _mesa_HashDeleteAll(shared
->ShaderObjects
, delete_shader_cb
, ctx
);
366 _mesa_DeleteHashTable(shared
->ShaderObjects
);
369 if (shared
->Programs
) {
370 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
371 _mesa_DeleteHashTable(shared
->Programs
);
374 if (shared
->DefaultVertexProgram
)
375 _mesa_reference_program(ctx
, &shared
->DefaultVertexProgram
, NULL
);
377 if (shared
->DefaultFragmentProgram
)
378 _mesa_reference_program(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
380 if (shared
->DefaultFragmentShader
)
381 _mesa_delete_ati_fragment_shader(ctx
, shared
->DefaultFragmentShader
);
383 if (shared
->ATIShaders
) {
384 _mesa_HashDeleteAll(shared
->ATIShaders
, delete_fragshader_cb
, ctx
);
385 _mesa_DeleteHashTable(shared
->ATIShaders
);
388 if (shared
->BufferObjects
) {
389 _mesa_HashDeleteAll(shared
->BufferObjects
, delete_bufferobj_cb
, ctx
);
390 _mesa_DeleteHashTable(shared
->BufferObjects
);
393 if (shared
->FrameBuffers
) {
394 _mesa_HashDeleteAll(shared
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
395 _mesa_DeleteHashTable(shared
->FrameBuffers
);
398 if (shared
->RenderBuffers
) {
399 _mesa_HashDeleteAll(shared
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
400 _mesa_DeleteHashTable(shared
->RenderBuffers
);
403 if (shared
->SyncObjects
) {
404 set_foreach(shared
->SyncObjects
, entry
) {
405 _mesa_unref_sync_object(ctx
, (struct gl_sync_object
*) entry
->key
, 1);
408 _mesa_set_destroy(shared
->SyncObjects
, NULL
);
411 if (shared
->SamplerObjects
) {
412 _mesa_HashDeleteAll(shared
->SamplerObjects
, delete_sampler_object_cb
,
414 _mesa_DeleteHashTable(shared
->SamplerObjects
);
418 * Free texture objects (after FBOs since some textures might have
419 * been bound to FBOs).
421 assert(ctx
->Driver
.DeleteTexture
);
422 /* the default textures */
423 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
424 if (shared
->DefaultTex
[i
])
425 ctx
->Driver
.DeleteTexture(ctx
, shared
->DefaultTex
[i
]);
428 /* all other textures */
429 if (shared
->TexObjects
) {
430 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
431 _mesa_DeleteHashTable(shared
->TexObjects
);
434 _mesa_free_shared_handles(shared
);
436 /* ARB_shading_language_include */
437 _mesa_destroy_shader_includes(shared
);
438 mtx_destroy(&shared
->ShaderIncludeMutex
);
440 if (shared
->MemoryObjects
) {
441 _mesa_HashDeleteAll(shared
->MemoryObjects
, delete_memory_object_cb
, ctx
);
442 _mesa_DeleteHashTable(shared
->MemoryObjects
);
445 if (shared
->SemaphoreObjects
) {
446 _mesa_HashDeleteAll(shared
->SemaphoreObjects
, delete_semaphore_object_cb
, ctx
);
447 _mesa_DeleteHashTable(shared
->SemaphoreObjects
);
450 simple_mtx_destroy(&shared
->Mutex
);
451 mtx_destroy(&shared
->TexMutex
);
458 * gl_shared_state objects are ref counted.
459 * If ptr's refcount goes to zero, free the shared state.
462 _mesa_reference_shared_state(struct gl_context
*ctx
,
463 struct gl_shared_state
**ptr
,
464 struct gl_shared_state
*state
)
470 /* unref old state */
471 struct gl_shared_state
*old
= *ptr
;
474 simple_mtx_lock(&old
->Mutex
);
475 assert(old
->RefCount
>= 1);
477 delete = (old
->RefCount
== 0);
478 simple_mtx_unlock(&old
->Mutex
);
481 free_shared_state(ctx
, old
);
488 /* reference new state */
489 simple_mtx_lock(&state
->Mutex
);
492 simple_mtx_unlock(&state
->Mutex
);