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