summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/sct/sct.c
blob: 722d2b7e66e05a8e7cadb70e5d5a6e4ee236f072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**************************************************************************
 * 
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


#include "util/u_memory.h"
#include "pipe/p_state.h"
#include "sct.h"


struct texture_list
{
   struct pipe_texture *texture;
   struct texture_list *next;
};



#define MAX_SURFACES  ((PIPE_MAX_COLOR_BUFS) + 1)

struct sct_context
{
   const struct pipe_context *context;

   /** surfaces the context is drawing into */
   struct pipe_surface *surfaces[MAX_SURFACES];

   /** currently bound textures */
   struct pipe_texture *textures[PIPE_MAX_SAMPLERS];

   /** previously bound textures, used but not flushed */
   struct texture_list *textures_used;

   boolean needs_flush;

   struct sct_context *next;
};



struct sct_surface
{
   const struct pipe_surface *surface;

   /** list of contexts drawing to this surface */
   struct sct_context_list *contexts;

   struct sct_surface *next;
};



/**
 * Find the surface_info for the given pipe_surface
 */
static struct sct_surface *
find_surface_info(struct surface_context_tracker *sct,
                  const struct pipe_surface *surface)
{
   struct sct_surface *si;
   for (si = sct->surfaces; si; si = si->next)
      if (si->surface == surface)
         return si;
   return NULL;
}


/**
 * As above, but create new surface_info if surface is new.
 */
static struct sct_surface *
find_create_surface_info(struct surface_context_tracker *sct,
                         const struct pipe_surface *surface)
{
   struct sct_surface *si = find_surface_info(sct, surface);
   if (si)
      return si;

   /* alloc new */
   si = CALLOC_STRUCT(sct_surface);
   if (si) {
      si->surface = surface;

      /* insert at head */
      si->next = sct->surfaces;
      sct->surfaces = si;
   }

   return si;
}


/**
 * Find a context_info for the given context.
 */
static struct sct_context *
find_context_info(struct surface_context_tracker *sct,
                  const struct pipe_context *context)
{
   struct sct_context *ci;
   for (ci = sct->contexts; ci; ci = ci->next)
      if (ci->context == context)
         return ci;
   return NULL;
}


/**
 * As above, but create new context_info if context is new.
 */
static struct sct_context *
find_create_context_info(struct surface_context_tracker *sct,
                         const struct pipe_context *context)
{
   struct sct_context *ci = find_context_info(sct, context);
   if (ci)
      return ci;

   /* alloc new */
   ci = CALLOC_STRUCT(sct_context);
   if (ci) {
      ci->context = context;

      /* insert at head */
      ci->next = sct->contexts;
      sct->contexts = ci;
   }

   return ci;
}


/**
 * Is the context already bound to the surface?
 */
static boolean
find_surface_context(const struct sct_surface *si,
                     const struct pipe_context *context)
{
   const struct sct_context_list *cl;
   for (cl = si->contexts; cl; cl = cl->next) {
      if (cl->context == context) {
         return TRUE;
      }
   }
   return FALSE;
}


/**
 * Add a context to the list of contexts associated with a surface.
 */
static void
add_context_to_surface(struct sct_surface *si,
                       const struct pipe_context *context)
{
   struct sct_context_list *cl = CALLOC_STRUCT(sct_context_list);
   if (cl) {
      cl->context = context;
      /* insert at head of list of contexts */
      cl->next = si->contexts;
      si->contexts = cl;
   }
}


/**
 * Remove a context from the list of contexts associated with a surface.
 */
static void
remove_context_from_surface(struct sct_surface *si,
                            const struct pipe_context *context)
{
   struct sct_context_list *prev = NULL, *curr, *next;

   for (curr = si->contexts; curr; curr = next) {
      if (curr->context == context) {
         /* remove */
         if (prev)
            prev->next = curr->next;
         else
            si->contexts = curr->next;
         next = curr->next;
         FREE(curr);
      }
      else {
         prev = curr;
         next = curr->next;
      }
   }
}


/**
 * Unbind context from surface.
 */
static void
unbind_context_surface(struct surface_context_tracker *sct,
                       struct pipe_context *context,
                       struct pipe_surface *surface)
{
   struct sct_surface *si = find_surface_info(sct, surface);
   if (si) {
      remove_context_from_surface(si, context);
   }
}


/**
 * Bind context to a set of surfaces (color + Z).
 * Like MakeCurrent().
 */
void
sct_bind_surfaces(struct surface_context_tracker *sct,
                  struct pipe_context *context,
                  uint num_surf,
                  struct pipe_surface **surfaces)
{
   struct sct_context *ci = find_create_context_info(sct, context);
   uint i;

   if (!ci) {
      return; /* out of memory */
   }

