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