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