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