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