xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/nouveau/nv50/nv50_miptree.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "util/u_inlines.h"
26 #include "util/format/u_format.h"
27 
28 #include "nv50/nv50_context.h"
29 #include "nv50/nv50_resource.h"
30 
31 uint32_t
nv50_tex_choose_tile_dims_helper(unsigned nx,unsigned ny,unsigned nz,bool is_3d)32 nv50_tex_choose_tile_dims_helper(unsigned nx, unsigned ny, unsigned nz,
33                                  bool is_3d)
34 {
35    uint32_t tile_mode = 0x000;
36 
37    if (ny > 64) tile_mode = 0x040; /* height 128 tiles */
38    else
39    if (ny > 32) tile_mode = 0x030; /* height 64 tiles */
40    else
41    if (ny > 16) tile_mode = 0x020; /* height 32 tiles */
42    else
43    if (ny >  8) tile_mode = 0x010; /* height 16 tiles */
44 
45    if (!is_3d)
46       return tile_mode;
47    else
48       if (tile_mode > 0x020)
49          tile_mode = 0x020;
50 
51    if (nz > 16 && tile_mode < 0x020)
52       return tile_mode | 0x500; /* depth 32 tiles */
53    if (nz > 8) return tile_mode | 0x400; /* depth 16 tiles */
54    if (nz > 4) return tile_mode | 0x300; /* depth 8 tiles */
55    if (nz > 2) return tile_mode | 0x200; /* depth 4 tiles */
56    if (nz > 1) return tile_mode | 0x100; /* depth 2 tiles */
57 
58    return tile_mode;
59 }
60 
61 static uint32_t
nv50_tex_choose_tile_dims(unsigned nx,unsigned ny,unsigned nz,bool is_3d)62 nv50_tex_choose_tile_dims(unsigned nx, unsigned ny, unsigned nz, bool is_3d)
63 {
64    return nv50_tex_choose_tile_dims_helper(nx, ny * 2, nz, is_3d);
65 }
66 
67 static uint32_t
nv50_mt_choose_storage_type(struct nv50_miptree * mt,bool compressed)68 nv50_mt_choose_storage_type(struct nv50_miptree *mt, bool compressed)
69 {
70    const unsigned ms = util_logbase2(mt->base.base.nr_samples);
71    uint32_t tile_flags;
72 
73    if (unlikely(mt->base.base.flags & NOUVEAU_RESOURCE_FLAG_LINEAR))
74       return 0;
75    if (unlikely(mt->base.base.bind & PIPE_BIND_CURSOR))
76       return 0;
77 
78    switch (mt->base.base.format) {
79    case PIPE_FORMAT_Z16_UNORM:
80       tile_flags = 0x6c + ms;
81       break;
82    case PIPE_FORMAT_X8Z24_UNORM:
83    case PIPE_FORMAT_S8X24_UINT:
84    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
85       tile_flags = 0x18 + ms;
86       break;
87    case PIPE_FORMAT_X24S8_UINT:
88    case PIPE_FORMAT_Z24X8_UNORM:
89    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
90       tile_flags = 0x128 + ms;
91       break;
92    case PIPE_FORMAT_Z32_FLOAT:
93       tile_flags = 0x40 + ms;
94       break;
95    case PIPE_FORMAT_X32_S8X24_UINT:
96    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
97       tile_flags = 0x60 + ms;
98       break;
99    default:
100       /* Most color formats don't work with compression. */
101       compressed = false;
102       FALLTHROUGH;
103    case PIPE_FORMAT_R8G8B8A8_UNORM:
104    case PIPE_FORMAT_R8G8B8A8_SRGB:
105    case PIPE_FORMAT_R8G8B8X8_UNORM:
106    case PIPE_FORMAT_R8G8B8X8_SRGB:
107    case PIPE_FORMAT_B8G8R8A8_UNORM:
108    case PIPE_FORMAT_B8G8R8A8_SRGB:
109    case PIPE_FORMAT_B8G8R8X8_UNORM:
110    case PIPE_FORMAT_B8G8R8X8_SRGB:
111    case PIPE_FORMAT_R10G10B10A2_UNORM:
112    case PIPE_FORMAT_B10G10R10A2_UNORM:
113    case PIPE_FORMAT_R16G16B16A16_FLOAT:
114    case PIPE_FORMAT_R16G16B16X16_FLOAT:
115    case PIPE_FORMAT_R11G11B10_FLOAT:
116       switch (util_format_get_blocksizebits(mt->base.base.format)) {
117       case 128:
118          assert(ms < 3);
119          tile_flags = 0x74;
120          break;
121       case 64:
122          switch (ms) {
123          case 2: tile_flags = 0xfc; break;
124          case 3: tile_flags = 0xfd; break;
125          default:
126             tile_flags = 0x70;
127             break;
128          }
129          break;
130       case 32:
131          if (mt->base.base.bind & PIPE_BIND_SCANOUT) {
132             assert(ms == 0);
133             tile_flags = 0x7a;
134          } else {
135             switch (ms) {
136             case 2: tile_flags = 0xf8; break;
137             case 3: tile_flags = 0xf9; break;
138             default:
139                tile_flags = 0x70;
140                break;
141             }
142          }
143          break;
144       case 16:
145       case 8:
146          tile_flags = 0x70;
147          break;
148       default:
149          return 0;
150       }
151       if (mt->base.base.bind & PIPE_BIND_CURSOR)
152          tile_flags = 0;
153    }
154 
155    if (!compressed)
156       tile_flags &= ~0x180;
157 
158    return tile_flags;
159 }
160 
161 void
nv50_miptree_destroy(struct pipe_screen * pscreen,struct pipe_resource * pt)162 nv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
163 {
164    struct nv50_miptree *mt = nv50_miptree(pt);
165 
166    nouveau_fence_work(mt->base.fence, nouveau_fence_unref_bo, mt->base.bo);
167    nouveau_fence_ref(NULL, &mt->base.fence);
168    nouveau_fence_ref(NULL, &mt->base.fence_wr);
169 
170    NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, -1);
171    NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_bytes,
172                     -(uint64_t)mt->total_size);
173 
174    FREE(mt);
175 }
176 
177 bool
nv50_miptree_get_handle(struct pipe_screen * pscreen,struct pipe_context * context,struct pipe_resource * pt,struct winsys_handle * whandle,unsigned usage)178 nv50_miptree_get_handle(struct pipe_screen *pscreen,
179                         struct pipe_context *context,
180                         struct pipe_resource *pt,
181                         struct winsys_handle *whandle,
182                         unsigned usage)
183 {
184    if (pt->target == PIPE_BUFFER)
185       return false;
186 
187    struct nv50_miptree *mt = nv50_miptree(pt);
188    unsigned stride;
189 
190    if (!mt || !mt->base.bo)
191       return false;
192 
193    stride = mt->level[0].pitch;
194 
195    return nouveau_screen_bo_get_handle(pscreen,
196                                        mt->base.bo,
197                                        stride,
198                                        whandle);
199 }
200 
201 static inline bool
nv50_miptree_init_ms_mode(struct nv50_miptree * mt)202 nv50_miptree_init_ms_mode(struct nv50_miptree *mt)
203 {
204    switch (mt->base.base.nr_samples) {
205    case 8:
206       mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS8;
207       mt->ms_x = 2;
208       mt->ms_y = 1;
209       break;
210    case 4:
211       mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS4;
212       mt->ms_x = 1;
213       mt->ms_y = 1;
214       break;
215    case 2:
216       mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS2;
217       mt->ms_x = 1;
218       break;
219    case 1:
220    case 0:
221       mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS1;
222       break;
223    default:
224       NOUVEAU_ERR("invalid nr_samples: %u\n", mt->base.base.nr_samples);
225       return false;
226    }
227    return true;
228 }
229 
230 bool
nv50_miptree_init_layout_linear(struct nv50_miptree * mt,unsigned pitch_align)231 nv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
232 {
233    struct pipe_resource *pt = &mt->base.base;
234    const unsigned blocksize = util_format_get_blocksize(pt->format);
235    unsigned h = pt->height0;
236 
237    if (util_format_is_depth_or_stencil(pt->format))
238       return false;
239 
240    if ((pt->last_level > 0) || (pt->depth0 > 1) || (pt->array_size > 1))
241       return false;
242    if (mt->ms_x | mt->ms_y)
243       return false;
244 
245    mt->level[0].pitch = align(pt->width0 * blocksize, pitch_align);
246 
247    /* Account for very generous prefetch (allocate size as if tiled). */
248    h = MAX2(h, 8);
249    h = util_next_power_of_two(h);
250 
251    mt->total_size = mt->level[0].pitch * h;
252 
253    return true;
254 }
255 
256 static void
nv50_miptree_init_layout_video(struct nv50_miptree * mt)257 nv50_miptree_init_layout_video(struct nv50_miptree *mt)
258 {
259    const struct pipe_resource *pt = &mt->base.base;
260    const unsigned blocksize = util_format_get_blocksize(pt->format);
261 
262    assert(pt->last_level == 0);
263    assert(mt->ms_x == 0 && mt->ms_y == 0);
264    assert(!util_format_is_compressed(pt->format));
265 
266    mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
267 
268    mt->level[0].tile_mode = 0x20;
269    mt->level[0].pitch = align(pt->width0 * blocksize, 64);
270    mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
271 
272    if (pt->array_size > 1) {
273       mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
274       mt->total_size = mt->layer_stride * pt->array_size;
275    }
276 }
277 
278 static void
nv50_miptree_init_layout_tiled(struct nv50_miptree * mt)279 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
280 {
281    struct pipe_resource *pt = &mt->base.base;
282    unsigned w, h, d, l;
283    const unsigned blocksize = util_format_get_blocksize(pt->format);
284 
285    mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
286 
287    w = pt->width0 << mt->ms_x;
288    h = pt->height0 << mt->ms_y;
289 
290    /* For 3D textures, a mipmap is spanned by all the layers, for array
291     * textures and cube maps, each layer contains its own mipmaps.
292     */
293    d = mt->layout_3d ? pt->depth0 : 1;
294 
295    for (l = 0; l <= pt->last_level; ++l) {
296       struct nv50_miptree_level *lvl = &mt->level[l];
297       unsigned tsx, tsy, tsz;
298       unsigned nbx = util_format_get_nblocksx(pt->format, w);
299       unsigned nby = util_format_get_nblocksy(pt->format, h);
300 
301       lvl->offset = mt->total_size;
302 
303       lvl->tile_mode = nv50_tex_choose_tile_dims(nbx, nby, d, mt->layout_3d);
304 
305       tsx = NV50_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
306       tsy = NV50_TILE_SIZE_Y(lvl->tile_mode);
307       tsz = NV50_TILE_SIZE_Z(lvl->tile_mode);
308 
309       lvl->pitch = align(nbx * blocksize, tsx);
310 
311       mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
312 
313       w = u_minify(w, 1);
314       h = u_minify(h, 1);
315       d = u_minify(d, 1);
316    }
317 
318    if (pt->array_size > 1) {
319       mt->layer_stride = align(mt->total_size,
320                                NV50_TILE_SIZE(mt->level[0].tile_mode));
321       mt->total_size = mt->layer_stride * pt->array_size;
322    }
323 }
324 
325 struct pipe_resource *
nv50_miptree_create(struct pipe_screen * pscreen,const struct pipe_resource * templ)326 nv50_miptree_create(struct pipe_screen *pscreen,
327                     const struct pipe_resource *templ)
328 {
329    struct nouveau_device *dev = nouveau_screen(pscreen)->device;
330    struct nouveau_drm *drm = nouveau_screen(pscreen)->drm;
331    struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
332    struct pipe_resource *pt = &mt->base.base;
333    bool compressed = drm->version >= 0x01000101;
334    int ret;
335    union nouveau_bo_config bo_config;
336    uint32_t bo_flags;
337    unsigned pitch_align;
338 
339    if (!mt)
340       return NULL;
341 
342    *pt = *templ;
343    pipe_reference_init(&pt->reference, 1);
344    pt->screen = pscreen;
345 
346    if (pt->bind & PIPE_BIND_LINEAR)
347       pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
348 
349    bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, compressed);
350 
351    if (!nv50_miptree_init_ms_mode(mt)) {
352       FREE(mt);
353       return NULL;
354    }
355 
356    if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
357       nv50_miptree_init_layout_video(mt);
358       if (pt->flags & NV50_RESOURCE_FLAG_NOALLOC) {
359          /* BO allocation done by client */
360          return pt;
361       }
362    } else
363    if (bo_config.nv50.memtype != 0) {
364       nv50_miptree_init_layout_tiled(mt);
365    } else {
366       if (pt->usage & PIPE_BIND_CURSOR)
367          pitch_align = 1;
368       else if (pt->usage & PIPE_BIND_SCANOUT)
369          pitch_align = 256;
370       else
371          pitch_align = 64;
372       if (!nv50_miptree_init_layout_linear(mt, pitch_align)) {
373          FREE(mt);
374          return NULL;
375       }
376    }
377    bo_config.nv50.tile_mode = mt->level[0].tile_mode;
378 
379    if (!bo_config.nv50.memtype && (pt->bind & PIPE_BIND_SHARED))
380       mt->base.domain = NOUVEAU_BO_GART;
381    else
382       mt->base.domain = NV_VRAM_DOMAIN(nouveau_screen(pscreen));
383 
384    bo_flags = mt->base.domain | NOUVEAU_BO_NOSNOOP;
385    if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
386       bo_flags |= NOUVEAU_BO_CONTIG;
387 
388    ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
389                         &mt->base.bo);
390    if (ret) {
391       FREE(mt);
392       return NULL;
393    }
394    mt->base.address = mt->base.bo->offset;
395 
396    return pt;
397 }
398 
399 struct pipe_resource *
nv50_miptree_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * templ,struct winsys_handle * whandle)400 nv50_miptree_from_handle(struct pipe_screen *pscreen,
401                          const struct pipe_resource *templ,
402                          struct winsys_handle *whandle)
403 {
404    struct nv50_miptree *mt;
405    unsigned stride;
406 
407    /* only supports 2D, non-mipmapped textures for the moment */
408    if ((templ->target != PIPE_TEXTURE_2D &&
409         templ->target != PIPE_TEXTURE_RECT) ||
410        templ->last_level != 0 ||
411        templ->depth0 != 1 ||
412        templ->array_size > 1)
413       return NULL;
414 
415    mt = CALLOC_STRUCT(nv50_miptree);
416    if (!mt)
417       return NULL;
418 
419    mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
420    if (mt->base.bo == NULL) {
421       FREE(mt);
422       return NULL;
423    }
424    mt->base.domain = mt->base.bo->flags & NOUVEAU_BO_APER;
425    mt->base.address = mt->base.bo->offset;
426 
427    mt->base.base = *templ;
428    pipe_reference_init(&mt->base.base.reference, 1);
429    mt->base.base.screen = pscreen;
430    mt->level[0].pitch = stride;
431    mt->level[0].offset = 0;
432    mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
433 
434    NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, 1);
435 
436    /* no need to adjust bo reference count */
437    return &mt->base.base;
438 }
439 
440 
441 /* Offset of zslice @z from start of level @l. */
442 inline unsigned
nv50_mt_zslice_offset(const struct nv50_miptree * mt,unsigned l,unsigned z)443 nv50_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
444 {
445    const struct pipe_resource *pt = &mt->base.base;
446 
447    unsigned tds = NV50_TILE_SHIFT_Z(mt->level[l].tile_mode);
448    unsigned ths = NV50_TILE_SHIFT_Y(mt->level[l].tile_mode);
449 
450    unsigned nby = util_format_get_nblocksy(pt->format,
451                                            u_minify(pt->height0, l));
452 
453    /* to next 2D tile slice within a 3D tile */
454    unsigned stride_2d = NV50_TILE_SIZE_2D(mt->level[l].tile_mode);
455 
456    /* to slice in the next (in z direction) 3D tile */
457    unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
458 
459    return (z & ((1 << tds) - 1)) * stride_2d + (z >> tds) * stride_3d;
460 }
461 
462 /* Surface functions.
463  */
464 
465 struct nv50_surface *
nv50_surface_from_miptree(struct nv50_miptree * mt,const struct pipe_surface * templ)466 nv50_surface_from_miptree(struct nv50_miptree *mt,
467                           const struct pipe_surface *templ)
468 {
469    struct pipe_surface *ps;
470    struct nv50_surface *ns = CALLOC_STRUCT(nv50_surface);
471    if (!ns)
472       return NULL;
473    ps = &ns->base;
474 
475    pipe_reference_init(&ps->reference, 1);
476    pipe_resource_reference(&ps->texture, &mt->base.base);
477 
478    ps->format = templ->format;
479    ps->writable = templ->writable;
480    ps->u.tex.level = templ->u.tex.level;
481    ps->u.tex.first_layer = templ->u.tex.first_layer;
482    ps->u.tex.last_layer = templ->u.tex.last_layer;
483 
484    ns->width = u_minify(mt->base.base.width0, ps->u.tex.level);
485    ns->height = u_minify(mt->base.base.height0, ps->u.tex.level);
486    ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
487    ns->offset = mt->level[templ->u.tex.level].offset;
488 
489    /* comment says there are going to be removed, but they're used by the st */
490    ps->width = ns->width;
491    ps->height = ns->height;
492 
493    ns->width <<= mt->ms_x;
494    ns->height <<= mt->ms_y;
495 
496    return ns;
497 }
498 
499 struct pipe_surface *
nv50_miptree_surface_new(struct pipe_context * pipe,struct pipe_resource * pt,const struct pipe_surface * templ)500 nv50_miptree_surface_new(struct pipe_context *pipe,
501                          struct pipe_resource *pt,
502                          const struct pipe_surface *templ)
503 {
504    struct nv50_miptree *mt = nv50_miptree(pt);
505    struct nv50_surface *ns = nv50_surface_from_miptree(mt, templ);
506    if (!ns)
507       return NULL;
508    ns->base.context = pipe;
509 
510    if (ns->base.u.tex.first_layer) {
511       const unsigned l = ns->base.u.tex.level;
512       const unsigned z = ns->base.u.tex.first_layer;
513 
514       if (mt->layout_3d) {
515          ns->offset += nv50_mt_zslice_offset(mt, l, z);
516 
517          /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
518          if (ns->depth > 1 &&
519              (z & (NV50_TILE_SIZE_Z(mt->level[l].tile_mode) - 1)))
520             NOUVEAU_ERR("Creating unsupported 3D surface !\n");
521       } else {
522          ns->offset += mt->layer_stride * z;
523       }
524    }
525 
526    return &ns->base;
527 }
528