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