nouveau: unreference fences on resource destruction
[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 "util/u_inlines.h"
26 #include "util/u_format.h"
27
28 #include "nv50_context.h"
29 #include "nv50_resource.h"
30
31 static INLINE uint32_t
32 nv50_tex_choose_tile_dims(unsigned nx, unsigned ny, unsigned nz)
33 {
34 return nvc0_tex_choose_tile_dims(nx, ny * 2, nz);
35 }
36
37 static uint32_t
38 nv50_mt_choose_storage_type(struct nv50_miptree *mt, boolean compressed)
39 {
40 const unsigned ms = util_logbase2(mt->base.base.nr_samples);
41
42 uint32_t tile_flags;
43
44 if (unlikely(mt->base.base.flags & NOUVEAU_RESOURCE_FLAG_LINEAR))
45 return 0;
46 if (unlikely(mt->base.base.bind & PIPE_BIND_CURSOR))
47 return 0;
48
49 switch (mt->base.base.format) {
50 case PIPE_FORMAT_Z16_UNORM:
51 tile_flags = 0x6c + (ms << 8);
52 break;
53 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
54 tile_flags = 0x18 + (ms << 8);
55 break;
56 case PIPE_FORMAT_Z24X8_UNORM:
57 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
58 tile_flags = 0x128 + (ms << 8);
59 break;
60 case PIPE_FORMAT_Z32_FLOAT:
61 tile_flags = 0x40 + (ms << 8);
62 break;
63 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
64 tile_flags = 0x60 + (ms << 8);
65 break;
66 default:
67 switch (util_format_get_blocksizebits(mt->base.base.format)) {
68 case 128:
69 assert(ms < 3);
70 tile_flags = 0x74;
71 break;
72 case 64:
73 switch (ms) {
74 case 2: tile_flags = 0xfc; break;
75 case 3: tile_flags = 0xfd; break;
76 default:
77 tile_flags = 0x70;
78 break;
79 }
80 break;
81 case 32:
82 if (mt->base.base.bind & PIPE_BIND_SCANOUT) {
83 assert(ms == 0);
84 tile_flags = 0x7a;
85 } else {
86 switch (ms) {
87 case 2: tile_flags = 0xf8; break;
88 case 3: tile_flags = 0xf9; break;
89 default:
90 tile_flags = 0x70;
91 break;
92 }
93 }
94 break;
95 case 16:
96 case 8:
97 tile_flags = 0x70;
98 break;
99 default:
100 return 0;
101 }
102 if (mt->base.base.bind & PIPE_BIND_CURSOR)
103 tile_flags = 0;
104 }
105
106 if (!compressed)
107 tile_flags &= ~0x180;
108
109 return tile_flags;
110 }
111
112 void
113 nv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
114 {
115 struct nv50_miptree *mt = nv50_miptree(pt);
116
117 nouveau_bo_ref(NULL, &mt->base.bo);
118
119 nouveau_fence_ref(NULL, &mt->base.fence);
120 nouveau_fence_ref(NULL, &mt->base.fence_wr);
121
122 FREE(mt);
123 }
124
125 boolean
126 nv50_miptree_get_handle(struct pipe_screen *pscreen,
127 struct pipe_resource *pt,
128 struct winsys_handle *whandle)
129 {
130 struct nv50_miptree *mt = nv50_miptree(pt);
131 unsigned stride;
132
133 if (!mt || !mt->base.bo)
134 return FALSE;
135
136 stride = util_format_get_stride(mt->base.base.format,
137 mt->base.base.width0);
138
139 return nouveau_screen_bo_get_handle(pscreen,
140 mt->base.bo,
141 stride,
142 whandle);
143 }
144
145 const struct u_resource_vtbl nv50_miptree_vtbl =
146 {
147 nv50_miptree_get_handle, /* get_handle */
148 nv50_miptree_destroy, /* resource_destroy */
149 nv50_miptree_transfer_new, /* get_transfer */
150 nv50_miptree_transfer_del, /* transfer_destroy */
151 nv50_miptree_transfer_map, /* transfer_map */
152 u_default_transfer_flush_region, /* transfer_flush_region */
153 nv50_miptree_transfer_unmap, /* transfer_unmap */
154 u_default_transfer_inline_write /* transfer_inline_write */
155 };
156
157 static INLINE boolean
158 nv50_miptree_init_ms_mode(struct nv50_miptree *mt)
159 {
160 switch (mt->base.base.nr_samples) {
161 case 8:
162 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS8;
163 mt->ms_x = 2;
164 mt->ms_y = 1;
165 break;
166 case 4:
167 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS4;
168 mt->ms_x = 1;
169 mt->ms_y = 1;
170 break;
171 case 2:
172 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS2;
173 mt->ms_x = 1;
174 break;
175 case 1:
176 case 0:
177 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS1;
178 break;
179 default:
180 NOUVEAU_ERR("invalid nr_samples: %u\n", mt->base.base.nr_samples);
181 return FALSE;
182 }
183 return TRUE;
184 }
185
186 boolean
187 nv50_miptree_init_layout_linear(struct nv50_miptree *mt)
188 {
189 struct pipe_resource *pt = &mt->base.base;
190 const unsigned blocksize = util_format_get_blocksize(pt->format);
191
192 if (util_format_is_depth_or_stencil(pt->format))
193 return FALSE;
194
195 if ((pt->last_level > 0) || (pt->depth0 > 1) || (pt->array_size > 1))
196 return FALSE;
197 if (mt->ms_x | mt->ms_y)
198 return FALSE;
199
200 mt->level[0].pitch = align(pt->width0 * blocksize, 64);
201
202 mt->total_size = mt->level[0].pitch * pt->height0;
203
204 return TRUE;
205 }
206
207 static void
208 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
209 {
210 struct pipe_resource *pt = &mt->base.base;
211 unsigned w, h, d, l;
212 const unsigned blocksize = util_format_get_blocksize(pt->format);
213
214 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
215
216 w = pt->width0 << mt->ms_x;
217 h = pt->height0 << mt->ms_y;
218
219 /* For 3D textures, a mipmap is spanned by all the layers, for array
220 * textures and cube maps, each layer contains its own mipmaps.
221 */
222 d = mt->layout_3d ? pt->depth0 : 1;
223
224 for (l = 0; l <= pt->last_level; ++l) {
225 struct nv50_miptree_level *lvl = &mt->level[l];
226 unsigned tsx, tsy, tsz;
227 unsigned nbx = util_format_get_nblocksx(pt->format, w);
228 unsigned nby = util_format_get_nblocksy(pt->format, h);
229
230 lvl->offset = mt->total_size;
231
232 lvl->tile_mode = nv50_tex_choose_tile_dims(nbx, nby, d);
233
234 tsx = NV50_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
235 tsy = NV50_TILE_SIZE_Y(lvl->tile_mode);
236 tsz = NV50_TILE_SIZE_Z(lvl->tile_mode);
237
238 lvl->pitch = align(nbx * blocksize, tsx);
239
240 mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
241
242 w = u_minify(w, 1);
243 h = u_minify(h, 1);
244 d = u_minify(d, 1);
245 }
246
247 if (pt->array_size > 1) {
248 mt->layer_stride = align(mt->total_size,
249 NV50_TILE_SIZE(mt->level[0].tile_mode));
250 mt->total_size = mt->layer_stride * pt->array_size;
251 }
252 }
253
254 struct pipe_resource *
255 nv50_miptree_create(struct pipe_screen *pscreen,
256 const struct pipe_resource *templ)
257 {
258 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
259 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
260 struct pipe_resource *pt = &mt->base.base;
261 int ret;
262 union nouveau_bo_config bo_config;
263 uint32_t bo_flags;
264
265 if (!mt)
266 return NULL;
267
268 mt->base.vtbl = &nv50_miptree_vtbl;
269 *pt = *templ;
270 pipe_reference_init(&pt->reference, 1);
271 pt->screen = pscreen;
272
273 bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, TRUE);
274
275 if (!nv50_miptree_init_ms_mode(mt)) {
276 FREE(mt);
277 return NULL;
278 }
279
280 if (bo_config.nv50.memtype != 0) {
281 nv50_miptree_init_layout_tiled(mt);
282 } else
283 if (!nv50_miptree_init_layout_linear(mt)) {
284 FREE(mt);
285 return NULL;
286 }
287 bo_config.nv50.tile_mode = mt->level[0].tile_mode;
288
289 bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP;
290 if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
291 bo_flags |= NOUVEAU_BO_CONTIG;
292
293 ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
294 &mt->base.bo);
295 if (ret) {
296 FREE(mt);
297 return NULL;
298 }
299 mt->base.domain = NOUVEAU_BO_VRAM;
300 mt->base.address = mt->base.bo->offset;
301
302 return pt;
303 }
304
305 struct pipe_resource *
306 nv50_miptree_from_handle(struct pipe_screen *pscreen,
307 const struct pipe_resource *templ,
308 struct winsys_handle *whandle)
309 {
310 struct nv50_miptree *mt;
311 unsigned stride;
312
313 /* only supports 2D, non-mipmapped textures for the moment */
314 if ((templ->target != PIPE_TEXTURE_2D &&
315 templ->target != PIPE_TEXTURE_RECT) ||
316 templ->last_level != 0 ||
317 templ->depth0 != 1 ||
318 templ->array_size > 1)
319 return NULL;
320
321 mt = CALLOC_STRUCT(nv50_miptree);
322 if (!mt)
323 return NULL;
324
325 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
326 if (mt->base.bo == NULL) {
327 FREE(mt);
328 return NULL;
329 }
330 mt->base.domain = NOUVEAU_BO_VRAM;
331 mt->base.address = mt->base.bo->offset;
332
333 mt->base.base = *templ;
334 mt->base.vtbl = &nv50_miptree_vtbl;
335 pipe_reference_init(&mt->base.base.reference, 1);
336 mt->base.base.screen = pscreen;
337 mt->level[0].pitch = stride;
338 mt->level[0].offset = 0;
339 mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
340
341 /* no need to adjust bo reference count */
342 return &mt->base.base;
343 }
344
345
346 /* Offset of zslice @z from start of level @l. */
347 INLINE unsigned
348 nv50_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
349 {
350 const struct pipe_resource *pt = &mt->base.base;
351
352 unsigned tds = NV50_TILE_SHIFT_Z(mt->level[l].tile_mode);
353 unsigned ths = NV50_TILE_SHIFT_Y(mt->level[l].tile_mode);
354
355 unsigned nby = util_format_get_nblocksy(pt->format,
356 u_minify(pt->height0, l));
357
358 /* to next 2D tile slice within a 3D tile */
359 unsigned stride_2d = NV50_TILE_SIZE_2D(mt->level[l].tile_mode);
360
361 /* to slice in the next (in z direction) 3D tile */
362 unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
363
364 return (z & ((1 << tds) - 1)) * stride_2d + (z >> tds) * stride_3d;
365 }
366
367 /* Surface functions.
368 */
369
370 struct nv50_surface *
371 nv50_surface_from_miptree(struct nv50_miptree *mt,
372 const struct pipe_surface *templ)
373 {
374 struct pipe_surface *ps;
375 struct nv50_surface *ns = CALLOC_STRUCT(nv50_surface);
376 if (!ns)
377 return NULL;
378 ps = &ns->base;
379
380 pipe_reference_init(&ps->reference, 1);
381 pipe_resource_reference(&ps->texture, &mt->base.base);
382
383 ps->format = templ->format;
384 ps->usage = templ->usage;
385 ps->u.tex.level = templ->u.tex.level;
386 ps->u.tex.first_layer = templ->u.tex.first_layer;
387 ps->u.tex.last_layer = templ->u.tex.last_layer;
388
389 ns->width = u_minify(mt->base.base.width0, ps->u.tex.level);
390 ns->height = u_minify(mt->base.base.height0, ps->u.tex.level);
391 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
392 ns->offset = mt->level[templ->u.tex.level].offset;
393
394 /* comment says there are going to be removed, but they're used by the st */
395 ps->width = ns->width;
396 ps->height = ns->height;
397
398 ns->width <<= mt->ms_x;
399 ns->height <<= mt->ms_y;
400
401 return ns;
402 }
403
404 struct pipe_surface *
405 nv50_miptree_surface_new(struct pipe_context *pipe,
406 struct pipe_resource *pt,
407 const struct pipe_surface *templ)
408 {
409 struct nv50_miptree *mt = nv50_miptree(pt);
410 struct nv50_surface *ns = nv50_surface_from_miptree(mt, templ);
411 if (!ns)
412 return NULL;
413 ns->base.context = pipe;
414
415 if (ns->base.u.tex.first_layer) {
416 const unsigned l = ns->base.u.tex.level;
417 const unsigned z = ns->base.u.tex.first_layer;
418
419 if (mt->layout_3d) {
420 ns->offset += nv50_mt_zslice_offset(mt, l, z);
421
422 /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
423 if (ns->depth > 1 &&
424 (z & (NV50_TILE_SIZE_Z(mt->level[l].tile_mode) - 1)))
425 NOUVEAU_ERR("Creating unsupported 3D surface !\n");
426 } else {
427 ns->offset += mt->layer_stride * z;
428 }
429 }
430
431 return &ns->base;
432 }