9eeca05ada3f0f3a7ad5e39d1f946418dd88c437
[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 get_tile_dims(unsigned nx, unsigned ny, unsigned nz)
34 {
35 uint32_t tile_mode = 0x00;
36
37 if (ny > 32) tile_mode = 0x04; /* height 128 tiles */
38 else
39 if (ny > 16) tile_mode = 0x03; /* height 64 tiles */
40 else
41 if (ny > 8) tile_mode = 0x02; /* height 32 tiles */
42 else
43 if (ny > 4) tile_mode = 0x01; /* height 16 tiles */
44
45 if (nz == 1)
46 return tile_mode;
47 else
48 if (tile_mode > 0x02)
49 tile_mode = 0x02;
50
51 if (nz > 16 && tile_mode < 0x02)
52 return tile_mode | 0x50; /* depth 32 tiles */
53 if (nz > 8) return tile_mode | 0x40; /* depth 16 tiles */
54 if (nz > 4) return tile_mode | 0x30; /* depth 8 tiles */
55 if (nz > 2) return tile_mode | 0x20; /* depth 4 tiles */
56
57 return tile_mode | 0x10;
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 = NV50_TILE_HEIGHT(tile_mode);
64 unsigned tile_d_shift = NV50_TILE_DIM_SHIFT(tile_mode, 1);
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 * NV50_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 nv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
77 {
78 struct nv50_miptree *mt = nv50_miptree(pt);
79
80 nouveau_screen_bo_release(pscreen, mt->base.bo);
81
82 FREE(mt);
83 }
84
85 static boolean
86 nv50_miptree_get_handle(struct pipe_screen *pscreen,
87 struct pipe_resource *pt,
88 struct winsys_handle *whandle)
89 {
90 struct nv50_miptree *mt = nv50_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 nv50_miptree_vtbl =
106 {
107 nv50_miptree_get_handle, /* get_handle */
108 nv50_miptree_destroy, /* resource_destroy */
109 nv50_miptree_transfer_new, /* get_transfer */
110 nv50_miptree_transfer_del, /* transfer_destroy */
111 nv50_miptree_transfer_map, /* transfer_map */
112 u_default_transfer_flush_region, /* transfer_flush_region */
113 nv50_miptree_transfer_unmap, /* transfer_unmap */
114 u_default_transfer_inline_write /* transfer_inline_write */
115 };
116
117 struct pipe_resource *
118 nv50_miptree_create(struct pipe_screen *pscreen,
119 const struct pipe_resource *templ)
120 {
121 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
122 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_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 = &nv50_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 = 0x6c00;
145 break;
146 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
147 tile_flags = 0x1800;
148 break;
149 case PIPE_FORMAT_Z24X8_UNORM:
150 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
151 tile_flags = 0x2800;
152 break;
153 case PIPE_FORMAT_R32G32B32A32_FLOAT:
154 case PIPE_FORMAT_R32G32B32_FLOAT:
155 tile_flags = 0x7400;
156 break;
157 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
158 tile_flags = 0x6000;
159 break;
160 default:
161 if ((pt->bind & PIPE_BIND_SCANOUT) &&
162 util_format_get_blocksizebits(pt->format) == 32)
163 tile_flags = 0x7a00;
164 else
165 tile_flags = 0x7000;
166 break;
167 }
168
169 /* For 3D textures, a mipmap is spanned by all the layers, for array
170 * textures and cube maps, each layer contains its own mipmaps.
171 */
172 for (l = 0; l <= pt->last_level; ++l) {
173 struct nv50_miptree_level *lvl = &mt->level[l];
174 unsigned nbx = util_format_get_nblocksx(pt->format, w);
175 unsigned nby = util_format_get_nblocksy(pt->format, h);
176 unsigned blocksize = util_format_get_blocksize(pt->format);
177
178 lvl->offset = mt->total_size;
179 lvl->tile_mode = get_tile_dims(nbx, nby, d);
180 lvl->pitch = align(nbx * blocksize, NV50_TILE_PITCH(lvl->tile_mode));
181
182 mt->total_size += lvl->pitch *
183 align(nby, NV50_TILE_HEIGHT(lvl->tile_mode)) *
184 align(d, NV50_TILE_DEPTH(lvl->tile_mode));
185
186 w = u_minify(w, 1);
187 h = u_minify(h, 1);
188 d = u_minify(d, 1);
189 }
190
191 if (pt->array_size > 1) {
192 mt->layer_stride = align(mt->total_size,
193 NV50_TILE_SIZE(mt->level[0].tile_mode));
194 mt->total_size = mt->layer_stride * pt->array_size;
195 }
196
197 alloc_size = mt->total_size;
198
199 ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 256, alloc_size,
200 mt->level[0].tile_mode, tile_flags,
201 &mt->base.bo);
202 if (ret) {
203 FREE(mt);
204 return NULL;
205 }
206 mt->base.domain = NOUVEAU_BO_VRAM;
207
208 return pt;
209 }
210
211 struct pipe_resource *
212 nv50_miptree_from_handle(struct pipe_screen *pscreen,
213 const struct pipe_resource *templ,
214 struct winsys_handle *whandle)
215 {
216 struct nv50_miptree *mt;
217 unsigned stride;
218
219 /* only supports 2D, non-mipmapped textures for the moment */
220 if ((templ->target != PIPE_TEXTURE_2D &&
221 templ->target != PIPE_TEXTURE_RECT) ||
222 templ->last_level != 0 ||
223 templ->depth0 != 1 ||
224 templ->array_size > 1)
225 return NULL;
226
227 mt = CALLOC_STRUCT(nv50_miptree);
228 if (!mt)
229 return NULL;
230
231 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
232 if (mt->base.bo == NULL) {
233 FREE(mt);
234 return NULL;
235 }
236
237 mt->base.base = *templ;
238 mt->base.vtbl = &nv50_miptree_vtbl;
239 pipe_reference_init(&mt->base.base.reference, 1);
240 mt->base.base.screen = pscreen;
241 mt->level[0].pitch = stride;
242 mt->level[0].offset = 0;
243 mt->level[0].tile_mode = mt->base.bo->tile_mode;
244
245 /* no need to adjust bo reference count */
246 return &mt->base.base;
247 }
248
249
250 /* Surface functions.
251 */
252
253 struct pipe_surface *
254 nv50_miptree_surface_new(struct pipe_context *pipe,
255 struct pipe_resource *pt,
256 const struct pipe_surface *templ)
257 {
258 struct nv50_miptree *mt = nv50_miptree(pt); /* guaranteed */
259 struct nv50_surface *ns;
260 struct pipe_surface *ps;
261 struct nv50_miptree_level *lvl = &mt->level[templ->u.tex.level];
262
263 ns = CALLOC_STRUCT(nv50_surface);
264 if (!ns)
265 return NULL;
266 ps = &ns->base;
267
268 pipe_reference_init(&ps->reference, 1);
269 pipe_resource_reference(&ps->texture, pt);
270 ps->context = pipe;
271 ps->format = templ->format;
272 ps->usage = templ->usage;
273 ps->u.tex.level = templ->u.tex.level;
274 ps->u.tex.first_layer = templ->u.tex.first_layer;
275 ps->u.tex.last_layer = templ->u.tex.last_layer;
276
277 ns->width = u_minify(pt->width0, ps->u.tex.level);
278 ns->height = u_minify(pt->height0, ps->u.tex.level);
279 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
280 ns->offset = lvl->offset;
281
282 /* comment says there are going to be removed, but they're used by the st */
283 ps->width = ns->width;
284 ps->height = ns->height;
285
286 if (mt->layout_3d) {
287 unsigned zslice = ps->u.tex.first_layer;
288
289 /* TODO: re-layout the texture to use only depth 1 tiles in this case: */
290 if (ns->depth > 1 && (zslice & (NV50_TILE_DEPTH(lvl->tile_mode) - 1)))
291 NOUVEAU_ERR("Creating unsupported 3D surface of slices [%u:%u].\n",
292 zslice, ps->u.tex.last_layer);
293
294 ns->offset += calc_zslice_offset(lvl->tile_mode, zslice, lvl->pitch,
295 util_format_get_nblocksy(pt->format,
296 ns->height));
297 } else {
298 ns->offset += mt->layer_stride * ps->u.tex.first_layer;
299 }
300
301 return ps;
302 }
303
304 void
305 nv50_miptree_surface_del(struct pipe_context *pipe, struct pipe_surface *ps)
306 {
307 struct nv50_surface *s = nv50_surface(ps);
308
309 pipe_resource_reference(&ps->texture, NULL);
310
311 FREE(s);
312 }