r200_blit.c revision 5a99ca490fee65d37a4c7469888680b412d27f7f
11ced546577745d361ad06577914f44f484656d37Alex Deucher/*
21ced546577745d361ad06577914f44f484656d37Alex Deucher * Copyright (C) 2009 Maciej Cencora <m.cencora@gmail.com>
31ced546577745d361ad06577914f44f484656d37Alex Deucher *
41ced546577745d361ad06577914f44f484656d37Alex Deucher * All Rights Reserved.
51ced546577745d361ad06577914f44f484656d37Alex Deucher *
61ced546577745d361ad06577914f44f484656d37Alex Deucher * Permission is hereby granted, free of charge, to any person obtaining
71ced546577745d361ad06577914f44f484656d37Alex Deucher * a copy of this software and associated documentation files (the
81ced546577745d361ad06577914f44f484656d37Alex Deucher * "Software"), to deal in the Software without restriction, including
91ced546577745d361ad06577914f44f484656d37Alex Deucher * without limitation the rights to use, copy, modify, merge, publish,
101ced546577745d361ad06577914f44f484656d37Alex Deucher * distribute, sublicense, and/or sell copies of the Software, and to
111ced546577745d361ad06577914f44f484656d37Alex Deucher * permit persons to whom the Software is furnished to do so, subject to
121ced546577745d361ad06577914f44f484656d37Alex Deucher * the following conditions:
131ced546577745d361ad06577914f44f484656d37Alex Deucher *
141ced546577745d361ad06577914f44f484656d37Alex Deucher * The above copyright notice and this permission notice (including the
151ced546577745d361ad06577914f44f484656d37Alex Deucher * next paragraph) shall be included in all copies or substantial
161ced546577745d361ad06577914f44f484656d37Alex Deucher * portions of the Software.
171ced546577745d361ad06577914f44f484656d37Alex Deucher *
181ced546577745d361ad06577914f44f484656d37Alex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
191ced546577745d361ad06577914f44f484656d37Alex Deucher * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
201ced546577745d361ad06577914f44f484656d37Alex Deucher * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
211ced546577745d361ad06577914f44f484656d37Alex Deucher * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
221ced546577745d361ad06577914f44f484656d37Alex Deucher * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
231ced546577745d361ad06577914f44f484656d37Alex Deucher * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
241ced546577745d361ad06577914f44f484656d37Alex Deucher * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
251ced546577745d361ad06577914f44f484656d37Alex Deucher *
261ced546577745d361ad06577914f44f484656d37Alex Deucher */
271ced546577745d361ad06577914f44f484656d37Alex Deucher
281ced546577745d361ad06577914f44f484656d37Alex Deucher#include "radeon_common.h"
291ced546577745d361ad06577914f44f484656d37Alex Deucher#include "r200_context.h"
301ced546577745d361ad06577914f44f484656d37Alex Deucher#include "r200_blit.h"
311ced546577745d361ad06577914f44f484656d37Alex Deucher
321ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline uint32_t cmdpacket0(struct radeon_screen *rscrn,
331ced546577745d361ad06577914f44f484656d37Alex Deucher                                  int reg, int count)
341ced546577745d361ad06577914f44f484656d37Alex Deucher{
351ced546577745d361ad06577914f44f484656d37Alex Deucher    if (count)
361ced546577745d361ad06577914f44f484656d37Alex Deucher	    return CP_PACKET0(reg, count - 1);
371ced546577745d361ad06577914f44f484656d37Alex Deucher    return CP_PACKET2;
381ced546577745d361ad06577914f44f484656d37Alex Deucher}
391ced546577745d361ad06577914f44f484656d37Alex Deucher
402b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher/* common formats supported as both textures and render targets */
412b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucherstatic unsigned is_blit_supported(gl_format mesa_format)
422b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher{
432b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    /* XXX others?  BE/LE? */
442b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    switch (mesa_format) {
452b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_ARGB8888:
462b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_XRGB8888:
472b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_RGB565:
482b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_ARGB4444:
492b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_ARGB1555:
502b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    case MESA_FORMAT_A8:
512b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    break;
522b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    default:
532b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    return 0;
542b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    }
552b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
562b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    /* ??? */
572b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    if (_mesa_get_format_bits(mesa_format, GL_DEPTH_BITS) > 0)
582b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    return 0;
592b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
602b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    return 1;
612b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher}
622b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
631ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_vtx_state(struct r200_context *r200)
641ced546577745d361ad06577914f44f484656d37Alex Deucher{
651ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
661ced546577745d361ad06577914f44f484656d37Alex Deucher
671ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(14);
681ced546577745d361ad06577914f44f484656d37Alex Deucher    if (r200->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
691ced546577745d361ad06577914f44f484656d37Alex Deucher	    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS, 0);
701ced546577745d361ad06577914f44f484656d37Alex Deucher    } else {
711ced546577745d361ad06577914f44f484656d37Alex Deucher	    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS, RADEON_TCL_BYPASS);
721ced546577745d361ad06577914f44f484656d37Alex Deucher    }
731ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL, (R200_VAP_FORCE_W_TO_ONE |
741ced546577745d361ad06577914f44f484656d37Alex Deucher					(9 << R200_VAP_VF_MAX_VTX_NUM__SHIFT)));
751ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_STATE_CNTL, 0);
761ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTE_CNTL, 0);
771ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_FMT_0, R200_VTX_XY);
781ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_FMT_1, (2 << R200_VTX_TEX0_COMP_CNT_SHIFT));
791ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_SE_CNTL, (RADEON_DIFFUSE_SHADE_GOURAUD |
801ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_BFACE_SOLID |
811ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_FFACE_SOLID |
821ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_VTX_PIX_CENTER_OGL |
831ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_ROUND_MODE_ROUND |
841ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_ROUND_PREC_4TH_PIX));
851ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
861ced546577745d361ad06577914f44f484656d37Alex Deucher}
871ced546577745d361ad06577914f44f484656d37Alex Deucher
881ced546577745d361ad06577914f44f484656d37Alex Deucherstatic void inline emit_tx_setup(struct r200_context *r200,
891ced546577745d361ad06577914f44f484656d37Alex Deucher				 gl_format mesa_format,
901ced546577745d361ad06577914f44f484656d37Alex Deucher				 struct radeon_bo *bo,
911ced546577745d361ad06577914f44f484656d37Alex Deucher				 intptr_t offset,
921ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned width,
931ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned height,
941ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned pitch)
951ced546577745d361ad06577914f44f484656d37Alex Deucher{
961ced546577745d361ad06577914f44f484656d37Alex Deucher    uint32_t txformat = R200_TXFORMAT_NON_POWER2;
971ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
981ced546577745d361ad06577914f44f484656d37Alex Deucher
991ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(width <= 2047);
1001ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(height <= 2047);
1011ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(offset % 32 == 0);
1021ced546577745d361ad06577914f44f484656d37Alex Deucher
1031ced546577745d361ad06577914f44f484656d37Alex Deucher    /* XXX others?  BE/LE? */
1041ced546577745d361ad06577914f44f484656d37Alex Deucher    switch (mesa_format) {
1051ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB8888:
1061ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB8888 | R200_TXFORMAT_ALPHA_IN_MAP;
1071ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1081ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_XRGB8888:
1091ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB8888;
1101ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1111ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_RGB565:
1121ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_RGB565;
1131ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
114a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher    case MESA_FORMAT_ARGB4444:
115a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    txformat |= R200_TXFORMAT_ARGB4444 | R200_TXFORMAT_ALPHA_IN_MAP;
116a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    break;
1171ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB1555:
1181ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB1555 | R200_TXFORMAT_ALPHA_IN_MAP;
1191ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1201ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_A8:
1211ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_I8 | R200_TXFORMAT_ALPHA_IN_MAP;
1221ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1231ced546577745d361ad06577914f44f484656d37Alex Deucher    default:
1241ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1251ced546577745d361ad06577914f44f484656d37Alex Deucher    }
1261ced546577745d361ad06577914f44f484656d37Alex Deucher
1271ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(28);
1281ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_PP_CNTL, RADEON_TEX_0_ENABLE | RADEON_TEX_BLEND_0_ENABLE);
1291ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_CNTL_X, 0);
1301ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXMULTI_CTL_0, 0);
1311ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXCBLEND_0, (R200_TXC_ARG_A_ZERO |
1321ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_ARG_B_ZERO |
1331ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_ARG_C_R0_COLOR |
1341ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_OP_MADD));
1351ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXCBLEND2_0, R200_TXC_CLAMP_0_1 | R200_TXC_OUTPUT_REG_R0);
1361ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXABLEND_0, (R200_TXA_ARG_A_ZERO |
1371ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_ARG_B_ZERO |
1381ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_ARG_C_R0_ALPHA |
1391ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_OP_MADD));
1401ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXABLEND2_0, R200_TXA_CLAMP_0_1 | R200_TXA_OUTPUT_REG_R0);
1411ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFILTER_0, (R200_CLAMP_S_CLAMP_LAST |
1421ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_CLAMP_T_CLAMP_LAST |
1431ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_MAG_FILTER_NEAREST |
1441ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_MIN_FILTER_NEAREST));
1451ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFORMAT_0, txformat);
1461ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFORMAT_X_0, 0);
1471ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXSIZE_0, ((width - 1) |
1481ced546577745d361ad06577914f44f484656d37Alex Deucher					((height - 1) << RADEON_TEX_VSIZE_SHIFT)));
1491f0709fd8f69bf8cc3e9502bad8d3e7296d935fbMaciej Cencora    OUT_BATCH_REGVAL(R200_PP_TXPITCH_0, pitch * _mesa_get_format_bytes(mesa_format) - 32);
1501ced546577745d361ad06577914f44f484656d37Alex Deucher
1511ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(R200_PP_TXOFFSET_0, 1);
1521ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(0, bo, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
1531ced546577745d361ad06577914f44f484656d37Alex Deucher
1541ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
1551ced546577745d361ad06577914f44f484656d37Alex Deucher}
1561ced546577745d361ad06577914f44f484656d37Alex Deucher
1571ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_cb_setup(struct r200_context *r200,
1581ced546577745d361ad06577914f44f484656d37Alex Deucher				 struct radeon_bo *bo,
1591ced546577745d361ad06577914f44f484656d37Alex Deucher				 intptr_t offset,
1601ced546577745d361ad06577914f44f484656d37Alex Deucher				 gl_format mesa_format,
1611ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned pitch,
1621ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned width,
1631ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned height)
1641ced546577745d361ad06577914f44f484656d37Alex Deucher{
1651bb6b1d9dbabafdb864ee112526b1212744ac614Alex Deucher    uint32_t dst_pitch = pitch;
1661ced546577745d361ad06577914f44f484656d37Alex Deucher    uint32_t dst_format = 0;
1671ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
1681ced546577745d361ad06577914f44f484656d37Alex Deucher
1691ced546577745d361ad06577914f44f484656d37Alex Deucher    /* XXX others?  BE/LE? */
1701ced546577745d361ad06577914f44f484656d37Alex Deucher    switch (mesa_format) {
1711ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB8888:
1721ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_XRGB8888:
1731ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB8888;
1741ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1751ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_RGB565:
1761ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_RGB565;
1771ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
178a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher    case MESA_FORMAT_ARGB4444:
179a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB4444;
180a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    break;
1811ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB1555:
1821ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB1555;
1831ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1841ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_A8:
1851ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_RGB8;
1861ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1871ced546577745d361ad06577914f44f484656d37Alex Deucher    default:
1881ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1891ced546577745d361ad06577914f44f484656d37Alex Deucher    }
1901ced546577745d361ad06577914f44f484656d37Alex Deucher
1911ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH_NO_AUTOSTATE(22);
1921ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_RE_AUX_SCISSOR_CNTL, 0);
1931ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_RE_CNTL, 0);
1941ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RE_TOP_LEFT, 0);
1951ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RE_WIDTH_HEIGHT, ((width << RADEON_RE_WIDTH_SHIFT) |
1961ced546577745d361ad06577914f44f484656d37Alex Deucher					      (height << RADEON_RE_HEIGHT_SHIFT)));
1971ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_PLANEMASK, 0xffffffff);
1981ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_BLENDCNTL, RADEON_SRC_BLEND_GL_ONE | RADEON_DST_BLEND_GL_ZERO);
1991ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_CNTL, dst_format);
2001ced546577745d361ad06577914f44f484656d37Alex Deucher
2011ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(RADEON_RB3D_COLOROFFSET, 1);
2021ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(0, bo, 0, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0);
2031ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(RADEON_RB3D_COLORPITCH, 1);
2041ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(dst_pitch, bo, dst_pitch, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0);
2051ced546577745d361ad06577914f44f484656d37Alex Deucher
2061ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
2071ced546577745d361ad06577914f44f484656d37Alex Deucher}
2081ced546577745d361ad06577914f44f484656d37Alex Deucher
2091ced546577745d361ad06577914f44f484656d37Alex Deucherstatic GLboolean validate_buffers(struct r200_context *r200,
2101ced546577745d361ad06577914f44f484656d37Alex Deucher                                  struct radeon_bo *src_bo,
2111ced546577745d361ad06577914f44f484656d37Alex Deucher                                  struct radeon_bo *dst_bo)
2121ced546577745d361ad06577914f44f484656d37Alex Deucher{
2131ced546577745d361ad06577914f44f484656d37Alex Deucher    int ret;
2141ced546577745d361ad06577914f44f484656d37Alex Deucher    radeon_cs_space_add_persistent_bo(r200->radeon.cmdbuf.cs,
2151ced546577745d361ad06577914f44f484656d37Alex Deucher                                      src_bo, RADEON_GEM_DOMAIN_VRAM, 0);
2161ced546577745d361ad06577914f44f484656d37Alex Deucher
2171ced546577745d361ad06577914f44f484656d37Alex Deucher    radeon_cs_space_add_persistent_bo(r200->radeon.cmdbuf.cs,
2181ced546577745d361ad06577914f44f484656d37Alex Deucher                                      dst_bo, 0, RADEON_GEM_DOMAIN_VRAM);
2191ced546577745d361ad06577914f44f484656d37Alex Deucher
2201ced546577745d361ad06577914f44f484656d37Alex Deucher    ret = radeon_cs_space_check_with_bo(r200->radeon.cmdbuf.cs,
2211ced546577745d361ad06577914f44f484656d37Alex Deucher                                        first_elem(&r200->radeon.dma.reserved)->bo,
2221ced546577745d361ad06577914f44f484656d37Alex Deucher                                        RADEON_GEM_DOMAIN_GTT, 0);
2231ced546577745d361ad06577914f44f484656d37Alex Deucher    if (ret)
2241ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
2251ced546577745d361ad06577914f44f484656d37Alex Deucher
2261ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
2271ced546577745d361ad06577914f44f484656d37Alex Deucher}
2281ced546577745d361ad06577914f44f484656d37Alex Deucher
2291ced546577745d361ad06577914f44f484656d37Alex Deucher/**
2301ced546577745d361ad06577914f44f484656d37Alex Deucher * Calculate texcoords for given image region.
2311ced546577745d361ad06577914f44f484656d37Alex Deucher * Output values are [minx, maxx, miny, maxy]
2321ced546577745d361ad06577914f44f484656d37Alex Deucher */
2331ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void calc_tex_coords(float img_width, float img_height,
2341ced546577745d361ad06577914f44f484656d37Alex Deucher				   float x, float y,
2351ced546577745d361ad06577914f44f484656d37Alex Deucher				   float reg_width, float reg_height,
2361ced546577745d361ad06577914f44f484656d37Alex Deucher				   unsigned flip_y, float *buf)
2371ced546577745d361ad06577914f44f484656d37Alex Deucher{
2381ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[0] = x / img_width;
2391ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[1] = buf[0] + reg_width / img_width;
2401ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[2] = y / img_height;
2411ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[3] = buf[2] + reg_height / img_height;
2421ced546577745d361ad06577914f44f484656d37Alex Deucher    if (flip_y)
2431ced546577745d361ad06577914f44f484656d37Alex Deucher    {
2445a99ca490fee65d37a4c7469888680b412d27f7fAlex Deucher        buf[2] = 1.0 - buf[2];
24576cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher        buf[3] = 1.0 - buf[3];
2461ced546577745d361ad06577914f44f484656d37Alex Deucher    }
2471ced546577745d361ad06577914f44f484656d37Alex Deucher}
2481ced546577745d361ad06577914f44f484656d37Alex Deucher
2491ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_draw_packet(struct r200_context *r200,
2501ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_width, unsigned src_height,
2511ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_x_offset, unsigned src_y_offset,
2521ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned dst_x_offset, unsigned dst_y_offset,
2531ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned reg_width, unsigned reg_height,
2541ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned flip_y)
2551ced546577745d361ad06577914f44f484656d37Alex Deucher{
2561ced546577745d361ad06577914f44f484656d37Alex Deucher    float texcoords[4];
2571ced546577745d361ad06577914f44f484656d37Alex Deucher    float verts[12];
2581ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
2591ced546577745d361ad06577914f44f484656d37Alex Deucher
2601ced546577745d361ad06577914f44f484656d37Alex Deucher    calc_tex_coords(src_width, src_height,
2611ced546577745d361ad06577914f44f484656d37Alex Deucher                    src_x_offset, src_y_offset,
2621ced546577745d361ad06577914f44f484656d37Alex Deucher                    reg_width, reg_height,
2631ced546577745d361ad06577914f44f484656d37Alex Deucher                    flip_y, texcoords);
2641ced546577745d361ad06577914f44f484656d37Alex Deucher
2651ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[0] = dst_x_offset;
2661ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[1] = dst_y_offset + reg_height;
2671ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[2] = texcoords[0];
26876cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[3] = texcoords[3];
2691ced546577745d361ad06577914f44f484656d37Alex Deucher
2701ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[4] = dst_x_offset + reg_width;
2711ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[5] = dst_y_offset + reg_height;
2721ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[6] = texcoords[1];
27376cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[7] = texcoords[3];
2741ced546577745d361ad06577914f44f484656d37Alex Deucher
2751ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[8] = dst_x_offset + reg_width;
2761ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[9] = dst_y_offset;
2771ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[10] = texcoords[1];
27876cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[11] = texcoords[2];
2791ced546577745d361ad06577914f44f484656d37Alex Deucher
2801ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(14);
2811ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(R200_CP_CMD_3D_DRAW_IMMD_2 | (12 << 16));
2821ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(RADEON_CP_VC_CNTL_PRIM_WALK_RING |
2831ced546577745d361ad06577914f44f484656d37Alex Deucher	      RADEON_CP_VC_CNTL_PRIM_TYPE_RECT_LIST |
2841ced546577745d361ad06577914f44f484656d37Alex Deucher              (3 << 16));
2851ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_TABLE(verts, 12);
2861ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
2871ced546577745d361ad06577914f44f484656d37Alex Deucher}
2881ced546577745d361ad06577914f44f484656d37Alex Deucher
2891ced546577745d361ad06577914f44f484656d37Alex Deucher/**
2901ced546577745d361ad06577914f44f484656d37Alex Deucher * Copy a region of [@a width x @a height] pixels from source buffer
2911ced546577745d361ad06577914f44f484656d37Alex Deucher * to destination buffer.
2921ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] r200 r200 context
2931ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_bo source radeon buffer object
2941ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_offset offset of the source image in the @a src_bo
2951ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_mesaformat source image format
2961ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_pitch aligned source image width
2971ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_width source image width
2981ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_height source image height
2991ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_x_offset x offset in the source image
3001ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_y_offset y offset in the source image
3011ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_bo destination radeon buffer object
3021ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_offset offset of the destination image in the @a dst_bo
3031ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_mesaformat destination image format
3041ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_pitch aligned destination image width
3051ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_width destination image width
3061ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_height destination image height
3071ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_x_offset x offset in the destination image
3081ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_y_offset y offset in the destination image
3091ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] width region width
3101ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] height region height
3111ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] flip_y set if y coords of the source image need to be flipped
3121ced546577745d361ad06577914f44f484656d37Alex Deucher */
3132706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencoraunsigned r200_blit(GLcontext *ctx,
3142706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *src_bo,
3152706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t src_offset,
3162706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format src_mesaformat,
3172706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_pitch,
3182706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_width,
3192706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_height,
3202706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_x_offset,
3212706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_y_offset,
3222706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *dst_bo,
3232706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t dst_offset,
3242706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format dst_mesaformat,
3252706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_pitch,
3262706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_width,
3272706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_height,
3282706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_x_offset,
3292706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_y_offset,
3302706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_width,
3312706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_height,
3322706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned flip_y)
3331ced546577745d361ad06577914f44f484656d37Alex Deucher{
3342706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    struct r200_context *r200 = R200_CONTEXT(ctx);
3352706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora
336ef631104d51d011ef1dbaf7b4aeba274ae103ad7Alex Deucher    if (!is_blit_supported(dst_mesaformat))
3371ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3381ced546577745d361ad06577914f44f484656d37Alex Deucher
3391ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Make sure that colorbuffer has even width - hw limitation */
3401ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch % 2 > 0)
3411ced546577745d361ad06577914f44f484656d37Alex Deucher        ++dst_pitch;
3421ced546577745d361ad06577914f44f484656d37Alex Deucher
3431ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Rendering to small buffer doesn't work.
3441ced546577745d361ad06577914f44f484656d37Alex Deucher     * Looks like a hw limitation.
3451ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3461ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch < 32)
3471ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3481ced546577745d361ad06577914f44f484656d37Alex Deucher
3491ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Need to clamp the region size to make sure
3501ced546577745d361ad06577914f44f484656d37Alex Deucher     * we don't read outside of the source buffer
3511ced546577745d361ad06577914f44f484656d37Alex Deucher     * or write outside of the destination buffer.
3521ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3531ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + src_x_offset > src_width)
3541ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = src_width - src_x_offset;
3551ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + src_y_offset > src_height)
3561ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = src_height - src_y_offset;
3571ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + dst_x_offset > dst_width)
3581ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = dst_width - dst_x_offset;
3591ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + dst_y_offset > dst_height)
3601ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = dst_height - dst_y_offset;
3611ced546577745d361ad06577914f44f484656d37Alex Deucher
3621ced546577745d361ad06577914f44f484656d37Alex Deucher    if (src_bo == dst_bo) {
3631ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3641ced546577745d361ad06577914f44f484656d37Alex Deucher    }
3651ced546577745d361ad06577914f44f484656d37Alex Deucher
3661ced546577745d361ad06577914f44f484656d37Alex Deucher    if (0) {
3671ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "src: size [%d x %d], pitch %d, "
3681ced546577745d361ad06577914f44f484656d37Alex Deucher                "offset [%d x %d], format %s, bo %p\n",
3691ced546577745d361ad06577914f44f484656d37Alex Deucher                src_width, src_height, src_pitch,
3701ced546577745d361ad06577914f44f484656d37Alex Deucher                src_x_offset, src_y_offset,
3711ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(src_mesaformat),
3721ced546577745d361ad06577914f44f484656d37Alex Deucher                src_bo);
3731ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "dst: pitch %d, offset[%d x %d], format %s, bo %p\n",
3741ced546577745d361ad06577914f44f484656d37Alex Deucher                dst_pitch, dst_x_offset, dst_y_offset,
3751ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(dst_mesaformat), dst_bo);
3761ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "region: %d x %d\n", reg_width, reg_height);
3771ced546577745d361ad06577914f44f484656d37Alex Deucher    }
3781ced546577745d361ad06577914f44f484656d37Alex Deucher
3791ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Flush is needed to make sure that source buffer has correct data */
3801ced546577745d361ad06577914f44f484656d37Alex Deucher    radeonFlush(r200->radeon.glCtx);
3811ced546577745d361ad06577914f44f484656d37Alex Deucher
3821ced546577745d361ad06577914f44f484656d37Alex Deucher    rcommonEnsureCmdBufSpace(&r200->radeon, 78, __FUNCTION__);
3831ced546577745d361ad06577914f44f484656d37Alex Deucher
3841ced546577745d361ad06577914f44f484656d37Alex Deucher    if (!validate_buffers(r200, src_bo, dst_bo))
3851ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3861ced546577745d361ad06577914f44f484656d37Alex Deucher
3871ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
3881ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_vtx_state(r200);
3891ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 28 */
3901ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_tx_setup(r200, src_mesaformat, src_bo, src_offset, src_width, src_height, src_pitch);
3911ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 22 */
3921ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_cb_setup(r200, dst_bo, dst_offset, dst_mesaformat, dst_pitch, dst_width, dst_height);
3931ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
3941ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_draw_packet(r200, src_width, src_height,
3951ced546577745d361ad06577914f44f484656d37Alex Deucher                     src_x_offset, src_y_offset,
3961ced546577745d361ad06577914f44f484656d37Alex Deucher                     dst_x_offset, dst_y_offset,
3971ced546577745d361ad06577914f44f484656d37Alex Deucher                     reg_width, reg_height,
3981ced546577745d361ad06577914f44f484656d37Alex Deucher                     flip_y);
3991ced546577745d361ad06577914f44f484656d37Alex Deucher
4002706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    radeonFlush(ctx);
4011ced546577745d361ad06577914f44f484656d37Alex Deucher
4021ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
4031ced546577745d361ad06577914f44f484656d37Alex Deucher}
404