Merge branch 'mesa_7_6_branch'
[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 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 tx->base.nblocksx = pt->nblocksx[level];
146 tx->base.nblocksy = pt->nblocksy[level];
147 tx->base.stride = (w * pt->block.size);
148 tx->base.usage = usage;
149
150 tx->level_pitch = lvl->pitch;
151 tx->level_width = mt->base.base.width[level];
152 tx->level_height = mt->base.base.height[level];
153 tx->level_offset = lvl->image_offset[image];
154 tx->level_tiling = lvl->tile_mode;
155 tx->level_x = x;
156 tx->level_y = y;
157 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
158 w * pt->block.size * h, &tx->bo);
159 if (ret) {
160 FREE(tx);
161 return NULL;
162 }
163
164 if (usage & PIPE_TRANSFER_READ) {
165 nv50_transfer_rect_m2mf(pscreen, mt->base.bo, tx->level_offset,
166 tx->level_pitch, tx->level_tiling,
167 x, y,
168 tx->level_width, tx->level_height,
169 tx->bo, 0, tx->base.stride,
170 tx->bo->tile_mode, 0, 0,
171 tx->base.width, tx->base.height,
172 tx->base.block.size, w, h,
173 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART,
174 NOUVEAU_BO_GART);
175 }
176
177 return &tx->base;
178 }
179
180 static void
181 nv50_transfer_del(struct pipe_transfer *ptx)
182 {
183 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
184 struct nv50_miptree *mt = nv50_miptree(ptx->texture);
185
186 if (ptx->usage & PIPE_TRANSFER_WRITE) {
187 struct pipe_screen *pscreen = ptx->texture->screen;
188 nv50_transfer_rect_m2mf(pscreen, tx->bo, 0, tx->base.stride,
189 tx->bo->tile_mode, 0, 0,
190 tx->base.width, tx->base.height,
191 mt->base.bo, tx->level_offset,
192 tx->level_pitch, tx->level_tiling,
193 tx->level_x, tx->level_y,
194 tx->level_width, tx->level_height,
195 tx->base.block.size, tx->base.width,
196 tx->base.height,
197 NOUVEAU_BO_GART, NOUVEAU_BO_VRAM |
198 NOUVEAU_BO_GART);
199 }
200
201 nouveau_bo_ref(NULL, &tx->bo);
202 pipe_texture_reference(&ptx->texture, NULL);
203 FREE(ptx);
204 }
205
206 static void *
207 nv50_transfer_map(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
208 {
209 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
210 unsigned flags = 0;
211 int ret;
212
213 if (ptx->usage & PIPE_TRANSFER_WRITE)
214 flags |= NOUVEAU_BO_WR;
215 if (ptx->usage & PIPE_TRANSFER_READ)
216 flags |= NOUVEAU_BO_RD;
217
218 ret = nouveau_bo_map(tx->bo, flags);
219 if (ret)
220 return NULL;
221 return tx->bo->map;
222 }
223
224 static void
225 nv50_transfer_unmap(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
226 {
227 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
228
229 nouveau_bo_unmap(tx->bo);
230 }
231
232 void
233 nv50_transfer_init_screen_functions(struct pipe_screen *pscreen)
234 {
235 pscreen->get_tex_transfer = nv50_transfer_new;
236 pscreen->tex_transfer_destroy = nv50_transfer_del;
237 pscreen->transfer_map = nv50_transfer_map;
238 pscreen->transfer_unmap = nv50_transfer_unmap;
239 }