nvc0: use tile flags in a way compatible with nouveau
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_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 #include <stdint.h>
24
25 #include "pipe/p_defines.h"
26
27 #include "util/u_inlines.h"
28 #include "util/u_pack_color.h"
29 #include "util/u_format.h"
30
31 #include "nvc0_context.h"
32 #include "nvc0_resource.h"
33
34 #include "nv50_defs.xml.h"
35
36 /* return TRUE for formats that can be converted among each other by NVC0_2D */
37 static INLINE boolean
38 nvc0_2d_format_faithful(enum pipe_format format)
39 {
40 switch (format) {
41 case PIPE_FORMAT_B8G8R8A8_UNORM:
42 case PIPE_FORMAT_B8G8R8X8_UNORM:
43 case PIPE_FORMAT_B8G8R8A8_SRGB:
44 case PIPE_FORMAT_B8G8R8X8_SRGB:
45 case PIPE_FORMAT_B5G6R5_UNORM:
46 case PIPE_FORMAT_B5G5R5A1_UNORM:
47 case PIPE_FORMAT_B10G10R10A2_UNORM:
48 case PIPE_FORMAT_R8_UNORM:
49 case PIPE_FORMAT_R32G32B32A32_FLOAT:
50 case PIPE_FORMAT_R32G32B32_FLOAT:
51 return TRUE;
52 default:
53 return FALSE;
54 }
55 }
56
57 static INLINE uint8_t
58 nvc0_2d_format(enum pipe_format format)
59 {
60 uint8_t id = nvc0_format_table[format].rt;
61
62 /* Hardware values for color formats range from 0xc0 to 0xff,
63 * but the 2D engine doesn't support all of them.
64 */
65 if ((id >= 0xc0) && (0xff0843e080608409ULL & (1ULL << (id - 0xc0))))
66 return id;
67
68 switch (util_format_get_blocksize(format)) {
69 case 1:
70 return NV50_SURFACE_FORMAT_R8_UNORM;
71 case 2:
72 return NV50_SURFACE_FORMAT_R16_UNORM;
73 case 4:
74 return NV50_SURFACE_FORMAT_A8R8G8B8_UNORM;
75 default:
76 return 0;
77 }
78 }
79
80 static int
81 nvc0_surface_set(struct nvc0_screen *screen, struct pipe_surface *ps, int dst)
82 {
83 struct nvc0_miptree *mt = nvc0_miptree(ps->texture);
84 struct nouveau_channel *chan = screen->base.channel;
85 struct nouveau_bo *bo = nvc0_miptree(ps->texture)->base.bo;
86 int format, mthd = dst ? NVC0_2D_DST_FORMAT : NVC0_2D_SRC_FORMAT;
87 int flags = NOUVEAU_BO_VRAM | (dst ? NOUVEAU_BO_WR : NOUVEAU_BO_RD);
88
89 format = nvc0_2d_format(ps->format);
90 if (!format) {
91 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
92 util_format_name(ps->format));
93 return 1;
94 }
95
96 if (!bo->tile_flags) {
97 BEGIN_RING(chan, RING_2D_(mthd), 2);
98 OUT_RING (chan, format);
99 OUT_RING (chan, 1);
100 BEGIN_RING(chan, RING_2D_(mthd + 0x14), 5);
101 OUT_RING (chan, mt->level[ps->level].pitch);
102 OUT_RING (chan, ps->width);
103 OUT_RING (chan, ps->height);
104 OUT_RELOCh(chan, bo, ps->offset, flags);
105 OUT_RELOCl(chan, bo, ps->offset, flags);
106 } else {
107 BEGIN_RING(chan, RING_2D_(mthd), 5);
108 OUT_RING (chan, format);
109 OUT_RING (chan, 0);
110 OUT_RING (chan, mt->level[ps->level].tile_mode);
111 OUT_RING (chan, 1);
112 OUT_RING (chan, 0);
113 BEGIN_RING(chan, RING_2D_(mthd + 0x18), 4);
114 OUT_RING (chan, ps->width);
115 OUT_RING (chan, ps->height);
116 OUT_RELOCh(chan, bo, ps->offset, flags);
117 OUT_RELOCl(chan, bo, ps->offset, flags);
118 }
119
120 #if 0
121 if (dst) {
122 BEGIN_RING(chan, RING_2D_(NVC0_2D_CLIP_X), 4);
123 OUT_RING (chan, 0);
124 OUT_RING (chan, 0);
125 OUT_RING (chan, surf->width);
126 OUT_RING (chan, surf->height);
127 }
128 #endif
129 return 0;
130 }
131
132 static int
133 nvc0_surface_do_copy(struct nvc0_screen *screen,
134 struct pipe_surface *dst, int dx, int dy,
135 struct pipe_surface *src, int sx, int sy,
136 int w, int h)
137 {
138 struct nouveau_channel *chan = screen->base.channel;
139 int ret;
140
141 ret = MARK_RING(chan, 2*16 + 32, 4);
142 if (ret)
143 return ret;
144
145 ret = nvc0_surface_set(screen, dst, 1);
146 if (ret)
147 return ret;
148
149 ret = nvc0_surface_set(screen, src, 0);
150 if (ret)
151 return ret;
152
153 /* 0/1 = CENTER/CORNER, 10/00 = POINT/BILINEAR */
154 BEGIN_RING(chan, RING_2D(BLIT_CONTROL), 1);
155 OUT_RING (chan, 0);
156 BEGIN_RING(chan, RING_2D(BLIT_DST_X), 4);
157 OUT_RING (chan, dx);
158 OUT_RING (chan, dy);
159 OUT_RING (chan, w);
160 OUT_RING (chan, h);
161 BEGIN_RING(chan, RING_2D(BLIT_DU_DX_FRACT), 4);
162 OUT_RING (chan, 0);
163 OUT_RING (chan, 1);
164 OUT_RING (chan, 0);
165 OUT_RING (chan, 1);
166 BEGIN_RING(chan, RING_2D(BLIT_SRC_X_FRACT), 4);
167 OUT_RING (chan, 0);
168 OUT_RING (chan, sx);
169 OUT_RING (chan, 0);
170 OUT_RING (chan, sy);
171
172 return 0;
173 }
174
175 static void
176 nvc0_surface_copy(struct pipe_context *pipe,
177 struct pipe_resource *dest, struct pipe_subresource subdst,
178 unsigned destx, unsigned desty, unsigned destz,
179 struct pipe_resource *src, struct pipe_subresource subsrc,
180 unsigned srcx, unsigned srcy, unsigned srcz,
181 unsigned width, unsigned height)
182 {
183 struct nvc0_context *nv50 = nvc0_context(pipe);
184 struct nvc0_screen *screen = nv50->screen;
185 struct pipe_surface *ps_dst, *ps_src;
186
187 assert((src->format == dest->format) ||
188 (nvc0_2d_format_faithful(src->format) &&
189 nvc0_2d_format_faithful(dest->format)));
190
191 ps_src = nvc0_miptree_surface_new(pipe->screen, src, subsrc.face,
192 subsrc.level, srcz, 0 /* bind flags */);
193 ps_dst = nvc0_miptree_surface_new(pipe->screen, dest, subdst.face,
194 subdst.level, destz, 0 /* bind flags */);
195
196 nvc0_surface_do_copy(screen, ps_dst, destx, desty, ps_src, srcx,
197 srcy, width, height);
198
199 nvc0_miptree_surface_del(ps_src);
200 nvc0_miptree_surface_del(ps_dst);
201 }
202
203 static void
204 nvc0_clear_render_target(struct pipe_context *pipe,
205 struct pipe_surface *dst,
206 const float *rgba,
207 unsigned dstx, unsigned dsty,
208 unsigned width, unsigned height)
209 {
210 struct nvc0_context *nv50 = nvc0_context(pipe);
211 struct nvc0_screen *screen = nv50->screen;
212 struct nouveau_channel *chan = screen->base.channel;
213 struct nvc0_miptree *mt = nvc0_miptree(dst->texture);
214 struct nouveau_bo *bo = mt->base.bo;
215
216 BEGIN_RING(chan, RING_3D(CLEAR_COLOR(0)), 4);
217 OUT_RINGf (chan, rgba[0]);
218 OUT_RINGf (chan, rgba[1]);
219 OUT_RINGf (chan, rgba[2]);
220 OUT_RINGf (chan, rgba[3]);
221
222 if (MARK_RING(chan, 18, 2))
223 return;
224
225 BEGIN_RING(chan, RING_3D(RT_CONTROL), 1);
226 OUT_RING (chan, 1);
227 BEGIN_RING(chan, RING_3D(RT_ADDRESS_HIGH(0)), 8);
228 OUT_RELOCh(chan, bo, dst->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
229 OUT_RELOCl(chan, bo, dst->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
230 OUT_RING (chan, dst->width);
231 OUT_RING (chan, dst->height);
232 OUT_RING (chan, nvc0_format_table[dst->format].rt);
233 OUT_RING (chan, mt->level[dst->level].tile_mode);
234 OUT_RING (chan, 1);
235 OUT_RING (chan, 0);
236
237 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
238
239 BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
240 OUT_RING (chan, (width << 16) | dstx);
241 OUT_RING (chan, (height << 16) | dsty);
242
243 BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
244 OUT_RING (chan, 0x3c);
245
246 nv50->dirty |= NVC0_NEW_FRAMEBUFFER;
247 }
248
249 static void
250 nvc0_clear_depth_stencil(struct pipe_context *pipe,
251 struct pipe_surface *dst,
252 unsigned clear_flags,
253 double depth,
254 unsigned stencil,
255 unsigned dstx, unsigned dsty,
256 unsigned width, unsigned height)
257 {
258 struct nvc0_context *nv50 = nvc0_context(pipe);
259 struct nvc0_screen *screen = nv50->screen;
260 struct nouveau_channel *chan = screen->base.channel;
261 struct nvc0_miptree *mt = nvc0_miptree(dst->texture);
262 struct nouveau_bo *bo = mt->base.bo;
263 uint32_t mode = 0;
264
265 if (clear_flags & PIPE_CLEAR_DEPTH) {
266 BEGIN_RING(chan, RING_3D(CLEAR_DEPTH), 1);
267 OUT_RINGf (chan, depth);
268 mode |= NVC0_3D_CLEAR_BUFFERS_Z;
269 }
270
271 if (clear_flags & PIPE_CLEAR_STENCIL) {
272 BEGIN_RING(chan, RING_3D(CLEAR_STENCIL), 1);
273 OUT_RING (chan, stencil & 0xff);
274 mode |= NVC0_3D_CLEAR_BUFFERS_S;
275 }
276
277 if (MARK_RING(chan, 17, 2))
278 return;
279
280 BEGIN_RING(chan, RING_3D(ZETA_ADDRESS_HIGH), 5);
281 OUT_RELOCh(chan, bo, dst->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
282 OUT_RELOCl(chan, bo, dst->offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
283 OUT_RING (chan, nvc0_format_table[dst->format].rt);
284 OUT_RING (chan, mt->level[dst->level].tile_mode);
285 OUT_RING (chan, 0);
286 BEGIN_RING(chan, RING_3D(ZETA_ENABLE), 1);
287 OUT_RING (chan, 1);
288 BEGIN_RING(chan, RING_3D(ZETA_HORIZ), 3);
289 OUT_RING (chan, dst->width);
290 OUT_RING (chan, dst->height);
291 OUT_RING (chan, (1 << 16) | 1);
292
293 BEGIN_RING(chan, RING_3D(VIEWPORT_HORIZ(0)), 2);
294 OUT_RING (chan, (width << 16) | dstx);
295 OUT_RING (chan, (height << 16) | dsty);
296
297 BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
298 OUT_RING (chan, mode);
299
300 nv50->dirty |= NVC0_NEW_FRAMEBUFFER;
301 }
302
303 void
304 nvc0_clear(struct pipe_context *pipe, unsigned buffers,
305 const float *rgba, double depth, unsigned stencil)
306 {
307 struct nvc0_context *nvc0 = nvc0_context(pipe);
308 struct nouveau_channel *chan = nvc0->screen->base.channel;
309 struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
310 unsigned i;
311 const unsigned dirty = nvc0->dirty;
312 uint32_t mode = 0;
313
314 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
315 nvc0->dirty &= NVC0_NEW_FRAMEBUFFER;
316 if (!nvc0_state_validate(nvc0))
317 return;
318
319 if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
320 BEGIN_RING(chan, RING_3D(CLEAR_COLOR(0)), 4);
321 OUT_RINGf (chan, rgba[0]);
322 OUT_RINGf (chan, rgba[1]);
323 OUT_RINGf (chan, rgba[2]);
324 OUT_RINGf (chan, rgba[3]);
325 mode =
326 NVC0_3D_CLEAR_BUFFERS_R | NVC0_3D_CLEAR_BUFFERS_G |
327 NVC0_3D_CLEAR_BUFFERS_B | NVC0_3D_CLEAR_BUFFERS_A;
328 }
329
330 if (buffers & PIPE_CLEAR_DEPTH) {
331 BEGIN_RING(chan, RING_3D(CLEAR_DEPTH), 1);
332 OUT_RING (chan, fui(depth));
333 mode |= NVC0_3D_CLEAR_BUFFERS_Z;
334 }
335
336 if (buffers & PIPE_CLEAR_STENCIL) {
337 BEGIN_RING(chan, RING_3D(CLEAR_STENCIL), 1);
338 OUT_RING (chan, stencil & 0xff);
339 mode |= NVC0_3D_CLEAR_BUFFERS_S;
340 }
341
342 BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
343 OUT_RING (chan, mode);
344
345 for (i = 1; i < fb->nr_cbufs; i++) {
346 BEGIN_RING(chan, RING_3D(CLEAR_BUFFERS), 1);
347 OUT_RING (chan, (i << 6) | 0x3c);
348 }
349
350 nvc0->dirty = dirty & ~NVC0_NEW_FRAMEBUFFER;
351 }
352
353 void
354 nvc0_init_surface_functions(struct nvc0_context *nvc0)
355 {
356 nvc0->pipe.resource_copy_region = nvc0_surface_copy;
357 nvc0->pipe.clear_render_target = nvc0_clear_render_target;
358 nvc0->pipe.clear_depth_stencil = nvc0_clear_depth_stencil;
359 }
360
361