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