nv50,nvc0: add correct storage type for Z32_FLOAT
[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 uint32_t
33 nvc0_tex_choose_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 uint32_t
61 nvc0_mt_choose_storage_type(struct nv50_miptree *mt, boolean compressed)
62 {
63 const unsigned ms = util_logbase2(mt->base.base.nr_samples);
64
65 uint32_t tile_flags;
66
67 compressed = FALSE; /* not yet supported */
68
69 if (mt->base.base.bind & PIPE_BIND_CURSOR)
70 return NOUVEAU_BO_TILE_SCANOUT;
71
72 switch (mt->base.base.format) {
73 case PIPE_FORMAT_Z16_UNORM:
74 if (compressed)
75 tile_flags = 0x0200 + (ms << 8);
76 else
77 tile_flags = 0x0100;
78 break;
79 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
80 if (compressed)
81 tile_flags = 0x5100 + (ms << 8);
82 else
83 tile_flags = 0x4600;
84 break;
85 case PIPE_FORMAT_Z24X8_UNORM:
86 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
87 if (compressed)
88 tile_flags = 0x1700 + (ms << 8);
89 else
90 tile_flags = 0x1100;
91 break;
92 case PIPE_FORMAT_Z32_FLOAT:
93 if (compressed)
94 tile_flags = 0x8600 + (ms << 8);
95 else
96 tile_flags = 0x7b00;
97 break;
98 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED:
99 if (compressed)
100 tile_flags = 0xce00 + (ms << 8);
101 else
102 tile_flags = 0xc300;
103 break;
104 default:
105 switch (util_format_get_blocksizebits(mt->base.base.format)) {
106 case 128:
107 if (compressed)
108 tile_flags = 0xf400 + (ms << 9);
109 else
110 tile_flags = 0xfe00;
111 break;
112 case 64:
113 if (compressed) {
114 switch (ms) {
115 case 0: tile_flags = 0xe600; break;
116 case 1: tile_flags = 0xeb00; break;
117 case 2: tile_flags = 0xed00; break;
118 case 3: tile_flags = 0xf200; break;
119 default:
120 return 0;
121 }
122 } else {
123 tile_flags = 0xfe00;
124 }
125 break;
126 case 32:
127 if (compressed) {
128 switch (ms) {
129 case 0: tile_flags = 0xdb00; break;
130 case 1: tile_flags = 0xdd00; break;
131 case 2: tile_flags = 0xdf00; break;
132 case 3: tile_flags = 0xe400; break;
133 default:
134 return 0;
135 }
136 } else {
137 tile_flags = 0xfe00;
138 }
139 break;
140 case 16:
141 case 8:
142 tile_flags = 0xfe00;
143 break;
144 default:
145 return 0;
146 }
147 break;
148 }
149
150 if (mt->base.base.bind & PIPE_BIND_SCANOUT)
151 tile_flags |= NOUVEAU_BO_TILE_SCANOUT;
152
153 return tile_flags;
154 }
155
156 static INLINE boolean
157 nvc0_miptree_init_ms_mode(struct nv50_miptree *mt)
158 {
159 switch (mt->base.base.nr_samples) {
160 case 8:
161 mt->ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS8;
162 mt->ms_x = 2;
163 mt->ms_y = 1;
164 break;
165 case 4:
166 mt->ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS4;
167 mt->ms_x = 1;
168 mt->ms_y = 1;
169 break;
170 case 2:
171 mt->ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS2;
172 mt->ms_x = 1;
173 break;
174 case 1:
175 case 0:
176 mt->ms_mode = NVC0_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 *);
187
188 static void
189 nvc0_miptree_init_layout_tiled(struct nv50_miptree *mt)
190 {
191 struct pipe_resource *pt = &mt->base.base;
192 unsigned w, h, d, l;
193 const unsigned blocksize = util_format_get_blocksize(pt->format);
194
195 mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
196
197 w = pt->width0 << mt->ms_x;
198 h = pt->height0 << mt->ms_y;
199
200 /* For 3D textures, a mipmap is spanned by all the layers, for array
201 * textures and cube maps, each layer contains its own mipmaps.
202 */
203 d = mt->layout_3d ? pt->depth0 : 1;
204
205 for (l = 0; l <= pt->last_level; ++l) {
206 struct nv50_miptree_level *lvl = &mt->level[l];
207 unsigned tsx, tsy, tsz;
208 unsigned nbx = util_format_get_nblocksx(pt->format, w);
209 unsigned nby = util_format_get_nblocksy(pt->format, h);
210
211 lvl->offset = mt->total_size;
212
213 lvl->tile_mode = nvc0_tex_choose_tile_dims(nbx, nby, d);
214
215 tsx = NVC0_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
216 tsy = NVC0_TILE_SIZE_Y(lvl->tile_mode);
217 tsz = NVC0_TILE_SIZE_Z(lvl->tile_mode);
218
219 lvl->pitch = align(nbx * blocksize, tsx);
220
221 mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
222
223 w = u_minify(w, 1);
224 h = u_minify(h, 1);
225 d = u_minify(d, 1);
226 }
227
228 if (pt->array_size > 1) {
229 mt->layer_stride = align(mt->total_size,
230 NVC0_TILE_SIZE(mt->level[0].tile_mode));
231 mt->total_size = mt->layer_stride * pt->array_size;
232 }
233 }
234
235 const struct u_resource_vtbl nvc0_miptree_vtbl =
236 {
237 nv50_miptree_get_handle, /* get_handle */
238 nv50_miptree_destroy, /* resource_destroy */
239 nvc0_miptree_transfer_new, /* get_transfer */
240 nvc0_miptree_transfer_del, /* transfer_destroy */
241 nvc0_miptree_transfer_map, /* transfer_map */
242 u_default_transfer_flush_region, /* transfer_flush_region */
243 nvc0_miptree_transfer_unmap, /* transfer_unmap */
244 u_default_transfer_inline_write /* transfer_inline_write */
245 };
246
247 struct pipe_resource *
248 nvc0_miptree_create(struct pipe_screen *pscreen,
249 const struct pipe_resource *templ)
250 {
251 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
252 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
253 struct pipe_resource *pt = &mt->base.base;
254 int ret;
255 uint32_t tile_flags;
256
257 if (!mt)
258 return NULL;
259
260 mt->base.vtbl = &nvc0_miptree_vtbl;
261 *pt = *templ;
262 pipe_reference_init(&pt->reference, 1);
263 pt->screen = pscreen;
264
265 tile_flags = nvc0_mt_choose_storage_type(mt, TRUE);
266
267 if (!nvc0_miptree_init_ms_mode(mt)) {
268 FREE(mt);
269 return NULL;
270 }
271
272 if (tile_flags & NOUVEAU_BO_TILE_LAYOUT_MASK) {
273 nvc0_miptree_init_layout_tiled(mt);
274 } else
275 if (!nv50_miptree_init_layout_linear(mt)) {
276 FREE(mt);
277 return NULL;
278 }
279
280 ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 4096,
281 mt->total_size,
282 mt->level[0].tile_mode, tile_flags,
283 &mt->base.bo);
284 if (ret) {
285 FREE(mt);
286 return NULL;
287 }
288 mt->base.domain = NOUVEAU_BO_VRAM;
289
290 return pt;
291 }
292
293 /* Offset of zslice @z from start of level @l. */
294 INLINE unsigned
295 nvc0_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
296 {
297 const struct pipe_resource *pt = &mt->base.base;
298
299 unsigned tds = NVC0_TILE_SHIFT_Z(mt->level[l].tile_mode);
300 unsigned ths = NVC0_TILE_SHIFT_Y(mt->level[l].tile_mode);
301
302 unsigned nby = util_format_get_nblocksy(pt->format,
303 u_minify(pt->height0, l));
304
305 /* to next 2D tile slice within a 3D tile */
306 unsigned stride_2d = NVC0_TILE_SIZE_2D(mt->level[l].tile_mode);
307
308 /* to slice in the next (in z direction) 3D tile */
309 unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
310
311 return (z & (1 << (tds - 1))) * stride_2d + (z >> tds) * stride_3d;
312 }
313
314 /* Surface functions.
315 */
316
317 struct pipe_surface *
318 nvc0_miptree_surface_new(struct pipe_context *pipe,
319 struct pipe_resource *pt,
320 const struct pipe_surface *templ)
321 {
322 struct nv50_surface *ns = nv50_surface_from_miptree(nv50_miptree(pt), templ);
323 if (!ns)
324 return NULL;
325 ns->base.context = pipe;
326 return &ns->base;
327 }