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 #define NVC0_ENG2D_SUPPORTED_FORMATS 0xff9ccfe1cce3ccc9ULL
38 /* return TRUE for formats that can be converted among each other by NVC0_2D */
40 nvc0_2d_format_faithful(enum pipe_format format
)
42 uint8_t id
= nvc0_format_table
[format
].rt
;
44 return (id
>= 0xc0) && (NVC0_ENG2D_SUPPORTED_FORMATS
& (1ULL << (id
- 0xc0)));
48 nvc0_2d_format(enum pipe_format format
)
50 uint8_t id
= nvc0_format_table
[format
].rt
;
52 /* Hardware values for color formats range from 0xc0 to 0xff,
53 * but the 2D engine doesn't support all of them.
55 if (nvc0_2d_format_faithful(format
))
58 switch (util_format_get_blocksize(format
)) {
60 return NV50_SURFACE_FORMAT_R8_UNORM
;
62 return NV50_SURFACE_FORMAT_R16_UNORM
;
64 return NV50_SURFACE_FORMAT_A8R8G8B8_UNORM
;
66 return NV50_SURFACE_FORMAT_R16G16B16A16_UNORM
;
68 return NV50_SURFACE_FORMAT_R32G32B32A32_FLOAT
;
75 nvc0_2d_texture_set(struct nouveau_channel
*chan
, int dst
,
76 struct nvc0_miptree
*mt
, unsigned level
, unsigned layer
)
78 struct nouveau_bo
*bo
= mt
->base
.bo
;
79 uint32_t width
, height
, depth
;
81 uint32_t mthd
= dst
? NVC0_2D_DST_FORMAT
: NVC0_2D_SRC_FORMAT
;
82 uint32_t flags
= mt
->base
.domain
| (dst
? NOUVEAU_BO_WR
: NOUVEAU_BO_RD
);
83 uint32_t offset
= mt
->level
[level
].offset
;
85 format
= nvc0_2d_format(mt
->base
.base
.format
);
87 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
88 util_format_name(mt
->base
.base
.format
));
92 width
= u_minify(mt
->base
.base
.width0
, level
);
93 height
= u_minify(mt
->base
.base
.height0
, level
);
95 offset
= mt
->level
[level
].offset
;
97 offset
+= mt
->layer_stride
* layer
;
101 depth
= u_minify(mt
->base
.base
.depth0
, level
);
104 if (!(bo
->tile_flags
& NOUVEAU_BO_TILE_LAYOUT_MASK
)) {
105 BEGIN_RING(chan
, RING_2D_(mthd
), 2);
106 OUT_RING (chan
, format
);
108 BEGIN_RING(chan
, RING_2D_(mthd
+ 0x14), 5);
109 OUT_RING (chan
, mt
->level
[level
].pitch
);
110 OUT_RING (chan
, width
);
111 OUT_RING (chan
, height
);
112 OUT_RELOCh(chan
, bo
, offset
, flags
);
113 OUT_RELOCl(chan
, bo
, offset
, flags
);
115 BEGIN_RING(chan
, RING_2D_(mthd
), 5);
116 OUT_RING (chan
, format
);
118 OUT_RING (chan
, mt
->level
[level
].tile_mode
);
119 OUT_RING (chan
, depth
);
120 OUT_RING (chan
, layer
);
121 BEGIN_RING(chan
, RING_2D_(mthd
+ 0x18), 4);
122 OUT_RING (chan
, width
);
123 OUT_RING (chan
, height
);
124 OUT_RELOCh(chan
, bo
, offset
, flags
);
125 OUT_RELOCl(chan
, bo
, offset
, flags
);
130 BEGIN_RING(chan
, RING_2D_(NVC0_2D_CLIP_X
), 4);
133 OUT_RING (chan
, width
);
134 OUT_RING (chan
, height
);
141 nvc0_2d_texture_do_copy(struct nouveau_channel
*chan
,
142 struct nvc0_miptree
*dst
, unsigned dst_level
,
143 unsigned dx
, unsigned dy
, unsigned dz
,
144 struct nvc0_miptree
*src
, unsigned src_level
,
145 unsigned sx
, unsigned sy
, unsigned sz
,
146 unsigned w
, unsigned h
)
150 ret
= MARK_RING(chan
, 2 * 16 + 32, 4);
154 ret
= nvc0_2d_texture_set(chan
, 1, dst
, dst_level
, dz
);
158 ret
= nvc0_2d_texture_set(chan
, 0, src
, src_level
, sz
);
162 /* 0/1 = CENTER/CORNER, 10/00 = POINT/BILINEAR */
163 BEGIN_RING(chan
, RING_2D(BLIT_CONTROL
), 1);
165 BEGIN_RING(chan
, RING_2D(BLIT_DST_X
), 4);
170 BEGIN_RING(chan
, RING_2D(BLIT_DU_DX_FRACT
), 4);
175 BEGIN_RING(chan
, RING_2D(BLIT_SRC_X_FRACT
), 4);
185 nvc0_resource_copy_region(struct pipe_context
*pipe
,
186 struct pipe_resource
*dst
, unsigned dst_level
,
187 unsigned dstx
, unsigned dsty
, unsigned dstz
,
188 struct pipe_resource
*src
, unsigned src_level
,
189 const struct pipe_box
*src_box
)
191 struct nvc0_screen
*screen
= nvc0_context(pipe
)->screen
;
193 unsigned dst_layer
= dstz
, src_layer
= src_box
->z
;
195 assert((src
->format
== dst
->format
) ||
196 (nvc0_2d_format_faithful(src
->format
) &&
197 nvc0_2d_format_faithful(dst
->format
)));
199 for (; dst_layer
< dstz
+ src_box
->depth
; ++dst_layer
, ++src_layer
) {
200 ret
= nvc0_2d_texture_do_copy(screen
->base
.channel
,
201 nvc0_miptree(dst
), dst_level
,
202 dstx
, dsty
, dst_layer
,
203 nvc0_miptree(src
), src_level
,
204 src_box
->x
, src_box
->y
, src_layer
,
205 src_box
->width
, src_box
->height
);
212 nvc0_clear_render_target(struct pipe_context
*pipe
,
213 struct pipe_surface
*dst
,
215 unsigned dstx
, unsigned dsty
,
216 unsigned width
, unsigned height
)
218 struct nvc0_context
*nv50
= nvc0_context(pipe
);
219 struct nvc0_screen
*screen
= nv50
->screen
;
220 struct nouveau_channel
*chan
= screen
->base
.channel
;
221 struct nvc0_miptree
*mt
= nvc0_miptree(dst
->texture
);
222 struct nvc0_surface
*sf
= nvc0_surface(dst
);
223 struct nouveau_bo
*bo
= mt
->base
.bo
;
225 BEGIN_RING(chan
, RING_3D(CLEAR_COLOR(0)), 4);
226 OUT_RINGf (chan
, rgba
[0]);
227 OUT_RINGf (chan
, rgba
[1]);
228 OUT_RINGf (chan
, rgba
[2]);
229 OUT_RINGf (chan
, rgba
[3]);
231 if (MARK_RING(chan
, 18, 2))
234 BEGIN_RING(chan
, RING_3D(RT_CONTROL
), 1);
236 BEGIN_RING(chan
, RING_3D(RT_ADDRESS_HIGH(0)), 8);
237 OUT_RELOCh(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
238 OUT_RELOCl(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
239 OUT_RING (chan
, sf
->width
);
240 OUT_RING (chan
, sf
->height
);
241 OUT_RING (chan
, nvc0_format_table
[dst
->format
].rt
);
242 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
246 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
248 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
249 OUT_RING (chan
, (width
<< 16) | dstx
);
250 OUT_RING (chan
, (height
<< 16) | dsty
);
252 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
253 OUT_RING (chan
, 0x3c);
255 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
259 nvc0_clear_depth_stencil(struct pipe_context
*pipe
,
260 struct pipe_surface
*dst
,
261 unsigned clear_flags
,
264 unsigned dstx
, unsigned dsty
,
265 unsigned width
, unsigned height
)
267 struct nvc0_context
*nv50
= nvc0_context(pipe
);
268 struct nvc0_screen
*screen
= nv50
->screen
;
269 struct nouveau_channel
*chan
= screen
->base
.channel
;
270 struct nvc0_miptree
*mt
= nvc0_miptree(dst
->texture
);
271 struct nvc0_surface
*sf
= nvc0_surface(dst
);
272 struct nouveau_bo
*bo
= mt
->base
.bo
;
275 if (clear_flags
& PIPE_CLEAR_DEPTH
) {
276 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
277 OUT_RINGf (chan
, depth
);
278 mode
|= NVC0_3D_CLEAR_BUFFERS_Z
;
281 if (clear_flags
& PIPE_CLEAR_STENCIL
) {
282 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
283 OUT_RING (chan
, stencil
& 0xff);
284 mode
|= NVC0_3D_CLEAR_BUFFERS_S
;
287 if (MARK_RING(chan
, 17, 2))
290 BEGIN_RING(chan
, RING_3D(ZETA_ADDRESS_HIGH
), 5);
291 OUT_RELOCh(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
292 OUT_RELOCl(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
293 OUT_RING (chan
, nvc0_format_table
[dst
->format
].rt
);
294 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
296 BEGIN_RING(chan
, RING_3D(ZETA_ENABLE
), 1);
298 BEGIN_RING(chan
, RING_3D(ZETA_HORIZ
), 3);
299 OUT_RING (chan
, sf
->width
);
300 OUT_RING (chan
, sf
->height
);
301 OUT_RING (chan
, (1 << 16) | 1);
303 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
304 OUT_RING (chan
, (width
<< 16) | dstx
);
305 OUT_RING (chan
, (height
<< 16) | dsty
);
307 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
308 OUT_RING (chan
, mode
);
310 nv50
->dirty
|= NVC0_NEW_FRAMEBUFFER
;
314 nvc0_clear(struct pipe_context
*pipe
, unsigned buffers
,
315 const float *rgba
, double depth
, unsigned stencil
)
317 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
318 struct nouveau_channel
*chan
= nvc0
->screen
->base
.channel
;
319 struct pipe_framebuffer_state
*fb
= &nvc0
->framebuffer
;
321 const unsigned dirty
= nvc0
->dirty
;
324 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
325 nvc0
->dirty
&= NVC0_NEW_FRAMEBUFFER
;
326 if (!nvc0_state_validate(nvc0
))
329 if (buffers
& PIPE_CLEAR_COLOR
&& fb
->nr_cbufs
) {
330 BEGIN_RING(chan
, RING_3D(CLEAR_COLOR(0)), 4);
331 OUT_RINGf (chan
, rgba
[0]);
332 OUT_RINGf (chan
, rgba
[1]);
333 OUT_RINGf (chan
, rgba
[2]);
334 OUT_RINGf (chan
, rgba
[3]);
336 NVC0_3D_CLEAR_BUFFERS_R
| NVC0_3D_CLEAR_BUFFERS_G
|
337 NVC0_3D_CLEAR_BUFFERS_B
| NVC0_3D_CLEAR_BUFFERS_A
;
340 if (buffers
& PIPE_CLEAR_DEPTH
) {
341 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
342 OUT_RING (chan
, fui(depth
));
343 mode
|= NVC0_3D_CLEAR_BUFFERS_Z
;
346 if (buffers
& PIPE_CLEAR_STENCIL
) {
347 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
348 OUT_RING (chan
, stencil
& 0xff);
349 mode
|= NVC0_3D_CLEAR_BUFFERS_S
;
352 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
353 OUT_RING (chan
, mode
);
355 for (i
= 1; i
< fb
->nr_cbufs
; i
++) {
356 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
357 OUT_RING (chan
, (i
<< 6) | 0x3c);
360 nvc0
->dirty
= dirty
& ~NVC0_NEW_FRAMEBUFFER
;
364 nvc0_init_surface_functions(struct nvc0_context
*nvc0
)
366 nvc0
->pipe
.resource_copy_region
= nvc0_resource_copy_region
;
367 nvc0
->pipe
.clear_render_target
= nvc0_clear_render_target
;
368 nvc0
->pipe
.clear_depth_stencil
= nvc0_clear_depth_stencil
;