gallium: make p_winsys internal
[mesa.git] / src / gallium / drivers / nv50 / nv50_miptree.c
1 /*
2 * Copyright 2008 Ben Skeggs
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_inlines.h"
26
27 #include "nv50_context.h"
28
29 static struct pipe_texture *
30 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *tmp)
31 {
32 struct pipe_winsys *ws = pscreen->winsys;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 struct pipe_texture *pt = &mt->base;
35 unsigned usage, width = tmp->width[0], height = tmp->height[0];
36 unsigned depth = tmp->depth[0];
37 int i, l;
38
39 mt->base = *tmp;
40 mt->base.refcount = 1;
41 mt->base.screen = pscreen;
42
43 usage = PIPE_BUFFER_USAGE_PIXEL;
44 switch (pt->format) {
45 case PIPE_FORMAT_Z24S8_UNORM:
46 case PIPE_FORMAT_Z16_UNORM:
47 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
48 break;
49 default:
50 break;
51 }
52
53 switch (pt->target) {
54 case PIPE_TEXTURE_3D:
55 mt->image_nr = pt->depth[0];
56 break;
57 case PIPE_TEXTURE_CUBE:
58 mt->image_nr = 6;
59 break;
60 default:
61 mt->image_nr = 1;
62 break;
63 }
64
65 for (l = 0; l <= pt->last_level; l++) {
66 struct nv50_miptree_level *lvl = &mt->level[l];
67
68 pt->width[l] = width;
69 pt->height[l] = height;
70 pt->depth[l] = depth;
71 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
72 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
73
74 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
75 lvl->image = CALLOC(mt->image_nr, sizeof(struct pipe_buffer *));
76
77 width = MAX2(1, width >> 1);
78 height = MAX2(1, height >> 1);
79 depth = MAX2(1, depth >> 1);
80 }
81
82 for (i = 0; i < mt->image_nr; i++) {
83 for (l = 0; l <= pt->last_level; l++) {
84 struct nv50_miptree_level *lvl = &mt->level[l];
85 int size;
86
87 size = align(pt->width[l], 8) * pt->block.size;
88 size = align(size, 64);
89 size *= align(pt->height[l], 8) * pt->block.size;
90
91 lvl->image[i] = ws->buffer_create(ws, 256, 0, size);
92 lvl->image_offset[i] = mt->total_size;
93
94 mt->total_size += size;
95 }
96 }
97
98 mt->buffer = ws->buffer_create(ws, 256, usage, mt->total_size);
99 if (!mt->buffer) {
100 FREE(mt);
101 return NULL;
102 }
103
104 return &mt->base;
105 }
106
107 static INLINE void
108 mark_dirty(uint32_t *flags, unsigned image)
109 {
110 flags[image / 32] |= (1 << (image % 32));
111 }
112
113 static INLINE void
114 mark_clean(uint32_t *flags, unsigned image)
115 {
116 flags[image / 32] &= ~(1 << (image % 32));
117 }
118
119 static INLINE int
120 is_dirty(uint32_t *flags, unsigned image)
121 {
122 return !!(flags[image / 32] & (1 << (image % 32)));
123 }
124
125 static void
126 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
127 {
128 struct pipe_texture *pt = *ppt;
129
130 *ppt = NULL;
131
132 if (--pt->refcount <= 0) {
133 struct nv50_miptree *mt = nv50_miptree(pt);
134
135 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
136 FREE(mt);
137 }
138 }
139
140 void
141 nv50_miptree_sync(struct pipe_screen *pscreen, struct nv50_miptree *mt,
142 unsigned level, unsigned image)
143 {
144 struct nouveau_winsys *nvws = nv50_screen(pscreen)->nvws;
145 struct nv50_miptree_level *lvl = &mt->level[level];
146 struct pipe_surface *dst, *src;
147 unsigned face = 0, zslice = 0;
148
149 if (!is_dirty(lvl->image_dirty_cpu, image))
150 return;
151
152 if (mt->base.target == PIPE_TEXTURE_CUBE)
153 face = image;
154 else
155 if (mt->base.target == PIPE_TEXTURE_3D)
156 zslice = image;
157
158 /* Mark as clean already - so we don't continually call this function
159 * trying to get a GPU_WRITE pipe_surface!
160 */
161 mark_clean(lvl->image_dirty_cpu, image);
162
163 /* Pretend we're doing CPU access so we get the backing pipe_surface
164 * and not a view into the larger miptree.
165 */
166 src = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
167 PIPE_BUFFER_USAGE_CPU_READ);
168
169 /* Pretend we're only reading with the GPU so surface doesn't get marked
170 * as dirtied by the GPU.
171 */
172 dst = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
173 PIPE_BUFFER_USAGE_GPU_READ);
174
175 nvws->surface_copy(nvws, dst, 0, 0, src, 0, 0, dst->width, dst->height);
176
177 pscreen->tex_surface_release(pscreen, &dst);
178 pscreen->tex_surface_release(pscreen, &src);
179 }
180
181 /* The reverse of the above */
182 void
183 nv50_miptree_sync_cpu(struct pipe_screen *pscreen, struct nv50_miptree *mt,
184 unsigned level, unsigned image)
185 {
186 struct nouveau_winsys *nvws = nv50_screen(pscreen)->nvws;
187 struct nv50_miptree_level *lvl = &mt->level[level];
188 struct pipe_surface *dst, *src;
189 unsigned face = 0, zslice = 0;
190
191 if (!is_dirty(lvl->image_dirty_gpu, image))
192 return;
193
194 if (mt->base.target == PIPE_TEXTURE_CUBE)
195 face = image;
196 else
197 if (mt->base.target == PIPE_TEXTURE_3D)
198 zslice = image;
199
200 mark_clean(lvl->image_dirty_gpu, image);
201
202 src = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
203 PIPE_BUFFER_USAGE_GPU_READ);
204 dst = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
205 PIPE_BUFFER_USAGE_CPU_READ);
206
207 nvws->surface_copy(nvws, dst, 0, 0, src, 0, 0, dst->width, dst->height);
208
209 pscreen->tex_surface_release(pscreen, &dst);
210 pscreen->tex_surface_release(pscreen, &src);
211 }
212
213 static struct pipe_surface *
214 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
215 unsigned face, unsigned level, unsigned zslice,
216 unsigned flags)
217 {
218 struct nv50_miptree *mt = nv50_miptree(pt);
219 struct nv50_miptree_level *lvl = &mt->level[level];
220 struct pipe_surface *ps;
221 int img;
222
223 if (pt->target == PIPE_TEXTURE_CUBE)
224 img = face;
225 else
226 if (pt->target == PIPE_TEXTURE_3D)
227 img = zslice;
228 else
229 img = 0;
230
231 ps = CALLOC_STRUCT(pipe_surface);
232 if (!ps)
233 return NULL;
234 pipe_texture_reference(&ps->texture, pt);
235 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
236 ps->format = pt->format;
237 ps->width = pt->width[level];
238 ps->height = pt->height[level];
239 ps->block = pt->block;
240 ps->nblocksx = pt->nblocksx[level];
241 ps->nblocksy = pt->nblocksy[level];
242 ps->stride = ps->width * ps->block.size;
243 ps->usage = flags;
244 ps->status = PIPE_SURFACE_STATUS_DEFINED;
245 ps->refcount = 1;
246 ps->face = face;
247 ps->level = level;
248 ps->zslice = zslice;
249
250 if (flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
251 assert(!(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE));
252 nv50_miptree_sync_cpu(pscreen, mt, level, img);
253
254 ps->offset = 0;
255 pipe_texture_reference(&ps->texture, pt);
256 pipe_buffer_reference(pscreen, &ps->buffer, lvl->image[img]);
257
258 if (flags & PIPE_BUFFER_USAGE_CPU_WRITE)
259 mark_dirty(lvl->image_dirty_cpu, img);
260 } else {
261 nv50_miptree_sync(pscreen, mt, level, img);
262
263 ps->offset = lvl->image_offset[img];
264 pipe_texture_reference(&ps->texture, pt);
265 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
266
267 if (flags & PIPE_BUFFER_USAGE_GPU_WRITE)
268 mark_dirty(lvl->image_dirty_gpu, img);
269 }
270
271 return ps;
272 }
273
274 static void
275 nv50_miptree_surface_del(struct pipe_screen *pscreen,
276 struct pipe_surface **psurface)
277 {
278 struct pipe_surface *ps = *psurface;
279 struct nv50_surface *s = nv50_surface(ps);
280
281 *psurface = NULL;
282
283 if (--ps->refcount <= 0) {
284 pipe_texture_reference(&ps->texture, NULL);
285 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
286 FREE(s);
287 }
288 }
289
290 void
291 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
292 {
293 pscreen->texture_create = nv50_miptree_create;
294 pscreen->texture_release = nv50_miptree_release;
295 pscreen->get_tex_surface = nv50_miptree_surface_new;
296 pscreen->tex_surface_release = nv50_miptree_surface_del;
297 }
298