Merge branch 'gallium-strict-aliasing'
[mesa.git] / src / gallium / drivers / nv50 / nv50_transfer.c
1
2 #include "pipe/p_context.h"
3 #include "pipe/p_inlines.h"
4 #include "util/u_math.h"
5
6 #include "nv50_context.h"
7
8 struct nv50_transfer {
9 struct pipe_transfer base;
10 struct nouveau_bo *bo;
11 unsigned level_offset;
12 unsigned level_tiling;
13 int level_pitch;
14 int level_width;
15 int level_height;
16 int level_depth;
17 int level_x;
18 int level_y;
19 unsigned nblocksx;
20 unsigned nblocksy;
21 };
22
23 static void
24 nv50_transfer_rect_m2mf(struct pipe_screen *pscreen,
25 struct nouveau_bo *src_bo, unsigned src_offset,
26 int src_pitch, unsigned src_tile_mode,
27 int sx, int sy, int sw, int sh, int sd,
28 struct nouveau_bo *dst_bo, unsigned dst_offset,
29 int dst_pitch, unsigned dst_tile_mode,
30 int dx, int dy, int dw, int dh, int dd,
31 int cpp, int width, int height,
32 unsigned src_reloc, unsigned dst_reloc)
33 {
34 struct nv50_screen *screen = nv50_screen(pscreen);
35 struct nouveau_channel *chan = screen->m2mf->channel;
36 struct nouveau_grobj *m2mf = screen->m2mf;
37
38 src_reloc |= NOUVEAU_BO_RD;
39 dst_reloc |= NOUVEAU_BO_WR;
40
41 WAIT_RING (chan, 14);
42
43 if (!src_bo->tile_flags) {
44 BEGIN_RING(chan, m2mf,
45 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_IN, 1);
46 OUT_RING (chan, 1);
47 BEGIN_RING(chan, m2mf,
48 NV50_MEMORY_TO_MEMORY_FORMAT_PITCH_IN, 1);
49 OUT_RING (chan, src_pitch);
50 src_offset += (sy * src_pitch) + (sx * cpp);
51 } else {
52 BEGIN_RING(chan, m2mf,
53 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_IN, 6);
54 OUT_RING (chan, 0);
55 OUT_RING (chan, src_tile_mode << 4);
56 OUT_RING (chan, sw * cpp);
57 OUT_RING (chan, sh);
58 OUT_RING (chan, sd);
59 OUT_RING (chan, 0);
60 }
61
62 if (!dst_bo->tile_flags) {
63 BEGIN_RING(chan, m2mf,
64 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_OUT, 1);
65 OUT_RING (chan, 1);
66 BEGIN_RING(chan, m2mf,
67 NV50_MEMORY_TO_MEMORY_FORMAT_PITCH_OUT, 1);
68 OUT_RING (chan, dst_pitch);
69 dst_offset += (dy * dst_pitch) + (dx * cpp);
70 } else {
71 BEGIN_RING(chan, m2mf,
72 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_OUT, 6);
73 OUT_RING (chan, 0);
74 OUT_RING (chan, dst_tile_mode << 4);
75 OUT_RING (chan, dw * cpp);
76 OUT_RING (chan, dh);
77 OUT_RING (chan, dd);
78 OUT_RING (chan, 0);
79 }
80
81 while (height) {
82 int line_count = height > 2047 ? 2047 : height;
83
84 WAIT_RING (chan, 15);
85 BEGIN_RING(chan, m2mf,
86 NV50_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN_HIGH, 2);
87 OUT_RELOCh(chan, src_bo, src_offset, src_reloc);
88 OUT_RELOCh(chan, dst_bo, dst_offset, dst_reloc);
89 BEGIN_RING(chan, m2mf,
90 NV50_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 2);
91 OUT_RELOCl(chan, src_bo, src_offset, src_reloc);
92 OUT_RELOCl(chan, dst_bo, dst_offset, dst_reloc);
93 if (src_bo->tile_flags) {
94 BEGIN_RING(chan, m2mf,
95 NV50_MEMORY_TO_MEMORY_FORMAT_TILING_POSITION_IN, 1);
96 OUT_RING (chan, (sy << 16) | (sx * cpp));
97 } else {
98 src_offset += (line_count * src_pitch);
99 }
100 if (dst_bo->tile_flags) {
101 BEGIN_RING(chan, m2mf,
102 NV50_MEMORY_TO_MEMORY_FORMAT_TILING_POSITION_OUT, 1);
103 OUT_RING (chan, (dy << 16) | (dx * cpp));
104 } else {
105 dst_offset += (line_count * dst_pitch);
106 }
107 BEGIN_RING(chan, m2mf,
108 NV50_MEMORY_TO_MEMORY_FORMAT_LINE_LENGTH_IN, 4);
109 OUT_RING (chan, width * cpp);
110 OUT_RING (chan, line_count);
111 OUT_RING (chan, 0x00000101);
112 OUT_RING (chan, 0);
113 FIRE_RING (chan);
114
115 height -= line_count;
116 sy += line_count;
117 dy += line_count;
118 }
119 }
120
121 static INLINE unsigned
122 get_zslice_offset(unsigned tile_mode, unsigned z, unsigned pitch, unsigned ny)
123 {
124 unsigned tile_h = get_tile_height(tile_mode);
125 unsigned tile_d = get_tile_depth(tile_mode);
126
127 /* pitch_2d == to next slice within this volume-tile */
128 /* pitch_3d == to next slice in next 2D array of blocks */
129 unsigned pitch_2d = tile_h * 64;
130 unsigned pitch_3d = tile_d * align(ny, tile_h) * pitch;
131
132 return (z % tile_d) * pitch_2d + (z / tile_d) * pitch_3d;
133 }
134
135 static struct pipe_transfer *
136 nv50_transfer_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
137 unsigned face, unsigned level, unsigned zslice,
138 enum pipe_transfer_usage usage,
139 unsigned x, unsigned y, unsigned w, unsigned h)
140 {
141 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
142 struct nv50_miptree *mt = nv50_miptree(pt);
143 struct nv50_miptree_level *lvl = &mt->level[level];
144 struct nv50_transfer *tx;
145 unsigned nx, ny, image = 0;
146 int ret;
147
148 if (pt->target == PIPE_TEXTURE_CUBE)
149 image = face;
150
151 tx = CALLOC_STRUCT(nv50_transfer);
152 if (!tx)
153 return NULL;
154
155 pipe_texture_reference(&tx->base.texture, pt);
156 tx->nblocksx = pf_get_nblocksx(pt->format, u_minify(pt->width0, level));
157 tx->nblocksy = pf_get_nblocksy(pt->format, u_minify(pt->height0, level));
158 tx->base.width = w;
159 tx->base.height = h;
160 tx->base.stride = tx->nblocksx * pf_get_blocksize(pt->format);
161 tx->base.usage = usage;
162
163 tx->level_pitch = lvl->pitch;
164 tx->level_width = u_minify(mt->base.base.width0, level);
165 tx->level_height = u_minify(mt->base.base.height0, level);
166 tx->level_depth = u_minify(mt->base.base.depth0, level);
167 tx->level_offset = lvl->image_offset[image];
168 tx->level_tiling = lvl->tile_mode;
169 tx->level_x = pf_get_nblocksx(pt->format, x);
170 tx->level_y = pf_get_nblocksy(pt->format, y);
171 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
172 tx->nblocksy * tx->base.stride, &tx->bo);
173 if (ret) {
174 FREE(tx);
175 return NULL;
176 }
177
178 if (pt->target == PIPE_TEXTURE_3D)
179 tx->level_offset += get_zslice_offset(lvl->tile_mode, zslice,
180 lvl->pitch,
181 tx->nblocksy);
182
183 if (usage & PIPE_TRANSFER_READ) {
184 nx = pf_get_nblocksx(pt->format, tx->base.width);
185 ny = pf_get_nblocksy(pt->format, tx->base.height);
186
187 nv50_transfer_rect_m2mf(pscreen, mt->base.bo, tx->level_offset,
188 tx->level_pitch, tx->level_tiling,
189 x, y,
190 tx->nblocksx, tx->nblocksy,
191 tx->level_depth,
192 tx->bo, 0,
193 tx->base.stride, tx->bo->tile_mode,
194 0, 0,
195 tx->nblocksx, tx->nblocksy, 1,
196 pf_get_blocksize(pt->format), nx, ny,
197 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART,
198 NOUVEAU_BO_GART);
199 }
200
201 return &tx->base;
202 }
203
204 static void
205 nv50_transfer_del(struct pipe_transfer *ptx)
206 {
207 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
208 struct nv50_miptree *mt = nv50_miptree(ptx->texture);
209 struct pipe_texture *pt = ptx->texture;
210
211 unsigned nx = pf_get_nblocksx(pt->format, tx->base.width);
212 unsigned ny = pf_get_nblocksy(pt->format, tx->base.height);
213
214 if (ptx->usage & PIPE_TRANSFER_WRITE) {
215 struct pipe_screen *pscreen = pt->screen;
216
217 nv50_transfer_rect_m2mf(pscreen, tx->bo, 0,
218 tx->base.stride, tx->bo->tile_mode,
219 0, 0,
220 tx->nblocksx, tx->nblocksy, 1,
221 mt->base.bo, tx->level_offset,
222 tx->level_pitch, tx->level_tiling,
223 tx->level_x, tx->level_y,
224 tx->nblocksx, tx->nblocksy,
225 tx->level_depth,
226 pf_get_blocksize(pt->format), nx, ny,
227 NOUVEAU_BO_GART, NOUVEAU_BO_VRAM |
228 NOUVEAU_BO_GART);
229 }
230
231 nouveau_bo_ref(NULL, &tx->bo);
232 pipe_texture_reference(&ptx->texture, NULL);
233 FREE(ptx);
234 }
235
236 static void *
237 nv50_transfer_map(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
238 {
239 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
240 unsigned flags = 0;
241 int ret;
242
243 if (ptx->usage & PIPE_TRANSFER_WRITE)
244 flags |= NOUVEAU_BO_WR;
245 if (ptx->usage & PIPE_TRANSFER_READ)
246 flags |= NOUVEAU_BO_RD;
247
248 ret = nouveau_bo_map(tx->bo, flags);
249 if (ret)
250 return NULL;
251 return tx->bo->map;
252 }
253
254 static void
255 nv50_transfer_unmap(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
256 {
257 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
258
259 nouveau_bo_unmap(tx->bo);
260 }
261
262 void
263 nv50_transfer_init_screen_functions(struct pipe_screen *pscreen)
264 {
265 pscreen->get_tex_transfer = nv50_transfer_new;
266 pscreen->tex_transfer_destroy = nv50_transfer_del;
267 pscreen->transfer_map = nv50_transfer_map;
268 pscreen->transfer_unmap = nv50_transfer_unmap;
269 }
270
271 void
272 nv50_upload_sifc(struct nv50_context *nv50,
273 struct nouveau_bo *bo, unsigned dst_offset, unsigned reloc,
274 unsigned dst_format, int dst_w, int dst_h, int dst_pitch,
275 void *src, unsigned src_format, int src_pitch,
276 int x, int y, int w, int h, int cpp)
277 {
278 struct nouveau_channel *chan = nv50->screen->base.channel;
279 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
280 struct nouveau_grobj *tesla = nv50->screen->tesla;
281 unsigned line_dwords = (w * cpp + 3) / 4;
282
283 reloc |= NOUVEAU_BO_WR;
284
285 WAIT_RING (chan, 32);
286
287 if (bo->tile_flags) {
288 BEGIN_RING(chan, eng2d, NV50_2D_DST_FORMAT, 5);
289 OUT_RING (chan, dst_format);
290 OUT_RING (chan, 0);
291 OUT_RING (chan, bo->tile_mode << 4);
292 OUT_RING (chan, 1);
293 OUT_RING (chan, 0);
294 } else {
295 BEGIN_RING(chan, eng2d, NV50_2D_DST_FORMAT, 2);
296 OUT_RING (chan, dst_format);
297 OUT_RING (chan, 1);
298 BEGIN_RING(chan, eng2d, NV50_2D_DST_PITCH, 1);
299 OUT_RING (chan, dst_pitch);
300 }
301
302 BEGIN_RING(chan, eng2d, NV50_2D_DST_WIDTH, 4);
303 OUT_RING (chan, dst_w);
304 OUT_RING (chan, dst_h);
305 OUT_RELOCh(chan, bo, dst_offset, reloc);
306 OUT_RELOCl(chan, bo, dst_offset, reloc);
307
308 /* NV50_2D_OPERATION_SRCCOPY assumed already set */
309
310 BEGIN_RING(chan, eng2d, NV50_2D_SIFC_UNK0800, 2);
311 OUT_RING (chan, 0);
312 OUT_RING (chan, src_format);
313 BEGIN_RING(chan, eng2d, NV50_2D_SIFC_WIDTH, 10);
314 OUT_RING (chan, w);
315 OUT_RING (chan, h);
316 OUT_RING (chan, 0);
317 OUT_RING (chan, 1);
318 OUT_RING (chan, 0);
319 OUT_RING (chan, 1);
320 OUT_RING (chan, 0);
321 OUT_RING (chan, x);
322 OUT_RING (chan, 0);
323 OUT_RING (chan, y);
324
325 while (h--) {
326 const uint32_t *p = src;
327 unsigned count = line_dwords;
328
329 while (count) {
330 unsigned nr = MIN2(count, 1792);
331
332 if (chan->pushbuf->remaining <= nr) {
333 FIRE_RING (chan);
334
335 BEGIN_RING(chan, eng2d,
336 NV50_2D_DST_ADDRESS_HIGH, 2);
337 OUT_RELOCh(chan, bo, dst_offset, reloc);
338 OUT_RELOCl(chan, bo, dst_offset, reloc);
339 }
340 assert(chan->pushbuf->remaining > nr);
341
342 BEGIN_RING(chan, eng2d,
343 NV50_2D_SIFC_DATA | (2 << 29), nr);
344 OUT_RINGp (chan, p, nr);
345
346 p += nr;
347 count -= nr;
348 }
349
350 src += src_pitch;
351 }
352
353 BEGIN_RING(chan, tesla, 0x1440, 1);
354 OUT_RING (chan, 0);
355 }