Merge branch 'master' into gallium-texture-transfer
[mesa.git] / src / gallium / drivers / nv50 / nv50_surface.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 #define __NOUVEAU_PUSH_H__
24 #include <stdint.h>
25 #include "nouveau/nouveau_pushbuf.h"
26 #include "nv50_context.h"
27 #include "pipe/p_defines.h"
28 #include "pipe/internal/p_winsys_screen.h"
29 #include "pipe/p_inlines.h"
30
31 #include "util/u_tile.h"
32
33 static INLINE int
34 nv50_format(enum pipe_format format)
35 {
36 switch (format) {
37 case PIPE_FORMAT_A8R8G8B8_UNORM:
38 case PIPE_FORMAT_Z24S8_UNORM:
39 return NV50_2D_DST_FORMAT_32BPP;
40 case PIPE_FORMAT_X8R8G8B8_UNORM:
41 return NV50_2D_DST_FORMAT_24BPP;
42 case PIPE_FORMAT_R5G6B5_UNORM:
43 return NV50_2D_DST_FORMAT_16BPP;
44 case PIPE_FORMAT_A8_UNORM:
45 return NV50_2D_DST_FORMAT_8BPP;
46 default:
47 return -1;
48 }
49 }
50
51 static int
52 nv50_surface_set(struct nv50_screen *screen, struct pipe_surface *ps, int dst)
53 {
54 struct nouveau_channel *chan = screen->nvws->channel;
55 struct nouveau_grobj *eng2d = screen->eng2d;
56 struct nouveau_bo *bo;
57 int format, mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
58 int flags = NOUVEAU_BO_VRAM | (dst ? NOUVEAU_BO_WR : NOUVEAU_BO_RD);
59
60 bo = screen->nvws->get_bo(nv50_miptree(ps->texture)->buffer);
61 if (!bo)
62 return 1;
63
64 format = nv50_format(ps->format);
65 if (format < 0)
66 return 1;
67
68 if (!bo->tiled) {
69 BEGIN_RING(chan, eng2d, mthd, 2);
70 OUT_RING (chan, format);
71 OUT_RING (chan, 1);
72 BEGIN_RING(chan, eng2d, mthd + 0x14, 5);
73 OUT_RING (chan, ps->stride);
74 OUT_RING (chan, ps->width);
75 OUT_RING (chan, ps->height);
76 OUT_RELOCh(chan, bo, ps->offset, flags);
77 OUT_RELOCl(chan, bo, ps->offset, flags);
78 } else {
79 BEGIN_RING(chan, eng2d, mthd, 5);
80 OUT_RING (chan, format);
81 OUT_RING (chan, 0);
82 OUT_RING (chan, 0);
83 OUT_RING (chan, 1);
84 OUT_RING (chan, 0);
85 BEGIN_RING(chan, eng2d, mthd + 0x18, 4);
86 OUT_RING (chan, ps->width);
87 OUT_RING (chan, ps->height);
88 OUT_RELOCh(chan, bo, ps->offset, flags);
89 OUT_RELOCl(chan, bo, ps->offset, flags);
90 }
91
92 #if 0
93 if (dst) {
94 BEGIN_RING(chan, eng2d, NV50_2D_CLIP_X, 4);
95 OUT_RING (chan, 0);
96 OUT_RING (chan, 0);
97 OUT_RING (chan, surf->width);
98 OUT_RING (chan, surf->height);
99 }
100 #endif
101
102 return 0;
103 }
104
105 int
106 nv50_surface_do_copy(struct nv50_screen *screen, struct pipe_surface *dst,
107 int dx, int dy, struct pipe_surface *src, int sx, int sy,
108 int w, int h)
109 {
110 struct nouveau_channel *chan = screen->nvws->channel;
111 struct nouveau_grobj *eng2d = screen->eng2d;
112 int ret;
113
114 WAIT_RING (chan, 32);
115
116 ret = nv50_surface_set(screen, dst, 1);
117 if (ret)
118 return ret;
119
120 ret = nv50_surface_set(screen, src, 0);
121 if (ret)
122 return ret;
123
124 BEGIN_RING(chan, eng2d, 0x088c, 1);
125 OUT_RING (chan, 0);
126 BEGIN_RING(chan, eng2d, NV50_2D_BLIT_DST_X, 4);
127 OUT_RING (chan, dx);
128 OUT_RING (chan, dy);
129 OUT_RING (chan, w);
130 OUT_RING (chan, h);
131 BEGIN_RING(chan, eng2d, 0x08c0, 4);
132 OUT_RING (chan, 0);
133 OUT_RING (chan, 1);
134 OUT_RING (chan, 0);
135 OUT_RING (chan, 1);
136 BEGIN_RING(chan, eng2d, 0x08d0, 4);
137 OUT_RING (chan, 0);
138 OUT_RING (chan, sx);
139 OUT_RING (chan, 0);
140 OUT_RING (chan, sy);
141
142 return 0;
143 }
144
145 static void
146 nv50_surface_copy(struct pipe_context *pipe, boolean flip,
147 struct pipe_surface *dest, unsigned destx, unsigned desty,
148 struct pipe_surface *src, unsigned srcx, unsigned srcy,
149 unsigned width, unsigned height)
150 {
151 struct nv50_context *nv50 = (struct nv50_context *)pipe;
152 struct nv50_screen *screen = nv50->screen;
153
154 assert(src->format == dest->format);
155
156 if (flip) {
157 desty += height;
158 while (height--) {
159 nv50_surface_do_copy(screen, dest, destx, desty--, src,
160 srcx, srcy++, width, 1);
161 }
162 } else {
163 nv50_surface_do_copy(screen, dest, destx, desty, src, srcx,
164 srcy, width, height);
165 }
166 }
167
168 static void
169 nv50_surface_fill(struct pipe_context *pipe, struct pipe_surface *dest,
170 unsigned destx, unsigned desty, unsigned width,
171 unsigned height, unsigned value)
172 {
173 struct nv50_context *nv50 = (struct nv50_context *)pipe;
174 struct nv50_screen *screen = nv50->screen;
175 struct nouveau_channel *chan = screen->nvws->channel;
176 struct nouveau_grobj *eng2d = screen->eng2d;
177 int format, ret;
178
179 format = nv50_format(dest->format);
180 if (format < 0)
181 return;
182
183 WAIT_RING (chan, 32);
184
185 ret = nv50_surface_set(screen, dest, 1);
186 if (ret)
187 return;
188
189 BEGIN_RING(chan, eng2d, 0x0580, 3);
190 OUT_RING (chan, 4);
191 OUT_RING (chan, format);
192 OUT_RING (chan, value);
193 BEGIN_RING(chan, eng2d, NV50_2D_RECT_X1, 4);
194 OUT_RING (chan, destx);
195 OUT_RING (chan, desty);
196 OUT_RING (chan, width);
197 OUT_RING (chan, height);
198 }
199
200 static void *
201 nv50_surface_map(struct pipe_screen *screen, struct pipe_surface *ps,
202 unsigned flags )
203 {
204 struct pipe_winsys *ws = screen->winsys;
205
206 return ws->buffer_map(ws, nv50_surface_buffer(ps), flags);
207 }
208
209 static void
210 nv50_surface_unmap(struct pipe_screen *pscreen, struct pipe_surface *ps)
211 {
212 struct pipe_winsys *ws = pscreen->winsys;
213
214 ws->buffer_unmap(ws, nv50_surface_buffer(ps));
215 }
216
217 void
218 nv50_init_surface_functions(struct nv50_context *nv50)
219 {
220 nv50->pipe.surface_copy = nv50_surface_copy;
221 nv50->pipe.surface_fill = nv50_surface_fill;
222 }
223
224 void
225 nv50_surface_init_screen_functions(struct pipe_screen *pscreen)
226 {
227 pscreen->surface_map = nv50_surface_map;
228 pscreen->surface_unmap = nv50_surface_unmap;
229 }
230