r200_blit.c revision a840bf4146b7e06cc94eb59d8265fe6593b232c4
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:
51a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_L8:
52a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_I8:
532b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    break;
542b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    default:
552b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    return 0;
562b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    }
572b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
582b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    /* ??? */
592b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    if (_mesa_get_format_bits(mesa_format, GL_DEPTH_BITS) > 0)
602b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher	    return 0;
612b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
622b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher    return 1;
632b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher}
642b1d5ea4f0250a6a7fa312ced0a7af85e909381bAlex Deucher
651ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_vtx_state(struct r200_context *r200)
661ced546577745d361ad06577914f44f484656d37Alex Deucher{
671ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
681ced546577745d361ad06577914f44f484656d37Alex Deucher
691ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(14);
701ced546577745d361ad06577914f44f484656d37Alex Deucher    if (r200->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
711ced546577745d361ad06577914f44f484656d37Alex Deucher	    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS, 0);
721ced546577745d361ad06577914f44f484656d37Alex Deucher    } else {
731ced546577745d361ad06577914f44f484656d37Alex Deucher	    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL_STATUS, RADEON_TCL_BYPASS);
741ced546577745d361ad06577914f44f484656d37Alex Deucher    }
751ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VAP_CNTL, (R200_VAP_FORCE_W_TO_ONE |
761ced546577745d361ad06577914f44f484656d37Alex Deucher					(9 << R200_VAP_VF_MAX_VTX_NUM__SHIFT)));
771ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_STATE_CNTL, 0);
781ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTE_CNTL, 0);
791ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_FMT_0, R200_VTX_XY);
801ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_SE_VTX_FMT_1, (2 << R200_VTX_TEX0_COMP_CNT_SHIFT));
811ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_SE_CNTL, (RADEON_DIFFUSE_SHADE_GOURAUD |
821ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_BFACE_SOLID |
831ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_FFACE_SOLID |
841ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_VTX_PIX_CENTER_OGL |
851ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_ROUND_MODE_ROUND |
861ced546577745d361ad06577914f44f484656d37Alex Deucher				      RADEON_ROUND_PREC_4TH_PIX));
871ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
881ced546577745d361ad06577914f44f484656d37Alex Deucher}
891ced546577745d361ad06577914f44f484656d37Alex Deucher
901ced546577745d361ad06577914f44f484656d37Alex Deucherstatic void inline emit_tx_setup(struct r200_context *r200,
911ced546577745d361ad06577914f44f484656d37Alex Deucher				 gl_format mesa_format,
921ced546577745d361ad06577914f44f484656d37Alex Deucher				 struct radeon_bo *bo,
931ced546577745d361ad06577914f44f484656d37Alex Deucher				 intptr_t offset,
941ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned width,
951ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned height,
961ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned pitch)
971ced546577745d361ad06577914f44f484656d37Alex Deucher{
981ced546577745d361ad06577914f44f484656d37Alex Deucher    uint32_t txformat = R200_TXFORMAT_NON_POWER2;
991ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
1001ced546577745d361ad06577914f44f484656d37Alex Deucher
1011ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(width <= 2047);
1021ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(height <= 2047);
1031ced546577745d361ad06577914f44f484656d37Alex Deucher    assert(offset % 32 == 0);
1041ced546577745d361ad06577914f44f484656d37Alex Deucher
1051ced546577745d361ad06577914f44f484656d37Alex Deucher    /* XXX others?  BE/LE? */
1061ced546577745d361ad06577914f44f484656d37Alex Deucher    switch (mesa_format) {
1071ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB8888:
1081ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB8888 | R200_TXFORMAT_ALPHA_IN_MAP;
1091ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
110a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_RGBA8888:
111a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    txformat |= R200_TXFORMAT_RGBA8888 | R200_TXFORMAT_ALPHA_IN_MAP;
112a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    break;
113a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_RGBA8888_REV:
114a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    txformat |= R200_TXFORMAT_ABGR8888 | R200_TXFORMAT_ALPHA_IN_MAP;
115a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    break;
1161ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_XRGB8888:
1171ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB8888;
1181ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1191ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_RGB565:
1201ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_RGB565;
1211ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
122a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher    case MESA_FORMAT_ARGB4444:
123a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    txformat |= R200_TXFORMAT_ARGB4444 | R200_TXFORMAT_ALPHA_IN_MAP;
124a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    break;
1251ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB1555:
1261ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_ARGB1555 | R200_TXFORMAT_ALPHA_IN_MAP;
1271ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1281ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_A8:
129a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_I8:
1301ced546577745d361ad06577914f44f484656d37Alex Deucher	    txformat |= R200_TXFORMAT_I8 | R200_TXFORMAT_ALPHA_IN_MAP;
1311ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
132a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_L8:
133a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    txformat |= R200_TXFORMAT_I8;
134a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    break;
135a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_AL88:
136a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    txformat |= R200_TXFORMAT_AI88 | R200_TXFORMAT_ALPHA_IN_MAP;
137a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher	    break;
1381ced546577745d361ad06577914f44f484656d37Alex Deucher    default:
1391ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1401ced546577745d361ad06577914f44f484656d37Alex Deucher    }
1411ced546577745d361ad06577914f44f484656d37Alex Deucher
1421ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(28);
1431ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_PP_CNTL, RADEON_TEX_0_ENABLE | RADEON_TEX_BLEND_0_ENABLE);
1441ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_CNTL_X, 0);
1451ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXMULTI_CTL_0, 0);
1461ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXCBLEND_0, (R200_TXC_ARG_A_ZERO |
1471ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_ARG_B_ZERO |
1481ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_ARG_C_R0_COLOR |
1491ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXC_OP_MADD));
1501ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXCBLEND2_0, R200_TXC_CLAMP_0_1 | R200_TXC_OUTPUT_REG_R0);
1511ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXABLEND_0, (R200_TXA_ARG_A_ZERO |
1521ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_ARG_B_ZERO |
1531ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_ARG_C_R0_ALPHA |
1541ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_TXA_OP_MADD));
1551ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXABLEND2_0, R200_TXA_CLAMP_0_1 | R200_TXA_OUTPUT_REG_R0);
1561ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFILTER_0, (R200_CLAMP_S_CLAMP_LAST |
1571ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_CLAMP_T_CLAMP_LAST |
1581ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_MAG_FILTER_NEAREST |
1591ced546577745d361ad06577914f44f484656d37Alex Deucher					  R200_MIN_FILTER_NEAREST));
1601ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFORMAT_0, txformat);
1611ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXFORMAT_X_0, 0);
1621ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_PP_TXSIZE_0, ((width - 1) |
1631ced546577745d361ad06577914f44f484656d37Alex Deucher					((height - 1) << RADEON_TEX_VSIZE_SHIFT)));
1641f0709fd8f69bf8cc3e9502bad8d3e7296d935fbMaciej Cencora    OUT_BATCH_REGVAL(R200_PP_TXPITCH_0, pitch * _mesa_get_format_bytes(mesa_format) - 32);
1651ced546577745d361ad06577914f44f484656d37Alex Deucher
1661ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(R200_PP_TXOFFSET_0, 1);
1671ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(0, bo, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0);
1681ced546577745d361ad06577914f44f484656d37Alex Deucher
1691ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
1701ced546577745d361ad06577914f44f484656d37Alex Deucher}
1711ced546577745d361ad06577914f44f484656d37Alex Deucher
1721ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_cb_setup(struct r200_context *r200,
1731ced546577745d361ad06577914f44f484656d37Alex Deucher				 struct radeon_bo *bo,
1741ced546577745d361ad06577914f44f484656d37Alex Deucher				 intptr_t offset,
1751ced546577745d361ad06577914f44f484656d37Alex Deucher				 gl_format mesa_format,
1761ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned pitch,
1771ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned width,
1781ced546577745d361ad06577914f44f484656d37Alex Deucher				 unsigned height)
1791ced546577745d361ad06577914f44f484656d37Alex Deucher{
1801bb6b1d9dbabafdb864ee112526b1212744ac614Alex Deucher    uint32_t dst_pitch = pitch;
1811ced546577745d361ad06577914f44f484656d37Alex Deucher    uint32_t dst_format = 0;
1821ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
1831ced546577745d361ad06577914f44f484656d37Alex Deucher
1841ced546577745d361ad06577914f44f484656d37Alex Deucher    /* XXX others?  BE/LE? */
1851ced546577745d361ad06577914f44f484656d37Alex Deucher    switch (mesa_format) {
1861ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB8888:
1871ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_XRGB8888:
1881ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB8888;
1891ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1901ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_RGB565:
1911ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_RGB565;
1921ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
193a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher    case MESA_FORMAT_ARGB4444:
194a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB4444;
195a67cd1994f3474dd638af76b2bf5b19490863cbaAlex Deucher	    break;
1961ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_ARGB1555:
1971ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_ARGB1555;
1981ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
1991ced546577745d361ad06577914f44f484656d37Alex Deucher    case MESA_FORMAT_A8:
200a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_L8:
201a840bf4146b7e06cc94eb59d8265fe6593b232c4Alex Deucher    case MESA_FORMAT_I8:
2021ced546577745d361ad06577914f44f484656d37Alex Deucher	    dst_format = RADEON_COLOR_FORMAT_RGB8;
2031ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
2041ced546577745d361ad06577914f44f484656d37Alex Deucher    default:
2051ced546577745d361ad06577914f44f484656d37Alex Deucher	    break;
2061ced546577745d361ad06577914f44f484656d37Alex Deucher    }
2071ced546577745d361ad06577914f44f484656d37Alex Deucher
2081ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH_NO_AUTOSTATE(22);
2091ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_RE_AUX_SCISSOR_CNTL, 0);
2101ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(R200_RE_CNTL, 0);
2111ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RE_TOP_LEFT, 0);
2121ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RE_WIDTH_HEIGHT, ((width << RADEON_RE_WIDTH_SHIFT) |
2131ced546577745d361ad06577914f44f484656d37Alex Deucher					      (height << RADEON_RE_HEIGHT_SHIFT)));
2141ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_PLANEMASK, 0xffffffff);
2151ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_BLENDCNTL, RADEON_SRC_BLEND_GL_ONE | RADEON_DST_BLEND_GL_ZERO);
2161ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGVAL(RADEON_RB3D_CNTL, dst_format);
2171ced546577745d361ad06577914f44f484656d37Alex Deucher
2181ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(RADEON_RB3D_COLOROFFSET, 1);
2191ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(0, bo, 0, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0);
2201ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_REGSEQ(RADEON_RB3D_COLORPITCH, 1);
2211ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_RELOC(dst_pitch, bo, dst_pitch, 0, RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0);
2221ced546577745d361ad06577914f44f484656d37Alex Deucher
2231ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
2241ced546577745d361ad06577914f44f484656d37Alex Deucher}
2251ced546577745d361ad06577914f44f484656d37Alex Deucher
2261ced546577745d361ad06577914f44f484656d37Alex Deucherstatic GLboolean validate_buffers(struct r200_context *r200,
2271ced546577745d361ad06577914f44f484656d37Alex Deucher                                  struct radeon_bo *src_bo,
2281ced546577745d361ad06577914f44f484656d37Alex Deucher                                  struct radeon_bo *dst_bo)
2291ced546577745d361ad06577914f44f484656d37Alex Deucher{
2301ced546577745d361ad06577914f44f484656d37Alex Deucher    int ret;
2317959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher
2327959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher    radeon_cs_space_reset_bos(r200->radeon.cmdbuf.cs);
2337959274858fe66a90e6f97fed81141c39cb6702bAlex Deucher
234daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    ret = radeon_cs_space_check_with_bo(r200->radeon.cmdbuf.cs,
23539ab5ae30c303dd561252cb592d4de35814b6a70Alex Deucher                                        src_bo, RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT, 0);
236daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    if (ret)
237daf85c460875c944d6918fdf4041467d97cba41eDave Airlie        return GL_FALSE;
2381ced546577745d361ad06577914f44f484656d37Alex Deucher
239daf85c460875c944d6918fdf4041467d97cba41eDave Airlie    ret = radeon_cs_space_check_with_bo(r200->radeon.cmdbuf.cs,
24039ab5ae30c303dd561252cb592d4de35814b6a70Alex Deucher                                        dst_bo, 0, RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT);
2411ced546577745d361ad06577914f44f484656d37Alex Deucher    if (ret)
2421ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
2431ced546577745d361ad06577914f44f484656d37Alex Deucher
2441ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
2451ced546577745d361ad06577914f44f484656d37Alex Deucher}
2461ced546577745d361ad06577914f44f484656d37Alex Deucher
2471ced546577745d361ad06577914f44f484656d37Alex Deucher/**
2481ced546577745d361ad06577914f44f484656d37Alex Deucher * Calculate texcoords for given image region.
2491ced546577745d361ad06577914f44f484656d37Alex Deucher * Output values are [minx, maxx, miny, maxy]
2501ced546577745d361ad06577914f44f484656d37Alex Deucher */
2511ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void calc_tex_coords(float img_width, float img_height,
2521ced546577745d361ad06577914f44f484656d37Alex Deucher				   float x, float y,
2531ced546577745d361ad06577914f44f484656d37Alex Deucher				   float reg_width, float reg_height,
2541ced546577745d361ad06577914f44f484656d37Alex Deucher				   unsigned flip_y, float *buf)
2551ced546577745d361ad06577914f44f484656d37Alex Deucher{
2561ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[0] = x / img_width;
2571ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[1] = buf[0] + reg_width / img_width;
2581ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[2] = y / img_height;
2591ced546577745d361ad06577914f44f484656d37Alex Deucher    buf[3] = buf[2] + reg_height / img_height;
2601ced546577745d361ad06577914f44f484656d37Alex Deucher    if (flip_y)
2611ced546577745d361ad06577914f44f484656d37Alex Deucher    {
2625a99ca490fee65d37a4c7469888680b412d27f7fAlex Deucher        buf[2] = 1.0 - buf[2];
26376cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher        buf[3] = 1.0 - buf[3];
2641ced546577745d361ad06577914f44f484656d37Alex Deucher    }
2651ced546577745d361ad06577914f44f484656d37Alex Deucher}
2661ced546577745d361ad06577914f44f484656d37Alex Deucher
2671ced546577745d361ad06577914f44f484656d37Alex Deucherstatic inline void emit_draw_packet(struct r200_context *r200,
2681ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_width, unsigned src_height,
2691ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned src_x_offset, unsigned src_y_offset,
2701ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned dst_x_offset, unsigned dst_y_offset,
2711ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned reg_width, unsigned reg_height,
2721ced546577745d361ad06577914f44f484656d37Alex Deucher				    unsigned flip_y)
2731ced546577745d361ad06577914f44f484656d37Alex Deucher{
2741ced546577745d361ad06577914f44f484656d37Alex Deucher    float texcoords[4];
2751ced546577745d361ad06577914f44f484656d37Alex Deucher    float verts[12];
2761ced546577745d361ad06577914f44f484656d37Alex Deucher    BATCH_LOCALS(&r200->radeon);
2771ced546577745d361ad06577914f44f484656d37Alex Deucher
2781ced546577745d361ad06577914f44f484656d37Alex Deucher    calc_tex_coords(src_width, src_height,
2791ced546577745d361ad06577914f44f484656d37Alex Deucher                    src_x_offset, src_y_offset,
2801ced546577745d361ad06577914f44f484656d37Alex Deucher                    reg_width, reg_height,
2811ced546577745d361ad06577914f44f484656d37Alex Deucher                    flip_y, texcoords);
2821ced546577745d361ad06577914f44f484656d37Alex Deucher
2831ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[0] = dst_x_offset;
2841ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[1] = dst_y_offset + reg_height;
2851ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[2] = texcoords[0];
28676cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[3] = texcoords[3];
2871ced546577745d361ad06577914f44f484656d37Alex Deucher
2881ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[4] = dst_x_offset + reg_width;
2891ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[5] = dst_y_offset + reg_height;
2901ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[6] = texcoords[1];
29176cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[7] = texcoords[3];
2921ced546577745d361ad06577914f44f484656d37Alex Deucher
2931ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[8] = dst_x_offset + reg_width;
2941ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[9] = dst_y_offset;
2951ced546577745d361ad06577914f44f484656d37Alex Deucher    verts[10] = texcoords[1];
29676cf2618327a7f008dcfd0d91d64d6d9e01f9a9cAlex Deucher    verts[11] = texcoords[2];
2971ced546577745d361ad06577914f44f484656d37Alex Deucher
2981ced546577745d361ad06577914f44f484656d37Alex Deucher    BEGIN_BATCH(14);
2991ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(R200_CP_CMD_3D_DRAW_IMMD_2 | (12 << 16));
3001ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH(RADEON_CP_VC_CNTL_PRIM_WALK_RING |
3011ced546577745d361ad06577914f44f484656d37Alex Deucher	      RADEON_CP_VC_CNTL_PRIM_TYPE_RECT_LIST |
3021ced546577745d361ad06577914f44f484656d37Alex Deucher              (3 << 16));
3031ced546577745d361ad06577914f44f484656d37Alex Deucher    OUT_BATCH_TABLE(verts, 12);
3041ced546577745d361ad06577914f44f484656d37Alex Deucher    END_BATCH();
3051ced546577745d361ad06577914f44f484656d37Alex Deucher}
3061ced546577745d361ad06577914f44f484656d37Alex Deucher
3071ced546577745d361ad06577914f44f484656d37Alex Deucher/**
3081ced546577745d361ad06577914f44f484656d37Alex Deucher * Copy a region of [@a width x @a height] pixels from source buffer
3091ced546577745d361ad06577914f44f484656d37Alex Deucher * to destination buffer.
3101ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] r200 r200 context
3111ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_bo source radeon buffer object
3121ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_offset offset of the source image in the @a src_bo
3131ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_mesaformat source image format
3141ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_pitch aligned source image width
3151ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_width source image width
3161ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_height source image height
3171ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_x_offset x offset in the source image
3181ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] src_y_offset y offset in the source image
3191ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_bo destination radeon buffer object
3201ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_offset offset of the destination image in the @a dst_bo
3211ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_mesaformat destination image format
3221ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_pitch aligned destination image width
3231ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_width destination image width
3241ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_height destination image height
3251ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_x_offset x offset in the destination image
3261ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] dst_y_offset y offset in the destination image
3271ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] width region width
3281ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] height region height
3291ced546577745d361ad06577914f44f484656d37Alex Deucher * @param[in] flip_y set if y coords of the source image need to be flipped
3301ced546577745d361ad06577914f44f484656d37Alex Deucher */
3312706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencoraunsigned r200_blit(GLcontext *ctx,
3322706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *src_bo,
3332706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t src_offset,
3342706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format src_mesaformat,
3352706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_pitch,
3362706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_width,
3372706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_height,
3382706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_x_offset,
3392706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned src_y_offset,
3402706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   struct radeon_bo *dst_bo,
3412706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   intptr_t dst_offset,
3422706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   gl_format dst_mesaformat,
3432706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_pitch,
3442706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_width,
3452706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_height,
3462706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_x_offset,
3472706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned dst_y_offset,
3482706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_width,
3492706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned reg_height,
3502706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora                   unsigned flip_y)
3511ced546577745d361ad06577914f44f484656d37Alex Deucher{
3522706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    struct r200_context *r200 = R200_CONTEXT(ctx);
3532706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora
3543594bf233d16ceb21e97fcdfb57ea45cb0c5e41bAlex Deucher    if (!r200_check_blit(dst_mesaformat))
3551ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3561ced546577745d361ad06577914f44f484656d37Alex Deucher
3571ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Make sure that colorbuffer has even width - hw limitation */
3581ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch % 2 > 0)
3591ced546577745d361ad06577914f44f484656d37Alex Deucher        ++dst_pitch;
3601ced546577745d361ad06577914f44f484656d37Alex Deucher
3611ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Rendering to small buffer doesn't work.
3621ced546577745d361ad06577914f44f484656d37Alex Deucher     * Looks like a hw limitation.
3631ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3641ced546577745d361ad06577914f44f484656d37Alex Deucher    if (dst_pitch < 32)
3651ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3661ced546577745d361ad06577914f44f484656d37Alex Deucher
3671ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Need to clamp the region size to make sure
3681ced546577745d361ad06577914f44f484656d37Alex Deucher     * we don't read outside of the source buffer
3691ced546577745d361ad06577914f44f484656d37Alex Deucher     * or write outside of the destination buffer.
3701ced546577745d361ad06577914f44f484656d37Alex Deucher     */
3711ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + src_x_offset > src_width)
3721ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = src_width - src_x_offset;
3731ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + src_y_offset > src_height)
3741ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = src_height - src_y_offset;
3751ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_width + dst_x_offset > dst_width)
3761ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_width = dst_width - dst_x_offset;
3771ced546577745d361ad06577914f44f484656d37Alex Deucher    if (reg_height + dst_y_offset > dst_height)
3781ced546577745d361ad06577914f44f484656d37Alex Deucher        reg_height = dst_height - dst_y_offset;
3791ced546577745d361ad06577914f44f484656d37Alex Deucher
3801ced546577745d361ad06577914f44f484656d37Alex Deucher    if (src_bo == dst_bo) {
3811ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
3821ced546577745d361ad06577914f44f484656d37Alex Deucher    }
3831ced546577745d361ad06577914f44f484656d37Alex Deucher
384156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher    if (src_offset % 32 || dst_offset % 32) {
385156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher        return GL_FALSE;
386156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher    }
387156c90e5c3289fda8290bdd04c5cb5041a65113dAlex Deucher
3881ced546577745d361ad06577914f44f484656d37Alex Deucher    if (0) {
3891ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "src: size [%d x %d], pitch %d, "
3901ced546577745d361ad06577914f44f484656d37Alex Deucher                "offset [%d x %d], format %s, bo %p\n",
3911ced546577745d361ad06577914f44f484656d37Alex Deucher                src_width, src_height, src_pitch,
3921ced546577745d361ad06577914f44f484656d37Alex Deucher                src_x_offset, src_y_offset,
3931ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(src_mesaformat),
3941ced546577745d361ad06577914f44f484656d37Alex Deucher                src_bo);
3951ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "dst: pitch %d, offset[%d x %d], format %s, bo %p\n",
3961ced546577745d361ad06577914f44f484656d37Alex Deucher                dst_pitch, dst_x_offset, dst_y_offset,
3971ced546577745d361ad06577914f44f484656d37Alex Deucher                _mesa_get_format_name(dst_mesaformat), dst_bo);
3981ced546577745d361ad06577914f44f484656d37Alex Deucher        fprintf(stderr, "region: %d x %d\n", reg_width, reg_height);
3991ced546577745d361ad06577914f44f484656d37Alex Deucher    }
4001ced546577745d361ad06577914f44f484656d37Alex Deucher
4011ced546577745d361ad06577914f44f484656d37Alex Deucher    /* Flush is needed to make sure that source buffer has correct data */
4021ced546577745d361ad06577914f44f484656d37Alex Deucher    radeonFlush(r200->radeon.glCtx);
4031ced546577745d361ad06577914f44f484656d37Alex Deucher
4041ced546577745d361ad06577914f44f484656d37Alex Deucher    rcommonEnsureCmdBufSpace(&r200->radeon, 78, __FUNCTION__);
4051ced546577745d361ad06577914f44f484656d37Alex Deucher
4061ced546577745d361ad06577914f44f484656d37Alex Deucher    if (!validate_buffers(r200, src_bo, dst_bo))
4071ced546577745d361ad06577914f44f484656d37Alex Deucher        return GL_FALSE;
4081ced546577745d361ad06577914f44f484656d37Alex Deucher
4091ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
4101ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_vtx_state(r200);
4111ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 28 */
4121ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_tx_setup(r200, src_mesaformat, src_bo, src_offset, src_width, src_height, src_pitch);
4131ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 22 */
4141ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_cb_setup(r200, dst_bo, dst_offset, dst_mesaformat, dst_pitch, dst_width, dst_height);
4151ced546577745d361ad06577914f44f484656d37Alex Deucher    /* 14 */
4161ced546577745d361ad06577914f44f484656d37Alex Deucher    emit_draw_packet(r200, src_width, src_height,
4171ced546577745d361ad06577914f44f484656d37Alex Deucher                     src_x_offset, src_y_offset,
4181ced546577745d361ad06577914f44f484656d37Alex Deucher                     dst_x_offset, dst_y_offset,
4191ced546577745d361ad06577914f44f484656d37Alex Deucher                     reg_width, reg_height,
4201ced546577745d361ad06577914f44f484656d37Alex Deucher                     flip_y);
4211ced546577745d361ad06577914f44f484656d37Alex Deucher
4222706bc6a8898c7d7e155440cfa793035e56186b8Maciej Cencora    radeonFlush(ctx);
4231ced546577745d361ad06577914f44f484656d37Alex Deucher
4241ced546577745d361ad06577914f44f484656d37Alex Deucher    return GL_TRUE;
4251ced546577745d361ad06577914f44f484656d37Alex Deucher}
426