mesa: remove array size so the static assert can work
[mesa.git] / src / mesa / main / shared.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shared.c
27 * Shared-context state
28 */
29
30 #include "imports.h"
31 #include "mfeatures.h"
32 #include "mtypes.h"
33 #include "hash.h"
34 #include "atifragshader.h"
35 #include "bufferobj.h"
36 #include "shared.h"
37 #include "program/program.h"
38 #include "dlist.h"
39 #include "samplerobj.h"
40 #include "shaderobj.h"
41 #include "syncobj.h"
42
43
44 /**
45 * Allocate and initialize a shared context state structure.
46 * Initializes the display list, texture objects and vertex programs hash
47 * tables, allocates the texture objects. If it runs out of memory, frees
48 * everything already allocated before returning NULL.
49 *
50 * \return pointer to a gl_shared_state structure on success, or NULL on
51 * failure.
52 */
53 struct gl_shared_state *
54 _mesa_alloc_shared_state(struct gl_context *ctx)
55 {
56 struct gl_shared_state *shared;
57 GLuint i;
58
59 shared = CALLOC_STRUCT(gl_shared_state);
60 if (!shared)
61 return NULL;
62
63 _glthread_INIT_MUTEX(shared->Mutex);
64
65 shared->DisplayList = _mesa_NewHashTable();
66 shared->TexObjects = _mesa_NewHashTable();
67 shared->Programs = _mesa_NewHashTable();
68
69 shared->DefaultVertexProgram =
70 gl_vertex_program(ctx->Driver.NewProgram(ctx,
71 GL_VERTEX_PROGRAM_ARB, 0));
72 shared->DefaultFragmentProgram =
73 gl_fragment_program(ctx->Driver.NewProgram(ctx,
74 GL_FRAGMENT_PROGRAM_ARB, 0));
75
76 shared->ATIShaders = _mesa_NewHashTable();
77 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
78
79 shared->ShaderObjects = _mesa_NewHashTable();
80
81 shared->BufferObjects = _mesa_NewHashTable();
82
83 /* GL_ARB_sampler_objects */
84 shared->SamplerObjects = _mesa_NewHashTable();
85
86 /* Allocate the default buffer object */
87 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
88
89 /* Create default texture objects */
90 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
91 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
92 static const GLenum targets[] = {
93 GL_TEXTURE_BUFFER,
94 GL_TEXTURE_2D_ARRAY_EXT,
95 GL_TEXTURE_1D_ARRAY_EXT,
96 GL_TEXTURE_EXTERNAL_OES,
97 GL_TEXTURE_CUBE_MAP,
98 GL_TEXTURE_3D,
99 GL_TEXTURE_RECTANGLE_NV,
100 GL_TEXTURE_2D,
101 GL_TEXTURE_1D
102 };
103 STATIC_ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
104 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
105 }
106
107 /* sanity check */
108 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
109
110 /* Mutex and timestamp for texobj state validation */
111 _glthread_INIT_MUTEX(shared->TexMutex);
112 shared->TextureStateStamp = 0;
113
114 shared->FrameBuffers = _mesa_NewHashTable();
115 shared->RenderBuffers = _mesa_NewHashTable();
116
117 make_empty_list(& shared->SyncObjects);
118
119 return shared;
120 }
121
122
123 /**
124 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
125 */
126 static void
127 delete_displaylist_cb(GLuint id, void *data, void *userData)
128 {
129 struct gl_display_list *list = (struct gl_display_list *) data;
130 struct gl_context *ctx = (struct gl_context *) userData;
131 _mesa_delete_list(ctx, list);
132 }
133
134
135 /**
136 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
137 */
138 static void
139 delete_texture_cb(GLuint id, void *data, void *userData)
140 {
141 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
142 struct gl_context *ctx = (struct gl_context *) userData;
143 ctx->Driver.DeleteTexture(ctx, texObj);
144 }
145
146
147 /**
148 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
149 */
150 static void
151 delete_program_cb(GLuint id, void *data, void *userData)
152 {
153 struct gl_program *prog = (struct gl_program *) data;
154 struct gl_context *ctx = (struct gl_context *) userData;
155 if(prog != &_mesa_DummyProgram) {
156 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
157 prog->RefCount = 0; /* now going away */
158 ctx->Driver.DeleteProgram(ctx, prog);
159 }
160 }
161
162
163 /**
164 * Callback for deleting an ATI fragment shader object.
165 * Called by _mesa_HashDeleteAll().
166 */
167 static void
168 delete_fragshader_cb(GLuint id, void *data, void *userData)
169 {
170 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
171 struct gl_context *ctx = (struct gl_context *) userData;
172 _mesa_delete_ati_fragment_shader(ctx, shader);
173 }
174
175
176 /**
177 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
178 */
179 static void
180 delete_bufferobj_cb(GLuint id, void *data, void *userData)
181 {
182 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
183 struct gl_context *ctx = (struct gl_context *) userData;
184 if (_mesa_bufferobj_mapped(bufObj)) {
185 ctx->Driver.UnmapBuffer(ctx, bufObj);
186 bufObj->Pointer = NULL;
187 }
188 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
189 }
190
191
192 /**
193 * Callback for freeing shader program data. Call it before delete_shader_cb
194 * to avoid memory access error.
195 */
196 static void
197 free_shader_program_data_cb(GLuint id, void *data, void *userData)
198 {
199 struct gl_context *ctx = (struct gl_context *) userData;
200 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
201
202 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
203 _mesa_free_shader_program_data(ctx, shProg);
204 }
205 }
206
207
208 /**
209 * Callback for deleting shader and shader programs objects.
210 * Called by _mesa_HashDeleteAll().
211 */
212 static void
213 delete_shader_cb(GLuint id, void *data, void *userData)
214 {
215 struct gl_context *ctx = (struct gl_context *) userData;
216 struct gl_shader *sh = (struct gl_shader *) data;
217 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
218 ctx->Driver.DeleteShader(ctx, sh);
219 }
220 else {
221 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
222 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
223 ctx->Driver.DeleteShaderProgram(ctx, shProg);
224 }
225 }
226
227
228 /**
229 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
230 */
231 static void
232 delete_framebuffer_cb(GLuint id, void *data, void *userData)
233 {
234 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
235 /* The fact that the framebuffer is in the hashtable means its refcount
236 * is one, but we're removing from the hashtable now. So clear refcount.
237 */
238 /*assert(fb->RefCount == 1);*/
239 fb->RefCount = 0;
240
241 /* NOTE: Delete should always be defined but there are two reports
242 * of it being NULL (bugs 13507, 14293). Work-around for now.
243 */
244 if (fb->Delete)
245 fb->Delete(fb);
246 }
247
248
249 /**
250 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
251 */
252 static void
253 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
254 {
255 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
256 rb->RefCount = 0; /* see comment for FBOs above */
257 if (rb->Delete)
258 rb->Delete(rb);
259 }
260
261
262 /**
263 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
264 */
265 static void
266 delete_sampler_object_cb(GLuint id, void *data, void *userData)
267 {
268 struct gl_context *ctx = (struct gl_context *) userData;
269 struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
270 _mesa_reference_sampler_object(ctx, &sampObj, NULL);
271 }
272
273
274 /**
275 * Deallocate a shared state object and all children structures.
276 *
277 * \param ctx GL context.
278 * \param shared shared state pointer.
279 *
280 * Frees the display lists, the texture objects (calling the driver texture
281 * deletion callback to free its private data) and the vertex programs, as well
282 * as their hash tables.
283 *
284 * \sa alloc_shared_state().
285 */
286 static void
287 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
288 {
289 GLuint i;
290
291 /* Free the dummy/fallback texture objects */
292 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
293 if (shared->FallbackTex[i])
294 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
295 }
296
297 /*
298 * Free display lists
299 */
300 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
301 _mesa_DeleteHashTable(shared->DisplayList);
302
303 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
304 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
305 _mesa_DeleteHashTable(shared->ShaderObjects);
306
307 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
308 _mesa_DeleteHashTable(shared->Programs);
309
310 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
311 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
312
313 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
314 _mesa_DeleteHashTable(shared->ATIShaders);
315 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
316
317 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
318 _mesa_DeleteHashTable(shared->BufferObjects);
319
320 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
321 _mesa_DeleteHashTable(shared->FrameBuffers);
322 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
323 _mesa_DeleteHashTable(shared->RenderBuffers);
324
325 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
326
327 {
328 struct simple_node *node;
329 struct simple_node *temp;
330
331 foreach_s(node, temp, & shared->SyncObjects) {
332 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node);
333 }
334 }
335
336 _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb, ctx);
337 _mesa_DeleteHashTable(shared->SamplerObjects);
338
339 /*
340 * Free texture objects (after FBOs since some textures might have
341 * been bound to FBOs).
342 */
343 ASSERT(ctx->Driver.DeleteTexture);
344 /* the default textures */
345 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
346 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
347 }
348
349 /* all other textures */
350 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
351 _mesa_DeleteHashTable(shared->TexObjects);
352
353 _glthread_DESTROY_MUTEX(shared->Mutex);
354 _glthread_DESTROY_MUTEX(shared->TexMutex);
355
356 free(shared);
357 }
358
359
360 /**
361 * gl_shared_state objects are ref counted.
362 * If ptr's refcount goes to zero, free the shared state.
363 */
364 void
365 _mesa_reference_shared_state(struct gl_context *ctx,
366 struct gl_shared_state **ptr,
367 struct gl_shared_state *state)
368 {
369 if (*ptr == state)
370 return;
371
372 if (*ptr) {
373 /* unref old state */
374 struct gl_shared_state *old = *ptr;
375 GLboolean delete;
376
377 _glthread_LOCK_MUTEX(old->Mutex);
378 assert(old->RefCount >= 1);
379 old->RefCount--;
380 delete = (old->RefCount == 0);
381 _glthread_UNLOCK_MUTEX(old->Mutex);
382
383 if (delete) {
384 free_shared_state(ctx, old);
385 }
386
387 *ptr = NULL;
388 }
389
390 if (state) {
391 /* reference new state */
392 _glthread_LOCK_MUTEX(state->Mutex);
393 state->RefCount++;
394 *ptr = state;
395 _glthread_UNLOCK_MUTEX(state->Mutex);
396 }
397 }