~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/mesa/main/shared.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
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 "externalobjects.h"
39
 
#include "samplerobj.h"
40
 
#include "shaderapi.h"
41
 
#include "shaderobj.h"
42
 
#include "syncobj.h"
43
 
#include "texobj.h"
44
 
#include "texturebindless.h"
45
 
 
46
 
#include "util/hash_table.h"
47
 
#include "util/set.h"
48
 
#include "util/u_memory.h"
49
 
 
50
 
static void
51
 
free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared);
52
 
 
53
 
/**
54
 
 * Allocate and initialize a shared context state structure.
55
 
 * Initializes the display list, texture objects and vertex programs hash
56
 
 * tables, allocates the texture objects. If it runs out of memory, frees
57
 
 * everything already allocated before returning NULL.
58
 
 *
59
 
 * \return pointer to a gl_shared_state structure on success, or NULL on
60
 
 * failure.
61
 
 */
62
 
struct gl_shared_state *
63
 
_mesa_alloc_shared_state(struct gl_context *ctx)
64
 
{
65
 
   struct gl_shared_state *shared;
66
 
   GLuint i;
67
 
 
68
 
   shared = CALLOC_STRUCT(gl_shared_state);
69
 
   if (!shared)
70
 
      return NULL;
71
 
 
72
 
   simple_mtx_init(&shared->Mutex, mtx_plain);
73
 
 
74
 
   shared->DisplayList = _mesa_NewHashTable();
75
 
   shared->BitmapAtlas = _mesa_NewHashTable();
76
 
   shared->TexObjects = _mesa_NewHashTable();
77
 
   shared->Programs = _mesa_NewHashTable();
78
 
 
79
 
   shared->DefaultVertexProgram =
80
 
      ctx->Driver.NewProgram(ctx, MESA_SHADER_VERTEX, 0, true);
81
 
   shared->DefaultFragmentProgram =
82
 
      ctx->Driver.NewProgram(ctx, MESA_SHADER_FRAGMENT, 0, true);
83
 
 
84
 
   shared->ATIShaders = _mesa_NewHashTable();
85
 
   shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
86
 
 
87
 
   shared->ShaderObjects = _mesa_NewHashTable();
88
 
 
89
 
   shared->BufferObjects = _mesa_NewHashTable();
90
 
   shared->ZombieBufferObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
91
 
                                                  _mesa_key_pointer_equal);
92
 
 
93
 
   /* GL_ARB_sampler_objects */
94
 
   shared->SamplerObjects = _mesa_NewHashTable();
95
 
 
96
 
   /* GL_ARB_bindless_texture */
97
 
   _mesa_init_shared_handles(shared);
98
 
 
99
 
   /* ARB_shading_language_include */
100
 
   _mesa_init_shader_includes(shared);
101
 
   simple_mtx_init(&shared->ShaderIncludeMutex, mtx_plain);
102
 
 
103
 
   /* Create default texture objects */
104
 
   for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
105
 
      /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
106
 
      static const GLenum targets[] = {
107
 
         GL_TEXTURE_2D_MULTISAMPLE,
108
 
         GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
109
 
         GL_TEXTURE_CUBE_MAP_ARRAY,
110
 
         GL_TEXTURE_BUFFER,
111
 
         GL_TEXTURE_2D_ARRAY_EXT,
112
 
         GL_TEXTURE_1D_ARRAY_EXT,
113
 
         GL_TEXTURE_EXTERNAL_OES,
114
 
         GL_TEXTURE_CUBE_MAP,
115
 
         GL_TEXTURE_3D,
116
 
         GL_TEXTURE_RECTANGLE_NV,
117
 
         GL_TEXTURE_2D,
118
 
         GL_TEXTURE_1D
119
 
      };
120
 
      STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
121
 
      shared->DefaultTex[i] = _mesa_new_texture_object(ctx, 0, targets[i]);
122
 
      /* Need to explicitly set/overwrite the TargetIndex field here since
123
 
       * the call to _mesa_tex_target_to_index() in NewTextureObject() may
124
 
       * fail if the texture target is not supported.
125
 
       */
126
 
      shared->DefaultTex[i]->TargetIndex = i;
127
 
   }
128
 
 
129
 
   /* sanity check */
130
 
   assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
131
 
 
132
 
   /* Mutex and timestamp for texobj state validation */
133
 
   simple_mtx_init(&shared->TexMutex, mtx_plain);
134
 
   shared->TextureStateStamp = 0;
135
 
 
136
 
   shared->FrameBuffers = _mesa_NewHashTable();
137
 
   shared->RenderBuffers = _mesa_NewHashTable();
138
 
 
139
 
   shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
140
 
                                          _mesa_key_pointer_equal);
