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 "nv50_context.h"
32 #include "nv50_resource.h"
34 #include "nv50_defs.xml.h"
36 /* return TRUE for formats that can be converted among each other by NV50_2D */
38 nv50_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 nv50_2d_format(enum pipe_format format
)
60 uint8_t id
= nv50_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 nv50_2d_texture_set(struct nouveau_channel
*chan
, int dst
,
82 struct nv50_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
? NV50_2D_DST_FORMAT
: NV50_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
= nv50_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
<< 4);
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_(NV50_2D_CLIP_X
), 4);
139 OUT_RING (chan
, width
);
140 OUT_RING (chan
, height
);
147 nv50_2d_texture_do_copy(struct nouveau_channel
*chan
,
148 struct nv50_miptree
*dst
, unsigned dst_level
,
149 unsigned dx
, unsigned dy
, unsigned dz
,
150 struct nv50_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
= nv50_2d_texture_set(chan
, 1, dst
, dst_level
, dz
);
164 ret
= nv50_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 nv50_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 nv50_screen
*screen
= nv50_context(pipe
)->screen
;
199 unsigned dst_layer
= dstz
, src_layer
= src_box
->z
;
201 assert((src
->format
== dst
->format
) ||
202 (nv50_2d_format_faithful(src
->format
) &&
203 nv50_2d_format_faithful(dst
->format
)));
205 for (; dst_layer
< dstz
+ src_box
->depth
; ++dst_layer
, ++src_layer
) {
206 ret
= nv50_2d_texture_do_copy(screen
->base
.channel
,
207 nv50_miptree(dst
), dst_level
,
208 dstx
, dsty
, dst_layer
,
209 nv50_miptree(src
), src_level
,
210 src_box
->x
, src_box
->y
, src_layer
,
211 src_box
->width
, src_box
->height
);
218 nv50_clear_render_target(struct pipe_context
*pipe
,
219 struct pipe_surface
*dst
,
221 unsigned dstx
, unsigned dsty
,
222 unsigned width
, unsigned height
)
224 struct nv50_context
*nv50
= nv50_context(pipe
);
225 struct nv50_screen
*screen
= nv50
->screen
;
226 struct nouveau_channel
*chan
= screen
->base
.channel
;
227 struct nv50_miptree
*mt
= nv50_miptree(dst
->texture
);
228 struct nv50_surface
*sf
= nv50_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)), 5);
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
, nv50_format_table
[dst
->format
].rt
);
246 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
<< 4);
248 BEGIN_RING(chan
, RING_3D(RT_HORIZ(0)), 2);
249 OUT_RING (chan
, sf
->width
);
250 OUT_RING (chan
, sf
->height
);
251 BEGIN_RING(chan
, RING_3D(RT_ARRAY_MODE
), 1);
254 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
256 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
257 OUT_RING (chan
, (width
<< 16) | dstx
);
258 OUT_RING (chan
, (height
<< 16) | dsty
);
260 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
261 OUT_RING (chan
, 0x3c);
263 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
267 nv50_clear_depth_stencil(struct pipe_context
*pipe
,
268 struct pipe_surface
*dst
,
269 unsigned clear_flags
,
272 unsigned dstx
, unsigned dsty
,
273 unsigned width
, unsigned height
)
275 struct nv50_context
*nv50
= nv50_context(pipe
);
276 struct nv50_screen
*screen
= nv50
->screen
;
277 struct nouveau_channel
*chan
= screen
->base
.channel
;
278 struct nv50_miptree
*mt
= nv50_miptree(dst
->texture
);
279 struct nv50_surface
*sf
= nv50_surface(dst
);
280 struct nouveau_bo
*bo
= mt
->base
.bo
;
283 if (clear_flags
& PIPE_CLEAR_DEPTH
) {
284 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
285 OUT_RINGf (chan
, depth
);
286 mode
|= NV50_3D_CLEAR_BUFFERS_Z
;
289 if (clear_flags
& PIPE_CLEAR_STENCIL
) {
290 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
291 OUT_RING (chan
, stencil
& 0xff);
292 mode
|= NV50_3D_CLEAR_BUFFERS_S
;
295 if (MARK_RING(chan
, 17, 2))
298 BEGIN_RING(chan
, RING_3D(ZETA_ADDRESS_HIGH
), 5);
299 OUT_RELOCh(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
300 OUT_RELOCl(chan
, bo
, sf
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
301 OUT_RING (chan
, nv50_format_table
[dst
->format
].rt
);
302 OUT_RING (chan
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
<< 4);
304 BEGIN_RING(chan
, RING_3D(ZETA_ENABLE
), 1);
306 BEGIN_RING(chan
, RING_3D(ZETA_HORIZ
), 3);
307 OUT_RING (chan
, sf
->width
);
308 OUT_RING (chan
, sf
->height
);
309 OUT_RING (chan
, (1 << 16) | 1);
311 BEGIN_RING(chan
, RING_3D(VIEWPORT_HORIZ(0)), 2);
312 OUT_RING (chan
, (width
<< 16) | dstx
);
313 OUT_RING (chan
, (height
<< 16) | dsty
);
315 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
316 OUT_RING (chan
, mode
);
318 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
322 nv50_clear(struct pipe_context
*pipe
, unsigned buffers
,
323 const float *rgba
, double depth
, unsigned stencil
)
325 struct nv50_context
*nv50
= nv50_context(pipe
);
326 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
327 struct pipe_framebuffer_state
*fb
= &nv50
->framebuffer
;
329 const unsigned dirty
= nv50
->dirty
;
332 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
333 nv50
->dirty
&= NV50_NEW_FRAMEBUFFER
;
334 if (!nv50_state_validate(nv50
))
337 if (buffers
& PIPE_CLEAR_COLOR
&& fb
->nr_cbufs
) {
338 BEGIN_RING(chan
, RING_3D(CLEAR_COLOR(0)), 4);
339 OUT_RINGf (chan
, rgba
[0]);
340 OUT_RINGf (chan
, rgba
[1]);
341 OUT_RINGf (chan
, rgba
[2]);
342 OUT_RINGf (chan
, rgba
[3]);
344 NV50_3D_CLEAR_BUFFERS_R
| NV50_3D_CLEAR_BUFFERS_G
|
345 NV50_3D_CLEAR_BUFFERS_B
| NV50_3D_CLEAR_BUFFERS_A
;
348 if (buffers
& PIPE_CLEAR_DEPTH
) {
349 BEGIN_RING(chan
, RING_3D(CLEAR_DEPTH
), 1);
350 OUT_RING (chan
, fui(depth
));
351 mode
|= NV50_3D_CLEAR_BUFFERS_Z
;
354 if (buffers
& PIPE_CLEAR_STENCIL
) {
355 BEGIN_RING(chan
, RING_3D(CLEAR_STENCIL
), 1);
356 OUT_RING (chan
, stencil
& 0xff);
357 mode
|= NV50_3D_CLEAR_BUFFERS_S
;
360 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
361 OUT_RING (chan
, mode
);
363 for (i
= 1; i
< fb
->nr_cbufs
; i
++) {
364 BEGIN_RING(chan
, RING_3D(CLEAR_BUFFERS
), 1);
365 OUT_RING (chan
, (i
<< 6) | 0x3c);
368 nv50
->dirty
= dirty
& ~NV50_NEW_FRAMEBUFFER
;
372 nv50_init_surface_functions(struct nv50_context
*nv50
)
374 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
376 pipe
->resource_copy_region
= nv50_resource_copy_region
;
377 pipe
->clear_render_target
= nv50_clear_render_target
;
378 pipe
->clear_depth_stencil
= nv50_clear_depth_stencil
;