r200_blit.c revision 39ab5ae30c303dd561252cb592d4de35814b6a70
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 */
413594bf233d16ceb21e97fcdfb57ea45cb0c5e41bAlex Deucherunsigned r200_check_blit(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;
2147959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher
2157959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher    radeon_cs_space_reset_bos(r200->radeon.cmdbuf.cs);
2167959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher
217daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    ret = radeon_cs_space_check_with_bo(r200->radeon.cmdbuf.cs,
21839ab5ae30c303dd561252cb592d4de35814b6a70Alex Deucher                                        src_bo, RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT, 0);
219daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    if (ret)
220daf85c460875c944d6918fdf4041467d97cba41eDave Airlie        return GL_FALSE;
2211ced546577745d361ad06577914f44f484656d37Alex Deucher
222daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    ret = radeon_cs_space_check_with_bo(r200->radeon.cmdbuf.cs,
22339ab5ae30c303dd561252cb592d4de35814b6a70Alex Deucher                                        dst_bo, 0, RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT);
2241ced546577745d361ad06577914f44f484656d37Alex Deucher    if (ret)
2251ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
2261ced546577745d361ad06577914f44f484656d37Alex Deucher
2271ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
2281ced546577745d361ad06577914f44f484656d37Alex Deucher}
2291ced546577745d361ad06577914f44f484656d37Alex Deucher
2301ced546577745d361ad06577914f44f484656d37Alex Deucher/**
2311ced546577745d361ad06577914f44f484656d37Alex Deucher * Calculate texcoords for given image region.
2321ced546577745d361ad06577914f44f484656d37Alex Deucher * Output values are [minx, maxx, miny, maxy]
2331ced546577745d361ad06577914f44f484656d37Alex Deucher */
2341ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void calc_tex_coords(float img_width, float img_height,
2351ced546577745d361ad06577914f44f484656d37Alex Deucher				   float x, float y,
2361ced546577745d361ad06577914f44f484656d37Alex Deucher				   float reg_width, float reg_height,
2371ced546577745d361ad06577914f44f484656d37Alex Deucher				   unsigned flip_y, float *buf)
2381ced546577745d361ad06577914f44f484656d37Alex Deucher{
2391ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[0] = x / img_width;
2401ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[1] = buf[0] + reg_width / img_width;
2411ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[2] = y / img_height;
2421ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[3] = buf[2] + reg_height / img_height;
2431ced546577745d361ad06577914f44f484656d37Alex Deucher    if (flip_y)
2441ced546577745d361ad06577914f44f484656d37Alex Deucher    {
2455a99ca490fee65d37a4c7469888680b412d27f7fAlex Deucher        buf[2] = 1.0 - buf[2];
24676cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher        buf[3] = 1.0 - buf[3];
2471ced546577745d361ad06577914f44f484656d37Alex Deucher    }
2481ced546577745d361ad06577914f44f484656d37Alex Deucher}
2491ced546577745d361ad06577914f44f484656d37Alex Deucher
2501ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_draw_packet(struct r200_context *r200,
2511ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_width, unsigned src_height,
2521ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_x_offset, unsigned src_y_offset,
2531ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned dst_x_offset, unsigned dst_y_offset,
2541ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned reg_width, unsigned reg_height,
2551ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned flip_y)
2561ced546577745d361ad06577914f44f484656d37Alex Deucher{
2571ced546577745d361ad06577914f44f484656d37Alex Deucher    float texcoords[4];
2581ced546577745d361ad06577914f44f484656d37Alex Deucher    float verts[12];
2591ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
2601ced546577745d361ad06577914f44f484656d37Alex Deucher
2611ced546577745d361ad06577914f44f484656d37Alex Deucher    calc_tex_coords(src_width, src_height,
2621ced546577745d361ad06577914f44f484656d37Alex Deucher                    src_x_offset, src_y_offset,
2631ced546577745d361ad06577914f44f484656d37Alex Deucher                    reg_width, reg_height,
2641ced546577745d361ad06577914f44f484656d37Alex Deucher                    flip_y, texcoords);
2651ced546577745d361ad06577914f44f484656d37Alex Deucher
2661ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[0] = dst_x_offset;
2671ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[1] = dst_y_offset + reg_height;
2681ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[2] = texcoords[0];
26976cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[3] = texcoords[3];
2701ced546577745d361ad06577914f44f484656d37Alex Deucher
2711ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[4] = dst_x_offset + reg_width;
2721ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[5] = dst_y_offset + reg_height;
2731ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[6] = texcoords[1];
27476cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[7] = texcoords[3];
2751ced546577745d361ad06577914f44f484656d37Alex Deucher
2761ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[8] = dst_x_offset + reg_width;
2771ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[9] = dst_y_offset;
2781ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[10] = texcoords[1];
27976cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[11] = texcoords[2];
2801ced546577745d361ad06577914f44f484656d37Alex Deucher
2811ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(14);
2821ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(R200_CP_CMD_3D_DRAW_IMMD_2 | (12 << 16));
2831ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(RADEON_CP_VC_CNTL_PRIM_WALK_RING |
2841ced546577745d361ad06577914f44f484656d37Alex Deucher	      RADEON_CP_VC_CNTL_PRIM_TYPE_RECT_LIST |
2851ced546577745d361ad06577914f44f484656d37Alex Deucher              (3 << 16));
2861ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_TABLE(verts, 12);
2871ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
2881ced546577745d361ad06577914f44f484656d37Alex Deucher}
2891ced546577745d361ad06577914f44f484656d37Alex Deucher
2901ced546577745d361ad06577914f44f484656d37Alex Deucher/**
2911ced546577745d361ad06577914f44f484656d37Alex Deucher * Copy a region of [@a width x @a height] pixels from source buffer
2921ced546577745d361ad06577914f44f484656d37Alex Deucher * to destination buffer.
2931ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] r200 r200 context
2941ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_bo source radeon buffer object
2951ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_offset offset of the source image in the @a src_bo
2961ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_mesaformat source image format
2971ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_pitch aligned source image width
2981ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_width source image width
2991ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_height source image height
3001ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_x_offset x offset in the source image
3011ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_y_offset y offset in the source image
3021ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_bo destination radeon buffer object
3031ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_offset offset of the destination image in the @a dst_bo
3041ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_mesaformat destination image format
3051ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_pitch aligned destination image width
3061ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_width destination image width
3071ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_height destination image height
3081ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_x_offset x offset in the destination image
3091ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_y_offset y offset in the destination image
3101ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] width region width
3111ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] height region height
3121ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] flip_y set if y coords of the source image need to be flipped
3131ced546577745d361ad06577914f44f484656d37Alex Deucher */
3142706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencoraunsigned r200_blit(GLcontext *ctx,
3152706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *src_bo,
3162706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t src_offset,
3172706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format src_mesaformat,
3182706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_pitch,
3192706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_width,
3202706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_height,
3212706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_x_offset,
3222706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_y_offset,
3232706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *dst_bo,
3242706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t dst_offset,
3252706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format dst_mesaformat,
3262706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_pitch,
3272706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_width,
3282706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_height,
3292706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_x_offset,
3302706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_y_offset,
3312706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_width,
3322706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_height,
3332706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned flip_y)
3341ced546577745d361ad06577914f44f484656d37Alex Deucher{
3352706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    struct r200_context *r200 = R200_CONTEXT(ctx);
3362706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora
3373594bf233d16ceb21e97fcdfb57ea45cb0c5e41bAlex Deucher    if (!r200_check_blit(dst_mesaformat))
3381ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3391ced546577745d361ad06577914f44f484656d37Alex Deucher
3401ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Make sure that colorbuffer has even width - hw limitation */
3411ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch % 2 > 0)
3421ced546577745d361ad06577914f44f484656d37Alex Deucher        ++dst_pitch;
3431ced546577745d361ad06577914f44f484656d37Alex Deucher
3441ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Rendering to small buffer doesn't work.
3451ced546577745d361ad06577914f44f484656d37Alex Deucher     * Looks like a hw limitation.
3461ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3471ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch < 32)
3481ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3491ced546577745d361ad06577914f44f484656d37Alex Deucher
3501ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Need to clamp the region size to make sure
3511ced546577745d361ad06577914f44f484656d37Alex Deucher     * we don't read outside of the source buffer
3521ced546577745d361ad06577914f44f484656d37Alex Deucher     * or write outside of the destination buffer.
3531ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3541ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + src_x_offset > src_width)
3551ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = src_width - src_x_offset;
3561ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + src_y_offset > src_height)
3571ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = src_height - src_y_offset;
3581ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + dst_x_offset > dst_width)
3591ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = dst_width - dst_x_offset;
3601ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + dst_y_offset > dst_height)
3611ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = dst_height - dst_y_offset;
3621ced546577745d361ad06577914f44f484656d37Alex Deucher
3631ced546577745d361ad06577914f44f484656d37Alex Deucher    if (src_bo == dst_bo) {
3641ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3651ced546577745d361ad06577914f44f484656d37Alex Deucher    }
3661ced546577745d361ad06577914f44f484656d37Alex Deucher
367156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher    if (src_offset % 32 || dst_offset % 32) {
368156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher        return GL_FALSE;
369156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher    }
370156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher
3711ced546577745d361ad06577914f44f484656d37Alex Deucher    if (0) {
3721ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "src: size [%d x %d], pitch %d, "
3731ced546577745d361ad06577914f44f484656d37Alex Deucher                "offset [%d x %d], format %s, bo %p\n",
3741ced546577745d361ad06577914f44f484656d37Alex Deucher                src_width, src_height, src_pitch,
3751ced546577745d361ad06577914f44f484656d37Alex Deucher                src_x_offset, src_y_offset,
3761ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(src_mesaformat),
3771ced546577745d361ad06577914f44f484656d37Alex Deucher                src_bo);
3781ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "dst: pitch %d, offset[%d x %d], format %s, bo %p\n",
3791ced546577745d361ad06577914f44f484656d37Alex Deucher                dst_pitch, dst_x_offset, dst_y_offset,
3801ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(dst_mesaformat), dst_bo);
3811ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "region: %d x %d\n", reg_width, reg_height);
3821ced546577745d361ad06577914f44f484656d37Alex Deucher    }
3831ced546577745d361ad06577914f44f484656d37Alex Deucher
3841ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Flush is needed to make sure that source buffer has correct data */
3851ced546577745d361ad06577914f44f484656d37Alex Deucher    radeonFlush(r200->radeon.glCtx);
3861ced546577745d361ad06577914f44f484656d37Alex Deucher
3871ced546577745d361ad06577914f44f484656d37Alex Deucher    rcommonEnsureCmdBufSpace(&r200->radeon, 78, __FUNCTION__);
3881ced546577745d361ad06577914f44f484656d37Alex Deucher
3891ced546577745d361ad06577914f44f484656d37Alex Deucher    if (!validate_buffers(r200, src_bo, dst_bo))
3901ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3911ced546577745d361ad06577914f44f484656d37Alex Deucher
3921ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
3931ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_vtx_state(r200);
3941ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 28 */
3951ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_tx_setup(r200, src_mesaformat, src_bo, src_offset, src_width, src_height, src_pitch);
3961ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 22 */
3971ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_cb_setup(r200, dst_bo, dst_offset, dst_mesaformat, dst_pitch, dst_width, dst_height);
3981ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
3991ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_draw_packet(r200, src_width, src_height,
4001ced546577745d361ad06577914f44f484656d37Alex Deucher                     src_x_offset, src_y_offset,
4011ced546577745d361ad06577914f44f484656d37Alex Deucher                     dst_x_offset, dst_y_offset,
4021ced546577745d361ad06577914f44f484656d37Alex Deucher                     reg_width, reg_height,
4031ced546577745d361ad06577914f44f484656d37Alex Deucher                     flip_y);
4041ced546577745d361ad06577914f44f484656d37Alex Deucher
4052706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    radeonFlush(ctx);
4061ced546577745d361ad06577914f44f484656d37Alex Deucher
4071ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
4081ced546577745d361ad06577914f44f484656d37Alex Deucher}
409