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
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 /* Allocate the default buffer object */
95 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0);
96 if (!shared
->NullBufferObj
)
99 /* Create default texture objects */
100 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
101 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
102 static const GLenum targets
[] = {
103 GL_TEXTURE_2D_MULTISAMPLE
,
104 GL_TEXTURE_2D_MULTISAMPLE_ARRAY
,
105 GL_TEXTURE_CUBE_MAP_ARRAY
,
107 GL_TEXTURE_2D_ARRAY_EXT
,
108 GL_TEXTURE_1D_ARRAY_EXT
,
109 GL_TEXTURE_EXTERNAL_OES
,
112 GL_TEXTURE_RECTANGLE_NV
,
116 STATIC_ASSERT(ARRAY_SIZE(targets
) == NUM_TEXTURE_TARGETS
);
117 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
118 /* Need to explicitly set/overwrite the TargetIndex field here since
119 * the call to _mesa_tex_target_to_index() in NewTextureObject() may
120 * fail if the texture target is not supported.
122 shared
->DefaultTex
[i
]->TargetIndex
= i
;
126 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
128 /* Mutex and timestamp for texobj state validation */
129 mtx_init(&shared
->TexMutex
, mtx_recursive
);
130 shared
->TextureStateStamp
= 0;
132 shared
->FrameBuffers
= _mesa_NewHashTable();
133 shared
->RenderBuffers
= _mesa_NewHashTable();
135 shared
->SyncObjects
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
136 _mesa_key_pointer_equal
);
138 shared
->MemoryObjects
= _mesa_NewHashTable();
142 free_shared_state(ctx
, shared
);
148 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
151 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
153 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
154 struct gl_context
*ctx
= (struct gl_context
*) userData
;
155 _mesa_delete_list(ctx
, list
);
160 * Callback for deleting a bitmap atlas. Called by _mesa_HashDeleteAll().
163 delete_bitmap_atlas_cb(GLuint id
, void *data
, void *userData
)
165 struct gl_bitmap_atlas
*atlas
= (struct gl_bitmap_atlas
*) data
;
166 struct gl_context
*ctx
= (struct gl_context
*) userData
;
167 _mesa_delete_bitmap_atlas(ctx
, atlas
);
172 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
175 delete_texture_cb(GLuint id
, void *data
, void *userData
)
177 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
178 struct gl_context
*ctx
= (struct gl_context
*) userData
;
179 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
184 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
187 delete_program_cb(GLuint id
, void *data
, void *userData
)
189 struct gl_program
*prog
= (struct gl_program
*) data
;
190 struct gl_context
*ctx
= (struct gl_context
*) userData
;
191 if(prog
!= &_mesa_DummyProgram
) {
192 assert(prog
->RefCount
== 1); /* should only be referenced by hash table */
193 prog
->RefCount
= 0; /* now going away */
194 ctx
->Driver
.DeleteProgram(ctx
, prog
);
200 * Callback for deleting an ATI fragment shader object.
201 * Called by _mesa_HashDeleteAll().
204 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
206 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
207 struct gl_context
*ctx
= (struct gl_context
*) userData
;
208 _mesa_delete_ati_fragment_shader(ctx
, shader
);
213 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
216 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
218 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
219 struct gl_context
*ctx
= (struct gl_context
*) userData
;
221 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
222 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
227 * Callback for freeing shader program data. Call it before delete_shader_cb
228 * to avoid memory access error.
231 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
233 struct gl_context
*ctx
= (struct gl_context
*) userData
;
234 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
236 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
237 _mesa_free_shader_program_data(ctx
, shProg
);
243 * Callback for deleting shader and shader programs objects.
244 * Called by _mesa_HashDeleteAll().
247 delete_shader_cb(GLuint id
, void *data
, void *userData
)
249 struct gl_context
*ctx
= (struct gl_context
*) userData
;
250 struct gl_shader
*sh
= (struct gl_shader
*) data
;
251 if (_mesa_validate_shader_target(ctx
, sh
->Type
)) {
252 _mesa_delete_shader(ctx
, sh
);
255 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
256 assert(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
257 _mesa_delete_shader_program(ctx
, shProg
);
263 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
266 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
268 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
269 /* The fact that the framebuffer is in the hashtable means its refcount
270 * is one, but we're removing from the hashtable now. So clear refcount.
272 /*assert(fb->RefCount == 1);*/
275 /* NOTE: Delete should always be defined but there are two reports
276 * of it being NULL (bugs 13507, 14293). Work-around for now.
284 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
287 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
289 struct gl_context
*ctx
= (struct gl_context
*) userData
;
290 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
291 rb
->RefCount
= 0; /* see comment for FBOs above */
298 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
301 delete_sampler_object_cb(GLuint id
, void *data
, void *userData
)
303 struct gl_context
*ctx
= (struct gl_context
*) userData
;
304 struct gl_sampler_object
*sampObj
= (struct gl_sampler_object
*) data
;
305 _mesa_reference_sampler_object(ctx
, &sampObj
, NULL
);
309 * Callback for deleting a memory object. Called by _mesa_HashDeleteAll().
312 delete_memory_object_cb(GLuint id
, void *data
, void *userData
)
314 struct gl_memory_object
*memObj
= (struct gl_memory_object
*) data
;
315 struct gl_context
*ctx
= (struct gl_context
*) userData
;
316 ctx
->Driver
.DeleteMemoryObject(ctx
, memObj
);
321 * Deallocate a shared state object and all children structures.
323 * \param ctx GL context.
324 * \param shared shared state pointer.
326 * Frees the display lists, the texture objects (calling the driver texture
327 * deletion callback to free its private data) and the vertex programs, as well
328 * as their hash tables.
330 * \sa alloc_shared_state().
333 free_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
337 /* Free the dummy/fallback texture objects */
338 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
339 if (shared
->FallbackTex
[i
])
340 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
[i
]);
346 if (shared
->DisplayList
) {
347 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
348 _mesa_DeleteHashTable(shared
->DisplayList
);
351 if (shared
->BitmapAtlas
) {
352 _mesa_HashDeleteAll(shared
->BitmapAtlas
, delete_bitmap_atlas_cb
, ctx
);
353 _mesa_DeleteHashTable(shared
->BitmapAtlas
);
356 if (shared
->ShaderObjects
) {
357 _mesa_HashWalk(shared
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
358 _mesa_HashDeleteAll(shared
->ShaderObjects
, delete_shader_cb
, ctx
);
359 _mesa_DeleteHashTable(shared
->ShaderObjects
);
362 if (shared
->Programs
) {
363 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
364 _mesa_DeleteHashTable(shared
->Programs
);
367 if (shared
->DefaultVertexProgram
)
368 _mesa_reference_program(ctx
, &shared
->DefaultVertexProgram
, NULL
);
370 if (shared
->DefaultFragmentProgram
)
371 _mesa_reference_program(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
373 if (shared
->DefaultFragmentShader
)
374 _mesa_delete_ati_fragment_shader(ctx
, shared
->DefaultFragmentShader
);
376 if (shared
->ATIShaders
) {
377 _mesa_HashDeleteAll(shared
->ATIShaders
, delete_fragshader_cb
, ctx
);
378 _mesa_DeleteHashTable(shared
->ATIShaders
);
381 if (shared
->BufferObjects
) {
382 _mesa_HashDeleteAll(shared
->BufferObjects
, delete_bufferobj_cb
, ctx
);
383 _mesa_DeleteHashTable(shared
->BufferObjects
);
386 if (shared
->FrameBuffers
) {
387 _mesa_HashDeleteAll(shared
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
388 _mesa_DeleteHashTable(shared
->FrameBuffers
);
391 if (shared
->RenderBuffers
) {
392 _mesa_HashDeleteAll(shared
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
393 _mesa_DeleteHashTable(shared
->RenderBuffers
);
396 if (shared
->NullBufferObj
)
397 _mesa_reference_buffer_object(ctx
, &shared
->NullBufferObj
, NULL
);
399 if (shared
->SyncObjects
) {
400 struct set_entry
*entry
;
401 set_foreach(shared
->SyncObjects
, entry
) {
402 _mesa_unref_sync_object(ctx
, (struct gl_sync_object
*) entry
->key
, 1);
405 _mesa_set_destroy(shared
->SyncObjects
, NULL
);
408 if (shared
->SamplerObjects
) {
409 _mesa_HashDeleteAll(shared
->SamplerObjects
, delete_sampler_object_cb
,
411 _mesa_DeleteHashTable(shared
->SamplerObjects
);
415 * Free texture objects (after FBOs since some textures might have
416 * been bound to FBOs).
418 assert(ctx
->Driver
.DeleteTexture
);
419 /* the default textures */
420 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
421 if (shared
->DefaultTex
[i
])
422 ctx
->Driver
.DeleteTexture(ctx
, shared
->DefaultTex
[i
]);
425 /* all other textures */
426 if (shared
->TexObjects
) {
427 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
428 _mesa_DeleteHashTable(shared
->TexObjects
);
431 _mesa_free_shared_handles(shared
);
433 if (shared
->MemoryObjects
) {
434 _mesa_HashDeleteAll(shared
->MemoryObjects
, delete_memory_object_cb
, ctx
);
435 _mesa_DeleteHashTable(shared
->MemoryObjects
);
438 simple_mtx_destroy(&shared
->Mutex
);
439 mtx_destroy(&shared
->TexMutex
);
446 * gl_shared_state objects are ref counted.
447 * If ptr's refcount goes to zero, free the shared state.
450 _mesa_reference_shared_state(struct gl_context
*ctx
,
451 struct gl_shared_state
**ptr
,
452 struct gl_shared_state
*state
)
458 /* unref old state */
459 struct gl_shared_state
*old
= *ptr
;
462 simple_mtx_lock(&old
->Mutex
);
463 assert(old
->RefCount
>= 1);
465 delete = (old
->RefCount
== 0);
466 simple_mtx_unlock(&old
->Mutex
);
469 free_shared_state(ctx
, old
);
476 /* reference new state */
477 simple_mtx_lock(&state
->Mutex
);
480 simple_mtx_unlock(&state
->Mutex
);