vc4: Introduce XML-based packet header generation like Intel's.
[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 /**
48 * Allocate and initialize a shared context state structure.
49 * Initializes the display list, texture objects and vertex programs hash
50 * tables, allocates the texture objects. If it runs out of memory, frees
51 * everything already allocated before returning NULL.
52 *
53 * \return pointer to a gl_shared_state structure on success, or NULL on
54 * failure.
55 */
56 struct gl_shared_state *
57 _mesa_alloc_shared_state(struct gl_context *ctx)
58 {
59 struct gl_shared_state *shared;
60 GLuint i;
61
62 shared = CALLOC_STRUCT(gl_shared_state);
63 if (!shared)
64 return NULL;
65
66 mtx_init(&shared->Mutex, mtx_plain);
67
68 shared->DisplayList = _mesa_NewHashTable();
69 shared->BitmapAtlas = _mesa_NewHashTable();
70 shared->TexObjects = _mesa_NewHashTable();
71 shared->Programs = _mesa_NewHashTable();
72
73 shared->DefaultVertexProgram =
74 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0, true);
75 shared->DefaultFragmentProgram =
76 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0, true);
77
78 shared->ATIShaders = _mesa_NewHashTable();
79 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
80
81 shared->ShaderObjects = _mesa_NewHashTable();
82
83 shared->BufferObjects = _mesa_NewHashTable();
84
85 /* GL_ARB_sampler_objects */
86 shared->SamplerObjects = _mesa_NewHashTable();
87
88 /* GL_ARB_bindless_texture */
89 _mesa_init_shared_handles(shared);
90
91 /* Allocate the default buffer object */
92 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0);
93
94 /* Create default texture objects */
95 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
96 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
97 static const GLenum targets[] = {
98 GL_TEXTURE_2D_MULTISAMPLE,
99 GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
100 GL_TEXTURE_CUBE_MAP_ARRAY,
101 GL_TEXTURE_BUFFER,
102 GL_TEXTURE_2D_ARRAY_EXT,
103 GL_TEXTURE_1D_ARRAY_EXT,
104 GL_TEXTURE_EXTERNAL_OES,
105 GL_TEXTURE_CUBE_MAP,
106 GL_TEXTURE_3D,
107 GL_TEXTURE_RECTANGLE_NV,
108 GL_TEXTURE_2D,
109 GL_TEXTURE_1D
110 };
111 STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
112 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
113 /* Need to explicitly set/overwrite the TargetIndex field here since
114 * the call to _mesa_tex_target_to_index() in NewTextureObject() may
115 * fail if the texture target is not supported.
116 */
117 shared->DefaultTex[i]->TargetIndex = i;
118 }
119
120 /* sanity check */
121 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
122
123 /* Mutex and timestamp for texobj state validation */
124 mtx_init(&shared->TexMutex, mtx_recursive);
125 shared->TextureStateStamp = 0;
126
127 shared->FrameBuffers = _mesa_NewHashTable();
128 shared->RenderBuffers = _mesa_NewHashTable();
129
130 shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
131 _mesa_key_pointer_equal);
132
133 return shared;
134 }
135
136
137 /**
138 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
139 */
140 static void
141 delete_displaylist_cb(GLuint id, void *data, void *userData)
142 {
143 struct gl_display_list *list = (struct gl_display_list *) data;
144 struct gl_context *ctx = (struct gl_context *) userData;
145 _mesa_delete_list(ctx, list);
146 }
147
148
149 /**
150 * Callback for deleting a bitmap atlas. Called by _mesa_HashDeleteAll().
151 */
152 static void
153 delete_bitmap_atlas_cb(GLuint id, void *data, void *userData)
154 {
155 struct gl_bitmap_atlas *atlas = (struct gl_bitmap_atlas *) data;
156 struct gl_context *ctx = (struct gl_context *) userData;
157 _mesa_delete_bitmap_atlas(ctx, atlas);
158 }
159
160
161 /**
162 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
163 */
164 static void
165 delete_texture_cb(GLuint id, void *data, void *userData)
166 {
167 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
168 struct gl_context *ctx = (struct gl_context *) userData;
169 ctx->Driver.DeleteTexture(ctx, texObj);
170 }
171
172
173 /**
174 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
175 */
176 static void
177 delete_program_cb(GLuint id, void *data, void *userData)
178 {
179 struct gl_program *prog = (struct gl_program *) data;
180 struct gl_context *ctx = (struct gl_context *) userData;
181 if(prog != &_mesa_DummyProgram) {
182 assert(prog->RefCount == 1); /* should only be referenced by hash table */
183 prog->RefCount = 0; /* now going away */
184 ctx->Driver.DeleteProgram(ctx, prog);
185 }
186 }
187
188
189 /**
190 * Callback for deleting an ATI fragment shader object.
191 * Called by _mesa_HashDeleteAll().
192 */
193 static void
194 delete_fragshader_cb(GLuint id, void *data, void *userData)
195 {
196 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
197 struct gl_context *ctx = (struct gl_context *) userData;
198 _mesa_delete_ati_fragment_shader(ctx, shader);
199 }
200
201
202 /**
203 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
204 */
205 static void
206 delete_bufferobj_cb(GLuint id, void *data, void *userData)
207 {
208 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
209 struct gl_context *ctx = (struct gl_context *) userData;
210
211 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
212 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
213 }
214
215
216 /**
217 * Callback for freeing shader program data. Call it before delete_shader_cb
218 * to avoid memory access error.
219 */
220 static void
221 free_shader_program_data_cb(GLuint id, void *data, void *userData)
222 {
223 struct gl_context *ctx = (struct gl_context *) userData;
224 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
225
226 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
227 _mesa_free_shader_program_data(ctx, shProg);
228 }
229 }
230
231
232 /**
233 * Callback for deleting shader and shader programs objects.
234 * Called by _mesa_HashDeleteAll().
235 */
236 static void
237 delete_shader_cb(GLuint id, void *data, void *userData)
238 {
239 struct gl_context *ctx = (struct gl_context *) userData;
240 struct gl_shader *sh = (struct gl_shader *) data;
241 if (_mesa_validate_shader_target(ctx, sh->Type)) {
242 _mesa_delete_shader(ctx, sh);
243 }
244 else {
245 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
246 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
247 _mesa_delete_shader_program(ctx, shProg);
248 }
249 }
250
251
252 /**
253 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
254 */
255 static void
256 delete_framebuffer_cb(GLuint id, void *data, void *userData)
257 {
258 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
259 /* The fact that the framebuffer is in the hashtable means its refcount
260 * is one, but we're removing from the hashtable now. So clear refcount.
261 */
262 /*assert(fb->RefCount == 1);*/
263 fb->RefCount = 0;
264
265 /* NOTE: Delete should always be defined but there are two reports
266 * of it being NULL (bugs 13507, 14293). Work-around for now.
267 */
268 if (fb->Delete)
269 fb->Delete(fb);
270 }
271
272
273 /**
274 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
275 */
276 static void
277 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
278 {
279 struct gl_context *ctx = (struct gl_context *) userData;
280 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
281 rb->RefCount = 0; /* see comment for FBOs above */
282 if (rb->Delete)
283 rb->Delete(ctx, rb);
284 }
285
286
287 /**
288 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
289 */
290 static void
291 delete_sampler_object_cb(GLuint id, void *data, void *userData)
292 {
293 struct gl_context *ctx = (struct gl_context *) userData;
294 struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
295 _mesa_reference_sampler_object(ctx, &sampObj, NULL);
296 }
297
298
299 /**
300 * Deallocate a shared state object and all children structures.
301 *
302 * \param ctx GL context.
303 * \param shared shared state pointer.
304 *
305 * Frees the display lists, the texture objects (calling the driver texture
306 * deletion callback to free its private data) and the vertex programs, as well
307 * as their hash tables.
308 *
309 * \sa alloc_shared_state().
310 */
311 static void
312 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
313 {
314 GLuint i;
315
316 /* Free the dummy/fallback texture objects */
317 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
318 if (shared->FallbackTex[i])
319 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
320 }
321
322 /*
323 * Free display lists
324 */
325 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
326 _mesa_DeleteHashTable(shared->DisplayList);
327 _mesa_HashDeleteAll(shared->BitmapAtlas, delete_bitmap_atlas_cb, ctx);
328 _mesa_DeleteHashTable(shared->BitmapAtlas);
329
330 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
331 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
332 _mesa_DeleteHashTable(shared->ShaderObjects);
333
334 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
335 _mesa_DeleteHashTable(shared->Programs);
336
337 _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
338 _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
339
340 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
341 _mesa_DeleteHashTable(shared->ATIShaders);
342 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
343
344 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
345 _mesa_DeleteHashTable(shared->BufferObjects);
346
347 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
348 _mesa_DeleteHashTable(shared->FrameBuffers);
349 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
350 _mesa_DeleteHashTable(shared->RenderBuffers);
351
352 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
353
354 {
355 struct set_entry *entry;
356
357 set_foreach(shared->SyncObjects, entry) {
358 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
359 }
360 }
361 _mesa_set_destroy(shared->SyncObjects, NULL);
362
363 _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb, ctx);
364 _mesa_DeleteHashTable(shared->SamplerObjects);
365
366 /*
367 * Free texture objects (after FBOs since some textures might have
368 * been bound to FBOs).
369 */
370 assert(ctx->Driver.DeleteTexture);
371 /* the default textures */
372 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
373 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
374 }
375
376 /* all other textures */
377 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
378 _mesa_DeleteHashTable(shared->TexObjects);
379
380 _mesa_free_shared_handles(shared);
381
382 mtx_destroy(&shared->Mutex);
383 mtx_destroy(&shared->TexMutex);
384
385 free(shared);
386 }
387
388
389 /**
390 * gl_shared_state objects are ref counted.
391 * If ptr's refcount goes to zero, free the shared state.
392 */
393 void
394 _mesa_reference_shared_state(struct gl_context *ctx,
395 struct gl_shared_state **ptr,
396 struct gl_shared_state *state)
397 {
398 if (*ptr == state)
399 return;
400
401 if (*ptr) {
402 /* unref old state */
403 struct gl_shared_state *old = *ptr;
404 GLboolean delete;
405
406 mtx_lock(&old->Mutex);
407 assert(old->RefCount >= 1);
408 old->RefCount--;
409 delete = (old->RefCount == 0);
410 mtx_unlock(&old->Mutex);
411
412 if (delete) {
413 free_shared_state(ctx, old);
414 }
415
416 *ptr = NULL;
417 }
418
419 if (state) {
420 /* reference new state */
421 mtx_lock(&state->Mutex);
422 state->RefCount++;
423 *ptr = state;
424 mtx_unlock(&state->Mutex);
425 }
426 }