568526c5933f830ec10405e11cd5cfe84218ef3b
[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 = mt->ms_x + mt->ms_y;
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;
52 break;
53 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
54 tile_flags = 0x18 + ms;
55 break;
56 case PIPE_FORMAT_Z24X8_UNORM:
57 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
58 tile_flags = 0x128 + ms;
59 break;
60 case PIPE_FORMAT_Z32_FLOAT:
61 tile_flags = 0x40 + ms;
62 break;
63 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
64 tile_flags = 0x60 + ms;
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 = mt->level[0].pitch;
137
138 return nouveau_screen_bo_get_handle(pscreen,
139 mt->base.bo,
140 stride,
141 whandle);
142 }
143
144 const struct u_resource_vtbl nv50_miptree_vtbl =
145 {
146 nv50_miptree_get_handle, /* get_handle */
147 nv50_miptree_destroy, /* resource_destroy */
148 nv50_miptree_transfer_map, /* transfer_map */
149 u_default_transfer_flush_region, /* transfer_flush_region */
150 nv50_miptree_transfer_unmap, /* transfer_unmap */
151 u_default_transfer_inline_write /* transfer_inline_write */
152 };
153
154 static INLINE boolean
155 nv50_miptree_init_ms_mode(struct nv50_miptree *mt)
156 {
157 switch (mt->base.base.nr_samples) {
158 case 8:
159 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS8;
160 mt->ms_x = 2;
161 mt->ms_y = 1;
162 break;
163 case 4:
164 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS4;
165 mt->ms_x = 1;
166 mt->ms_y = 1;
167 break;
168 case 2:
169 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS2;
170 mt->ms_x = 1;
171 break;
172 case 1:
173 case 0:
174 mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS1;
175 break;
176 default:
177 NOUVEAU_ERR("invalid nr_samples: %u\n", mt->base.base.nr_samples);
178 return FALSE;
179 }
180 return TRUE;
181 }
182
183 boolean
184 nv50_miptree_init_layout_linear(struct nv50_miptree *mt)
185 {
186 struct pipe_resource *pt = &mt->base.base;
187 const unsigned blocksize = util_format_get_blocksize(pt->format);
188
189 if (util_format_is_depth_or_stencil(pt->format))
190 return FALSE;
191
192 if ((pt->last_level > 0) || (pt->depth0 > 1) || (pt->array_size > 1))
193 return FALSE;
194 if (mt->ms_x | mt->ms_y)
195 return FALSE;
196
197 mt->level[0].pitch = align(pt->width0 * blocksize, 64);
198
199 mt->total_size = mt->level[0].pitch * pt->height0;
200
201 return TRUE;
202 }
203
204 static void
205 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
206 {
207 struct pipe_resource *pt = &mt->base.base;
208 unsigned w, h, d, l;
209 const unsigned blocksize = util_format_get_blocksize(pt->format);
210
211 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
212
213 w = pt->width0 << mt->ms_x;
214 h = pt->height0 << mt->ms_y;
215
216 /* For 3D textures, a mipmap is spanned by all the layers, for array
217 * textures and cube maps, each layer contains its own mipmaps.
218 */
219 d = mt->layout_3d ? pt->depth0 : 1;
220
221 for (l = 0; l <= pt->last_level; ++l) {
222 struct nv50_miptree_level *lvl = &mt->level[l];
223 unsigned tsx, tsy, tsz;
224 unsigned nbx = util_format_get_nblocksx(pt->format, w);
225 unsigned nby = util_format_get_nblocksy(pt->format, h);
226
227 lvl->offset = mt->total_size;
228
229 lvl->tile_mode = nv50_tex_choose_tile_dims(nbx, nby, d);
230
231 tsx = NV50_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
232 tsy = NV50_TILE_SIZE_Y(lvl->tile_mode);
233 tsz = NV50_TILE_SIZE_Z(lvl->tile_mode);
234
235 lvl->pitch = align(nbx * blocksize, tsx);
236
237 mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
238
239 w = u_minify(w, 1);
240 h = u_minify(h, 1);
241 d = u_minify(d, 1);
242 }
243
244 if (pt->array_size > 1) {
245 mt->layer_stride = align(mt->total_size,
246 NV50_TILE_SIZE(mt->level[0].tile_mode));
247 mt->total_size = mt->layer_stride * pt->array_size;
248 }
249 }
250
251 struct pipe_resource *
252 nv50_miptree_create(struct pipe_screen *pscreen,
253 const struct pipe_resource *templ)
254 {
255 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
256 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
257 struct pipe_resource *pt = &mt->base.base;
258 int ret;
259 union nouveau_bo_config bo_config;
260 uint32_t bo_flags;
261
262 if (!mt)
263 return NULL;
264
265 mt->base.vtbl = &nv50_miptree_vtbl;
266 *pt = *templ;
267 pipe_reference_init(&pt->reference, 1);
268 pt->screen = pscreen;
269
270 bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, TRUE);
271
272 if (!nv50_miptree_init_ms_mode(mt)) {
273 FREE(mt);
274 return NULL;
275 }
276
277 if (bo_config.nv50.memtype != 0) {
278 nv50_miptree_init_layout_tiled(mt);
279 } else
280 if (!nv50_miptree_init_layout_linear(mt)) {
281 FREE(mt);
282 return NULL;
283 }
284 bo_config.nv50.tile_mode = mt->level[0].tile_mode;
285
286 bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP;
287 if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
288 bo_flags |= NOUVEAU_BO_CONTIG;
289
290 ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
291 &mt->base.bo);
292 if (ret) {
293 FREE(mt);
294 return NULL;
295 }
296 mt->base.domain = NOUVEAU_BO_VRAM;
297 mt->base.address = mt->base.bo->offset;
298
299 return pt;
300 }
301
302 struct pipe_resource *
303 nv50_miptree_from_handle(struct pipe_screen *pscreen,
304 const struct pipe_resource *templ,
305 struct winsys_handle *whandle)
306 {
307 struct nv50_miptree *mt;
308 unsigned stride;
309
310 /* only supports 2D, non-mipmapped textures for the moment */
311 if ((templ->target != PIPE_TEXTURE_2D &&
312 templ->target != PIPE_TEXTURE_RECT) ||
313 templ->last_level != 0 ||
314 templ->depth0 != 1 ||
315 templ->array_size > 1)
316 return NULL;
317
318 mt = CALLOC_STRUCT(nv50_miptree);
319 if (!mt)
320 return NULL;
321
322 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
323 if (mt->base.bo == NULL) {
324 FREE(mt);
325 return NULL;
326 }
327 mt->base.domain = NOUVEAU_BO_VRAM;
328 mt->base.address = mt->base.bo->offset;
329
330 mt->base.base = *templ;
331 mt->base.vtbl = &nv50_miptree_vtbl;
332 pipe_reference_init(&mt->base.base.reference, 1);
333 mt->base.base.screen = pscreen;
334 mt->level[0].pitch = stride;
335 mt->level[0].offset = 0;
336 mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
337
338 /* no need to adjust bo reference count */
339 return &mt->base.base;
340 }
341
342
343 /* Offset of zslice @z from start of level @l. */
344 INLINE unsigned
345 nv50_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
346 {
347 const struct pipe_resource *pt = &mt->base.base;
348
349 unsigned tds = NV50_TILE_SHIFT_Z(mt->level[l].tile_mode);
350 unsigned ths = NV50_TILE_SHIFT_Y(mt->level[l].tile_mode);
351
352 unsigned nby = util_format_get_nblocksy(pt->format,
353 u_minify(pt->height0, l));
354
355 /* to next 2D tile slice within a 3D tile */
356 unsigned stride_2d = NV50_TILE_SIZE_2D(mt->level[l].tile_mode);
357
358 /* to slice in the next (in z direction) 3D tile */
359 unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
360
361 return (z & ((1 << tds) - 1)) * stride_2d + (z >> tds) * stride_3d;
362 }
363
364 /* Surface functions.
365 */
366
367 struct nv50_surface *
368 nv50_surface_from_miptree(struct nv50_miptree *mt,
369 const struct pipe_surface *templ)
370 {
371 struct pipe_surface *ps;
372 struct nv50_surface *ns = CALLOC_STRUCT(nv50_surface);
373 if (!ns)
374 return NULL;
375 ps = &ns->base;
376
377 pipe_reference_init(&ps->reference, 1);
378 pipe_resource_reference(&ps->texture, &mt->base.base);
379
380 ps->format = templ->format;
381 ps->usage = templ->usage;
382 ps->u.tex.level = templ->u.tex.level;
383 ps->u.tex.first_layer = templ->u.tex.first_layer;
384 ps->u.tex.last_layer = templ->u.tex.last_layer;
385
386 ns->width = u_minify(mt->base.base.width0, ps->u.tex.level);
387 ns->height = u_minify(mt->base.base.height0, ps->u.tex.level);
388 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
389 ns->offset = mt->level[templ->u.tex.level].offset;
390
391 /* comment says there are going to be removed, but they're used by the st */
392 ps->width = ns->width;
393 ps->height = ns->height;
394
395 ns->width <<= mt->ms_x;
396 ns->height <<= mt->ms_y;
397
398 return ns;
399 }
400
401 struct pipe_surface *
402 nv50_miptree_surface_new(struct pipe_context *pipe,
403 struct pipe_resource *pt,
404 const struct pipe_surface *templ)
405 {
406 struct nv50_miptree *mt = nv50_miptree(pt);
407 struct nv50_surface *ns = nv50_surface_from_miptree(mt, templ);
408 if (!ns)
409 return NULL;
410 ns->base.context = pipe;
411
412 if (ns->base.u.tex.first_layer) {
413 const unsigned l = ns->base.u.tex.level;
414 const unsigned z = ns->base.u.tex.first_layer;
415
416 if (mt->layout_3d) {
417 ns->offset += nv50_mt_zslice_offset(mt, l, z);
418
419 /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
420 if (ns->depth > 1 &&
421 (z & (NV50_TILE_SIZE_Z(mt->level[l].tile_mode) - 1)))
422 NOUVEAU_ERR("Creating unsupported 3D surface !\n");
423 } else {
424 ns->offset += mt->layer_stride * z;
425 }
426 }
427
428 return &ns->base;
429 }