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