nvc0: fix layer stride state
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_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 "nvc0_context.h"
29 #include "nvc0_resource.h"
30 #include "nvc0_transfer.h"
31
32 static INLINE uint32_t
33 get_tile_dims(unsigned nx, unsigned ny, unsigned nz)
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 (nz == 1)
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
57 return tile_mode | 0x100;
58 }
59
60 static INLINE unsigned
61 calc_zslice_offset(uint32_t tile_mode, unsigned z, unsigned pitch, unsigned nbh)
62 {
63 unsigned tile_h = NVC0_TILE_HEIGHT(tile_mode);
64 unsigned tile_d_shift = NVC0_TILE_DIM_SHIFT(tile_mode, 2);
65 unsigned tile_d = 1 << tile_d_shift;
66
67 /* stride_2d == to next slice within this volume tile */
68 /* stride_3d == size (in bytes) of a volume tile */
69 unsigned stride_2d = tile_h * NVC0_TILE_PITCH(tile_mode);
70 unsigned stride_3d = tile_d * align(nbh, tile_h) * pitch;
71
72 return (z & (tile_d - 1)) * stride_2d + (z >> tile_d_shift) * stride_3d;
73 }
74
75 static void
76 nvc0_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
77 {
78 struct nvc0_miptree *mt = nvc0_miptree(pt);
79
80 nouveau_screen_bo_release(pscreen, mt->base.bo);
81
82 FREE(mt);
83 }
84
85 static boolean
86 nvc0_miptree_get_handle(struct pipe_screen *pscreen,
87 struct pipe_resource *pt,
88 struct winsys_handle *whandle)
89 {
90 struct nvc0_miptree *mt = nvc0_miptree(pt);
91 unsigned stride;
92
93 if (!mt || !mt->base.bo)
94 return FALSE;
95
96 stride = util_format_get_stride(mt->base.base.format,
97 mt->base.base.width0);
98
99 return nouveau_screen_bo_get_handle(pscreen,
100 mt->base.bo,
101 stride,
102 whandle);
103 }
104
105 const struct u_resource_vtbl nvc0_miptree_vtbl =
106 {
107 nvc0_miptree_get_handle, /* get_handle */
108 nvc0_miptree_destroy, /* resource_destroy */
109 NULL, /* is_resource_referenced */
110 nvc0_miptree_transfer_new, /* get_transfer */
111 nvc0_miptree_transfer_del, /* transfer_destroy */
112 nvc0_miptree_transfer_map, /* transfer_map */
113 u_default_transfer_flush_region, /* transfer_flush_region */
114 nvc0_miptree_transfer_unmap, /* transfer_unmap */
115 u_default_transfer_inline_write /* transfer_inline_write */
116 };
117
118 struct pipe_resource *
119 nvc0_miptree_create(struct pipe_screen *pscreen,
120 const struct pipe_resource *templ)
121 {
122 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
123 struct nvc0_miptree *mt = CALLOC_STRUCT(nvc0_miptree);
124 struct pipe_resource *pt = &mt->base.base;
125 int ret;
126 unsigned w, h, d, l, alloc_size;
127 uint32_t tile_flags;
128
129 if (!mt)
130 return NULL;
131
132 mt->base.vtbl = &nvc0_miptree_vtbl;
133 *pt = *templ;
134 pipe_reference_init(&pt->reference, 1);
135 pt->screen = pscreen;
136
137 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
138
139 w = pt->width0;
140 h = pt->height0;
141 d = mt->layout_3d ? pt->depth0 : 1;
142
143 switch (pt->format) {
144 case PIPE_FORMAT_Z16_UNORM:
145 tile_flags = 0x0700; /* COMPRESSED */
146 tile_flags = 0x0200; /* NORMAL ? */
147 tile_flags = 0x0100; /* NORMAL ? */
148 break;
149 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
150 tile_flags = 0x5300; /* MSAA 4, COMPRESSED */
151 tile_flags = 0x4600; /* NORMAL */
152 break;
153 case PIPE_FORMAT_Z24X8_UNORM:
154 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
155 tile_flags = 0x1100; /* NORMAL */
156 if (w * h >= 128 * 128 && 0)
157 tile_flags = 0x1700; /* COMPRESSED, requires magic */
158 break;
159 case PIPE_FORMAT_R32G32B32A32_FLOAT:
160 tile_flags = 0xf500; /* COMPRESSED */
161 tile_flags = 0xf700; /* MSAA 2 */
162 tile_flags = 0xf900; /* MSAA 4 */
163 tile_flags = 0xfe00; /* NORMAL */
164 break;
165 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
166 tile_flags = 0xce00; /* COMPRESSED */
167 tile_flags = 0xcf00; /* MSAA 2, COMPRESSED */
168 tile_flags = 0xd000; /* MSAA 4, COMPRESSED */
169 tile_flags = 0xc300; /* NORMAL */
170 break;
171 case PIPE_FORMAT_R16G16B16A16_UNORM:
172 tile_flags = 0xe900; /* COMPRESSED */
173 break;
174 default:
175 tile_flags = 0xe000; /* MSAA 4, COMPRESSED 32 BIT */
176 tile_flags = 0xfe00; /* NORMAL 32 BIT */
177 if (w * h >= 128 * 128 && 0)
178 tile_flags = 0xdb00; /* COMPRESSED 32 BIT, requires magic */
179 break;
180 }
181
182 /* For 3D textures, a mipmap is spanned by all the layers, for array
183 * textures and cube maps, each layer contains its own mipmaps.
184 */
185 for (l = 0; l <= pt->last_level; ++l) {
186 struct nvc0_miptree_level *lvl = &mt->level[l];
187 unsigned nbx = util_format_get_nblocksx(pt->format, w);
188 unsigned nby = util_format_get_nblocksy(pt->format, h);
189 unsigned blocksize = util_format_get_blocksize(pt->format);
190
191 lvl->offset = mt->total_size;
192 lvl->tile_mode = get_tile_dims(nbx, nby, d);
193 lvl->pitch = align(nbx * blocksize, NVC0_TILE_PITCH(lvl->tile_mode));
194
195 mt->total_size += lvl->pitch *
196 align(nby, NVC0_TILE_HEIGHT(lvl->tile_mode)) *
197 align(d, NVC0_TILE_DEPTH(lvl->tile_mode));
198
199 w = u_minify(w, 1);
200 h = u_minify(h, 1);
201 d = u_minify(d, 1);
202 }
203
204 if (pt->array_size > 1) {
205 mt->layer_stride = align(mt->total_size,
206 NVC0_TILE_SIZE(mt->level[0].tile_mode));
207 mt->total_size = mt->layer_stride * pt->array_size;
208 }
209
210 alloc_size = mt->total_size;
211 if (tile_flags == 0x1700)
212 alloc_size *= 3; /* HiZ, XXX: correct size */
213
214 ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 256, alloc_size,
215 mt->level[0].tile_mode, tile_flags,
216 &mt->base.bo);
217 if (ret) {
218 FREE(mt);
219 return NULL;
220 }
221 mt->base.domain = NOUVEAU_BO_VRAM;
222
223 return pt;
224 }
225
226 struct pipe_resource *
227 nvc0_miptree_from_handle(struct pipe_screen *pscreen,
228 const struct pipe_resource *templ,
229 struct winsys_handle *whandle)
230 {
231 struct nvc0_miptree *mt;
232 unsigned stride;
233
234 /* only supports 2D, non-mipmapped textures for the moment */
235 if ((templ->target != PIPE_TEXTURE_2D &&
236 templ->target != PIPE_TEXTURE_RECT) ||
237 templ->last_level != 0 ||
238 templ->depth0 != 1 ||
239 templ->array_size > 1)
240 return NULL;
241
242 mt = CALLOC_STRUCT(nvc0_miptree);
243 if (!mt)
244 return NULL;
245
246 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
247 if (mt->base.bo == NULL) {
248 FREE(mt);
249 return NULL;
250 }
251
252 mt->base.base = *templ;
253 mt->base.vtbl = &nvc0_miptree_vtbl;
254 pipe_reference_init(&mt->base.base.reference, 1);
255 mt->base.base.screen = pscreen;
256 mt->level[0].pitch = stride;
257 mt->level[0].offset = 0;
258 mt->level[0].tile_mode = mt->base.bo->tile_mode;
259
260 /* no need to adjust bo reference count */
261 return &mt->base.base;
262 }
263
264
265 /* Surface functions.
266 */
267
268 struct pipe_surface *
269 nvc0_miptree_surface_new(struct pipe_context *pipe,
270 struct pipe_resource *pt,
271 const struct pipe_surface *templ)
272 {
273 struct nvc0_miptree *mt = nvc0_miptree(pt); /* guaranteed */
274 struct nvc0_surface *ns;
275 struct pipe_surface *ps;
276 struct nvc0_miptree_level *lvl = &mt->level[templ->u.tex.level];
277
278 ns = CALLOC_STRUCT(nvc0_surface);
279 if (!ns)
280 return NULL;
281 ps = &ns->base;
282
283 pipe_reference_init(&ps->reference, 1);
284 pipe_resource_reference(&ps->texture, pt);
285 ps->context = pipe;
286 ps->format = pt->format;
287 ps->usage = templ->usage;
288 ps->u.tex.level = templ->u.tex.level;
289 ps->u.tex.first_layer = templ->u.tex.first_layer;
290 ps->u.tex.last_layer = templ->u.tex.last_layer;
291
292 ns->width = u_minify(pt->width0, ps->u.tex.level);
293 ns->height = u_minify(pt->height0, ps->u.tex.level);
294 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
295 ns->offset = lvl->offset;
296
297 /* comment says there are going to be removed, but they're used by the st */
298 ps->width = ns->width;
299 ps->height = ns->height;
300
301 if (mt->layout_3d) {
302 unsigned zslice = ps->u.tex.first_layer;
303
304 /* TODO: re-layout the texture to use only depth 1 tiles in this case: */
305 if (ns->depth > 1 && (zslice & (NVC0_TILE_DEPTH(lvl->tile_mode) - 1)))
306 NOUVEAU_ERR("Creating unsupported 3D surface of slices [%u:%u].\n",
307 zslice, ps->u.tex.last_layer);
308
309 ns->offset += calc_zslice_offset(lvl->tile_mode, zslice, lvl->pitch,
310 util_format_get_nblocksy(pt->format,
311 ns->height));
312 } else {
313 ns->offset += mt->layer_stride * ps->u.tex.first_layer;
314 }
315
316 return ps;
317 }
318
319 void
320 nvc0_miptree_surface_del(struct pipe_context *pipe, struct pipe_surface *ps)
321 {
322 struct nvc0_surface *s = nvc0_surface(ps);
323
324 pipe_resource_reference(&ps->texture, NULL);
325
326 FREE(s);
327 }