Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / drivers / nouveau / 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 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/u_format.h"
27
28 #include "nv50/nv50_context.h"
29 #include "nv50/nv50_resource.h"
30
31 uint32_t
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
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
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
162 nv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
163 {
164 struct nv50_miptree *mt = nv50_miptree(pt);
165
166 if (mt->base.fence && mt->base.fence->state < NOUVEAU_FENCE_STATE_FLUSHED)
167 nouveau_fence_work(mt->base.fence, nouveau_fence_unref_bo, mt->base.bo);
168 else
169 nouveau_bo_ref(NULL, &mt->base.bo);
170
171 nouveau_fence_ref(NULL, &mt->base.fence);
172 nouveau_fence_ref(NULL, &mt->base.fence_wr);
173
174 NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, -1);
175 NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_bytes,
176 -(uint64_t)mt->total_size);
177
178 FREE(mt);
179 }
180
181 boolean
182 nv50_miptree_get_handle(struct pipe_screen *pscreen,
183 struct pipe_resource *pt,
184 struct winsys_handle *whandle)
185 {
186 struct nv50_miptree *mt = nv50_miptree(pt);
187 unsigned stride;
188
189 if (!mt || !mt->base.bo)
190 return false;
191
192 stride = mt->level[0].pitch;
193
194 return nouveau_screen_bo_get_handle(pscreen,
195 mt->base.bo,
196 stride,
197 whandle);
198 }
199
200 const struct u_resource_vtbl nv50_miptree_vtbl =
201 {
202 nv50_miptree_get_handle, /* get_handle */
203 nv50_miptree_destroy, /* resource_destroy */
204 nv50_miptree_transfer_map, /* transfer_map */
205 u_default_transfer_flush_region, /* transfer_flush_region */
206 nv50_miptree_transfer_unmap, /* transfer_unmap */
207 u_default_transfer_inline_write /* transfer_inline_write */
208 };
209
210 static inline bool
211 nv50_miptree_init_ms_mode(struct nv50_miptree *mt)
212 {
213 switch (mt->base.base.nr_samples) {
214 case 8:
215 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS8;
216 mt->ms_x = 2;
217 mt->ms_y = 1;
218 break;
219 case 4:
220 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS4;
221 mt->ms_x = 1;
222 mt->ms_y = 1;
223 break;
224 case 2:
225 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS2;
226 mt->ms_x = 1;
227 break;
228 case 1:
229 case 0:
230 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS1;
231 break;
232 default:
233 NOUVEAU_ERR("invalid nr_samples: %u\n", mt->base.base.nr_samples);
234 return false;
235 }
236 return true;
237 }
238
239 bool
240 nv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
241 {
242 struct pipe_resource *pt = &mt->base.base;
243 const unsigned blocksize = util_format_get_blocksize(pt->format);
244 unsigned h = pt->height0;
245
246 if (util_format_is_depth_or_stencil(pt->format))
247 return false;
248
249 if ((pt->last_level > 0) || (pt->depth0 > 1) || (pt->array_size > 1))
250 return false;
251 if (mt->ms_x | mt->ms_y)
252 return false;
253
254 mt->level[0].pitch = align(pt->width0 * blocksize, pitch_align);
255
256 /* Account for very generous prefetch (allocate size as if tiled). */
257 h = MAX2(h, 8);
258 h = util_next_power_of_two(h);
259
260 mt->total_size = mt->level[0].pitch * h;
261
262 return true;
263 }
264
265 static void
266 nv50_miptree_init_layout_video(struct nv50_miptree *mt)
267 {
268 const struct pipe_resource *pt = &mt->base.base;
269 const unsigned blocksize = util_format_get_blocksize(pt->format);
270
271 assert(pt->last_level == 0);
272 assert(mt->ms_x == 0 && mt->ms_y == 0);
273 assert(!util_format_is_compressed(pt->format));
274
275 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
276
277 mt->level[0].tile_mode = 0x20;
278 mt->level[0].pitch = align(pt->width0 * blocksize, 64);
279 mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
280
281 if (pt->array_size > 1) {
282 mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
283 mt->total_size = mt->layer_stride * pt->array_size;
284 }
285 }
286
287 static void
288 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
289 {
290 struct pipe_resource *pt = &mt->base.base;
291 unsigned w, h, d, l;
292 const unsigned blocksize = util_format_get_blocksize(pt->format);
293
294 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
295
296 w = pt->width0 << mt->ms_x;
297 h = pt->height0 << mt->ms_y;
298
299 /* For 3D textures, a mipmap is spanned by all the layers, for array
300 * textures and cube maps, each layer contains its own mipmaps.
301 */
302 d = mt->layout_3d ? pt->depth0 : 1;
303
304 for (l = 0; l <= pt->last_level; ++l) {
305 struct nv50_miptree_level *lvl = &mt->level[l];
306 unsigned tsx, tsy, tsz;
307 unsigned nbx = util_format_get_nblocksx(pt->format, w);
308 unsigned nby = util_format_get_nblocksy(pt->format, h);
309
310 lvl->offset = mt->total_size;
311
312 lvl->tile_mode = nv50_tex_choose_tile_dims(nbx, nby, d, mt->layout_3d);
313
314 tsx = NV50_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
315 tsy = NV50_TILE_SIZE_Y(lvl->tile_mode);
316 tsz = NV50_TILE_SIZE_Z(lvl->tile_mode);
317
318 lvl->pitch = align(nbx * blocksize, tsx);
319
320 mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
321
322 w = u_minify(w, 1);
323 h = u_minify(h, 1);
324 d = u_minify(d, 1);
325 }
326
327 if (pt->array_size > 1) {
328 mt->layer_stride = align(mt->total_size,
329 NV50_TILE_SIZE(mt->level[0].tile_mode));
330 mt->total_size = mt->layer_stride * pt->array_size;
331 }
332 }
333
334 struct pipe_resource *
335 nv50_miptree_create(struct pipe_screen *pscreen,
336 const struct pipe_resource *templ)
337 {
338 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
339 struct nouveau_drm *drm = nouveau_screen(pscreen)->drm;
340 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
341 struct pipe_resource *pt = &mt->base.base;
342 bool compressed = drm->version >= 0x01000101;
343 int ret;
344 union nouveau_bo_config bo_config;
345 uint32_t bo_flags;
346
347 if (!mt)
348 return NULL;
349
350 mt->base.vtbl = &nv50_miptree_vtbl;
351 *pt = *templ;
352 pipe_reference_init(&pt->reference, 1);
353 pt->screen = pscreen;
354
355 if (pt->bind & PIPE_BIND_LINEAR)
356 pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
357
358 bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, compressed);
359
360 if (!nv50_miptree_init_ms_mode(mt)) {
361 FREE(mt);
362 return NULL;
363 }
364
365 if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
366 nv50_miptree_init_layout_video(mt);
367 if (pt->flags & NV50_RESOURCE_FLAG_NOALLOC) {
368 /* BO allocation done by client */
369 return pt;
370 }
371 } else
372 if (bo_config.nv50.memtype != 0) {
373 nv50_miptree_init_layout_tiled(mt);
374 } else
375 if (!nv50_miptree_init_layout_linear(mt, 64)) {
376 FREE(mt);
377 return NULL;
378 }
379 bo_config.nv50.tile_mode = mt->level[0].tile_mode;
380
381 if (!bo_config.nv50.memtype && (pt->bind & PIPE_BIND_SHARED))
382 mt->base.domain = NOUVEAU_BO_GART;
383 else
384 mt->base.domain = NV_VRAM_DOMAIN(nouveau_screen(pscreen));
385
386 bo_flags = mt->base.domain | NOUVEAU_BO_NOSNOOP;
387 if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
388 bo_flags |= NOUVEAU_BO_CONTIG;
389
390 ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
391 &mt->base.bo);
392 if (ret) {
393 FREE(mt);
394 return NULL;
395 }
396 mt->base.address = mt->base.bo->offset;
397
398 return pt;
399 }
400
401 struct pipe_resource *
402 nv50_miptree_from_handle(struct pipe_screen *pscreen,
403 const struct pipe_resource *templ,
404 struct winsys_handle *whandle)
405 {
406 struct nv50_miptree *mt;
407 unsigned stride;
408
409 /* only supports 2D, non-mipmapped textures for the moment */
410 if ((templ->target != PIPE_TEXTURE_2D &&
411 templ->target != PIPE_TEXTURE_RECT) ||
412 templ->last_level != 0 ||
413 templ->depth0 != 1 ||
414 templ->array_size > 1)
415 return NULL;
416
417 mt = CALLOC_STRUCT(nv50_miptree);
418 if (!mt)
419 return NULL;
420
421 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
422 if (mt->base.bo == NULL) {
423 FREE(mt);
424 return NULL;
425 }
426 mt->base.domain = mt->base.bo->flags & NOUVEAU_BO_APER;
427 mt->base.address = mt->base.bo->offset;
428
429 mt->base.base = *templ;
430 mt->base.vtbl = &nv50_miptree_vtbl;
431 pipe_reference_init(&mt->base.base.reference, 1);
432 mt->base.base.screen = pscreen;
433 mt->level[0].pitch = stride;
434 mt->level[0].offset = 0;
435 mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
436
437 NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, 1);
438
439 /* no need to adjust bo reference count */
440 return &mt->base.base;
441 }
442
443
444 /* Offset of zslice @z from start of level @l. */
445 inline unsigned
446 nv50_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
447 {
448 const struct pipe_resource *pt = &mt->base.base;
449
450 unsigned tds = NV50_TILE_SHIFT_Z(mt->level[l].tile_mode);
451 unsigned ths = NV50_TILE_SHIFT_Y(mt->level[l].tile_mode);
452
453 unsigned nby = util_format_get_nblocksy(pt->format,
454 u_minify(pt->height0, l));
455
456 /* to next 2D tile slice within a 3D tile */
457 unsigned stride_2d = NV50_TILE_SIZE_2D(mt->level[l].tile_mode);
458
459 /* to slice in the next (in z direction) 3D tile */
460 unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
461
462 return (z & ((1 << tds) - 1)) * stride_2d + (z >> tds) * stride_3d;
463 }
464
465 /* Surface functions.
466 */
467
468 struct nv50_surface *
469 nv50_surface_from_miptree(struct nv50_miptree *mt,
470 const struct pipe_surface *templ)
471 {
472 struct pipe_surface *ps;
473 struct nv50_surface *ns = CALLOC_STRUCT(nv50_surface);
474 if (!ns)
475 return NULL;
476 ps = &ns->base;
477
478 pipe_reference_init(&ps->reference, 1);
479 pipe_resource_reference(&ps->texture, &mt->base.base);
480
481 ps->format = templ->format;
482 ps->writable = templ->writable;
483 ps->u.tex.level = templ->u.tex.level;
484 ps->u.tex.first_layer = templ->u.tex.first_layer;
485 ps->u.tex.last_layer = templ->u.tex.last_layer;
486
487 ns->width = u_minify(mt->base.base.width0, ps->u.tex.level);
488 ns->height = u_minify(mt->base.base.height0, ps->u.tex.level);
489 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
490 ns->offset = mt->level[templ->u.tex.level].offset;
491
492 /* comment says there are going to be removed, but they're used by the st */
493 ps->width = ns->width;
494 ps->height = ns->height;
495
496 ns->width <<= mt->ms_x;
497 ns->height <<= mt->ms_y;
498
499 return ns;
500 }
501
502 struct pipe_surface *
503 nv50_miptree_surface_new(struct pipe_context *pipe,
504 struct pipe_resource *pt,
505 const struct pipe_surface *templ)
506 {
507 struct nv50_miptree *mt = nv50_miptree(pt);
508 struct nv50_surface *ns = nv50_surface_from_miptree(mt, templ);
509 if (!ns)
510 return NULL;
511 ns->base.context = pipe;
512
513 if (ns->base.u.tex.first_layer) {
514 const unsigned l = ns->base.u.tex.level;
515 const unsigned z = ns->base.u.tex.first_layer;
516
517 if (mt->layout_3d) {
518 ns->offset += nv50_mt_zslice_offset(mt, l, z);
519
520 /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
521 if (ns->depth > 1 &&
522 (z & (NV50_TILE_SIZE_Z(mt->level[l].tile_mode) - 1)))
523 NOUVEAU_ERR("Creating unsupported 3D surface !\n");
524 } else {
525 ns->offset += mt->layer_stride * z;
526 }
527 }
528
529 return &ns->base;
530 }