470df1b2a20c7ccbe036e3f6ccd9fa9084d3d15c
[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 "nv50_resource.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_inlines.h"
30 #include "util/u_pack_color.h"
31
32 #include "util/u_tile.h"
33 #include "util/u_format.h"
34
35 /* return TRUE for formats that can be converted among each other by NV50_2D */
36 static INLINE boolean
37 nv50_2d_format_faithful(enum pipe_format format)
38 {
39 switch (format) {
40 case PIPE_FORMAT_B8G8R8A8_UNORM:
41 case PIPE_FORMAT_B8G8R8X8_UNORM:
42 case PIPE_FORMAT_B8G8R8A8_SRGB:
43 case PIPE_FORMAT_B8G8R8X8_SRGB:
44 case PIPE_FORMAT_B5G6R5_UNORM:
45 case PIPE_FORMAT_B5G5R5A1_UNORM:
46 case PIPE_FORMAT_B10G10R10A2_UNORM:
47 case PIPE_FORMAT_R8_UNORM:
48 case PIPE_FORMAT_R32G32B32A32_FLOAT:
49 case PIPE_FORMAT_R32G32B32_FLOAT:
50 return TRUE;
51 default:
52 return FALSE;
53 }
54 }
55
56 static INLINE int
57 nv50_format(enum pipe_format format)
58 {
59 switch (format) {
60 case PIPE_FORMAT_B8G8R8A8_UNORM:
61 return NV50_2D_DST_FORMAT_A8R8G8B8_UNORM;
62 case PIPE_FORMAT_B8G8R8X8_UNORM:
63 return NV50_2D_DST_FORMAT_X8R8G8B8_UNORM;
64 case PIPE_FORMAT_B8G8R8A8_SRGB:
65 return NV50_2D_DST_FORMAT_A8R8G8B8_SRGB;
66 case PIPE_FORMAT_B8G8R8X8_SRGB:
67 return NV50_2D_DST_FORMAT_X8R8G8B8_SRGB;
68 case PIPE_FORMAT_B5G6R5_UNORM:
69 return NV50_2D_DST_FORMAT_R5G6B5_UNORM;
70 case PIPE_FORMAT_B5G5R5A1_UNORM:
71 return NV50_2D_DST_FORMAT_A1R5G5B5_UNORM;
72 case PIPE_FORMAT_B10G10R10A2_UNORM:
73 return NV50_2D_DST_FORMAT_A2R10G10B10_UNORM;
74 case PIPE_FORMAT_A8_UNORM:
75 case PIPE_FORMAT_I8_UNORM:
76 case PIPE_FORMAT_L8_UNORM:
77 case PIPE_FORMAT_R8_UNORM:
78 return NV50_2D_DST_FORMAT_R8_UNORM;
79 case PIPE_FORMAT_R32G32B32A32_FLOAT:
80 return NV50_2D_DST_FORMAT_R32G32B32A32_FLOAT;
81 case PIPE_FORMAT_R32G32B32_FLOAT:
82 return NV50_2D_DST_FORMAT_R32G32B32X32_FLOAT;
83 case PIPE_FORMAT_Z32_FLOAT:
84 return NV50_2D_DST_FORMAT_R32_FLOAT;
85
86 /* only because we require src format == dst format: */
87 case PIPE_FORMAT_R16G16_SNORM:
88 case PIPE_FORMAT_R16G16_UNORM:
89 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
90 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
91 return NV50_2D_DST_FORMAT_A8R8G8B8_UNORM;
92 case PIPE_FORMAT_L8A8_UNORM:
93 case PIPE_FORMAT_B4G4R4A4_UNORM:
94 return NV50_2D_DST_FORMAT_R16_UNORM;
95
96 default:
97 return -1;
98 }
99 }
100
101 static int
102 nv50_surface_set(struct nv50_screen *screen, struct pipe_surface *ps, int dst)
103 {
104 struct nv50_miptree *mt = nv50_miptree(ps->texture);
105 struct nouveau_channel *chan = screen->eng2d->channel;
106 struct nouveau_grobj *eng2d = screen->eng2d;
107 struct nouveau_bo *bo = nv50_miptree(ps->texture)->base.bo;
108 int format, mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
109 int flags = NOUVEAU_BO_VRAM | (dst ? NOUVEAU_BO_WR : NOUVEAU_BO_RD);
110
111 format = nv50_format(ps->format);
112 if (format < 0) {
113 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
114 util_format_name(ps->format));
115 return 1;
116 }
117
118 if (!bo->tile_flags) {
119 MARK_RING (chan, 9, 2); /* flush on lack of space or relocs */
120 BEGIN_RING(chan, eng2d, mthd, 2);
121 OUT_RING (chan, format);
122 OUT_RING (chan, 1);
123 BEGIN_RING(chan, eng2d, mthd + 0x14, 5);
124 OUT_RING (chan, mt->level[ps->level].pitch);
125 OUT_RING (chan, ps->width);
126 OUT_RING (chan, ps->height);
127 OUT_RELOCh(chan, bo, ps->offset, flags);
128 OUT_RELOCl(chan, bo, ps->offset, flags);
129 } else {
130 MARK_RING (chan, 11, 2); /* flush on lack of space or relocs */
131 BEGIN_RING(chan, eng2d, mthd, 5);
132 OUT_RING (chan, format);
133 OUT_RING (chan, 0);
134 OUT_RING (chan, mt->level[ps->level].tile_mode << 4);
135 OUT_RING (chan, 1);
136 OUT_RING (chan, 0);
137 BEGIN_RING(chan, eng2d, mthd + 0x18, 4);
138 OUT_RING (chan, ps->width);
139 OUT_RING (chan, ps->height);
140 OUT_RELOCh(chan, bo, ps->offset, flags);
141 OUT_RELOCl(chan, bo, ps->offset, flags);
142 }
143
144 #if 0
145 if (dst) {
146 BEGIN_RING(chan, eng2d, NV50_2D_CLIP_X, 4);
147 OUT_RING (chan, 0);
148 OUT_RING (chan, 0);
149 OUT_RING (chan, surf->width);
150 OUT_RING (chan, surf->height);
151 }
152 #endif
153
154 return 0;
155 }
156
157 int
158 nv50_surface_do_copy(struct nv50_screen *screen, struct pipe_surface *dst,
159 int dx, int dy, struct pipe_surface *src, int sx, int sy,
160 int w, int h)
161 {
162 struct nouveau_channel *chan = screen->eng2d->channel;
163 struct nouveau_grobj *eng2d = screen->eng2d;
164 int ret;
165
166 WAIT_RING (chan, 32);
167
168 ret = nv50_surface_set(screen, dst, 1);
169 if (ret)
170 return ret;
171
172 ret = nv50_surface_set(screen, src, 0);
173 if (ret)
174 return ret;
175
176 BEGIN_RING(chan, eng2d, 0x088c, 1);
177 OUT_RING (chan, 0);
178 BEGIN_RING(chan, eng2d, NV50_2D_BLIT_DST_X, 4);
179 OUT_RING (chan, dx);
180 OUT_RING (chan, dy);
181 OUT_RING (chan, w);
182 OUT_RING (chan, h);
183 BEGIN_RING(chan, eng2d, 0x08c0, 4);
184 OUT_RING (chan, 0);
185 OUT_RING (chan, 1);
186 OUT_RING (chan, 0);
187 OUT_RING (chan, 1);
188 BEGIN_RING(chan, eng2d, 0x08d0, 4);
189 OUT_RING (chan, 0);
190 OUT_RING (chan, sx);
191 OUT_RING (chan, 0);
192 OUT_RING (chan, sy);
193
194 return 0;
195 }
196
197 static void
198 nv50_surface_copy(struct pipe_context *pipe,
199 struct pipe_resource *dest, struct pipe_subresource subdst,
200 unsigned destx, unsigned desty, unsigned destz,
201 struct pipe_resource *src, struct pipe_subresource subsrc,
202 unsigned srcx, unsigned srcy, unsigned srcz,
203 unsigned width, unsigned height)
204 {
205 struct nv50_context *nv50 = nv50_context(pipe);
206 struct nv50_screen *screen = nv50->screen;
207 struct pipe_surface *ps_dst, *ps_src;
208
209 assert((src->format == dest->format) ||
210 (nv50_2d_format_faithful(src->format) &&
211 nv50_2d_format_faithful(dest->format)));
212
213 ps_src = nv50_miptree_surface_new(pipe->screen, dest, subsrc.face,
214 subsrc.level, srcz, 0 /* bind flags */);
215 ps_dst = nv50_miptree_surface_new(pipe->screen, dest, subdst.face,
216 subdst.level, destz, 0 /* bindflags */);
217
218 nv50_surface_do_copy(screen, ps_dst, destx, desty, ps_src, srcx,
219 srcy, width, height);
220
221 nv50_miptree_surface_del(ps_src);
222 nv50_miptree_surface_del(ps_dst);
223 }
224
225 /* XXX this should probably look more along the lines of nv50_clear */
226 static void
227 nv50_clearRT(struct pipe_context *pipe,
228 struct pipe_surface *dst,
229 const float *rgba,
230 unsigned dstx, unsigned dsty,
231 unsigned width, unsigned height)
232 {
233 struct nv50_context *nv50 = nv50_context(pipe);
234 struct nv50_screen *screen = nv50->screen;
235 struct nouveau_channel *chan = screen->eng2d->channel;
236 struct nouveau_grobj *eng2d = screen->eng2d;
237 int format, ret;
238 union util_color uc;
239 util_pack_color(rgba, dst->format, &uc);
240
241 format = nv50_format(dst->format);
242 if (format < 0)
243 return;
244
245 WAIT_RING (chan, 32);
246
247 ret = nv50_surface_set(screen, dst, 1);
248 if (ret)
249 return;
250
251 BEGIN_RING(chan, eng2d, NV50_2D_DRAW_SHAPE, 3);
252 OUT_RING (chan, NV50_2D_DRAW_SHAPE_RECTANGLES);
253 OUT_RING (chan, format);
254 OUT_RING (chan, uc.ui);
255 BEGIN_RING(chan, eng2d, NV50_2D_DRAW_POINT32_X(0), 4);
256 OUT_RING (chan, dstx);
257 OUT_RING (chan, dsty);
258 OUT_RING (chan, width);
259 OUT_RING (chan, height);
260
261 }
262
263 void
264 nv50_init_surface_functions(struct nv50_context *nv50)
265 {
266 nv50->pipe.resource_copy_region = nv50_surface_copy;
267 nv50->pipe.clearRT = nv50_clearRT;
268 }
269
270