radeon/r200/r300: cleanup some of the renderbuffer code
[mesa.git] / src / gallium / drivers / cell / ppu / cell_fence.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <unistd.h>
29 #include "util/u_memory.h"
30 #include "pipe/p_inlines.h"
31 #include "cell_context.h"
32 #include "cell_batch.h"
33 #include "cell_fence.h"
34 #include "cell_texture.h"
35
36
37 void
38 cell_fence_init(struct cell_fence *fence)
39 {
40 uint i;
41 ASSERT_ALIGN16(fence->status);
42 for (i = 0; i < CELL_MAX_SPUS; i++) {
43 fence->status[i][0] = CELL_FENCE_IDLE;
44 }
45 }
46
47
48 boolean
49 cell_fence_signalled(const struct cell_context *cell,
50 const struct cell_fence *fence)
51 {
52 uint i;
53 for (i = 0; i < cell->num_spus; i++) {
54 if (fence->status[i][0] != CELL_FENCE_SIGNALLED)
55 return FALSE;
56 /*assert(fence->status[i][0] == CELL_FENCE_EMITTED);*/
57 }
58 return TRUE;
59 }
60
61
62 void
63 cell_fence_finish(const struct cell_context *cell,
64 const struct cell_fence *fence)
65 {
66 while (!cell_fence_signalled(cell, fence)) {
67 usleep(10);
68 }
69
70 #ifdef DEBUG
71 {
72 uint i;
73 for (i = 0; i < cell->num_spus; i++) {
74 assert(fence->status[i][0] == CELL_FENCE_SIGNALLED);
75 }
76 }
77 #endif
78 }
79
80
81
82
83 struct cell_buffer_node
84 {
85 struct pipe_buffer *buffer;
86 struct cell_buffer_node *next;
87 };
88
89
90 static void
91 cell_add_buffer_to_list(struct cell_context *cell,
92 struct cell_buffer_list *list,
93 struct pipe_buffer *buffer)
94 {
95 struct pipe_screen *ps = cell->pipe.screen;
96 struct cell_buffer_node *node = CALLOC_STRUCT(cell_buffer_node);
97 /* create new list node which references the buffer, insert at head */
98 if (node) {
99 pipe_buffer_reference(ps, &node->buffer, buffer);
100 node->next = list->head;
101 list->head = node;
102 }
103 }
104
105
106 /**
107 * Wait for completion of the given fence, then unreference any buffers
108 * on the list.
109 * This typically unrefs/frees texture buffers after any rendering which uses
110 * them has completed.
111 */
112 void
113 cell_free_fenced_buffers(struct cell_context *cell,
114 struct cell_buffer_list *list)
115 {
116 if (list->head) {
117 struct pipe_screen *ps = cell->pipe.screen;
118 struct cell_buffer_node *node;
119
120 cell_fence_finish(cell, &list->fence);
121
122 /* traverse the list, unreferencing buffers, freeing nodes */
123 node = list->head;
124 while (node) {
125 struct cell_buffer_node *next = node->next;
126 assert(node->buffer);
127 pipe_buffer_unmap(ps, node->buffer);
128 #if 0
129 printf("Unref buffer %p\n", node->buffer);
130 if (node->buffer->refcount == 1)
131 printf(" Delete!\n");
132 #endif
133 pipe_buffer_reference(ps, &node->buffer, NULL);
134 FREE(node);
135 node = next;
136 }
137 list->head = NULL;
138 }
139 }
140
141
142 /**
143 * This should be called for each render command.
144 * Any texture buffers that are current bound will be added to a fenced
145 * list to be freed later when the fence is executed/signalled.
146 */
147 void
148 cell_add_fenced_textures(struct cell_context *cell)
149 {
150 struct cell_buffer_list *list = &cell->fenced_buffers[cell->cur_batch];
151 uint i;
152
153 for (i = 0; i < cell->num_textures; i++) {
154 struct cell_texture *ct = cell->texture[i];
155 if (ct) {
156 uint level;
157 for (level = 0; level < CELL_MAX_TEXTURE_LEVELS; level++) {
158 if (ct->tiled_buffer[level]) {
159 #if 0
160 printf("Adding texture %p buffer %p to list\n",
161 ct, ct->tiled_buffer[level]);
162 #endif
163 cell_add_buffer_to_list(cell, list, ct->tiled_buffer[level]);
164 }
165 }
166 }
167 }
168 }