2 * Copyright 2008 Ben Skeggs
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 #include "pipe/p_defines.h"
27 #include "util/u_inlines.h"
28 #include "util/u_pack_color.h"
29 #include "util/u_format.h"
31 #include "nvc0_context.h"
32 #include "nvc0_resource.h"
34 #include "nv50_defs.xml.h"
36 /* return TRUE for formats that can be converted among each other by NVC0_2D */
38 nvc0_2d_format_faithful(enum pipe_format 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
:
58 nvc0_2d_format(enum pipe_format format
)
60 uint8_t id
= nvc0_format_table
[format
].rt
;
62 /* Hardware values for color formats range from 0xc0 to 0xff,
63 * but the 2D engine doesn't support all of them.
65 if ((id
>= 0xc0) && (0xff0843e080608409ULL
& (1ULL << (id
- 0xc0))))
68 switch (util_format_get_blocksize(format
)) {
70 return NV50_SURFACE_FORMAT_R8_UNORM
;
72 return NV50_SURFACE_FORMAT_R16_UNORM
;
74 return NV50_SURFACE_FORMAT_A8R8G8B8_UNORM
;
81 nvc0_surface_set(struct nvc0_screen
*screen
, struct pipe_surface
*ps
, int dst
)
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
);
89 format
= nvc0_2d_format(ps
->format
);
91 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
92 util_format_name(ps
->format
));
96 if (!bo
->tile_flags
) {
97 BEGIN_RING(chan
, RING_2D_(mthd
), 2);
98 OUT_RING (chan
, format
);
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
);
107 BEGIN_RING(chan
, RING_2D_(mthd
), 5);
108 OUT_RING (chan
, format
);
110 OUT_RING (chan
, mt
->level
[ps
->level
].tile_mode
);
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
);
122 BEGIN_RING(chan
, RING_2D_(NVC0_2D_CLIP_X
), 4);
125 OUT_RING (chan
, surf
->width
);
126 OUT_RING (chan
, surf
->height
);
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
,
138 struct nouveau_channel
*chan
= screen
->base
.channel
;
141 ret
= MARK_RING(chan
, 2*16 + 32, 4);
145 ret
= nvc0_surface_set(screen
, dst
, 1);
149 ret
= nvc0_surface_set(screen
, src
, 0);
153 /* 0/1 = CENTER/CORNER, 10/00 = POINT/BILINEAR */
154 BEGIN_RING(chan
, RING_2D(BLIT_CONTROL
), 1);
156 BEGIN_RING(chan
, RING_2D(BLIT_DST_X
), 4);
161 BEGIN_RING(chan
, RING_2D(BLIT_DU_DX_FRACT
), 4);
166 BEGIN_RING(chan
, RING_2D(BLIT_SRC_X_FRACT
), 4);
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
)
183 struct nvc0_context
*nv50
= nvc0_context(pipe
);
184 struct nvc0_screen
*screen
= nv50
->screen
;
185 struct pipe_surface
*ps_dst
, *ps_src
;
187 assert((src
->format
== dest
->format
) ||
188 (nvc0_2d_format_faithful(src
->format
) &&
189 nvc0_2d_format_faithful(dest
->format
)));
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 */);
196 nvc0_surface_do_copy(screen
, ps_dst
, destx
, desty
, ps_src
, srcx
,
197 srcy
, width
, height
);
199 nvc0_miptree_surface_del(ps_src
);
200 nvc0_miptree_surface_del(ps_dst
);
204 nvc0_clear_render_target(struct pipe_context
*pipe
,
205 struct pipe_surface
*dst
,
207 unsigned dstx
, unsigned dsty
,
208 unsigned width
, unsigned height
)
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
;
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]);
222 if (MARK_RING(chan
, 18, 2))
225 BEGIN_RING(chan
, RING_3D(RT_CONTROL
), 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
);
237 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
239 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
240 OUT_RING (chan
, (width
<< 16) | dstx
);
241 OUT_RING (chan
, (height
<< 16) | dsty
);
243 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
244 OUT_RING (chan
, 0x3c);
246 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
250 nvc0_clear_depth_stencil(struct pipe_context
*pipe
,
251 struct pipe_surface
*dst
,
252 unsigned clear_flags
,
255 unsigned dstx
, unsigned dsty
,
256 unsigned width
, unsigned height
)
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
;
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
;
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
;
277 if (MARK_RING(chan
, 17, 2))
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
);
286 BEGIN_RING(chan
, RING_3D(ZETA_ENABLE
), 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);
293 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
294 OUT_RING (chan
, (width
<< 16) | dstx
);
295 OUT_RING (chan
, (height
<< 16) | dsty
);
297 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
298 OUT_RING (chan
, mode
);
300 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
304 nvc0_clear(struct pipe_context
*pipe
, unsigned buffers
,
305 const float *rgba
, double depth
, unsigned stencil
)
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
;
311 const unsigned dirty
= nvc0
->dirty
;
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
))
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]);
326 NVC0_3D_CLEAR_BUFFERS_R
| NVC0_3D_CLEAR_BUFFERS_G
|
327 NVC0_3D_CLEAR_BUFFERS_B
| NVC0_3D_CLEAR_BUFFERS_A
;
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
;
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
;
342 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
343 OUT_RING (chan
, mode
);
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);
350 nvc0
->dirty
= dirty
& ~NVC0_NEW_FRAMEBUFFER
;
354 nvc0_init_surface_functions(struct nvc0_context
*nvc0
)
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
;