141
 
 
142
 
   shared->MemoryObjects = _mesa_NewHashTable();
143
 
   shared->SemaphoreObjects = _mesa_NewHashTable();
144
 
 
145
 
   return shared;
146
 
}
147
 
 
148
 
 
149
 
/**
150
 
 * Callback for deleting a display list.  Called by _mesa_HashDeleteAll().
151
 
 */
152
 
static void
153
 
delete_displaylist_cb(void *data, void *userData)
154
 
{
155
 
   struct gl_display_list *list = (struct gl_display_list *) data;
156
 
   struct gl_context *ctx = (struct gl_context *) userData;
157
 
   _mesa_delete_list(ctx, list);
158
 
}
159
 
 
160
 
 
161
 
/**
162
 
 * Callback for deleting a bitmap atlas.  Called by _mesa_HashDeleteAll().
163
 
 */
164
 
static void
165
 
delete_bitmap_atlas_cb(void *data, void *userData)
166
 
{
167
 
   struct gl_bitmap_atlas *atlas = (struct gl_bitmap_atlas *) data;
168
 
   struct gl_context *ctx = (struct gl_context *) userData;
169
 
   _mesa_delete_bitmap_atlas(ctx, atlas);
170
 
}
171
 
 
172
 
 
173
 
/**
174
 
 * Callback for deleting a texture object.  Called by _mesa_HashDeleteAll().
175
 
 */
176
 
static void
177
 
delete_texture_cb(void *data, void *userData)
178
 
{
179
 
   struct gl_texture_object *texObj = (struct gl_texture_object *) data;
180
 
   struct gl_context *ctx = (struct gl_context *) userData;
181
 
   _mesa_delete_texture_object(ctx, texObj);
182
 
}
183
 
 
184
 
 
185
 
/**
186
 
 * Callback for deleting a program object.  Called by _mesa_HashDeleteAll().
187
 
 */
188
 
static void
189
 
delete_program_cb(void *data, void *userData)
190
 
{
191
 
   struct gl_program *prog = (struct gl_program *) data;
192
 
   struct gl_context *ctx = (struct gl_context *) userData;
193
 
   if(prog != &_mesa_DummyProgram) {
194
 
      assert(prog->RefCount == 1); /* should only be referenced by hash table */
195
 
      prog->RefCount = 0;  /* now going away */
196
 
      _mesa_delete_program(ctx, prog);
197
 
   }
198
 
}
199
 
 
200
 
 
201
 
/**
202
 
 * Callback for deleting an ATI fragment shader object.
203
 
 * Called by _mesa_HashDeleteAll().
204
 
 */
205
 
static void
206
 
delete_fragshader_cb(void *data, void *userData)
207
 
{
208
 
   struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
209
 
   struct gl_context *ctx = (struct gl_context *) userData;
210
 
   _mesa_delete_ati_fragment_shader(ctx, shader);
211
 
}
212
 
 
213
 
 
214
 
/**
215
 
 * Callback for deleting a buffer object.  Called by _mesa_HashDeleteAll().
216
 
 */
217
 
static void
218
 
delete_bufferobj_cb(void *data, void *userData)
219
 
{
220
 
   struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
221
 
   struct gl_context *ctx = (struct gl_context *) userData;
222
 
 
223
 
   _mesa_buffer_unmap_all_mappings(ctx, bufObj);
224
 
   _mesa_reference_buffer_object(ctx, &bufObj, NULL);
225
 
}
226
 
 
227
 
 
228
 
/**
229
 
 * Callback for freeing shader program data. Call it before delete_shader_cb
230
 
 * to avoid memory access error.
231
 
 */
232
 
static void
233
 
free_shader_program_data_cb(void *data, void *userData)
234
 
{
235
 
   struct gl_context *ctx = (struct gl_context *) userData;
236
 
   struct gl_shader_program *shProg = (struct gl_shader_program *) data;
237
 
 
238
 
   if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
239
 
       _mesa_free_shader_program_data(ctx, shProg);
240
 
   }
241
 
}
242
 
 
243
 
 
244
 
/**
245
 
 * Callback for deleting shader and shader programs objects.
246
 
 * Called by _mesa_HashDeleteAll().
247
 
 */
248
 
static void
249
 
delete_shader_cb(void *data, void *userData)
250
 
{
251
 
   struct gl_context *ctx = (struct gl_context *) userData;
252
 
   struct gl_shader *sh = (struct gl_shader *) data;
253
 
   if (_mesa_validate_shader_target(ctx, sh->Type)) {
254
 
      _mesa_delete_shader(ctx, sh);
255
 
   }
256
 
   else {
257
 
      struct gl_shader_program *shProg = (struct gl_shader_program *) data;
258
 
      assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
259
 
      _mesa_delete_shader_program(ctx, shProg);
260
 
   }
261
 
}
262
 
 
263
 
 
264
 
