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