   /* unbind currently bound surfaces */
   for (i = 0; i < MAX_SURFACES; i++) {
      if (ci->surfaces[i]) {
         unbind_context_surface(sct, context, ci->surfaces[i]);
      }
   }

   /* bind new surfaces */
   for (i = 0; i < num_surf; i++) {
      struct sct_surface *si = find_create_surface_info(sct, surfaces[i]);
      if (!find_surface_context(si, context)) {
         add_context_to_surface(si, context);
      }
   }
}


/**
 * Return list of contexts bound to a surface.
 */
const struct sct_context_list *
sct_get_surface_contexts(struct surface_context_tracker *sct,
                         const struct pipe_surface *surface)
{
   const struct sct_surface *si = find_surface_info(sct, surface);
   return si->contexts;
}



static boolean
find_texture(const struct sct_context *ci,
             const struct pipe_texture *texture)
{
   const struct texture_list *tl;

   for (tl = ci->textures_used; tl; tl = tl->next) {
      if (tl->texture == texture) {
         return TRUE;
      }
   }
   return FALSE;
}


/**
 * Add the given texture to the context's list of used textures.
 */
static void
add_texture_used(struct sct_context *ci,
                 struct pipe_texture *texture)
{
   if (!find_texture(ci, texture)) {
      /* add to list */
      struct texture_list *tl = CALLOC_STRUCT(texture_list);
      if (tl) {
         pipe_texture_reference(&tl->texture, texture);
         /* insert at head */
         tl->next = ci->textures_used;
         ci->textures_used = tl;
      }
   }
}


/**
 * Bind a texture to a rendering context.
 */
void
sct_bind_texture(struct surface_context_tracker *sct,
                 struct pipe_context *context,
                 uint unit,
                 struct pipe_texture *tex)
{
   struct sct_context *ci = find_context_info(sct, context);

   if (ci->textures[unit] != tex) {
      /* put texture on the 'used' list */
      add_texture_used(ci, tex);
      /* bind new */
      pipe_texture_reference(&ci->textures[unit], tex);
   }
}


/**
 * Check if the given texture has been used by the rendering context
 * since the last call to sct_flush_textures().
 */
boolean
sct_is_texture_used(struct surface_context_tracker *sct,
                    const struct pipe_context *context,
                    const struct pipe_texture *texture)
{
   const struct sct_context *ci = find_context_info(sct, context);
   return find_texture(ci, texture);
}


/**
 * To be called when the image contents of a texture are changed, such
 * as for gl[Copy]TexSubImage().
 * XXX this may not be needed
 */
void
sct_update_texture(struct pipe_texture *tex)
{

}


/**
 * When a scene is flushed/rendered we can release the list of
 * used textures.
 */
void
sct_flush_textures(struct surface_context_tracker *sct,
                   struct pipe_context *context)
{
   struct sct_context *ci = find_context_info(sct, context);
   struct texture_list *tl, *next;
   uint i;

   for (tl = ci->textures_used; tl; tl = next) {
      next = tl->next;
      pipe_texture_reference(&tl->texture, NULL);
      FREE(tl);
   }
   ci->textures_used = NULL;

   /* put the currently bound textures on the 'used' list */
   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
      add_texture_used(ci, ci->textures[i]);
   }
}



void
sct_destroy_context(struct surface_context_tracker *sct,
                    struct pipe_context *context)
{
   /* XXX should we require an unbinding first? */
   {
      struct sct_surface *si;
      for (si = sct->surfaces; si; si = si->next) {
         remove_context_from_surface(si, context);
      }
   }

   /* remove context from context_info list */
   {
      struct sct_context *ci, *next, *prev = NULL;
      for (ci = sct->contexts; ci; ci = next) {
         next = ci->next;
         if (ci->context == context) {
            if (prev)
               prev->next = ci->next;
            else
               sct->contexts = ci->next;
            FREE(ci);
         }
         else {
            prev = ci;
         }
      }
   }

}


void
sct_destroy_surface(struct surface_context_tracker *sct,
                    struct pipe_surface *surface)
{
   if (1) {
      /* debug/sanity: no context should be bound to surface */
      struct sct_context *ci;
      uint i;
      for (ci = sct->contexts; ci; ci = ci->next) {
         for (i = 0; i < MAX_SURFACES; i++) {
            assert(ci->surfaces[i] != surface);
         }
      }
   }

   /* remove surface from sct_surface list */
   {
      struct sct_surface *si, *next, *prev = NULL;
      for (si = sct->surfaces; si; si = next) {
         next = si->next;
         if (si->surface == surface) {
            /* unlink */
            if (prev)
               prev->next = si->next;
            else
               sct->surfaces = si->next;
            FREE(si);
         }
         else {
            prev = si;
         }
      }
   }
}