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