2 * Copyright (C) 2009 Maciej Cencora <m.cencora@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "radeon_common.h"
29 #include "r200_context.h"
30 #include "r200_blit.h"
32 static inline uint32_t cmdpacket0(struct radeon_screen
*rscrn
,
36 return CP_PACKET0(reg
, count
- 1);
40 /* common formats supported as both textures and render targets */
41 unsigned r200_check_blit(gl_format mesa_format
)
43 /* XXX others? BE/LE? */
44 switch (mesa_format
) {
45 case MESA_FORMAT_ARGB8888
:
46 case MESA_FORMAT_XRGB8888
:
47 case MESA_FORMAT_RGB565
:
48 case MESA_FORMAT_ARGB4444
:
49 case MESA_FORMAT_ARGB1555
:
59 if (_mesa_get_format_bits(mesa_format
, GL_DEPTH_BITS
) > 0)
65 static inline void emit_vtx_state(struct r200_context
*r200
)
67 BATCH_LOCALS(&r200
->radeon
);
70 if (r200
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
71 OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS
, 0);
73 OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS
, RADEON_TCL_BYPASS
);
75 OUT_BATCH_REGVAL(R200_SE_VAP_CNTL
, (R200_VAP_FORCE_W_TO_ONE
|
76 (9 << R200_VAP_VF_MAX_VTX_NUM__SHIFT
)));
77 OUT_BATCH_REGVAL(R200_SE_VTX_STATE_CNTL
, 0);
78 OUT_BATCH_REGVAL(R200_SE_VTE_CNTL
, 0);
79 OUT_BATCH_REGVAL(R200_SE_VTX_FMT_0
, R200_VTX_XY
);
80 OUT_BATCH_REGVAL(R200_SE_VTX_FMT_1
, (2 << R200_VTX_TEX0_COMP_CNT_SHIFT
));
81 OUT_BATCH_REGVAL(RADEON_SE_CNTL
, (RADEON_DIFFUSE_SHADE_GOURAUD
|
84 RADEON_VTX_PIX_CENTER_OGL
|
85 RADEON_ROUND_MODE_ROUND
|
86 RADEON_ROUND_PREC_4TH_PIX
));
90 static void inline emit_tx_setup(struct r200_context
*r200
,
91 gl_format mesa_format
,
98 uint32_t txformat
= R200_TXFORMAT_NON_POWER2
;
99 BATCH_LOCALS(&r200
->radeon
);
101 assert(width
<= 2047);
102 assert(height
<= 2047);
103 assert(offset
% 32 == 0);
105 /* XXX others? BE/LE? */
106 switch (mesa_format
) {
107 case MESA_FORMAT_ARGB8888
:
108 txformat
|= R200_TXFORMAT_ARGB8888
| R200_TXFORMAT_ALPHA_IN_MAP
;
110 case MESA_FORMAT_RGBA8888
:
111 txformat
|= R200_TXFORMAT_RGBA8888
| R200_TXFORMAT_ALPHA_IN_MAP
;
113 case MESA_FORMAT_RGBA8888_REV
:
114 txformat
|= R200_TXFORMAT_ABGR8888
| R200_TXFORMAT_ALPHA_IN_MAP
;
116 case MESA_FORMAT_XRGB8888
:
117 txformat
|= R200_TXFORMAT_ARGB8888
;
119 case MESA_FORMAT_RGB565
:
120 txformat
|= R200_TXFORMAT_RGB565
;
122 case MESA_FORMAT_ARGB4444
:
123 txformat
|= R200_TXFORMAT_ARGB4444
| R200_TXFORMAT_ALPHA_IN_MAP
;
125 case MESA_FORMAT_ARGB1555
:
126 txformat
|= R200_TXFORMAT_ARGB1555
| R200_TXFORMAT_ALPHA_IN_MAP
;
130 txformat
|= R200_TXFORMAT_I8
| R200_TXFORMAT_ALPHA_IN_MAP
;
133 txformat
|= R200_TXFORMAT_I8
;
135 case MESA_FORMAT_AL88
:
136 txformat
|= R200_TXFORMAT_AI88
| R200_TXFORMAT_ALPHA_IN_MAP
;
143 OUT_BATCH_REGVAL(RADEON_PP_CNTL
, RADEON_TEX_0_ENABLE
| RADEON_TEX_BLEND_0_ENABLE
);
144 OUT_BATCH_REGVAL(R200_PP_CNTL_X
, 0);
145 OUT_BATCH_REGVAL(R200_PP_TXMULTI_CTL_0
, 0);
146 OUT_BATCH_REGVAL(R200_PP_TXCBLEND_0
, (R200_TXC_ARG_A_ZERO
|
147 R200_TXC_ARG_B_ZERO
|
148 R200_TXC_ARG_C_R0_COLOR
|
150 OUT_BATCH_REGVAL(R200_PP_TXCBLEND2_0
, R200_TXC_CLAMP_0_1
| R200_TXC_OUTPUT_REG_R0
);
151 OUT_BATCH_REGVAL(R200_PP_TXABLEND_0
, (R200_TXA_ARG_A_ZERO
|
152 R200_TXA_ARG_B_ZERO
|
153 R200_TXA_ARG_C_R0_ALPHA
|
155 OUT_BATCH_REGVAL(R200_PP_TXABLEND2_0
, R200_TXA_CLAMP_0_1
| R200_TXA_OUTPUT_REG_R0
);
156 OUT_BATCH_REGVAL(R200_PP_TXFILTER_0
, (R200_CLAMP_S_CLAMP_LAST
|
157 R200_CLAMP_T_CLAMP_LAST
|
158 R200_MAG_FILTER_NEAREST
|
159 R200_MIN_FILTER_NEAREST
));
160 OUT_BATCH_REGVAL(R200_PP_TXFORMAT_0
, txformat
);
161 OUT_BATCH_REGVAL(R200_PP_TXFORMAT_X_0
, 0);
162 OUT_BATCH_REGVAL(R200_PP_TXSIZE_0
, ((width
- 1) |
163 ((height
- 1) << RADEON_TEX_VSIZE_SHIFT
)));
164 OUT_BATCH_REGVAL(R200_PP_TXPITCH_0
, pitch
* _mesa_get_format_bytes(mesa_format
) - 32);
166 OUT_BATCH_REGSEQ(R200_PP_TXOFFSET_0
, 1);
167 OUT_BATCH_RELOC(0, bo
, 0, RADEON_GEM_DOMAIN_GTT
|RADEON_GEM_DOMAIN_VRAM
, 0, 0);
172 static inline void emit_cb_setup(struct r200_context
*r200
,
173 struct radeon_bo
*bo
,
175 gl_format mesa_format
,
180 uint32_t dst_pitch
= pitch
;
181 uint32_t dst_format
= 0;
182 BATCH_LOCALS(&r200
->radeon
);
184 /* XXX others? BE/LE? */
185 switch (mesa_format
) {
186 case MESA_FORMAT_ARGB8888
:
187 case MESA_FORMAT_XRGB8888
:
188 dst_format
= RADEON_COLOR_FORMAT_ARGB8888
;
190 case MESA_FORMAT_RGB565
:
191 dst_format
= RADEON_COLOR_FORMAT_RGB565
;
193 case MESA_FORMAT_ARGB4444
:
194 dst_format
= RADEON_COLOR_FORMAT_ARGB4444
;
196 case MESA_FORMAT_ARGB1555
:
197 dst_format
= RADEON_COLOR_FORMAT_ARGB1555
;
202 dst_format
= RADEON_COLOR_FORMAT_RGB8
;
208 BEGIN_BATCH_NO_AUTOSTATE(22);
209 OUT_BATCH_REGVAL(R200_RE_AUX_SCISSOR_CNTL
, 0);
210 OUT_BATCH_REGVAL(R200_RE_CNTL
, 0);
211 OUT_BATCH_REGVAL(RADEON_RE_TOP_LEFT
, 0);
212 OUT_BATCH_REGVAL(RADEON_RE_WIDTH_HEIGHT
, ((width
<< RADEON_RE_WIDTH_SHIFT
) |
213 (height
<< RADEON_RE_HEIGHT_SHIFT
)));
214 OUT_BATCH_REGVAL(RADEON_RB3D_PLANEMASK
, 0xffffffff);
215 OUT_BATCH_REGVAL(RADEON_RB3D_BLENDCNTL
, RADEON_SRC_BLEND_GL_ONE
| RADEON_DST_BLEND_GL_ZERO
);
216 OUT_BATCH_REGVAL(RADEON_RB3D_CNTL
, dst_format
);
218 OUT_BATCH_REGSEQ(RADEON_RB3D_COLOROFFSET
, 1);
219 OUT_BATCH_RELOC(0, bo
, 0, 0, RADEON_GEM_DOMAIN_GTT
|RADEON_GEM_DOMAIN_VRAM
, 0);
220 OUT_BATCH_REGSEQ(RADEON_RB3D_COLORPITCH
, 1);
221 OUT_BATCH_RELOC(dst_pitch
, bo
, dst_pitch
, 0, RADEON_GEM_DOMAIN_GTT
|RADEON_GEM_DOMAIN_VRAM
, 0);
226 static GLboolean
validate_buffers(struct r200_context
*r200
,
227 struct radeon_bo
*src_bo
,
228 struct radeon_bo
*dst_bo
)
232 radeon_cs_space_reset_bos(r200
->radeon
.cmdbuf
.cs
);
234 ret
= radeon_cs_space_check_with_bo(r200
->radeon
.cmdbuf
.cs
,
235 src_bo
, RADEON_GEM_DOMAIN_VRAM
| RADEON_GEM_DOMAIN_GTT
, 0);
239 ret
= radeon_cs_space_check_with_bo(r200
->radeon
.cmdbuf
.cs
,
240 dst_bo
, 0, RADEON_GEM_DOMAIN_VRAM
| RADEON_GEM_DOMAIN_GTT
);
248 * Calculate texcoords for given image region.
249 * Output values are [minx, maxx, miny, maxy]
251 static inline void calc_tex_coords(float img_width
, float img_height
,
253 float reg_width
, float reg_height
,
254 unsigned flip_y
, float *buf
)
256 buf
[0] = x
/ img_width
;
257 buf
[1] = buf
[0] + reg_width
/ img_width
;
258 buf
[2] = y
/ img_height
;
259 buf
[3] = buf
[2] + reg_height
/ img_height
;
262 buf
[2] = 1.0 - buf
[2];
263 buf
[3] = 1.0 - buf
[3];
267 static inline void emit_draw_packet(struct r200_context
*r200
,
268 unsigned src_width
, unsigned src_height
,
269 unsigned src_x_offset
, unsigned src_y_offset
,
270 unsigned dst_x_offset
, unsigned dst_y_offset
,
271 unsigned reg_width
, unsigned reg_height
,
276 BATCH_LOCALS(&r200
->radeon
);
278 calc_tex_coords(src_width
, src_height
,
279 src_x_offset
, src_y_offset
,
280 reg_width
, reg_height
,
283 verts
[0] = dst_x_offset
;
284 verts
[1] = dst_y_offset
+ reg_height
;
285 verts
[2] = texcoords
[0];
286 verts
[3] = texcoords
[3];
288 verts
[4] = dst_x_offset
+ reg_width
;
289 verts
[5] = dst_y_offset
+ reg_height
;
290 verts
[6] = texcoords
[1];
291 verts
[7] = texcoords
[3];
293 verts
[8] = dst_x_offset
+ reg_width
;
294 verts
[9] = dst_y_offset
;
295 verts
[10] = texcoords
[1];
296 verts
[11] = texcoords
[2];
299 OUT_BATCH(R200_CP_CMD_3D_DRAW_IMMD_2
| (12 << 16));
300 OUT_BATCH(RADEON_CP_VC_CNTL_PRIM_WALK_RING
|
301 RADEON_CP_VC_CNTL_PRIM_TYPE_RECT_LIST
|
303 OUT_BATCH_TABLE(verts
, 12);
308 * Copy a region of [@a width x @a height] pixels from source buffer
309 * to destination buffer.
310 * @param[in] r200 r200 context
311 * @param[in] src_bo source radeon buffer object
312 * @param[in] src_offset offset of the source image in the @a src_bo
313 * @param[in] src_mesaformat source image format
314 * @param[in] src_pitch aligned source image width
315 * @param[in] src_width source image width
316 * @param[in] src_height source image height
317 * @param[in] src_x_offset x offset in the source image
318 * @param[in] src_y_offset y offset in the source image
319 * @param[in] dst_bo destination radeon buffer object
320 * @param[in] dst_offset offset of the destination image in the @a dst_bo
321 * @param[in] dst_mesaformat destination image format
322 * @param[in] dst_pitch aligned destination image width
323 * @param[in] dst_width destination image width
324 * @param[in] dst_height destination image height
325 * @param[in] dst_x_offset x offset in the destination image
326 * @param[in] dst_y_offset y offset in the destination image
327 * @param[in] width region width
328 * @param[in] height region height
329 * @param[in] flip_y set if y coords of the source image need to be flipped
331 unsigned r200_blit(GLcontext
*ctx
,
332 struct radeon_bo
*src_bo
,
334 gl_format src_mesaformat
,
338 unsigned src_x_offset
,
339 unsigned src_y_offset
,
340 struct radeon_bo
*dst_bo
,
342 gl_format dst_mesaformat
,
346 unsigned dst_x_offset
,
347 unsigned dst_y_offset
,
352 struct r200_context
*r200
= R200_CONTEXT(ctx
);
354 if (!r200_check_blit(dst_mesaformat
))
357 /* Make sure that colorbuffer has even width - hw limitation */
358 if (dst_pitch
% 2 > 0)
361 /* Rendering to small buffer doesn't work.
362 * Looks like a hw limitation.
367 /* Need to clamp the region size to make sure
368 * we don't read outside of the source buffer
369 * or write outside of the destination buffer.
371 if (reg_width
+ src_x_offset
> src_width
)
372 reg_width
= src_width
- src_x_offset
;
373 if (reg_height
+ src_y_offset
> src_height
)
374 reg_height
= src_height
- src_y_offset
;
375 if (reg_width
+ dst_x_offset
> dst_width
)
376 reg_width
= dst_width
- dst_x_offset
;
377 if (reg_height
+ dst_y_offset
> dst_height
)
378 reg_height
= dst_height
- dst_y_offset
;
380 if (src_bo
== dst_bo
) {
384 if (src_offset
% 32 || dst_offset
% 32) {
389 fprintf(stderr
, "src: size [%d x %d], pitch %d, "
390 "offset [%d x %d], format %s, bo %p\n",
391 src_width
, src_height
, src_pitch
,
392 src_x_offset
, src_y_offset
,
393 _mesa_get_format_name(src_mesaformat
),
395 fprintf(stderr
, "dst: pitch %d, offset[%d x %d], format %s, bo %p\n",
396 dst_pitch
, dst_x_offset
, dst_y_offset
,
397 _mesa_get_format_name(dst_mesaformat
), dst_bo
);
398 fprintf(stderr
, "region: %d x %d\n", reg_width
, reg_height
);
401 /* Flush is needed to make sure that source buffer has correct data */
402 radeonFlush(r200
->radeon
.glCtx
);
404 rcommonEnsureCmdBufSpace(&r200
->radeon
, 78, __FUNCTION__
);
406 if (!validate_buffers(r200
, src_bo
, dst_bo
))
410 emit_vtx_state(r200
);
412 emit_tx_setup(r200
, src_mesaformat
, src_bo
, src_offset
, src_width
, src_height
, src_pitch
);
414 emit_cb_setup(r200
, dst_bo
, dst_offset
, dst_mesaformat
, dst_pitch
, dst_width
, dst_height
);
416 emit_draw_packet(r200
, src_width
, src_height
,
417 src_x_offset
, src_y_offset
,
418 dst_x_offset
, dst_y_offset
,
419 reg_width
, reg_height
,