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