Squashed commit of the following:
[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 /* The restrictions in tile mode selection probably aren't necessary. */
33 static INLINE uint32_t
34 get_tile_mode(unsigned ny, unsigned d)
35 {
36 uint32_t tile_mode = 0x00;
37
38 if (ny > 32) tile_mode = 0x04; /* height 64 tiles */
39 else
40 if (ny > 16) tile_mode = 0x03; /* height 32 tiles */
41 else
42 if (ny > 8) tile_mode = 0x02; /* height 16 tiles */
43 else
44 if (ny > 4) tile_mode = 0x01; /* height 8 tiles */
45
46 if (d == 1)
47 return tile_mode;
48 else
49 if (tile_mode > 0x02)
50 tile_mode = 0x02;
51
52 if (d > 16 && tile_mode < 0x02)
53 return tile_mode | 0x50; /* depth 32 tiles */
54 if (d > 8) return tile_mode | 0x40; /* depth 16 tiles */
55 if (d > 4) return tile_mode | 0x30; /* depth 8 tiles */
56 if (d > 2) return tile_mode | 0x20; /* depth 4 tiles */
57
58 return tile_mode | 0x10;
59 }
60
61 static INLINE unsigned
62 get_zslice_offset(unsigned tile_mode, unsigned z, unsigned pitch, unsigned nb_h)
63 {
64 unsigned tile_h = get_tile_height(tile_mode);
65 unsigned tile_d = get_tile_depth(tile_mode);
66
67 /* pitch_2d == to next slice within this volume-tile */
68 /* pitch_3d == size (in bytes) of a volume-tile */
69 unsigned pitch_2d = tile_h * 64;
70 unsigned pitch_3d = tile_d * align(nb_h, tile_h) * pitch;
71
72 return (z % tile_d) * pitch_2d + (z / tile_d) * pitch_3d;
73 }
74
75
76
77
78 static void
79 nv50_miptree_destroy(struct pipe_screen *pscreen,
80 struct pipe_resource *pt)
81 {
82 struct nv50_miptree *mt = nv50_miptree(pt);
83 unsigned l;
84
85 for (l = 0; l <= pt->last_level; ++l)
86 FREE(mt->level[l].image_offset);
87
88 nouveau_screen_bo_release(pscreen, mt->base.bo);
89 FREE(mt);
90 }
91
92 static boolean
93 nv50_miptree_get_handle(struct pipe_screen *pscreen,
94 struct pipe_resource *pt,
95 struct winsys_handle *whandle)
96 {
97 struct nv50_miptree *mt = nv50_miptree(pt);
98 unsigned stride;
99
100
101 if (!mt || !mt->base.bo)
102 return FALSE;
103
104 stride = util_format_get_stride(mt->base.base.format,
105 mt->base.base.width0);
106
107 return nouveau_screen_bo_get_handle(pscreen,
108 mt->base.bo,
109 stride,
110 whandle);
111 }
112
113
114 struct u_resource_vtbl nv50_miptree_vtbl =
115 {
116 nv50_miptree_get_handle, /* get_handle */
117 nv50_miptree_destroy, /* resource_destroy */
118 NULL, /* is_resource_referenced */
119 nv50_miptree_transfer_new, /* get_transfer */
120 nv50_miptree_transfer_del, /* transfer_destroy */
121 nv50_miptree_transfer_map, /* transfer_map */
122 u_default_transfer_flush_region, /* transfer_flush_region */
123 nv50_miptree_transfer_unmap, /* transfer_unmap */
124 u_default_transfer_inline_write /* transfer_inline_write */
125 };
126
127
128
129 struct pipe_resource *
130 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_resource *tmp)
131 {
132 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
133 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
134 struct pipe_resource *pt = &mt->base.base;
135 unsigned width = tmp->width0, height = tmp->height0;
136 unsigned depth = tmp->depth0, image_alignment;
137 uint32_t tile_flags;
138 int ret, i, l;
139
140 *pt = *tmp;
141 pipe_reference_init(&pt->reference, 1);
142 pt->screen = pscreen;
143
144 switch (pt->format) {
145 case PIPE_FORMAT_Z32_FLOAT:
146 tile_flags = 0x4800;
147 break;
148 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
149 tile_flags = 0x1800;
150 break;
151 case PIPE_FORMAT_Z16_UNORM:
152 tile_flags = 0x6c00;
153 break;
154 case PIPE_FORMAT_Z24X8_UNORM:
155 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
156 tile_flags = 0x2800;
157 break;
158 case PIPE_FORMAT_R32G32B32A32_FLOAT:
159 case PIPE_FORMAT_R32G32B32_FLOAT:
160 tile_flags = 0x7400;
161 break;
162 default:
163 if ((pt->bind & PIPE_BIND_SCANOUT) &&
164 util_format_get_blocksizebits(pt->format) == 32)
165 tile_flags = 0x7a00;
166 else
167 tile_flags = 0x7000;
168 break;
169 }
170
171 /* XXX: texture arrays */
172 mt->image_nr = (pt->target == PIPE_TEXTURE_CUBE) ? 6 : 1;
173
174 for (l = 0; l <= pt->last_level; l++) {
175 struct nv50_miptree_level *lvl = &mt->level[l];
176 unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
177
178 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
179 lvl->pitch = align(util_format_get_stride(pt->format, width), 64);
180 lvl->tile_mode = get_tile_mode(nblocksy, depth);
181
182 width = u_minify(width, 1);
183 height = u_minify(height, 1);
184 depth = u_minify(depth, 1);
185 }
186
187 image_alignment = get_tile_height(mt->level[0].tile_mode) * 64;
188 image_alignment *= get_tile_depth(mt->level[0].tile_mode);
189
190 /* NOTE the distinction between arrays of mip-mapped 2D textures and
191 * mip-mapped 3D textures. We can't use image_nr == depth for 3D mip.
192 */
193 for (i = 0; i < mt->image_nr; i++) {
194 for (l = 0; l <= pt->last_level; l++) {
195 struct nv50_miptree_level *lvl = &mt->level[l];
196 int size;
197 unsigned tile_h = get_tile_height(lvl->tile_mode);
198 unsigned tile_d = get_tile_depth(lvl->tile_mode);
199
200 size = lvl->pitch;
201 size *= align(util_format_get_nblocksy(pt->format, u_minify(pt->height0, l)), tile_h);
202 size *= align(u_minify(pt->depth0, l), tile_d);
203
204 lvl->image_offset[i] = mt->total_size;
205
206 mt->total_size += size;
207 }
208 mt->total_size = align(mt->total_size, image_alignment);
209 }
210
211 ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 256, mt->total_size,
212 mt->level[0].tile_mode, tile_flags,
213 &mt->base.bo);
214 if (ret) {
215 for (l = 0; l <= pt->last_level; ++l)
216 FREE(mt->level[l].image_offset);
217 FREE(mt);
218 return NULL;
219 }
220
221 return pt;
222 }
223
224
225 struct pipe_resource *
226 nv50_miptree_from_handle(struct pipe_screen *pscreen,
227 const struct pipe_resource *template,
228 struct winsys_handle *whandle)
229 {
230 struct nv50_miptree *mt;
231 unsigned stride;
232
233 /* Only supports 2D, non-mipmapped textures for the moment */
234 if (template->target != PIPE_TEXTURE_2D ||
235 template->last_level != 0 ||
236 template->depth0 != 1)
237 return NULL;
238
239 mt = CALLOC_STRUCT(nv50_miptree);
240 if (!mt)
241 return NULL;
242
243 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
244 if (mt->base.bo == NULL) {
245 FREE(mt);
246 return NULL;
247 }
248
249
250 mt->base.base = *template;
251 pipe_reference_init(&mt->base.base.reference, 1);
252 mt->base.base.screen = pscreen;
253 mt->image_nr = 1;
254 mt->level[0].pitch = stride;
255 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
256 mt->level[0].tile_mode = mt->base.bo->tile_mode;
257
258 /* XXX: Need to adjust bo refcount??
259 */
260 /* nouveau_bo_ref(bo, &mt->base.bo); */
261 return &mt->base.base;
262 }
263
264
265
266 /* Surface functions
267 */
268
269 struct pipe_surface *
270 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_resource *pt,
271 unsigned face, unsigned level, unsigned zslice,
272 unsigned flags)
273 {
274 struct nv50_miptree *mt = nv50_miptree(pt);
275 struct nv50_miptree_level *lvl = &mt->level[level];
276 struct pipe_surface *ps;
277 unsigned img = 0;
278
279 if (pt->target == PIPE_TEXTURE_CUBE)
280 img = face;
281
282 ps = CALLOC_STRUCT(pipe_surface);
283 if (!ps)
284 return NULL;
285 pipe_resource_reference(&ps->texture, pt);
286 ps->format = pt->format;
287 ps->width = u_minify(pt->width0, level);
288 ps->height = u_minify(pt->height0, level);
289 ps->usage = flags;
290 pipe_reference_init(&ps->reference, 1);
291 ps->face = face;
292 ps->level = level;
293 ps->zslice = zslice;
294 ps->offset = lvl->image_offset[img];
295
296 if (pt->target == PIPE_TEXTURE_3D) {
297 unsigned nb_h = util_format_get_nblocksy(pt->format, ps->height);
298 ps->offset += get_zslice_offset(lvl->tile_mode, zslice,
299 lvl->pitch, nb_h);
300 }
301
302 return ps;
303 }
304
305 void
306 nv50_miptree_surface_del(struct pipe_surface *ps)
307 {
308 struct nv50_surface *s = nv50_surface(ps);
309
310 pipe_resource_reference(&ps->texture, NULL);
311 FREE(s);
312 }