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_2d_texture_set(struct nouveau_channel
*chan
, int dst
,
82 struct nvc0_miptree
*mt
, unsigned level
, unsigned layer
)
84 struct nouveau_bo
*bo
= mt
->base
.bo
;
85 uint32_t width
, height
, depth
;
87 uint32_t mthd
= dst
? NVC0_2D_DST_FORMAT
: NVC0_2D_SRC_FORMAT
;
88 uint32_t flags
= mt
->base
.domain
| (dst
? NOUVEAU_BO_WR
: NOUVEAU_BO_RD
);
89 uint32_t offset
= mt
->level
[level
].offset
;
91 format
= nvc0_2d_format(mt
->base
.base
.format
);
93 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
94 util_format_name(mt
->base
.base
.format
));
98 width
= u_minify(mt
->base
.base
.width0
, level
);
99 height
= u_minify(mt
->base
.base
.height0
, level
);
101 offset
= mt
->level
[level
].offset
;
102 if (!mt
->layout_3d
) {
103 offset
+= mt
->layer_stride
* layer
;
107 depth
= u_minify(mt
->base
.base
.depth0
, level
);
110 if (!(bo
->tile_flags
& NOUVEAU_BO_TILE_LAYOUT_MASK
)) {
111 BEGIN_RING(chan
, RING_2D_(mthd
), 2);
112 OUT_RING (chan
, format
);
114 BEGIN_RING(chan
, RING_2D_(mthd
+ 0x14), 5);
115 OUT_RING (chan
, mt
->level
[level
].pitch
);
116 OUT_RING (chan
, width
);
117 OUT_RING (chan
, height
);
118 OUT_RELOCh(chan
, bo
, offset
, flags
);
119 OUT_RELOCl(chan
, bo
, offset
, flags
);
121 BEGIN_RING(chan
, RING_2D_(mthd
), 5);
122 OUT_RING (chan
, format
);
124 OUT_RING (chan
, mt
->level
[level
].tile_mode
);
125 OUT_RING (chan
, depth
);
126 OUT_RING (chan
, layer
);
127 BEGIN_RING(chan
, RING_2D_(mthd
+ 0x18), 4);
128 OUT_RING (chan
, width
);
129 OUT_RING (chan
, height
);
130 OUT_RELOCh(chan
, bo
, offset
, flags
);
131 OUT_RELOCl(chan
, bo
, offset
, flags
);
136 BEGIN_RING(chan
, RING_2D_(NVC0_2D_CLIP_X
), 4);
139 OUT_RING (chan
, width
);
140 OUT_RING (chan
, height
);
147 nvc0_2d_texture_do_copy(struct nouveau_channel
*chan
,
148 struct nvc0_miptree
*dst
, unsigned dst_level
,
149 unsigned dx
, unsigned dy
, unsigned dz
,
150 struct nvc0_miptree
*src
, unsigned src_level
,
151 unsigned sx
, unsigned sy
, unsigned sz
,
152 unsigned w
, unsigned h
)
156 ret
= MARK_RING(chan
, 2 * 16 + 32, 4);
160 ret
= nvc0_2d_texture_set(chan
, 1, dst
, dst_level
, dz
);
164 ret
= nvc0_2d_texture_set(chan
, 0, src
, src_level
, sz
);
168 /* 0/1 = CENTER/CORNER, 10/00 = POINT/BILINEAR */
169 BEGIN_RING(chan
, RING_2D(BLIT_CONTROL
), 1);
171 BEGIN_RING(chan
, RING_2D(BLIT_DST_X
), 4);
176 BEGIN_RING(chan
, RING_2D(BLIT_DU_DX_FRACT
), 4);
181 BEGIN_RING(chan
, RING_2D(BLIT_SRC_X_FRACT
), 4);
191 nvc0_resource_copy_region(struct pipe_context
*pipe
,
192 struct pipe_resource
*dst
, unsigned dst_level
,
193 unsigned dstx
, unsigned dsty
, unsigned dstz
,
194 struct pipe_resource
*src
, unsigned src_level
,
195 const struct pipe_box
*src_box
)
197 struct nvc0_screen
*screen
= nvc0_context(pipe
)->screen
;
199 unsigned dst_layer
= dstz
, src_layer
= src_box
->z
;
201 assert((src
->format
== dst
->format
) ||
202 (nvc0_2d_format_faithful(src
->format
) &&
203 nvc0_2d_format_faithful(dst
->format
)));
205 for (; dst_layer
< dstz
+ src_box
->depth
; ++dst_layer
, ++src_layer
) {
206 ret
= nvc0_2d_texture_do_copy(screen
->base
.channel
,
207 nvc0_miptree(dst
), dst_level
,
208 dstx
, dsty
, dst_layer
,
209 nvc0_miptree(src
), src_level
,
210 src_box
->x
, src_box
->y
, src_layer
,
211 src_box
->width
, src_box
->height
);
218 nvc0_clear_render_target(struct pipe_context
*pipe
,
219 struct pipe_surface
*dst
,
221 unsigned dstx
, unsigned dsty
,
222 unsigned width
, unsigned height
)
224 struct nvc0_context
*nv50
= nvc0_context(pipe
);
225 struct nvc0_screen
*screen
= nv50
->screen
;
226 struct nouveau_channel
*chan
= screen
->base
.channel
;
227 struct nvc0_miptree
*mt
= nvc0_miptree(dst
->texture
);
228 struct nvc0_surface
*sf
= nvc0_surface(dst
);
229 struct nouveau_bo
*bo
= mt
->base
.bo
;
231 BEGIN_RING(chan
, RING_3D(CLEAR_COLOR(0)), 4);
232 OUT_RINGf (chan
, rgba
[0]);
233 OUT_RINGf (chan
, rgba
[1]);
234 OUT_RINGf (chan
, rgba
[2]);
235 OUT_RINGf (chan
, rgba
[3]);
237 if (MARK_RING(chan
, 18, 2))
240 BEGIN_RING(chan
, RING_3D(RT_CONTROL
), 1);
242 BEGIN_RING(chan
, RING_3D(RT_ADDRESS_HIGH(0)), 8);
243 OUT_RELOCh(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
244 OUT_RELOCl(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
245 OUT_RING (chan
, sf
->width
);
246 OUT_RING (chan
, sf
->height
);
247 OUT_RING (chan
, nvc0_format_table
[dst
->format
].rt
);
248 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
252 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
254 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
255 OUT_RING (chan
, (width
<< 16) | dstx
);
256 OUT_RING (chan
, (height
<< 16) | dsty
);
258 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
259 OUT_RING (chan
, 0x3c);
261 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
265 nvc0_clear_depth_stencil(struct pipe_context
*pipe
,
266 struct pipe_surface
*dst
,
267 unsigned clear_flags
,
270 unsigned dstx
, unsigned dsty
,
271 unsigned width
, unsigned height
)
273 struct nvc0_context
*nv50
= nvc0_context(pipe
);
274 struct nvc0_screen
*screen
= nv50
->screen
;
275 struct nouveau_channel
*chan
= screen
->base
.channel
;
276 struct nvc0_miptree
*mt
= nvc0_miptree(dst
->texture
);
277 struct nvc0_surface
*sf
= nvc0_surface(dst
);
278 struct nouveau_bo
*bo
= mt
->base
.bo
;
281 if (clear_flags
& PIPE_CLEAR_DEPTH
) {
282 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
283 OUT_RINGf (chan
, depth
);
284 mode
|= NVC0_3D_CLEAR_BUFFERS_Z
;
287 if (clear_flags
& PIPE_CLEAR_STENCIL
) {
288 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
289 OUT_RING (chan
, stencil
& 0xff);
290 mode
|= NVC0_3D_CLEAR_BUFFERS_S
;
293 if (MARK_RING(chan
, 17, 2))
296 BEGIN_RING(chan
, RING_3D(ZETA_ADDRESS_HIGH
), 5);
297 OUT_RELOCh(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
298 OUT_RELOCl(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
299 OUT_RING (chan
, nvc0_format_table
[dst
->format
].rt
);
300 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
302 BEGIN_RING(chan
, RING_3D(ZETA_ENABLE
), 1);
304 BEGIN_RING(chan
, RING_3D(ZETA_HORIZ
), 3);
305 OUT_RING (chan
, sf
->width
);
306 OUT_RING (chan
, sf
->height
);
307 OUT_RING (chan
, (1 << 16) | 1);
309 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
310 OUT_RING (chan
, (width
<< 16) | dstx
);
311 OUT_RING (chan
, (height
<< 16) | dsty
);
313 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
314 OUT_RING (chan
, mode
);
316 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
320 nvc0_clear(struct pipe_context
*pipe
, unsigned buffers
,
321 const float *rgba
, double depth
, unsigned stencil
)
323 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
324 struct nouveau_channel
*chan
= nvc0
->screen
->base
.channel
;
325 struct pipe_framebuffer_state
*fb
= &nvc0
->framebuffer
;
327 const unsigned dirty
= nvc0
->dirty
;
330 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
331 nvc0
->dirty
&= NVC0_NEW_FRAMEBUFFER
;
332 if (!nvc0_state_validate(nvc0
))
335 if (buffers
& PIPE_CLEAR_COLOR
&& fb
->nr_cbufs
) {
336 BEGIN_RING(chan
, RING_3D(CLEAR_COLOR(0)), 4);
337 OUT_RINGf (chan
, rgba
[0]);
338 OUT_RINGf (chan
, rgba
[1]);
339 OUT_RINGf (chan
, rgba
[2]);
340 OUT_RINGf (chan
, rgba
[3]);
342 NVC0_3D_CLEAR_BUFFERS_R
| NVC0_3D_CLEAR_BUFFERS_G
|
343 NVC0_3D_CLEAR_BUFFERS_B
| NVC0_3D_CLEAR_BUFFERS_A
;
346 if (buffers
& PIPE_CLEAR_DEPTH
) {
347 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
348 OUT_RING (chan
, fui(depth
));
349 mode
|= NVC0_3D_CLEAR_BUFFERS_Z
;
352 if (buffers
& PIPE_CLEAR_STENCIL
) {
353 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
354 OUT_RING (chan
, stencil
& 0xff);
355 mode
|= NVC0_3D_CLEAR_BUFFERS_S
;
358 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
359 OUT_RING (chan
, mode
);
361 for (i
= 1; i
< fb
->nr_cbufs
; i
++) {
362 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
363 OUT_RING (chan
, (i
<< 6) | 0x3c);
366 nvc0
->dirty
= dirty
& ~NVC0_NEW_FRAMEBUFFER
;
370 nvc0_init_surface_functions(struct nvc0_context
*nvc0
)
372 nvc0
->pipe
.resource_copy_region
= nvc0_resource_copy_region
;
373 nvc0
->pipe
.clear_render_target
= nvc0_clear_render_target
;
374 nvc0
->pipe
.clear_depth_stencil
= nvc0_clear_depth_stencil
;