glapi / teximage: implement EGLImageTargetTexStorageEXT
[mesa.git] / src / mesa / main / shared.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 /**
26 * \file shared.c
27 * Shared-context state
28 */
29
30 #include "imports.h"
31 #include "mtypes.h"
32 #include "hash.h"
33 #include "atifragshader.h"
34 #include "bufferobj.h"
35 #include "shared.h"
36 #include "program/program.h"
37 #include "dlist.h"
38 #include "samplerobj.h"
39 #include "shaderapi.h"
40 #include "shaderobj.h"
41 #include "syncobj.h"
42 #include "texturebindless.h"
43
44 #include "util/hash_table.h"
45 #include "util/set.h"
46
47 static void
48 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared);
49
50 /**
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.
55 *
56 * \return pointer to a gl_shared_state structure on success, or NULL on
57 * failure.
58 */
59 struct gl_shared_state *
60 _mesa_alloc_shared_state(struct gl_context *ctx)
61 {
62 struct gl_shared_state *shared;
63 GLuint i;
64
65 shared = CALLOC_STRUCT(gl_shared_state);
66 if (!shared)
67 return NULL;
68
69 simple_mtx_init(&shared->Mutex, mtx_plain);
70
71 shared->DisplayList = _mesa_NewHashTable();
72 shared->BitmapAtlas = _mesa_NewHashTable();
73 shared->TexObjects = _mesa_NewHashTable();
74 shared->Programs = _mesa_NewHashTable();
75
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);
80
81 shared->ATIShaders = _mesa_NewHashTable();
82 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
83
84 shared->ShaderObjects = _mesa_NewHashTable();
85
86 shared->BufferObjects = _mesa_NewHashTable();
87
88 /* GL_ARB_sampler_objects */
89 shared->SamplerObjects = _mesa_NewHashTable();
90
91 /* GL_ARB_bindless_texture */
92 _mesa_init_shared_handles(shared);
93
94 /* ARB_shading_language_include */
95 _mesa_init_shader_includes(shared);
96 mtx_init(&shared->ShaderIncludeMutex, mtx_plain);
97
98 /* Allocate the default buffer object */
99 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0);
100 if (!shared->NullBufferObj)
101 goto fail;
102
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[] = {
107 GL_TEXTURE_2D_MULTISAMPLE,
108 GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
109 GL_TEXTURE_CUBE_MAP_ARRAY,
110 GL_TEXTURE_BUFFER,
111 GL_TEXTURE_2D_ARRAY_EXT,
112 GL_TEXTURE_1D_ARRAY_EXT,
113 GL_TEXTURE_EXTERNAL_OES,
114 GL_TEXTURE_CUBE_MAP,
115 GL_TEXTURE_3D,
116 GL_TEXTURE_RECTANGLE_NV,
117 GL_TEXTURE_2D,
118 GL_TEXTURE_1D
119 };
120 STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
121 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
122 /* Need to explicitly set/overwrite the TargetIndex field here since
123 * the call to _mesa_tex_target_to_index() in NewTextureObject() may
124 * fail if the texture target is not supported.
125 */
126 shared->DefaultTex[i]->TargetIndex = i;
127 }
128
129 /* sanity check */
130 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
131
132 /* Mutex and timestamp for texobj state validation */
133 mtx_init(&shared->TexMutex, mtx_recursive);
134 shared->TextureStateStamp = 0;
135
136 shared->FrameBuffers = _mesa_NewHashTable();
137 shared->RenderBuffers = _mesa_NewHashTable();
138
139 shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
140 _mesa_key_pointer_equal);
141
142 shared->MemoryObjects = _mesa_NewHashTable();
143 shared->SemaphoreObjects = _mesa_NewHashTable();
144
145 return shared;
146
147 fail:
148 free_shared_state(ctx, shared);
149 return NULL;
150 }
151
152
153 /**
154 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
155 */
156 static void
157 delete_displaylist_cb(GLuint id, void *data, void *userData)
158 {
159 struct gl_display_list *list = (struct gl_display_list *) data;
160 struct gl_context *ctx = (struct gl_context *) userData;
161 _mesa_delete_list(ctx, list);
162 }
163
164
165 /**
166 * Callback for deleting a bitmap atlas. Called by _mesa_HashDeleteAll().
167 */
168 static void
169 delete_bitmap_atlas_cb(GLuint id, void *data, void *userData)
170 {
171 struct gl_bitmap_atlas *atlas = (struct gl_bitmap_atlas *) data;
172 struct gl_context *ctx = (struct gl_context *) userData;
173 _mesa_delete_bitmap_atlas(ctx, atlas);
174 }
175
176
177 /**
178 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
179 */
180 static void
181 delete_texture_cb(GLuint id, void *data, void *userData)
182 {
183 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
184 struct gl_context *ctx = (struct gl_context *) userData;
185 ctx->Driver.DeleteTexture(ctx, texObj);
186 }
187
188
189 /**
190 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
191 */
192 static void
193 delete_program_cb(GLuint id, void *data, void *userData)
194 {
195 struct gl_program *prog = (struct gl_program *) data;
196 struct gl_context *ctx = (struct gl_context *) userData;
197 if(prog != &_mesa_DummyProgram) {
198 assert(prog->RefCount == 1); /* should only be referenced by hash table */
199 prog->RefCount = 0; /* now going away */
200 ctx->Driver.DeleteProgram(ctx, prog);
201 }
202 }
203
204
205 /**
206 * Callback for deleting an ATI fragment shader object.
207 * Called by _mesa_HashDeleteAll().
208 */
209 static void
210 delete_fragshader_cb(GLuint id, void *data, void *userData)
211 {
212 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
213 struct gl_context *ctx = (struct gl_context *) userData;
214 _mesa_delete_ati_fragment_shader(ctx, shader);
215 }
216
217
218 /**
219 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
220 */
221 static void
222 delete_bufferobj_cb(GLuint id, void *data, void *userData)
223 {
224 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
225 struct gl_context *ctx = (struct gl_context *) userData;
226
227 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
228 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
229 }
230
231
232 /**
233 * Callback for freeing shader program data. Call it before delete_shader_cb
234 * to avoid memory access error.
235 */
236 static void
237 free_shader_program_data_cb(GLuint id, void *data, void *userData)
238 {
239 struct gl_context *ctx = (struct gl_context *) userData;
240 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
241
242 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
243 _mesa_free_shader_program_data(ctx, shProg);
244 }
245 }
246
247
248 /**
249 * Callback for deleting shader and shader programs objects.
250 * Called by _mesa_HashDeleteAll().
251 */
252 static void
253 delete_shader_cb(GLuint id, void *data, void *userData)
254 {
255 struct gl_context *ctx = (struct gl_context *) userData;
256 struct gl_shader *sh = (struct gl_shader *) data;
257 if (_mesa_validate_shader_target(ctx, sh->Type)) {
258 _mesa_delete_shader(ctx, sh);
259 }
260 else {
261 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
262 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
263 _mesa_delete_shader_program(ctx, shProg);
264 }
265 }
266
267
268 /**
269 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
270 */
271 static void
272 delete_framebuffer_cb(GLuint id, void *data, void *userData)
273 {
274 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
275 /* The fact that the framebuffer is in the hashtable means its refcount
276 * is one, but we're removing from the hashtable now. So clear refcount.
277 */
278 /*assert(fb->RefCount == 1);*/
279 fb->RefCount = 0;
280
281 /* NOTE: Delete should always be defined but there are two reports
282 * of it being NULL (bugs 13507, 14293). Work-around for now.
283 */
284 if (fb->Delete)
285 fb->Delete(fb);
286 }
287
288
289 /**
290 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
291 */
292 static void
293 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
294 {
295 struct gl_context *ctx = (struct gl_context *) userData;
296 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
297 rb->RefCount = 0; /* see comment for FBOs above */
298 if (rb->Delete)
299 rb->Delete(ctx, rb);
300 }
301
302
303 /**
304 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
305 */
306 static void
307 delete_sampler_object_cb(GLuint id, void *data, void *userData)
308 {
309 struct gl_context *ctx = (struct gl_context *) userData;
310 struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
311 _mesa_reference_sampler_object(ctx, &sampObj, NULL);
312 }
313
314 /**
315 * Callback for deleting a memory object. Called by _mesa_HashDeleteAll().
316 */
317 static void
318 delete_memory_object_cb(GLuint id, void *data, void *userData)
319 {
320 struct gl_memory_object *memObj = (struct gl_memory_object *) data;
321 struct gl_context *ctx = (struct gl_context *) userData;
322 ctx->Driver.DeleteMemoryObject(ctx, memObj);
323 }
324
325 /**
326 * Callback for deleting a memory object. Called by _mesa_HashDeleteAll().
327 */
328 static void
329 delete_semaphore_object_cb(GLuint id, void *data, void *userData)
330 {
331 struct gl_semaphore_object *semObj = (struct gl_semaphore_object *) data;
332 struct gl_context *ctx = (struct gl_context *) userData;
333 ctx->Driver.DeleteSemaphoreObject(ctx, semObj);
334 }
335
336 /**
337 * Deallocate a shared state object and all children structures.
338 *
339 * \param ctx GL context.
340 * \param shared shared state pointer.
341 *
342 * Frees the display lists, the texture objects (calling the driver texture
343 * deletion callback to free its private data) and the vertex programs, as well
344 * as their hash tables.
345 *
346 * \sa alloc_shared_state().
347 */
348 static void
349 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
350 {
351 GLuint i;
352
353 /* Free the dummy/fallback texture objects */
354 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
355 if (shared->FallbackTex[i])
356 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
357 }
358
359 /*
360 * Free display lists
361 */
362 if (shared->DisplayList) {
363 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
364 _mesa_DeleteHashTable(shared->DisplayList);
365 }
366
367 if (shared->BitmapAtlas) {
368 _mesa_HashDeleteAll(shared->BitmapAtlas, delete_bitmap_atlas_cb, ctx);
369 _mesa_DeleteHashTable(shared->BitmapAtlas);
370 }
371
372 if (shared->ShaderObjects) {
373 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
374 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
375 _mesa_DeleteHashTable(shared->ShaderObjects);
376 }
377
378 if (shared->Programs) {
379 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
380 _mesa_DeleteHashTable(shared->Programs);
381 }
382
383 if (shared->DefaultVertexProgram)
384 _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
385
386 if (shared->DefaultFragmentProgram)
387 _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
388
389 if (shared->DefaultFragmentShader)
390 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
391
392 if (shared->ATIShaders) {
393 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
394 _mesa_DeleteHashTable(shared->ATIShaders);
395 }
396
397 if (shared->BufferObjects) {
398 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
399 _mesa_DeleteHashTable(shared->BufferObjects);
400 }
401
402 if (shared->FrameBuffers) {
403 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
404 _mesa_DeleteHashTable(shared->FrameBuffers);
405 }
406
407 if (shared->RenderBuffers) {
408 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
409 _mesa_DeleteHashTable(shared->RenderBuffers);
410 }
411
412 if (shared->NullBufferObj)
413 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
414
415 if (shared->SyncObjects) {
416 set_foreach(shared->SyncObjects, entry) {
417 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
418 }
419
420 _mesa_set_destroy(shared->SyncObjects, NULL);
421 }
422
423 if (shared->SamplerObjects) {
424 _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb,
425 ctx);
426 _mesa_DeleteHashTable(shared->SamplerObjects);
427 }
428
429 /*
430 * Free texture objects (after FBOs since some textures might have
431 * been bound to FBOs).
432 */
433 assert(ctx->Driver.DeleteTexture);
434 /* the default textures */
435 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
436 if (shared->DefaultTex[i])
437 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
438 }
439
440 /* all other textures */
441 if (shared->TexObjects) {
442 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
443 _mesa_DeleteHashTable(shared->TexObjects);
444 }
445
446 _mesa_free_shared_handles(shared);
447
448 /* ARB_shading_language_include */
449 _mesa_destroy_shader_includes(shared);
450 mtx_destroy(&shared->ShaderIncludeMutex);
451
452 if (shared->MemoryObjects) {
453 _mesa_HashDeleteAll(shared->MemoryObjects, delete_memory_object_cb, ctx);
454 _mesa_DeleteHashTable(shared->MemoryObjects);
455 }
456
457 if (shared->SemaphoreObjects) {
458 _mesa_HashDeleteAll(shared->SemaphoreObjects, delete_semaphore_object_cb, ctx);
459 _mesa_DeleteHashTable(shared->SemaphoreObjects);
460 }
461
462 simple_mtx_destroy(&shared->Mutex);
463 mtx_destroy(&shared->TexMutex);
464
465 free(shared);
466 }
467
468
469 /**
470 * gl_shared_state objects are ref counted.
471 * If ptr's refcount goes to zero, free the shared state.
472 */
473 void
474 _mesa_reference_shared_state(struct gl_context *ctx,
475 struct gl_shared_state **ptr,
476 struct gl_shared_state *state)
477 {
478 if (*ptr == state)
479 return;
480
481 if (*ptr) {
482 /* unref old state */
483 struct gl_shared_state *old = *ptr;
484 GLboolean delete;
485
486 simple_mtx_lock(&old->Mutex);
487 assert(old->RefCount >= 1);
488 old->RefCount--;
489 delete = (old->RefCount == 0);
490 simple_mtx_unlock(&old->Mutex);
491
492 if (delete) {
493 free_shared_state(ctx, old);
494 }
495
496 *ptr = NULL;
497 }
498
499 if (state) {
500 /* reference new state */
501 simple_mtx_lock(&state->Mutex);
502 state->RefCount++;
503 *ptr = state;
504 simple_mtx_unlock(&state->Mutex);
505 }
506 }