/**
265
 
 * Callback for deleting a framebuffer object.  Called by _mesa_HashDeleteAll()
266
 
 */
267
 
static void
268
 
delete_framebuffer_cb(void *data, UNUSED void *userData)
269
 
{
270
 
   struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
271
 
   /* The fact that the framebuffer is in the hashtable means its refcount
272
 
    * is one, but we're removing from the hashtable now.  So clear refcount.
273
 
    */
274
 
   /*assert(fb->RefCount == 1);*/
275
 
   fb->RefCount = 0;
276
 
 
277
 
   /* NOTE: Delete should always be defined but there are two reports
278
 
    * of it being NULL (bugs 13507, 14293).  Work-around for now.
279
 
    */
280
 
   if (fb->Delete)
281
 
      fb->Delete(fb);
282
 
}
283
 
 
284
 
 
285
 
/**
286
 
 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
287
 
 */
288
 
static void
289
 
delete_renderbuffer_cb(void *data, void *userData)
290
 
{
291
 
   struct gl_context *ctx = (struct gl_context *) userData;
292
 
   struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
293
 
   rb->RefCount = 0;  /* see comment for FBOs above */
294
 
   if (rb->Delete)
295
 
      rb->Delete(ctx, rb);
296
 
}
297
 
 
298
 
 
299
 
/**
300
 
 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
301
 
 */
302
 
static void
303
 
delete_sampler_object_cb(void *data, void *userData)
304
 
{
305
 
   struct gl_context *ctx = (struct gl_context *) userData;
306
 
   struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
307
 
   _mesa_reference_sampler_object(ctx, &sampObj, NULL);
308
 
}
309
 
 
310
 
/**
311
 
 * Callback for deleting a memory object.  Called by _mesa_HashDeleteAll().
312
 
 */
313
 
static void
314
 
delete_memory_object_cb(void *data, void *userData)
315
 
{
316
 
   struct gl_memory_object *memObj = (struct gl_memory_object *) data;
317
 
   struct gl_context *ctx = (struct gl_context *) userData;
318
 
   _mesa_delete_memory_object(ctx, memObj);
319
 
}
320
 
 
321
 
/**
322
 
 * Callback for deleting a memory object.  Called by _mesa_HashDeleteAll().
323
 
 */
324
 
static void
325
 
delete_semaphore_object_cb(void *data, void *userData)
326
 
{
327
 
   struct gl_semaphore_object *semObj = (struct gl_semaphore_object *) data;
328
 
   struct gl_context *ctx = (struct gl_context *) userData;
329
 
   _mesa_delete_semaphore_object(ctx, semObj);
330
 
}
331
 
 
332
 
/**
333
 
 * Deallocate a shared state object and all children structures.
334
 
 *
335
 
 * \param ctx GL context.
336
 
 * \param shared shared state pointer.
337
 
 *
338
 
 * Frees the display lists, the texture objects (calling the driver texture
339
 
 * deletion callback to free its private data) and the vertex programs, as well
340
 
 * as their hash tables.
341
 
 *
342
 
 * \sa alloc_shared_state().
343
 
 */
344
 
static void
345
 
free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
346
 
{
347
 
   GLuint i;
348
 
 
349
 
   /* Free the dummy/fallback texture objects */
350
 
   for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
351
 
      if (shared->FallbackTex[i])
352
 
         _mesa_delete_texture_object(ctx, shared->FallbackTex[i]);
353
 
   }
354
 
 
355
 
   /*
356
 
    * Free display lists
357
 
    */
358
 
   if (shared->DisplayList) {
359
 
      _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
360
 
      _mesa_DeleteHashTable(shared->DisplayList);
361
 
      free(shared->small_dlist_store.ptr);
362
 
      util_idalloc_fini(&shared->small_dlist_store.free_idx);
363
 
   }
364
 
 
365
 
   if (shared->BitmapAtlas) {
366
 
      _mesa_HashDeleteAll(shared->BitmapAtlas, delete_bitmap_atlas_cb, ctx);
367
 
      _mesa_DeleteHashTable(shared->BitmapAtlas);
368
 
   }
369
 
 
370
 
   if (shared->ShaderObjects) {
371
 
      _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
372
 
      _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
373
 
      _mesa_DeleteHashTable(shared->ShaderObjects);
374
 
   }
375
 
 
376
 
   if (shared->Programs) {
377
 
      _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
378
 
      _mesa_DeleteHashTable(shared->Programs);
379
 
   }
380
 
 
381
 
   if (shared->DefaultVertexProgram)
382
 
      _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
