Merge commit 'origin/7.8'
[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
31 #include "util/u_tile.h"
32 #include "util/u_format.h"
33
34 static INLINE int
35 nv50_format(enum pipe_format format)
36 {
37 switch (format) {
38 case PIPE_FORMAT_B8G8R8A8_UNORM:
39 return NV50_2D_DST_FORMAT_A8R8G8B8_UNORM;
40 case PIPE_FORMAT_B8G8R8X8_UNORM:
41 return NV50_2D_DST_FORMAT_X8R8G8B8_UNORM;
42 case PIPE_FORMAT_B8G8R8A8_SRGB:
43 return NV50_2D_DST_FORMAT_A8R8G8B8_SRGB;
44 case PIPE_FORMAT_B8G8R8X8_SRGB:
45 return NV50_2D_DST_FORMAT_X8R8G8B8_SRGB;
46 case PIPE_FORMAT_B5G6R5_UNORM:
47 return NV50_2D_DST_FORMAT_R5G6B5_UNORM;
48 case PIPE_FORMAT_B5G5R5A1_UNORM:
49 return NV50_2D_DST_FORMAT_A1R5G5B5_UNORM;
50 case PIPE_FORMAT_A8_UNORM:
51 case PIPE_FORMAT_I8_UNORM:
52 case PIPE_FORMAT_L8_UNORM:
53 return NV50_2D_DST_FORMAT_R8_UNORM;
54 case PIPE_FORMAT_R32G32B32A32_FLOAT:
55 return NV50_2D_DST_FORMAT_R32G32B32A32_FLOAT;
56 case PIPE_FORMAT_R32G32B32_FLOAT:
57 return NV50_2D_DST_FORMAT_R32G32B32X32_FLOAT;
58 case PIPE_FORMAT_Z32_FLOAT:
59 return NV50_2D_DST_FORMAT_R32_FLOAT;
60
61 /* only because we require src format == dst format: */
62 case PIPE_FORMAT_R16G16_SNORM:
63 case PIPE_FORMAT_R16G16_UNORM:
64 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
65 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
66 return NV50_2D_DST_FORMAT_A8R8G8B8_UNORM;
67 case PIPE_FORMAT_L8A8_UNORM:
68 case PIPE_FORMAT_B4G4R4A4_UNORM:
69 return NV50_2D_DST_FORMAT_R16_UNORM;
70
71 default:
72 return -1;
73 }
74 }
75
76 static int
77 nv50_surface_set(struct nv50_screen *screen, struct pipe_surface *ps, int dst)
78 {
79 struct nv50_miptree *mt = nv50_miptree(ps->texture);
80 struct nouveau_channel *chan = screen->eng2d->channel;
81 struct nouveau_grobj *eng2d = screen->eng2d;
82 struct nouveau_bo *bo = nv50_miptree(ps->texture)->base.bo;
83 int format, mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
84 int flags = NOUVEAU_BO_VRAM | (dst ? NOUVEAU_BO_WR : NOUVEAU_BO_RD);
85
86 format = nv50_format(ps->format);
87 if (format < 0) {
88 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
89 util_format_name(ps->format));
90 return 1;
91 }
92
93 if (!bo->tile_flags) {
94 MARK_RING (chan, 9, 2); /* flush on lack of space or relocs */
95 BEGIN_RING(chan, eng2d, mthd, 2);
96 OUT_RING (chan, format);
97 OUT_RING (chan, 1);
98 BEGIN_RING(chan, eng2d, mthd + 0x14, 5);
99 OUT_RING (chan, mt->level[ps->level].pitch);
100 OUT_RING (chan, ps->width);
101 OUT_RING (chan, ps->height);
102 OUT_RELOCh(chan, bo, ps->offset, flags);
103 OUT_RELOCl(chan, bo, ps->offset, flags);
104 } else {
105 MARK_RING (chan, 11, 2); /* flush on lack of space or relocs */
106 BEGIN_RING(chan, eng2d, mthd, 5);
107 OUT_RING (chan, format);
108 OUT_RING (chan, 0);
109 OUT_RING (chan, mt->level[ps->level].tile_mode << 4);
110 OUT_RING (chan, 1);
111 OUT_RING (chan, 0);
112 BEGIN_RING(chan, eng2d, mthd + 0x18, 4);
113 OUT_RING (chan, ps->width);
114 OUT_RING (chan, ps->height);
115 OUT_RELOCh(chan, bo, ps->offset, flags);
116 OUT_RELOCl(chan, bo, ps->offset, flags);
117 }
118
119 #if 0
120 if (dst) {
121 BEGIN_RING(chan, eng2d, NV50_2D_CLIP_X, 4);
122 OUT_RING (chan, 0);
123 OUT_RING (chan, 0);
124 OUT_RING (chan, surf->width);
125 OUT_RING (chan, surf->height);
126 }
127 #endif
128
129 return 0;
130 }
131
132 int
133 nv50_surface_do_copy(struct nv50_screen *screen, struct pipe_surface *dst,
134 int dx, int dy, struct pipe_surface *src, int sx, int sy,
135 int w, int h)
136 {
137 struct nouveau_channel *chan = screen->eng2d->channel;
138 struct nouveau_grobj *eng2d = screen->eng2d;
139 int ret;
140
141 WAIT_RING (chan, 32);
142
143 ret = nv50_surface_set(screen, dst, 1);
144 if (ret)
145 return ret;
146
147 ret = nv50_surface_set(screen, src, 0);
148 if (ret)
149 return ret;
150
151 BEGIN_RING(chan, eng2d, 0x088c, 1);
152 OUT_RING (chan, 0);
153 BEGIN_RING(chan, eng2d, NV50_2D_BLIT_DST_X, 4);
154 OUT_RING (chan, dx);
155 OUT_RING (chan, dy);
156 OUT_RING (chan, w);
157 OUT_RING (chan, h);
158 BEGIN_RING(chan, eng2d, 0x08c0, 4);
159 OUT_RING (chan, 0);
160 OUT_RING (chan, 1);
161 OUT_RING (chan, 0);
162 OUT_RING (chan, 1);
163 BEGIN_RING(chan, eng2d, 0x08d0, 4);
164 OUT_RING (chan, 0);
165 OUT_RING (chan, sx);
166 OUT_RING (chan, 0);
167 OUT_RING (chan, sy);
168
169 return 0;
170 }
171
172 static void
173 nv50_surface_copy(struct pipe_context *pipe,
174 struct pipe_surface *dest, unsigned destx, unsigned desty,
175 struct pipe_surface *src, unsigned srcx, unsigned srcy,
176 unsigned width, unsigned height)
177 {
178 struct nv50_context *nv50 = nv50_context(pipe);
179 struct nv50_screen *screen = nv50->screen;
180
181 assert(src->format == dest->format);
182
183 nv50_surface_do_copy(screen, dest, destx, desty, src, srcx,
184 srcy, width, height);
185 }
186
187 static void
188 nv50_surface_fill(struct pipe_context *pipe, struct pipe_surface *dest,
189 unsigned destx, unsigned desty, unsigned width,
190 unsigned height, unsigned value)
191 {
192 struct nv50_context *nv50 = nv50_context(pipe);
193 struct nv50_screen *screen = nv50->screen;
194 struct nouveau_channel *chan = screen->eng2d->channel;
195 struct nouveau_grobj *eng2d = screen->eng2d;
196 int format, ret;
197
198 format = nv50_format(dest->format);
199 if (format < 0)
200 return;
201
202 WAIT_RING (chan, 32);
203
204 ret = nv50_surface_set(screen, dest, 1);
205 if (ret)
206 return;
207
208 BEGIN_RING(chan, eng2d, NV50_2D_DRAW_SHAPE, 3);
209 OUT_RING (chan, NV50_2D_DRAW_SHAPE_RECTANGLES);
210 OUT_RING (chan, format);
211 OUT_RING (chan, value);
212 BEGIN_RING(chan, eng2d, NV50_2D_DRAW_POINT32_X(0), 4);
213 OUT_RING (chan, destx);
214 OUT_RING (chan, desty);
215 OUT_RING (chan, width);
216 OUT_RING (chan, height);
217 }
218
219 void
220 nv50_init_surface_functions(struct nv50_context *nv50)
221 {
222 nv50->pipe.surface_copy = nv50_surface_copy;
223 nv50->pipe.surface_fill = nv50_surface_fill;
224 }
225
226