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