383
 
 
384
 
   if (shared->DefaultFragmentProgram)
385
 
      _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
386
 
 
387
 
   if (shared->DefaultFragmentShader)
388
 
      _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
389
 
 
390
 
   if (shared->ATIShaders) {
391
 
      _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
392
 
      _mesa_DeleteHashTable(shared->ATIShaders);
393
 
   }
394
 
 
395
 
   if (shared->BufferObjects) {
396
 
      _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
397
 
      _mesa_DeleteHashTable(shared->BufferObjects);
398
 
   }
399
 
 
400
 
   if (shared->ZombieBufferObjects) {
401
 
      set_foreach(shared->ZombieBufferObjects, entry) {
402
 
         assert(!"ZombieBufferObjects should be empty");
403
 
      }
404
 
      _mesa_set_destroy(shared->ZombieBufferObjects, NULL);
405
 
   }
406
 
 
407
 
   if (shared->FrameBuffers) {
408
 
      _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
409
 
      _mesa_DeleteHashTable(shared->FrameBuffers);
410
 
   }
411
 
 
412
 
   if (shared->RenderBuffers) {
413
 
      _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
414
 
      _mesa_DeleteHashTable(shared->RenderBuffers);
415
 
   }
416
 
 
417
 
   if (shared->SyncObjects) {
418
 
      set_foreach(shared->SyncObjects, entry) {
419
 
         _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
420
 
      }
421
 
 
422
 
      _mesa_set_destroy(shared->SyncObjects, NULL);
423
 
   }
424
 
 
425
 
   if (shared->SamplerObjects) {
426
 
      _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb,
427
 
                          ctx);
428
 
      _mesa_DeleteHashTable(shared->SamplerObjects);
429
 
   }
430
 
 
431
 
   /*
432
 
    * Free texture objects (after FBOs since some textures might have
433
 
    * been bound to FBOs).
434
 
    */
435
 
   /* the default textures */
436
 
   for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
437
 
      if (shared->DefaultTex[i])
438
 
         _mesa_delete_texture_object(ctx, shared->DefaultTex[i]);
439
 
   }
440
 
 
441
 
   /* all other textures */
442
 
   if (shared->TexObjects) {
443
 
      _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
444
 
      _mesa_DeleteHashTable(shared->TexObjects);
445
 
   }
446
 
 
447
 
   _mesa_free_shared_handles(shared);
448
 
 
449
 
   /* ARB_shading_language_include */
450
 
   _mesa_destroy_shader_includes(shared);
451
 
   simple_mtx_destroy(&shared->ShaderIncludeMutex);
452
 
 
453
 
   if (shared->MemoryObjects) {
454
 
      _mesa_HashDeleteAll(shared->MemoryObjects, delete_memory_object_cb, ctx);
455
 
      _mesa_DeleteHashTable(shared->MemoryObjects);
456
 
   }
457
 
 
458
 
   if (shared->SemaphoreObjects) {
459
 
      _mesa_HashDeleteAll(shared->SemaphoreObjects, delete_semaphore_object_cb, ctx);
460
 
      _mesa_DeleteHashTable(shared->SemaphoreObjects);
461
 
   }
462
 
 
463
 
   simple_mtx_destroy(&shared->Mutex);
464
 
   simple_mtx_destroy(&shared->TexMutex);
465
 
 
466
 
   FREE(shared);
467
 
}
468
 
 
469
 
 
470
 
/**
471
 
 * gl_shared_state objects are ref counted.
472
 
 * If ptr's refcount goes to zero, free the shared state.
473
 
 */
474
 
void
475
 
_mesa_reference_shared_state(struct gl_context *ctx,
476
 
                             struct gl_shared_state **ptr,
477
 
                             struct gl_shared_state *state)
478
 
{
479
 
   if (*ptr == state)
480
 
      return;
481
 
 
482
 
   if (*ptr) {
483
 
      /* unref old state */
484
 
      struct gl_shared_state *old = *ptr;
485
 
      GLboolean delete;
486
 
 
487
 
      simple_mtx_lock(&old->Mutex);
488
 
      assert(old->RefCount >= 1);
489
 
      old->RefCount--;
490
 
      delete = (old->RefCount == 0);
491
 
      simple_mtx_unlock(&old->Mutex);
492
 
 
493
 
      if (delete) {
494
 
         free_shared_state(ctx, old);
495
 
      }
496
 
 
497
 
      *ptr = NULL;
498
 
   }
499
 
 
500
 
   if (state) {
501
 
      /* reference new state */
502
 
      simple_mtx_lock(&state->Mutex);
503
 
      state->RefCount++;
504
 
      *ptr = state;
505
 
      simple_mtx_unlock(&state->Mutex);
506
 
   }
507
 
}