14c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller/*
24c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Copyright 2008 Ben Skeggs
34c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
44c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Permission is hereby granted, free of charge, to any person obtaining a
54c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * copy of this software and associated documentation files (the "Software"),
64c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * to deal in the Software without restriction, including without limitation
74c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * and/or sell copies of the Software, and to permit persons to whom the
94c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Software is furnished to do so, subject to the following conditions:
104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
114c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * The above copyright notice and this permission notice shall be included in
124c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * all copies or substantial portions of the Software.
134c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
173d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
183d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * OTHER DEALINGS IN THE SOFTWARE.
214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller */
224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include <stdint.h>
244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
254c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "pipe/p_defines.h"
264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "util/u_inlines.h"
284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "util/u_pack_color.h"
294c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "util/u_format.h"
30883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák#include "util/u_surface.h"
314c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller#include "os/os_thread.h"
3336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
345eb7ff1175a644ffe3b0f1a75cb235400355f9fbJohannes Obermayr#include "nvc0/nvc0_context.h"
355eb7ff1175a644ffe3b0f1a75cb235400355f9fbJohannes Obermayr#include "nvc0/nvc0_resource.h"
364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
37546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs#include "nv50/g80_defs.xml.h"
38c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs#include "nv50/g80_texture.xml.h"
394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
40d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller/* these are used in nv50_blit.h */
41d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NV50_ENG2D_SUPPORTED_FORMATS 0xff9ccfe1cce3ccc9ULL
42d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NV50_ENG2D_NOCONVERT_FORMATS 0x009cc02000000000ULL
43d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NV50_ENG2D_LUMINANCE_FORMATS 0x001cc02000000000ULL
44d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NV50_ENG2D_INTENSITY_FORMATS 0x0080000000000000ULL
45d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NV50_ENG2D_OPERATION_FORMATS 0x060001c000638000ULL
461b4c0c8ea0b4e6065f23f9f2bbb954a7bd2549e4Christoph Bumiller
47d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#define NOUVEAU_DRIVER 0xc0
48d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller#include "nv50/nv50_blit.h"
494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
50a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkinstatic inline uint8_t
51cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetnvc0_2d_format(enum pipe_format format, bool dst, bool dst_src_equal)
524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   uint8_t id = nvc0_format_table[format].rt;
544c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
55d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   /* A8_UNORM is treated as I8_UNORM as far as the 2D engine is concerned. */
56d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   if (!dst && unlikely(format == PIPE_FORMAT_I8_UNORM) && !dst_src_equal)
57546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_A8_UNORM;
58d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller
594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   /* Hardware values for color formats range from 0xc0 to 0xff,
604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    * but the 2D engine doesn't support all of them.
614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    */
62d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   if (nv50_2d_format_supported(format))
634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return id;
64d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   assert(dst_src_equal);
654c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
664c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   switch (util_format_get_blocksize(format)) {
674c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   case 1:
68546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_R8_UNORM;
694c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   case 2:
70546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_RG8_UNORM;
714c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   case 4:
72546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_BGRA8_UNORM;
731b4c0c8ea0b4e6065f23f9f2bbb954a7bd2549e4Christoph Bumiller   case 8:
74546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_RGBA16_UNORM;
751b4c0c8ea0b4e6065f23f9f2bbb954a7bd2549e4Christoph Bumiller   case 16:
76546ccf3f8215b9546f20e38cea4afbda9bce6b44Ben Skeggs      return G80_SURFACE_FORMAT_RGBA32_FLOAT;
774c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   default:
78d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      assert(0);
794c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return 0;
804c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
814c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
824c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
834c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic int
84cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetnvc0_2d_texture_set(struct nouveau_pushbuf *push, bool dst,
8536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                    struct nv50_miptree *mt, unsigned level, unsigned layer,
86cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset                    enum pipe_format pformat, bool dst_src_pformat_equal)
874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
88ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   struct nouveau_bo *bo = mt->base.bo;
89ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   uint32_t width, height, depth;
90ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   uint32_t format;
9172283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin   uint32_t mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
92ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   uint32_t offset = mt->level[level].offset;
93ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
94d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   format = nvc0_2d_format(pformat, dst, dst_src_pformat_equal);
954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (!format) {
964c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
9736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                  util_format_name(pformat));
984c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return 1;
994c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
1004c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
101b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller   width = u_minify(mt->base.base.width0, level) << mt->ms_x;
102b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller   height = u_minify(mt->base.base.height0, level) << mt->ms_y;
1034fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller   depth = u_minify(mt->base.base.depth0, level);
1044fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller
1054fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller   /* layer has to be < depth, and depth > tile depth / 2 */
106ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
107ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   if (!mt->layout_3d) {
108ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller      offset += mt->layer_stride * layer;
1094fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller      layer = 0;
110ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller      depth = 1;
1114fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller   } else
1124fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller   if (!dst) {
113cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      offset += nvc0_mt_zslice_offset(mt, level, layer);
114ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller      layer = 0;
115ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   }
116ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
11736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (!nouveau_bo_memtype(bo)) {
1186d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, SUBC_2D(mthd), 2);
1196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, format);
1206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 1);
1216d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, SUBC_2D(mthd + 0x14), 5);
1226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, mt->level[level].pitch);
1236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, width);
1246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, height);
1256d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAh(push, bo->offset + offset);
1266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, bo->offset + offset);
1274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   } else {
1286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, SUBC_2D(mthd), 5);
1296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, format);
1306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 0);
1316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, mt->level[level].tile_mode);
1326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, depth);
1336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, layer);
1346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, SUBC_2D(mthd + 0x18), 4);
1356d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, width);
1366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, height);
1376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAh(push, bo->offset + offset);
1386d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, bo->offset + offset);
1394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
140ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
1414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#if 0
1424c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (dst) {
1436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, SUBC_2D(NVC0_2D_CLIP_X), 4);
1446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 0);
1456d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 0);
1466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, width);
1476d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, height);
1484c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
1494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#endif
1504c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   return 0;
1514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic int
1546d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumillernvc0_2d_texture_do_copy(struct nouveau_pushbuf *push,
155cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                        struct nv50_miptree *dst, unsigned dst_level,
156ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                        unsigned dx, unsigned dy, unsigned dz,
157cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                        struct nv50_miptree *src, unsigned src_level,
158ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                        unsigned sx, unsigned sy, unsigned sz,
159ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                        unsigned w, unsigned h)
1604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
16136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const enum pipe_format dfmt = dst->base.base.format;
16236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const enum pipe_format sfmt = src->base.base.format;
1634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   int ret;
164cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   bool eqfmt = dfmt == sfmt;
1654c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
166edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst   if (!PUSH_SPACE(push, 2 * 16 + 32))
167edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst      return PIPE_ERROR;
1684c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
169cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   ret = nvc0_2d_texture_set(push, true, dst, dst_level, dz, dfmt, eqfmt);
1704c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (ret)
1714c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return ret;
1724c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
173cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   ret = nvc0_2d_texture_set(push, false, src, src_level, sz, sfmt, eqfmt);
1744c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (ret)
1754c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return ret;
1764c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
17736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   IMMED_NVC0(push, NVC0_2D(BLIT_CONTROL), 0x00);
1786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_2D(BLIT_DST_X), 4);
1796d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, dx << dst->ms_x);
1806d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, dy << dst->ms_y);
1816d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, w << dst->ms_x);
1826d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, h << dst->ms_y);
1836d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_2D(BLIT_DU_DX_FRACT), 4);
18436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, 0);
18536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, 1);
18636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, 0);
18736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, 1);
1886d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_2D(BLIT_SRC_X_FRACT), 4);
1896d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0);
1906d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, sx << src->ms_x);
1916d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0);
192ec151e2f72bd4a239573770aea563d47d0268708Samuel Pitoiset   PUSH_DATA (push, sy << src->ms_y);
1934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   return 0;
1954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1964c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1974c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
198ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumillernvc0_resource_copy_region(struct pipe_context *pipe,
199ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                          struct pipe_resource *dst, unsigned dst_level,
200ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                          unsigned dstx, unsigned dsty, unsigned dstz,
201ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                          struct pipe_resource *src, unsigned src_level,
202ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                          const struct pipe_box *src_box)
2034c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
2046d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nvc0_context *nvc0 = nvc0_context(pipe);
205ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   int ret;
206cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   bool m2mf;
207ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   unsigned dst_layer = dstz, src_layer = src_box->z;
2084c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
209883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák   if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
2102a8145d36b0f04d0f26c1628222a8b5c4830f435Christoph Bumiller      nouveau_copy_buffer(&nvc0->base,
2112a8145d36b0f04d0f26c1628222a8b5c4830f435Christoph Bumiller                          nv04_resource(dst), dstx,
2122a8145d36b0f04d0f26c1628222a8b5c4830f435Christoph Bumiller                          nv04_resource(src), src_box->x, src_box->width);
213198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller      NOUVEAU_DRV_STAT(&nvc0->screen->base, buf_copy_bytes, src_box->width);
214883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák      return;
215883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák   }
216198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller   NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_copy_count, 1);
217883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák
218b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   /* 0 and 1 are equal, only supporting 0/1, 2, 4 and 8 */
219b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   assert((src->nr_samples | 1) == (dst->nr_samples | 1));
2209e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller
2219e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller   m2mf = (src->format == dst->format) ||
2229e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller      (util_format_get_blocksizebits(src->format) ==
2239e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller       util_format_get_blocksizebits(dst->format));
2249e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller
225f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller   nv04_resource(dst)->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
226f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
2279e466e87e6fde23f8ec0923be86005be81ac2d24Christoph Bumiller   if (m2mf) {
228ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin      struct nv50_miptree *src_mt = nv50_miptree(src);
229ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin      struct nv50_miptree *dst_mt = nv50_miptree(dst);
230b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      struct nv50_m2mf_rect drect, srect;
231f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller      unsigned i;
232ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin      unsigned nx = util_format_get_nblocksx(src->format, src_box->width)
233ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin         << src_mt->ms_x;
234ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin      unsigned ny = util_format_get_nblocksy(src->format, src_box->height)
235ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin         << src_mt->ms_y;
236f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
237b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nv50_m2mf_rect_setup(&drect, dst, dst_level, dstx, dsty, dstz);
238b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nv50_m2mf_rect_setup(&srect, src, src_level,
239f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller                           src_box->x, src_box->y, src_box->z);
240f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
241f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller      for (i = 0; i < src_box->depth; ++i) {
242e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller         nvc0->m2mf_copy_rect(nvc0, &drect, &srect, nx, ny);
243f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
244ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin         if (dst_mt->layout_3d)
245f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller            drect.z++;
246f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller         else
247ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin            drect.base += dst_mt->layer_stride;
248f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
249ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin         if (src_mt->layout_3d)
250f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller            srect.z++;
251f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller         else
252ebbd7b41c0be8666f47b06cb83ae5e5117088ea1Ilia Mirkin            srect.base += src_mt->layer_stride;
253f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller      }
254f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller      return;
255f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller   }
256f556b897eb5a41116529bec24d47f70a0c46789fChristoph Bumiller
257d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   assert(nv50_2d_dst_format_faithful(dst->format));
258d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   assert(nv50_2d_src_format_faithful(src->format));
259ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
2606d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BCTX_REFN(nvc0->bufctx, 2D, nv04_resource(src), RD);
2616d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BCTX_REFN(nvc0->bufctx, 2D, nv04_resource(dst), WR);
2626d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   nouveau_pushbuf_bufctx(nvc0->base.pushbuf, nvc0->bufctx);
2636d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   nouveau_pushbuf_validate(nvc0->base.pushbuf);
2646d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
265ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   for (; dst_layer < dstz + src_box->depth; ++dst_layer, ++src_layer) {
2666d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      ret = nvc0_2d_texture_do_copy(nvc0->base.pushbuf,
267cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                                    nv50_miptree(dst), dst_level,
268ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                                    dstx, dsty, dst_layer,
269cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                                    nv50_miptree(src), src_level,
270ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                                    src_box->x, src_box->y, src_layer,
271ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                                    src_box->width, src_box->height);
272ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller      if (ret)
2736d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         break;
274ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller   }
2756d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   nouveau_bufctx_reset(nvc0->bufctx, 0);
2764c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
2774c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
2784c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
2794c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_clear_render_target(struct pipe_context *pipe,
280ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                         struct pipe_surface *dst,
2816dd284f7c8fac22f64c13fdf9909094f5ec59086Dave Airlie                         const union pipe_color_union *color,
282ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller                         unsigned dstx, unsigned dsty,
283a909210131494a6a131855d7d344b61b81fbf40eMarek Olšák                         unsigned width, unsigned height,
284a909210131494a6a131855d7d344b61b81fbf40eMarek Olšák                         bool render_condition_enabled)
2854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
2866d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nvc0_context *nvc0 = nvc0_context(pipe);
2876d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
28828271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   struct nv50_surface *sf = nv50_surface(dst);
28928271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   struct nv04_resource *res = nv04_resource(sf->base.texture);
29028271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   unsigned z;
29128271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
292ea33920f7ed3a4100c921db563dc998e0ca24aadSamuel Pitoiset   assert(dst->texture->target != PIPE_BUFFER);
293ea33920f7ed3a4100c921db563dc998e0ca24aadSamuel Pitoiset
294edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst   if (!PUSH_SPACE(push, 32 + sf->depth))
295edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst      return;
296edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst
297edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst   PUSH_REFN (push, res->bo, res->domain | NOUVEAU_BO_WR);
298edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst
2996d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(CLEAR_COLOR(0)), 4);
3006d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, color->f[0]);
3016d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, color->f[1]);
3026d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, color->f[2]);
3036d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, color->f[3]);
3046d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
3056d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
3066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, ( width << 16) | dstx);
3076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, (height << 16) | dsty);
3086d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
3096d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
3106d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 1);
3116d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(0)), 9);
3126d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAh(push, res->address + sf->offset);
3136d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, res->address + sf->offset);
3146d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (likely(nouveau_bo_memtype(res->bo))) {
31528271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      struct nv50_miptree *mt = nv50_miptree(dst->texture);
31628271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
3176d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, sf->width);
3186d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, sf->height);
3196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, nvc0_format_table[dst->format].rt);
3206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, (mt->layout_3d << 16) |
32128271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller               mt->level[sf->base.u.tex.level].tile_mode);
3226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, dst->u.tex.first_layer + sf->depth);
3236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, mt->layer_stride >> 2);
3246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, dst->u.tex.first_layer);
325c4182bb9b0897b4a4ac4f06b54fc7f6a2ddeb105Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), mt->ms_mode);
32628271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   } else {
32728271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      if (res->base.target == PIPE_BUFFER) {
3286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, 262144);
3296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, 1);
33028271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      } else {
3316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, nv50_miptree(&res->base)->level[0].pitch);
3326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, sf->height);
33328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      }
3346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
3356d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, 1 << 12);
3366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, 1);
3376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, 0);
3386d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA(push, 0);
33928271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
3406d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(ZETA_ENABLE), 0);
341c4182bb9b0897b4a4ac4f06b54fc7f6a2ddeb105Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), 0);
34228271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
34328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      /* tiled textures don't have to be fenced, they're not mapped directly */
34428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller      nvc0_resource_fence(res, NOUVEAU_BO_WR);
34528271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   }
34628271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
347163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin   if (!render_condition_enabled)
348163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin      IMMED_NVC0(push, NVC0_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
349ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin
350edc8e8cbef874a37c721b576c3ecd2a9f2fd399bMaarten Lankhorst   BEGIN_NIC0(push, NVC0_3D(CLEAR_BUFFERS), sf->depth);
35128271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   for (z = 0; z < sf->depth; ++z) {
3526d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 0x3c |
35328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller                 (z << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
35428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller   }
35528271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
356163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin   if (!render_condition_enabled)
357163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin      IMMED_NVC0(push, NVC0_3D(COND_MODE), nvc0->cond_condmode);
358ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin
3599c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset   nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
3604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
3614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3624c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
3633ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkinnvc0_clear_buffer_push_nvc0(struct pipe_context *pipe,
3643ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            struct pipe_resource *res,
3653ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            unsigned offset, unsigned size,
3663ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            const void *data, int data_size)
367a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann{
3683ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nvc0_context *nvc0 = nvc0_context(pipe);
3693ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
370a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   struct nv04_resource *buf = nv04_resource(res);
3713ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned i;
372a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
3733ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_bufctx_refn(nvc0->bufctx, 0, buf->bo, buf->domain | NOUVEAU_BO_WR);
3743ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_pushbuf_bufctx(push, nvc0->bufctx);
3753ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_pushbuf_validate(push);
3763ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
3773ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned count = (size + 3) / 4;
3783ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned data_words = data_size / 4;
3793ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
3803ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   while (count) {
3813ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      unsigned nr_data = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN) / data_words;
3823ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      unsigned nr = nr_data * data_words;
3833ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
3843ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      if (!PUSH_SPACE(push, nr + 9))
3853ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin         break;
386a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
3873ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
3883ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATAh(push, buf->address + offset);
3893ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, buf->address + offset);
3903ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
3913ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, MIN2(size, nr * 4));
3923ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, 1);
3933ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
3943ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, 0x100111);
395a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
3963ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
3973ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NIC0(push, NVC0_M2MF(DATA), nr);
3983ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      for (i = 0; i < nr_data; i++)
3993ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin         PUSH_DATAp(push, data, data_words);
400a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
4013ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      count -= nr;
4023ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      offset += nr * 4;
4033ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      size -= nr * 4;
4043ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
405a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
4063ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (buf->mm) {
4073ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence);
4083ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence_wr);
4093ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
4103ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4113ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_bufctx_reset(nvc0->bufctx, 0);
4123ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin}
4133ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4143ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkinstatic void
4153ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkinnvc0_clear_buffer_push_nve4(struct pipe_context *pipe,
4163ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            struct pipe_resource *res,
4173ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            unsigned offset, unsigned size,
4183ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                            const void *data, int data_size)
4193ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin{
4203ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nvc0_context *nvc0 = nvc0_context(pipe);
4213ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
4223ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nv04_resource *buf = nv04_resource(res);
4233ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned i;
4243ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4253ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_bufctx_refn(nvc0->bufctx, 0, buf->bo, buf->domain | NOUVEAU_BO_WR);
4263ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_pushbuf_bufctx(push, nvc0->bufctx);
4273ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_pushbuf_validate(push);
4283ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4293ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned count = (size + 3) / 4;
4303ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned data_words = data_size / 4;
4313ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4323ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   while (count) {
4333ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      unsigned nr_data = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN) / data_words;
4343ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      unsigned nr = nr_data * data_words;
4353ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4363ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      if (!PUSH_SPACE(push, nr + 10))
4373ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin         break;
4383ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4393ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_DST_ADDRESS_HIGH), 2);
4403ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATAh(push, buf->address + offset);
4413ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, buf->address + offset);
4423ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_LINE_LENGTH_IN), 2);
4433ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, MIN2(size, nr * 4));
4443ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, 1);
4453ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
4463ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      BEGIN_1IC0(push, NVE4_P2MF(UPLOAD_EXEC), nr + 1);
4473ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      PUSH_DATA (push, 0x1001);
4483ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      for (i = 0; i < nr_data; i++)
4493ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin         PUSH_DATAp(push, data, data_words);
4503ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4513ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      count -= nr;
4523ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      offset += nr * 4;
4533ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      size -= nr * 4;
4543ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
4553ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4563ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (buf->mm) {
4573ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence);
4583ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence_wr);
4593ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
4603ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4613ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   nouveau_bufctx_reset(nvc0->bufctx, 0);
4623ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin}
4633ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4643ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkinstatic void
4653ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkinnvc0_clear_buffer_push(struct pipe_context *pipe,
4663ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                       struct pipe_resource *res,
4673ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                       unsigned offset, unsigned size,
4683ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                       const void *data, int data_size)
4693ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin{
4703ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   struct nvc0_context *nvc0 = nvc0_context(pipe);
4713ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   unsigned tmp;
4723ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4733ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (data_size == 1) {
4743ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      tmp = *(unsigned char *)data;
4753ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      tmp = (tmp << 24) | (tmp << 16) | (tmp << 8) | tmp;
4763ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      data = &tmp;
4773ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      data_size = 4;
4783ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   } else if (data_size == 2) {
4793ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      tmp = *(unsigned short *)data;
4803ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      tmp = (tmp << 16) | tmp;
4813ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      data = &tmp;
4823ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      data_size = 4;
4833ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
4843ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
4853ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (nvc0->screen->base.class_3d < NVE4_3D_CLASS)
4863ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nvc0_clear_buffer_push_nvc0(pipe, res, offset, size, data, data_size);
4873ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   else
4883ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nvc0_clear_buffer_push_nve4(pipe, res, offset, size, data, data_size);
489a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann}
490a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
491a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmannstatic void
492a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmannnvc0_clear_buffer(struct pipe_context *pipe,
493a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann                  struct pipe_resource *res,
494a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann                  unsigned offset, unsigned size,
495a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann                  const void *data, int data_size)
496a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann{
497a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   struct nvc0_context *nvc0 = nvc0_context(pipe);
498a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
499a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   struct nv04_resource *buf = nv04_resource(res);
500a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   union pipe_color_union color;
501a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   enum pipe_format dst_fmt;
502a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   unsigned width, height, elements;
503a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
504a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   assert(res->target == PIPE_BUFFER);
505a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   assert(nouveau_bo_memtype(buf->bo) == 0);
506a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
507a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   switch (data_size) {
508a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 16:
509a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      dst_fmt = PIPE_FORMAT_R32G32B32A32_UINT;
510a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memcpy(&color.ui, data, 16);
511a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
512a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 12:
5133ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      /* RGB32 is not a valid RT format. This will be handled by the pushbuf
5143ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin       * uploader.
515a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann       */
516a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
517a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 8:
518a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      dst_fmt = PIPE_FORMAT_R32G32_UINT;
519a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memcpy(&color.ui, data, 8);
520a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memset(&color.ui[2], 0, 8);
521a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
522a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 4:
523a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      dst_fmt = PIPE_FORMAT_R32_UINT;
524a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memcpy(&color.ui, data, 4);
525a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memset(&color.ui[1], 0, 12);
526a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
527a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 2:
528a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      dst_fmt = PIPE_FORMAT_R16_UINT;
529a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      color.ui[0] = util_cpu_to_le32(
530a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann            util_le16_to_cpu(*(unsigned short *)data));
531a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memset(&color.ui[1], 0, 12);
532a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
533a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   case 1:
534a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      dst_fmt = PIPE_FORMAT_R8_UINT;
535a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      color.ui[0] = util_cpu_to_le32(*(unsigned char *)data);
536a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      memset(&color.ui[1], 0, 12);
537a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      break;
538a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   default:
539a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      assert(!"Unsupported element size");
540a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      return;
541a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   }
542a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
543a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   assert(size % data_size == 0);
544a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
545a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   if (data_size == 12) {
5463ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nvc0_clear_buffer_push(pipe, res, offset, size, data, data_size);
547a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      return;
548a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   }
549a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
5503ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (offset & 0xff) {
5513ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      unsigned fixup_size = MIN2(size, align(offset, 0x100) - offset);
5523ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      assert(fixup_size % data_size == 0);
5533ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nvc0_clear_buffer_push(pipe, res, offset, fixup_size, data, data_size);
5543ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      offset += fixup_size;
5553ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      size -= fixup_size;
5563ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      if (!size)
5573ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin         return;
5583ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
5593ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
560a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   elements = size / data_size;
561a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   height = (elements + 16383) / 16384;
562a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   width = elements / height;
5633ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (height > 1)
5643ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      width &= ~0xff;
5653ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   assert(width > 0);
566a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
567a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   if (!PUSH_SPACE(push, 40))
568a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      return;
569a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
570a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_REFN (push, buf->bo, buf->domain | NOUVEAU_BO_WR);
571a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
572a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   BEGIN_NVC0(push, NVC0_3D(CLEAR_COLOR(0)), 4);
573a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATAf(push, color.f[0]);
574a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATAf(push, color.f[1]);
575a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATAf(push, color.f[2]);
576a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATAf(push, color.f[3]);
577a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
578a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, width << 16);
579a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, height << 16);
580a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
581a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   IMMED_NVC0(push, NVC0_3D(RT_CONTROL), 1);
582a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
583a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(0)), 9);
584a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATAh(push, buf->address + offset);
585a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, buf->address + offset);
5863ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   PUSH_DATA (push, align(width * data_size, 0x100));
587a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, height);
588a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, nvc0_format_table[dst_fmt].rt);
589a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, NVC0_3D_RT_TILE_MODE_LINEAR);
590a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, 1);
591a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, 0);
592a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   PUSH_DATA (push, 0);
593a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
594a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   IMMED_NVC0(push, NVC0_3D(ZETA_ENABLE), 0);
595c4182bb9b0897b4a4ac4f06b54fc7f6a2ddeb105Ilia Mirkin   IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), 0);
596a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
597ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin   IMMED_NVC0(push, NVC0_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
598ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin
599a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   IMMED_NVC0(push, NVC0_3D(CLEAR_BUFFERS), 0x3c);
600a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
6013ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   IMMED_NVC0(push, NVC0_3D(COND_MODE), nvc0->cond_condmode);
6023ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
6033ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   if (buf->mm) {
6043ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence);
6053ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nouveau_fence_ref(nvc0->screen->base.fence.current, &buf->fence_wr);
6063ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin   }
6073ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin
608a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   if (width * height != elements) {
609a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      offset += width * height * data_size;
610a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann      width = elements - width * height;
6113ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin      nvc0_clear_buffer_push(pipe, res, offset, width * data_size,
6123ca2001b537a2709e7ef60410e7dfad5d38663f4Ilia Mirkin                             data, data_size);
613a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   }
614a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
6159c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset   nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
616a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann}
617a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann
618a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmannstatic void
6194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_clear_depth_stencil(struct pipe_context *pipe,
6204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         struct pipe_surface *dst,
6214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         unsigned clear_flags,
6224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         double depth,
6234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         unsigned stencil,
6244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         unsigned dstx, unsigned dsty,
625a909210131494a6a131855d7d344b61b81fbf40eMarek Olšák                         unsigned width, unsigned height,
626a909210131494a6a131855d7d344b61b81fbf40eMarek Olšák                         bool render_condition_enabled)
6274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
62867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   struct nvc0_context *nvc0 = nvc0_context(pipe);
62967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
63067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   struct nv50_miptree *mt = nv50_miptree(dst->texture);
63167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   struct nv50_surface *sf = nv50_surface(dst);
63267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   uint32_t mode = 0;
63367635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   int unk = mt->base.base.target == PIPE_TEXTURE_2D;
63467635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   unsigned z;
63567635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
636ea33920f7ed3a4100c921db563dc998e0ca24aadSamuel Pitoiset   assert(dst->texture->target != PIPE_BUFFER);
637ea33920f7ed3a4100c921db563dc998e0ca24aadSamuel Pitoiset
63867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   if (!PUSH_SPACE(push, 32 + sf->depth))
63967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      return;
64067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
64167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_REFN (push, mt->base.bo, mt->base.domain | NOUVEAU_BO_WR);
64267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
64367635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   if (clear_flags & PIPE_CLEAR_DEPTH) {
64467635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      BEGIN_NVC0(push, NVC0_3D(CLEAR_DEPTH), 1);
64567635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      PUSH_DATAf(push, depth);
64667635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      mode |= NVC0_3D_CLEAR_BUFFERS_Z;
64767635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   }
64867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
64967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   if (clear_flags & PIPE_CLEAR_STENCIL) {
65067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      BEGIN_NVC0(push, NVC0_3D(CLEAR_STENCIL), 1);
65167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      PUSH_DATA (push, stencil & 0xff);
65267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      mode |= NVC0_3D_CLEAR_BUFFERS_S;
65367635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   }
65467635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
65567635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
65667635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, ( width << 16) | dstx);
65767635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, (height << 16) | dsty);
65867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
65967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NVC0(push, NVC0_3D(ZETA_ADDRESS_HIGH), 5);
66067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATAh(push, mt->base.address + sf->offset);
66167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, mt->base.address + sf->offset);
66267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, nvc0_format_table[dst->format].rt);
66367635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode);
66467635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, mt->layer_stride >> 2);
66567635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
66667635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, 1);
66767635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NVC0(push, NVC0_3D(ZETA_HORIZ), 3);
66867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, sf->width);
66967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, sf->height);
67067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, (unk << 16) | (dst->u.tex.first_layer + sf->depth));
67167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NVC0(push, NVC0_3D(ZETA_BASE_LAYER), 1);
67267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   PUSH_DATA (push, dst->u.tex.first_layer);
673c4182bb9b0897b4a4ac4f06b54fc7f6a2ddeb105Ilia Mirkin   IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), mt->ms_mode);
67467635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
675163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin   if (!render_condition_enabled)
676163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin      IMMED_NVC0(push, NVC0_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
677ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin
67867635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   BEGIN_NIC0(push, NVC0_3D(CLEAR_BUFFERS), sf->depth);
67967635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   for (z = 0; z < sf->depth; ++z) {
68067635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin      PUSH_DATA (push, mode |
68167635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin                 (z << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
68267635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin   }
68367635a0a713e54939f1f72ba8db2b3099988a925Ilia Mirkin
684163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin   if (!render_condition_enabled)
685163a029eba9aa73e3c0b624a3067218a01a3e47bIlia Mirkin      IMMED_NVC0(push, NVC0_3D(COND_MODE), nvc0->cond_condmode);
686ff17b3ccf4f8d9f989cc975cd0e11716ff48bc1dIlia Mirkin
6879c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset   nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
6884c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
6894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6904c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillervoid
6914c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_clear(struct pipe_context *pipe, unsigned buffers,
6926dd284f7c8fac22f64c13fdf9909094f5ec59086Dave Airlie           const union pipe_color_union *color,
6936dd284f7c8fac22f64c13fdf9909094f5ec59086Dave Airlie           double depth, unsigned stencil)
6944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
6954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   struct nvc0_context *nvc0 = nvc0_context(pipe);
6966d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
6974c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
698250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin   unsigned i, j, k;
6994c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   uint32_t mode = 0;
7004c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7014c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
702db9b41d3020452c71728995f39f5fc0bad2f3b1dSamuel Pitoiset   if (!nvc0_state_validate_3d(nvc0, NVC0_NEW_3D_FRAMEBUFFER))
7034c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return;
7044c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7054c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
7066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(CLEAR_COLOR(0)), 4);
7076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAf(push, color->f[0]);
7086d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAf(push, color->f[1]);
7096d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAf(push, color->f[2]);
7106d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAf(push, color->f[3]);
711e05de038bf651220b529c386d88039636aacd410Ilia Mirkin      if (buffers & PIPE_CLEAR_COLOR0)
712e05de038bf651220b529c386d88039636aacd410Ilia Mirkin         mode =
713e05de038bf651220b529c386d88039636aacd410Ilia Mirkin            NVC0_3D_CLEAR_BUFFERS_R | NVC0_3D_CLEAR_BUFFERS_G |
714e05de038bf651220b529c386d88039636aacd410Ilia Mirkin            NVC0_3D_CLEAR_BUFFERS_B | NVC0_3D_CLEAR_BUFFERS_A;
7154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
7164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7174c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (buffers & PIPE_CLEAR_DEPTH) {
7186d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(CLEAR_DEPTH), 1);
7196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, fui(depth));
7204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      mode |= NVC0_3D_CLEAR_BUFFERS_Z;
7214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
7224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (buffers & PIPE_CLEAR_STENCIL) {
7246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(CLEAR_STENCIL), 1);
7256d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, stencil & 0xff);
7264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      mode |= NVC0_3D_CLEAR_BUFFERS_S;
7274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
7284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
729e05de038bf651220b529c386d88039636aacd410Ilia Mirkin   if (mode) {
730250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      int zs_layers = 0, color0_layers = 0;
731250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      if (fb->cbufs[0] && (mode & 0x3c))
732250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         color0_layers = fb->cbufs[0]->u.tex.last_layer -
733250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin            fb->cbufs[0]->u.tex.first_layer + 1;
734250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      if (fb->zsbuf && (mode & ~0x3c))
735250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         zs_layers = fb->zsbuf->u.tex.last_layer -
736250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin            fb->zsbuf->u.tex.first_layer + 1;
737250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin
738250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      for (j = 0; j < MIN2(zs_layers, color0_layers); j++) {
739250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         BEGIN_NVC0(push, NVC0_3D(CLEAR_BUFFERS), 1);
740250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         PUSH_DATA(push, mode | (j << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
741250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      }
742250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      for (k = j; k < zs_layers; k++) {
743250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         BEGIN_NVC0(push, NVC0_3D(CLEAR_BUFFERS), 1);
744250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         PUSH_DATA(push, (mode & ~0x3c) | (k << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
745250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      }
746250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      for (k = j; k < color0_layers; k++) {
747250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         BEGIN_NVC0(push, NVC0_3D(CLEAR_BUFFERS), 1);
748250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         PUSH_DATA(push, (mode & 0x3c) | (k << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
749250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      }
750e05de038bf651220b529c386d88039636aacd410Ilia Mirkin   }
7514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   for (i = 1; i < fb->nr_cbufs; i++) {
753250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      struct pipe_surface *sf = fb->cbufs[i];
754250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      if (!sf || !(buffers & (PIPE_CLEAR_COLOR0 << i)))
755250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         continue;
756250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin      for (j = 0; j <= sf->u.tex.last_layer - sf->u.tex.first_layer; j++) {
757e05de038bf651220b529c386d88039636aacd410Ilia Mirkin         BEGIN_NVC0(push, NVC0_3D(CLEAR_BUFFERS), 1);
758250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin         PUSH_DATA (push, (i << 6) | 0x3c |
759250e7c835e6330cc792cbbb8ebff8b40daea724bIlia Mirkin                    (j << NVC0_3D_CLEAR_BUFFERS_LAYER__SHIFT));
760e05de038bf651220b529c386d88039636aacd410Ilia Mirkin      }
7614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
7624c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
7634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7647744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
76536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller/* =============================== BLIT CODE ===================================
76636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller */
76736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
76836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillerstruct nvc0_blitter
7697744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
77036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_program *fp[NV50_BLIT_MAX_TEXTURE_TYPES][NV50_BLIT_MODES];
77136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_program vp;
77236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
77336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nv50_tsc_entry sampler[2]; /* nearest, bilinear */
77436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
77536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   pipe_mutex mutex;
77636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
7777744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   struct nvc0_screen *screen;
77836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller};
77936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
78036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillerstruct nvc0_blitctx
78136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller{
78236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0;
78336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_program *fp;
78436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint8_t mode;
78536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint16_t color_mask;
78636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint8_t filter;
7874467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   uint8_t render_condition_enable;
78836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   enum pipe_texture_target target;
7897744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   struct {
7907744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct pipe_framebuffer_state fb;
791b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin      struct nvc0_window_rect_stateobj window_rect;
7928aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller      struct nvc0_rasterizer_stateobj *rast;
7937744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct nvc0_program *vp;
7947744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct nvc0_program *tcp;
7957744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct nvc0_program *tep;
7967744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct nvc0_program *gp;
7977744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      struct nvc0_program *fp;
7987744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      unsigned num_textures[5];
7997744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      unsigned num_samplers[5];
800b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      struct pipe_sampler_view *texture[2];
801b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      struct nv50_tsc_entry *sampler[2];
802af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin      unsigned min_samples;
8035330ed959e8e047495191ebe1a72dd54347cc004Samuel Pitoiset      uint32_t dirty_3d;
8047744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   } saved;
8058aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller   struct nvc0_rasterizer_stateobj rast;
8067744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller};
8077744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8087744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
80936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitter_make_vp(struct nvc0_blitter *blit)
8107744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
81136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   static const uint32_t code_nvc0[] =
81236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   {
813443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0xfff11c26, 0x06000080, /* vfetch b64 $r4:$r5 a[0x80] */
814443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0xfff01c46, 0x06000090, /* vfetch b96 $r0:$r1:$r2 a[0x90] */
815443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0x13f01c26, 0x0a7e0070, /* export b64 o[0x70] $r4:$r5 */
816443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0x03f01c46, 0x0a7e0080, /* export b96 o[0x80] $r0:$r1:$r2 */
81736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      0x00001de7, 0x80000000, /* exit */
81836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   };
81936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   static const uint32_t code_nve4[] =
8207744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   {
82136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      0x00000007, 0x20000000, /* sched */
822443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0xfff11c26, 0x06000080, /* vfetch b64 $r4:$r5 a[0x80] */
823443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0xfff01c46, 0x06000090, /* vfetch b96 $r0:$r1:$r2 a[0x90] */
824443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0x13f01c26, 0x0a7e0070, /* export b64 o[0x70] $r4:$r5 */
825443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller      0x03f01c46, 0x0a7e0080, /* export b96 o[0x80] $r0:$r1:$r2 */
8267744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      0x00001de7, 0x80000000, /* exit */
8277744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   };
828b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin   static const uint32_t code_gk110[] =
829b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin   {
830b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x00000000, 0x08000000, /* sched */
831b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x401ffc12, 0x7ec7fc00, /* ld b64 $r4d a[0x80] 0x0 0x0 */
832b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x481ffc02, 0x7ecbfc00, /* ld b96 $r0t a[0x90] 0x0 0x0 */
833b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x381ffc12, 0x7f07fc00, /* st b64 a[0x70] $r4d 0x0 0x0 */
834b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x401ffc02, 0x7f0bfc00, /* st b96 a[0x80] $r0t 0x0 0x0 */
835b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      0x001c003c, 0x18000000, /* exit */
836b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin   };
8379c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs   static const uint32_t code_gm107[] =
8389c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs   {
83975e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0xe4200701, 0x001d0400, /* sched (st 0x1 wr 0x0) (st 0x1 wr 0x1) (st 0x1 wr 0x2) */
84075e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0807ff00, 0xefd87f80, /* ld b32 $r0 a[0x80] 0x0 */
84175e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0847ff01, 0xefd87f80, /* ld b32 $r1 a[0x84] 0x0 */
84275e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0907ff02, 0xefd87f80, /* ld b32 $r2 a[0x90] 0x0 */
84375e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0xf0200761, 0x003f8400, /* sched (st 0x1 wr 0x3) (st 0x1 wr 0x4) (st 0x1 wt 0x1) */
84475e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0947ff03, 0xefd87f80, /* ld b32 $r3 a[0x94] 0x0 */
84575e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0987ff04, 0xefd87f80, /* ld b32 $r4 a[0x98] 0x0 */
84675e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0707ff00, 0xeff07f80, /* st b32 a[0x70] $r0 0x0 */
84775e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0xfc2017e1, 0x011f8404, /* sched (st 0x1 wt 0x2) (st 0x1 wt 0x4) (st 0x1 wt 0x8) */
84875e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0747ff01, 0xeff07f80, /* st b32 a[0x74] $r1 0x0 */
84975e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0807ff02, 0xeff07f80, /* st b32 a[0x80] $r2 0x0 */
85075e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0847ff03, 0xeff07f80, /* st b32 a[0x84] $r3 0x0 */
85175e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0xfde087e1, 0x001f8000, /* sched (st 0x1 wt 0x10) (st 0xf) (st 0x0) */
85275e6992379b239c618931925122a5d724d45e385Samuel Pitoiset      0x0887ff04, 0xeff07f80, /* st b32 a[0x88] $r4 0x0 */
8539c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs      0x0007000f, 0xe3000000, /* exit */
8549c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs   };
8557744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8567744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->vp.type = PIPE_SHADER_VERTEX;
857cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   blit->vp.translated = true;
8589c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs   if (blit->screen->base.class_3d >= GM107_3D_CLASS) {
8599c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs      blit->vp.code = (uint32_t *)code_gm107; /* const_cast */
8609c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs      blit->vp.code_size = sizeof(code_gm107);
8619c64cb80d29bb779a99f5a32b0974e8f92e8c902Ben Skeggs   } else
862b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin   if (blit->screen->base.class_3d >= NVF0_3D_CLASS) {
863b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      blit->vp.code = (uint32_t *)code_gk110; /* const_cast */
864b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin      blit->vp.code_size = sizeof(code_gk110);
865b9ec766bd02d55c20ff7ce5b95207ea7f6ed1de5Ilia Mirkin   } else
86636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (blit->screen->base.class_3d >= NVE4_3D_CLASS) {
86736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      blit->vp.code = (uint32_t *)code_nve4; /* const_cast */
86836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      blit->vp.code_size = sizeof(code_nve4);
86936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   } else {
87036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      blit->vp.code = (uint32_t *)code_nvc0; /* const_cast */
87136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      blit->vp.code_size = sizeof(code_nvc0);
87236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
873443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   blit->vp.num_gprs = 6;
8747744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->vp.vp.edgeflag = PIPE_MAX_ATTRIBS;
8757744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8767744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->vp.hdr[0]  = 0x00020461; /* vertprog magic */
8777744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->vp.hdr[4]  = 0x000ff000; /* no outputs read */
878443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   blit->vp.hdr[6]  = 0x00000073; /* a[0x80].xy, a[0x90].xyz */
879443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   blit->vp.hdr[13] = 0x00073000; /* o[0x70].xy, o[0x80].xyz */
8807744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
8817744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8827744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
88336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitter_make_sampler(struct nvc0_blitter *blit)
8847744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
8857744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* clamp to edge, min/max lod = 0, nearest filtering */
8867744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8877744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->sampler[0].id = -1;
8887744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
889c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs   blit->sampler[0].tsc[0] = G80_TSC_0_SRGB_CONVERSION |
890c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_U__SHIFT) |
891c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_V__SHIFT) |
892c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_P__SHIFT);
8938592933de82f7742f411cb2f2c339ff7d42266daChristoph Bumiller   blit->sampler[0].tsc[1] =
894c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MAG_FILTER_NEAREST |
895c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MIN_FILTER_NEAREST |
896c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MIP_FILTER_NONE;
8977744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
8987744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* clamp to edge, min/max lod = 0, bilinear filtering */
8997744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9007744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   blit->sampler[1].id = -1;
9017744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9028592933de82f7742f411cb2f2c339ff7d42266daChristoph Bumiller   blit->sampler[1].tsc[0] = blit->sampler[0].tsc[0];
9038592933de82f7742f411cb2f2c339ff7d42266daChristoph Bumiller   blit->sampler[1].tsc[1] =
904c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MAG_FILTER_LINEAR |
905c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MIN_FILTER_LINEAR |
906c999736c186ff7ef89f22547bd12eabff8f0dc62Ben Skeggs      G80_TSC_1_MIP_FILTER_NONE;
9077744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
9087744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9097744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
91036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit_select_fp(struct nvc0_blitctx *ctx, const struct pipe_blit_info *info)
9117744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
91236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_blitter *blitter = ctx->nvc0->screen->blitter;
91336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
91436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const enum pipe_texture_target ptarg =
915b9c8a98e216f4613dc0a40e26d08f2c1cb760e76Christoph Bumiller      nv50_blit_reinterpret_pipe_texture_target(info->src.resource->target);
91636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
91736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const unsigned targ = nv50_blit_texture_type(ptarg);
91836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const unsigned mode = ctx->mode;
91936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
92036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (!blitter->fp[targ][mode]) {
92136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      pipe_mutex_lock(blitter->mutex);
92236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (!blitter->fp[targ][mode])
92336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         blitter->fp[targ][mode] =
924b9c8a98e216f4613dc0a40e26d08f2c1cb760e76Christoph Bumiller            nv50_blitter_make_fp(&ctx->nvc0->base.pipe, mode, ptarg);
92536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      pipe_mutex_unlock(blitter->mutex);
9267744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   }
92736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->fp = blitter->fp[targ][mode];
9287744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
9297744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9307744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
93136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit_set_dst(struct nvc0_blitctx *ctx,
93236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                  struct pipe_resource *res, unsigned level, unsigned layer,
93336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                  enum pipe_format format)
9347744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
93536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0 = ctx->nvc0;
9366286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   struct pipe_context *pipe = &nvc0->base.pipe;
9376286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   struct pipe_surface templ;
9386286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller
93936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (util_format_is_depth_or_stencil(format))
94036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.format = nv50_blit_zeta_to_colour_format(format);
9416286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   else
94236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.format = format;
9437744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9446286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   templ.u.tex.level = level;
9456286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
9467744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
94736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (layer == -1) {
94836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.u.tex.first_layer = 0;
94936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.u.tex.last_layer =
95036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
95136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
95236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
9536286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   nvc0->framebuffer.cbufs[0] = nvc0_miptree_surface_new(pipe, res, &templ);
9547744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.nr_cbufs = 1;
9557744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.zsbuf = NULL;
9566286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   nvc0->framebuffer.width = nvc0->framebuffer.cbufs[0]->width;
9576286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   nvc0->framebuffer.height = nvc0->framebuffer.cbufs[0]->height;
9587744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
9597744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9607744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
96136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit_set_src(struct nvc0_blitctx *ctx,
962b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller                  struct pipe_resource *res, unsigned level, unsigned layer,
96336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                  enum pipe_format format, const uint8_t filter)
9647744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
96536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0 = ctx->nvc0;
9667744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   struct pipe_context *pipe = &nvc0->base.pipe;
9677744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   struct pipe_sampler_view templ;
96836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint32_t flags;
96936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   unsigned s;
97036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   enum pipe_texture_target target;
9717744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
97236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   target = nv50_blit_reinterpret_pipe_texture_target(res->target);
97336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
97436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   templ.format = format;
9757744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
9767744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   templ.u.tex.first_level = templ.u.tex.last_level = level;
977fb523cb6ad3ffef22ab4b9cce9e53859c17c5739Marek Olšák   templ.swizzle_r = PIPE_SWIZZLE_X;
978fb523cb6ad3ffef22ab4b9cce9e53859c17c5739Marek Olšák   templ.swizzle_g = PIPE_SWIZZLE_Y;
979fb523cb6ad3ffef22ab4b9cce9e53859c17c5739Marek Olšák   templ.swizzle_b = PIPE_SWIZZLE_Z;
980fb523cb6ad3ffef22ab4b9cce9e53859c17c5739Marek Olšák   templ.swizzle_a = PIPE_SWIZZLE_W;
9817744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
98236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (layer == -1) {
98336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.u.tex.first_layer = 0;
98436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      templ.u.tex.last_layer =
98536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
98636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
9877744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9882149ce41ed6b10f7bff65d7b3f23fd03b89753e3Christoph Bumiller   flags = res->last_level ? 0 : NV50_TEXVIEW_SCALED_COORDS;
9894da54c91d24da891c56957f29274e7821c8254f6Christoph Bumiller   flags |= NV50_TEXVIEW_ACCESS_RESOLVE;
99036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (filter && res->nr_samples == 8)
99136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      flags |= NV50_TEXVIEW_FILTER_MSAA8;
99236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
99336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->textures[4][0] = nvc0_create_texture_view(
99436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      pipe, res, &templ, flags, target);
99536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->textures[4][1] = NULL;
9967744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
9977744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   for (s = 0; s <= 3; ++s)
9987744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      nvc0->num_textures[s] = 0;
9997744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->num_textures[4] = 1;
1000b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller
100136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   templ.format = nv50_zs_to_s_format(format);
100236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (templ.format != format) {
100336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      nvc0->textures[4][1] = nvc0_create_texture_view(
100436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         pipe, res, &templ, flags, target);
1005b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      nvc0->num_textures[4] = 2;
1006b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   }
10077744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
10087744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10097744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
10107744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillernvc0_blitctx_prepare_state(struct nvc0_blitctx *blit)
10117744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
101236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nouveau_pushbuf *push = blit->nvc0->base.pushbuf;
10137744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10147744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* TODO: maybe make this a MACRO (if we need more logic) ? */
10157744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10164467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   if (blit->nvc0->cond_query && !blit->render_condition_enable)
101736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      IMMED_NVC0(push, NVC0_3D(COND_MODE), NVC0_3D_COND_MODE_ALWAYS);
101836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
10197744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* blend state */
10206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(COLOR_MASK(0)), 1);
10216d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, blit->color_mask);
102236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   IMMED_NVC0(push, NVC0_3D(BLEND_ENABLE(0)), 0);
10236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(LOGIC_OP_ENABLE), 0);
10247744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10257744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* rasterizer state */
102636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   IMMED_NVC0(push, NVC0_3D(FRAG_COLOR_CLAMP_EN), 0);
10276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_ENABLE), 0);
10286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(MSAA_MASK(0)), 4);
10296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0xffff);
10306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0xffff);
10316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0xffff);
10326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0xffff);
10337a40ae4a31f3ab526706fdfda7631d54f094512aChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(MACRO_POLYGON_MODE_FRONT), 1);
10347a40ae4a31f3ab526706fdfda7631d54f094512aChristoph Bumiller   PUSH_DATA (push, NVC0_3D_MACRO_POLYGON_MODE_FRONT_FILL);
10357a40ae4a31f3ab526706fdfda7631d54f094512aChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(MACRO_POLYGON_MODE_BACK), 1);
10367a40ae4a31f3ab526706fdfda7631d54f094512aChristoph Bumiller   PUSH_DATA (push, NVC0_3D_MACRO_POLYGON_MODE_BACK_FILL);
10376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(POLYGON_SMOOTH_ENABLE), 0);
10386d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(POLYGON_OFFSET_FILL_ENABLE), 0);
10396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(POLYGON_STIPPLE_ENABLE), 0);
10406d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(CULL_FACE_ENABLE), 0);
10417744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10427744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* zsa state */
10436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(DEPTH_TEST_ENABLE), 0);
1044abbf05cfc2bea0787bcf710ef984d73ee8ba8f9eIlia Mirkin   IMMED_NVC0(push, NVC0_3D(DEPTH_BOUNDS_EN), 0);
10456d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(STENCIL_ENABLE), 0);
10466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(ALPHA_TEST_ENABLE), 0);
10477744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
104814bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   /* disable transform feedback */
10496d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(TFB_ENABLE), 0);
10507744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
10517744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10527744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
1053b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkinnvc0_blitctx_pre_blit(struct nvc0_blitctx *ctx,
1054b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin                      const struct pipe_blit_info *info)
10557744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
105636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0 = ctx->nvc0;
105736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_blitter *blitter = nvc0->screen->blitter;
10587744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   int s;
10597744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
106036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fb.width = nvc0->framebuffer.width;
106136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fb.height = nvc0->framebuffer.height;
1062cdb6fa91fa79c0c9a796730e9ecae320bebdb825Ilia Mirkin   ctx->saved.fb.samples = nvc0->framebuffer.samples;
1063cdb6fa91fa79c0c9a796730e9ecae320bebdb825Ilia Mirkin   ctx->saved.fb.layers = nvc0->framebuffer.layers;
106436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fb.nr_cbufs = nvc0->framebuffer.nr_cbufs;
106536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fb.cbufs[0] = nvc0->framebuffer.cbufs[0];
106636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fb.zsbuf = nvc0->framebuffer.zsbuf;
10677744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
10688aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller   ctx->saved.rast = nvc0->rast;
10698aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller
107036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.vp = nvc0->vertprog;
107136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.tcp = nvc0->tctlprog;
107236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.tep = nvc0->tevlprog;
107336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.gp = nvc0->gmtyprog;
107436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.fp = nvc0->fragprog;
10757744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1076af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin   ctx->saved.min_samples = nvc0->min_samples;
1077b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   ctx->saved.window_rect = nvc0->window_rect;
1078af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin
10798aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller   nvc0->rast = &ctx->rast;
10808aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller
108136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->vertprog = &blitter->vp;
10827744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->tctlprog = NULL;
10837744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->tevlprog = NULL;
10847744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->gmtyprog = NULL;
108536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->fragprog = ctx->fp;
10867744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1087b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   nvc0->window_rect.rects =
1088b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin      MIN2(info->num_window_rectangles, NVC0_MAX_WINDOW_RECTANGLES);
1089b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   nvc0->window_rect.inclusive = info->window_rectangle_include;
1090b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   if (nvc0->window_rect.rects)
1091b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin      memcpy(nvc0->window_rect.rect, info->window_rectangles,
1092b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin             sizeof(struct pipe_scissor_state) * nvc0->window_rect.rects);
1093b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin
10947744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   for (s = 0; s <= 4; ++s) {
109536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      ctx->saved.num_textures[s] = nvc0->num_textures[s];
109636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      ctx->saved.num_samplers[s] = nvc0->num_samplers[s];
1097b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      nvc0->textures_dirty[s] = (1 << nvc0->num_textures[s]) - 1;
1098b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      nvc0->samplers_dirty[s] = (1 << nvc0->num_samplers[s]) - 1;
10997744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   }
110036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.texture[0] = nvc0->textures[4][0];
110136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.texture[1] = nvc0->textures[4][1];
110236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.sampler[0] = nvc0->samplers[4][0];
110336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   ctx->saved.sampler[1] = nvc0->samplers[4][1];
11047744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
110536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->samplers[4][0] = &blitter->sampler[ctx->filter];
110636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->samplers[4][1] = &blitter->sampler[ctx->filter];
11077744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11087744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   for (s = 0; s <= 3; ++s)
11097744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      nvc0->num_samplers[s] = 0;
1110b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->num_samplers[4] = 2;
11117744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1112af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin   nvc0->min_samples = 1;
1113af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin
11145330ed959e8e047495191ebe1a72dd54347cc004Samuel Pitoiset   ctx->saved.dirty_3d = nvc0->dirty_3d;
11157744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1116b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->textures_dirty[4] |= 3;
1117b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->samplers_dirty[4] |= 3;
1118b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller
11192999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
11202999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(4, 0));
11212999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(4, 1));
112236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
11239c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset   nvc0->dirty_3d = NVC0_NEW_3D_FRAMEBUFFER | NVC0_NEW_3D_MIN_SAMPLES |
11249c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset      NVC0_NEW_3D_VERTPROG | NVC0_NEW_3D_FRAGPROG |
11259c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset      NVC0_NEW_3D_TCTLPROG | NVC0_NEW_3D_TEVLPROG | NVC0_NEW_3D_GMTYPROG |
1126b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin      NVC0_NEW_3D_TEXTURES | NVC0_NEW_3D_SAMPLERS | NVC0_NEW_3D_WINDOW_RECTS;
11277744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
11287744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11297744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
113036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitctx_post_blit(struct nvc0_blitctx *blit)
11317744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
113236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0 = blit->nvc0;
11337744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   int s;
11347744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11356286d9810b7ebae588060370cd7a63c327478a2fChristoph Bumiller   pipe_surface_reference(&nvc0->framebuffer.cbufs[0], NULL);
11367744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11377744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.width = blit->saved.fb.width;
11387744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.height = blit->saved.fb.height;
1139cdb6fa91fa79c0c9a796730e9ecae320bebdb825Ilia Mirkin   nvc0->framebuffer.samples = blit->saved.fb.samples;
1140cdb6fa91fa79c0c9a796730e9ecae320bebdb825Ilia Mirkin   nvc0->framebuffer.layers = blit->saved.fb.layers;
11417744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.nr_cbufs = blit->saved.fb.nr_cbufs;
11427744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.cbufs[0] = blit->saved.fb.cbufs[0];
11437744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->framebuffer.zsbuf = blit->saved.fb.zsbuf;
11447744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11458aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller   nvc0->rast = blit->saved.rast;
11468aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller
11477744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->vertprog = blit->saved.vp;
11487744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->tctlprog = blit->saved.tcp;
11497744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->tevlprog = blit->saved.tep;
11507744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->gmtyprog = blit->saved.gp;
11517744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0->fragprog = blit->saved.fp;
11527744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1153af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin   nvc0->min_samples = blit->saved.min_samples;
1154b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   nvc0->window_rect = blit->saved.window_rect;
1155af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin
11567744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   pipe_sampler_view_reference(&nvc0->textures[4][0], NULL);
1157b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   pipe_sampler_view_reference(&nvc0->textures[4][1], NULL);
11587744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11597744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   for (s = 0; s <= 4; ++s) {
11607744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      nvc0->num_textures[s] = blit->saved.num_textures[s];
11617744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller      nvc0->num_samplers[s] = blit->saved.num_samplers[s];
1162b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      nvc0->textures_dirty[s] = (1 << nvc0->num_textures[s]) - 1;
1163b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller      nvc0->samplers_dirty[s] = (1 << nvc0->num_samplers[s]) - 1;
11647744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   }
1165b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->textures[4][0] = blit->saved.texture[0];
1166b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->textures[4][1] = blit->saved.texture[1];
1167b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->samplers[4][0] = blit->saved.sampler[0];
1168b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->samplers[4][1] = blit->saved.sampler[1];
1169b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller
1170b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->textures_dirty[4] |= 3;
1171b328949a37fee7b0f68ed3e068ffc4426c083042Christoph Bumiller   nvc0->samplers_dirty[4] |= 3;
11727744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11734467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   if (nvc0->cond_query && !blit->render_condition_enable)
117436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      nvc0->base.pipe.render_condition(&nvc0->base.pipe, nvc0->cond_query,
1175793e8e3d7ed816cc9a066245dde798afdcf8b581Roland Scheidegger                                       nvc0->cond_cond, nvc0->cond_mode);
117636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
11772999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_VTX_TMP);
11782999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
11792999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(4, 0));
11802999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(4, 1));
1181109c348284843054f708f4403260739b7db18275Ilia Mirkin   nouveau_scratch_done(&nvc0->base);
118236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
11835330ed959e8e047495191ebe1a72dd54347cc004Samuel Pitoiset   nvc0->dirty_3d = blit->saved.dirty_3d |
11849c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset      (NVC0_NEW_3D_FRAMEBUFFER | NVC0_NEW_3D_SCISSOR | NVC0_NEW_3D_SAMPLE_MASK |
11859c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset       NVC0_NEW_3D_RASTERIZER | NVC0_NEW_3D_ZSA | NVC0_NEW_3D_BLEND |
1186b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin       NVC0_NEW_3D_VIEWPORT | NVC0_NEW_3D_WINDOW_RECTS |
11879c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset       NVC0_NEW_3D_TEXTURES | NVC0_NEW_3D_SAMPLERS |
11889c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset       NVC0_NEW_3D_VERTPROG | NVC0_NEW_3D_FRAGPROG |
11899c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset       NVC0_NEW_3D_TCTLPROG | NVC0_NEW_3D_TEVLPROG | NVC0_NEW_3D_GMTYPROG |
11909c6a7bfb4060264f02830d6ca5e56511546e7078Samuel Pitoiset       NVC0_NEW_3D_TFB_TARGETS | NVC0_NEW_3D_VERTEX | NVC0_NEW_3D_ARRAYS);
1191a2cb3a4a4fa5f22e983ac6081b22a04594c7a10aTobias Klausmann   nvc0->scissors_dirty |= 1;
11927e7097a4f41caf311c98caae0ff7ebb1c5bc77caIlia Mirkin   nvc0->viewports_dirty |= 1;
1193af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin
1194af38ef907c89ecb1125bf258cafa0793f79a5eb7Ilia Mirkin   nvc0->base.pipe.set_min_samples(&nvc0->base.pipe, blit->saved.min_samples);
11957744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
11967744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
11977744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumillerstatic void
119836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit_3d(struct nvc0_context *nvc0, const struct pipe_blit_info *info)
11997744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
120036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_blitctx *blit = nvc0->blit;
120136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
120236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct pipe_resource *src = info->src.resource;
120336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct pipe_resource *dst = info->dst.resource;
1204c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   struct nouveau_bo *vtxbuf_bo;
1205c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   uint32_t stride, length, *vbuf;
1206c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   uint64_t vtxbuf;
120736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   int32_t minx, maxx, miny, maxy;
1208c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   int32_t i, n;
120936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   float x0, x1, y0, y1, z;
121036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   float dz;
12117744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   float x_range, y_range;
12127744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
121336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   blit->mode = nv50_blit_select_mode(info);
121436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   blit->color_mask = nv50_blit_derive_color_mask(info);
121536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   blit->filter = nv50_blit_get_filter(info);
12164467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   blit->render_condition_enable = info->render_condition_enable;
12177744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
121836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0_blit_select_fp(blit, info);
1219b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   nvc0_blitctx_pre_blit(blit, info);
12207744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1221443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   nvc0_blit_set_dst(blit, dst, info->dst.level, -1, info->dst.format);
122236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0_blit_set_src(blit, src, info->src.level, -1, info->src.format,
122336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller                     blit->filter);
12247744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
12257744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   nvc0_blitctx_prepare_state(blit);
12267744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1227db9b41d3020452c71728995f39f5fc0bad2f3b1dSamuel Pitoiset   nvc0_state_validate_3d(nvc0, ~0);
12287744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
122936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   x_range = (float)info->src.box.width / (float)info->dst.box.width;
123036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   y_range = (float)info->src.box.height / (float)info->dst.box.height;
12317744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
123236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   x0 = (float)info->src.box.x - x_range * (float)info->dst.box.x;
123336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   y0 = (float)info->src.box.y - y_range * (float)info->dst.box.y;
12347744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1235a651bc027d5ed4150bb5240fc9f46a6ca569f665Ilia Mirkin   x1 = x0 + 32768.0f * x_range;
1236a651bc027d5ed4150bb5240fc9f46a6ca569f665Ilia Mirkin   y1 = y0 + 32768.0f * y_range;
12377744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
12387744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   x0 *= (float)(1 << nv50_miptree(src)->ms_x);
12397744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   x1 *= (float)(1 << nv50_miptree(src)->ms_x);
12407744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   y0 *= (float)(1 << nv50_miptree(src)->ms_y);
12417744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   y1 *= (float)(1 << nv50_miptree(src)->ms_y);
12427744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
124328360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin   dz = (float)info->src.box.depth / (float)info->dst.box.depth;
124428360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin   z = (float)info->src.box.z;
124528360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin   if (nv50_miptree(src)->layout_3d)
124628360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin      z += 0.5f * dz;
124728360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin
124836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (src->last_level > 0) {
124936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      /* If there are mip maps, GPU always assumes normalized coordinates. */
125036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      const unsigned l = info->src.level;
125136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      const float fh = u_minify(src->width0 << nv50_miptree(src)->ms_x, l);
125236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      const float fv = u_minify(src->height0 << nv50_miptree(src)->ms_y, l);
125336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      x0 /= fh;
125436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      x1 /= fh;
125536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      y0 /= fv;
125636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      y1 /= fv;
125728360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin      if (nv50_miptree(src)->layout_3d) {
125828360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin         z /= u_minify(src->depth0, l);
125928360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin         dz /= u_minify(src->depth0, l);
126028360fcad75a6917db6af42fb17b81572850ec0dIlia Mirkin      }
126136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
126236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
12636d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   IMMED_NVC0(push, NVC0_3D(VIEWPORT_TRANSFORM_EN), 0);
12646417d56c1921c311a7b59001e0d37822a0ed1a65Christoph Bumiller   IMMED_NVC0(push, NVC0_3D(VIEW_VOLUME_CLIP_CTRL), 0x2 |
12656417d56c1921c311a7b59001e0d37822a0ed1a65Christoph Bumiller              NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_RANGE_0_1);
12662149ce41ed6b10f7bff65d7b3f23fd03b89753e3Christoph Bumiller   BEGIN_NVC0(push, NVC0_3D(VIEWPORT_HORIZ(0)), 2);
12672149ce41ed6b10f7bff65d7b3f23fd03b89753e3Christoph Bumiller   PUSH_DATA (push, nvc0->framebuffer.width << 16);
12682149ce41ed6b10f7bff65d7b3f23fd03b89753e3Christoph Bumiller   PUSH_DATA (push, nvc0->framebuffer.height << 16);
12697744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
12707744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   /* Draw a large triangle in screen coordinates covering the whole
12717744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller    * render target, with scissors defining the destination region.
12727744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller    * The vertex is supplied with non-normalized texture coordinates
12737744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller    * arranged in a way to yield the desired offset and scale.
12747744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller    */
12757744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
127636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   minx = info->dst.box.x;
127736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   maxx = info->dst.box.x + info->dst.box.width;
127836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   miny = info->dst.box.y;
127936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   maxy = info->dst.box.y + info->dst.box.height;
128036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->scissor_enable) {
128136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      minx = MAX2(minx, info->scissor.minx);
128236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      maxx = MIN2(maxx, info->scissor.maxx);
128336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      miny = MAX2(miny, info->scissor.miny);
128436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      maxy = MIN2(maxy, info->scissor.maxy);
128536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
12866d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(SCISSOR_HORIZ(0)), 2);
128736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, (maxx << 16) | minx);
128836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, (maxy << 16) | miny);
128936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1290c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   stride = (3 + 2) * 4;
1291c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   length = stride * 3 * info->dst.box.depth;
1292c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
1293c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   vbuf = nouveau_scratch_get(&nvc0->base, length, &vtxbuf, &vtxbuf_bo);
1294c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   if (!vbuf) {
1295c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      assert(vbuf);
1296c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      return;
1297c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   }
1298c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
12992999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset   BCTX_REFN_bo(nvc0->bufctx_3d, 3D_VTX_TMP,
13002999257e0fe703f73d32620fed88040d29ac5bacSamuel Pitoiset                NOUVEAU_BO_GART | NOUVEAU_BO_RD, vtxbuf_bo);
1301c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   nouveau_pushbuf_validate(push);
1302c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
1303c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(0)), 4);
1304c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE | stride <<
1305c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                    NVC0_3D_VERTEX_ARRAY_FETCH_STRIDE__SHIFT);
1306c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATAh(push, vtxbuf);
1307c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, vtxbuf);
1308c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, 0);
1309c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(0)), 2);
1310c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATAh(push, vtxbuf + length - 1);
1311c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, vtxbuf + length - 1);
1312c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
1313c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   n = MAX2(2, nvc0->state.num_vtxelts);
1314c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
1315c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   BEGIN_NVC0(push, NVC0_3D(VERTEX_ATTRIB_FORMAT(0)), n);
1316c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT |
1317c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                    NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32_32 | 0x00 <<
1318c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                    NVC0_3D_VERTEX_ATTRIB_FORMAT_OFFSET__SHIFT);
1319c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   PUSH_DATA (push, NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT |
1320c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                    NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32_32_32 | 0x08 <<
1321c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                    NVC0_3D_VERTEX_ATTRIB_FORMAT_OFFSET__SHIFT);
1322c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   for (i = 2; i < n; i++) {
1323c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      PUSH_DATA(push, NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT |
1324c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                      NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32 |
1325c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                      NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST);
1326c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   }
1327b9f1affb2e52a7a75d968af117a03b01866546cbIlia Mirkin   for (i = 1; i < n; ++i)
1328b9f1affb2e52a7a75d968af117a03b01866546cbIlia Mirkin      IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 0);
13297892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin   if (nvc0->state.instance_elts) {
13307892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin      nvc0->state.instance_elts = 0;
13317892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin      BEGIN_NVC0(push, NVC0_3D(MACRO_VERTEX_ARRAY_PER_INSTANCE), 2);
13327892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin      PUSH_DATA (push, n);
13337892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin      PUSH_DATA (push, 0);
13347892210400e8f3bd14697c0a3dd56e98454a45dfIlia Mirkin   }
1335c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs   nvc0->state.num_vtxelts = 2;
1336c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
133741100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin   if (nvc0->state.prim_restart) {
133841100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(PRIM_RESTART_ENABLE), 0);
133941100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin      nvc0->state.prim_restart = 0;
134041100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin   }
134141100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin
134241100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin   if (nvc0->state.index_bias) {
134341100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(VB_ELEMENT_BASE), 0);
134441100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(VERTEX_ID_BASE), 0);
134541100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin      nvc0->state.index_bias = 0;
134641100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin   }
134741100b6b44e747b9003937f123fce571fd3dec46Ilia Mirkin
134836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   for (i = 0; i < info->dst.box.depth; ++i, z += dz) {
134936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (info->dst.box.z + i) {
135036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         BEGIN_NVC0(push, NVC0_3D(LAYER), 1);
135136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, info->dst.box.z + i);
135236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      }
135336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1354c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(0.0f);
1355c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(0.0f);
1356c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(x0);
1357c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(y0);
1358c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(z);
135936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1360a651bc027d5ed4150bb5240fc9f46a6ca569f665Ilia Mirkin      *(vbuf++) = fui(32768 << nv50_miptree(dst)->ms_x);
1361c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(0.0f);
1362c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(x1);
1363c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(y0);
1364c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(z);
136536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1366c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(0.0f);
1367a651bc027d5ed4150bb5240fc9f46a6ca569f665Ilia Mirkin      *(vbuf++) = fui(32768 << nv50_miptree(dst)->ms_y);
1368c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(x0);
1369c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(y1);
1370c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      *(vbuf++) = fui(z);
1371c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs
1372c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      IMMED_NVC0(push, NVC0_3D(VERTEX_BEGIN_GL),
1373c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs                       NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_TRIANGLES);
1374c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      BEGIN_NVC0(push, NVC0_3D(VERTEX_BUFFER_FIRST), 2);
1375c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      PUSH_DATA (push, i * 3);
1376c42d7556d3343c41977a2852d0c9028fd01a4c86Ben Skeggs      PUSH_DATA (push, 3);
137736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_END_GL), 0);
137836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
137936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->dst.box.z + info->dst.box.depth - 1)
138036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      IMMED_NVC0(push, NVC0_3D(LAYER), 0);
13817744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1382443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   nvc0_blitctx_post_blit(blit);
13837744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
13847e7097a4f41caf311c98caae0ff7ebb1c5bc77caIlia Mirkin   /* restore viewport transform */
1385443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   IMMED_NVC0(push, NVC0_3D(VIEWPORT_TRANSFORM_EN), 1);
138636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller}
138736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
138836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillerstatic void
138936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit_eng2d(struct nvc0_context *nvc0, const struct pipe_blit_info *info)
139036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller{
139136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
139236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nv50_miptree *dst = nv50_miptree(info->dst.resource);
139336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nv50_miptree *src = nv50_miptree(info->src.resource);
139436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const int32_t srcx_adj = info->src.box.width < 0 ? -1 : 0;
139536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const int32_t srcy_adj = info->src.box.height < 0 ? -1 : 0;
139636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const int dz = info->dst.box.z;
139736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   const int sz = info->src.box.z;
139836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint32_t dstw, dsth;
139936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   int32_t dstx, dsty;
140036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   int64_t srcx, srcy;
140136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   int64_t du_dx, dv_dy;
140236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   int i;
140336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   uint32_t mode;
1404d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   uint32_t mask = nv50_blit_eng2d_get_mask(info);
1405cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   bool b;
140636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
140736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   mode = nv50_blit_get_filter(info) ?
140872283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      NV50_2D_BLIT_CONTROL_FILTER_BILINEAR :
140972283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      NV50_2D_BLIT_CONTROL_FILTER_POINT_SAMPLE;
141036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   mode |= (src->base.base.nr_samples > dst->base.base.nr_samples) ?
141172283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      NV50_2D_BLIT_CONTROL_ORIGIN_CORNER : NV50_2D_BLIT_CONTROL_ORIGIN_CENTER;
141236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
141336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   du_dx = ((int64_t)info->src.box.width << 32) / info->dst.box.width;
141436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   dv_dy = ((int64_t)info->src.box.height << 32) / info->dst.box.height;
141536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1416d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   b = info->dst.format == info->src.format;
1417d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   nvc0_2d_texture_set(push, 1, dst, info->dst.level, dz, info->dst.format, b);
1418d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   nvc0_2d_texture_set(push, 0, src, info->src.level, sz, info->src.format, b);
141936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
142036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->scissor_enable) {
142136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      BEGIN_NVC0(push, NVC0_2D(CLIP_X), 5);
142236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, info->scissor.minx << dst->ms_x);
142336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, info->scissor.miny << dst->ms_y);
142436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, (info->scissor.maxx - info->scissor.minx) << dst->ms_x);
142536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, (info->scissor.maxy - info->scissor.miny) << dst->ms_y);
142636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, 1); /* enable */
142736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
142836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
14294467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   if (nvc0->cond_query && info->render_condition_enable)
1430a2fc85f5d0ffe4e46234bebcf24c5d24c5754b44Tobias Klausmann      IMMED_NVC0(push, NVC0_2D(COND_MODE), nvc0->cond_condmode);
14314467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin
143236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (mask != 0xffffffff) {
1433351d3c59f2a1153047d45fcdb23cc487f231683dChristoph Bumiller      IMMED_NVC0(push, NVC0_2D(ROP), 0xca); /* DPSDxax */
143436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      IMMED_NVC0(push, NVC0_2D(PATTERN_COLOR_FORMAT),
143572283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin                       NV50_2D_PATTERN_COLOR_FORMAT_A8R8G8B8);
143672283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      BEGIN_NVC0(push, NVC0_2D(PATTERN_BITMAP_COLOR(0)), 4);
143736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, 0x00000000);
143836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, mask);
143936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, 0xffffffff);
144036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      PUSH_DATA (push, 0xffffffff);
144172283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      IMMED_NVC0(push, NVC0_2D(OPERATION), NV50_2D_OPERATION_ROP);
1442d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   } else
1443d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller   if (info->src.format != info->dst.format) {
1444d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      if (info->src.format == PIPE_FORMAT_R8_UNORM ||
1445d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller          info->src.format == PIPE_FORMAT_R8_SNORM ||
1446d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller          info->src.format == PIPE_FORMAT_R16_UNORM ||
1447d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller          info->src.format == PIPE_FORMAT_R16_SNORM ||
1448d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller          info->src.format == PIPE_FORMAT_R16_FLOAT ||
1449d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller          info->src.format == PIPE_FORMAT_R32_FLOAT) {
1450d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         mask = 0xffff0000; /* also makes condition for OPERATION reset true */
1451d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         BEGIN_NVC0(push, NVC0_2D(BETA4), 2);
1452d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         PUSH_DATA (push, mask);
145372283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin         PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY_PREMULT);
1454d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      } else
1455d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      if (info->src.format == PIPE_FORMAT_A8_UNORM) {
1456d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         mask = 0xff000000;
1457d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         BEGIN_NVC0(push, NVC0_2D(BETA4), 2);
1458d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         PUSH_DATA (push, mask);
145972283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin         PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY_PREMULT);
1460d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      }
146136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
146236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
146336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (src->ms_x > dst->ms_x || src->ms_y > dst->ms_y) {
146436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      /* ms_x is always >= ms_y */
146536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      du_dx <<= src->ms_x - dst->ms_x;
146636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dv_dy <<= src->ms_y - dst->ms_y;
146736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   } else {
146836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      du_dx >>= dst->ms_x - src->ms_x;
146936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dv_dy >>= dst->ms_y - src->ms_y;
147036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
147136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
147236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   srcx = (int64_t)(info->src.box.x + srcx_adj) << (src->ms_x + 32);
147336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   srcy = (int64_t)(info->src.box.y + srcy_adj) << (src->ms_y + 32);
147436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
147536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (src->base.base.nr_samples > dst->base.base.nr_samples) {
147636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      /* center src coorinates for proper MS resolve filtering */
147747c19a5819444728b8662da4a0dbd5ca9af4698aIlia Mirkin      srcx += (int64_t)1 << (src->ms_x + 31);
147847c19a5819444728b8662da4a0dbd5ca9af4698aIlia Mirkin      srcy += (int64_t)1 << (src->ms_y + 31);
147936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
148036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
148136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   dstx = info->dst.box.x << dst->ms_x;
148236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   dsty = info->dst.box.y << dst->ms_y;
148336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
148436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   dstw = info->dst.box.width << dst->ms_x;
148536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   dsth = info->dst.box.height << dst->ms_y;
148636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
148736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (dstx < 0) {
148836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dstw += dstx;
148936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      srcx -= du_dx * dstx;
149036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dstx = 0;
149136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
149236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (dsty < 0) {
149336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dsth += dsty;
149436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      srcy -= dv_dy * dsty;
149536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      dsty = 0;
149636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
149736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
149836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   IMMED_NVC0(push, NVC0_2D(BLIT_CONTROL), mode);
149936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   BEGIN_NVC0(push, NVC0_2D(BLIT_DST_X), 4);
150036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dstx);
150136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dsty);
150236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dstw);
150336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dsth);
150436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   BEGIN_NVC0(push, NVC0_2D(BLIT_DU_DX_FRACT), 4);
150536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, du_dx);
150636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, du_dx >> 32);
150736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dv_dy);
150836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   PUSH_DATA (push, dv_dy >> 32);
150936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
151036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   BCTX_REFN(nvc0->bufctx, 2D, &dst->base, WR);
151136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   BCTX_REFN(nvc0->bufctx, 2D, &src->base, RD);
151236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nouveau_pushbuf_bufctx(nvc0->base.pushbuf, nvc0->bufctx);
151336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (nouveau_pushbuf_validate(nvc0->base.pushbuf))
151436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      return;
151536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
151636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   for (i = 0; i < info->dst.box.depth; ++i) {
151736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (i > 0) {
151836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         /* no scaling in z-direction possible for eng2d blits */
151936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         if (dst->layout_3d) {
152036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            BEGIN_NVC0(push, NVC0_2D(DST_LAYER), 1);
152136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            PUSH_DATA (push, info->dst.box.z + i);
152236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         } else {
152336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            const unsigned z = info->dst.box.z + i;
15242514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            const uint64_t address = dst->base.address +
15252514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin               dst->level[info->dst.level].offset +
15262514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin               z * dst->layer_stride;
152736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            BEGIN_NVC0(push, NVC0_2D(DST_ADDRESS_HIGH), 2);
15282514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            PUSH_DATAh(push, address);
15292514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            PUSH_DATA (push, address);
153036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         }
153136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         if (src->layout_3d) {
153236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            /* not possible because of depth tiling */
153336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            assert(0);
153436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         } else {
153536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            const unsigned z = info->src.box.z + i;
15362514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            const uint64_t address = src->base.address +
15372514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin               src->level[info->src.level].offset +
15382514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin               z * src->layer_stride;
153936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            BEGIN_NVC0(push, NVC0_2D(SRC_ADDRESS_HIGH), 2);
15402514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            PUSH_DATAh(push, address);
15412514c78fba507ca8ab94d2e6de553b8b20d653d2Ilia Mirkin            PUSH_DATA (push, address);
154236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         }
154336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         BEGIN_NVC0(push, NVC0_2D(BLIT_SRC_Y_INT), 1); /* trigger */
154436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, srcy >> 32);
154536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      } else {
154636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         BEGIN_NVC0(push, NVC0_2D(BLIT_SRC_X_FRACT), 4);
154736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, srcx);
154836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, srcx >> 32);
154936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, srcy);
155036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         PUSH_DATA (push, srcy >> 32);
155136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      }
155236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
15538e1dd58a7e76834c1fb6d117b6ea5755e05cfb77Christoph Bumiller   nvc0_resource_validate(&dst->base, NOUVEAU_BO_WR);
15548e1dd58a7e76834c1fb6d117b6ea5755e05cfb77Christoph Bumiller   nvc0_resource_validate(&src->base, NOUVEAU_BO_RD);
155536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
155636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nouveau_bufctx_reset(nvc0->bufctx, NVC0_BIND_2D);
155736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
155836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->scissor_enable)
155936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      IMMED_NVC0(push, NVC0_2D(CLIP_ENABLE), 0);
156036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (mask != 0xffffffff)
156172283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      IMMED_NVC0(push, NVC0_2D(OPERATION), NV50_2D_OPERATION_SRCCOPY);
15624467c0c9fbf2c13b6c73a002e8247448ee12d4c4Ilia Mirkin   if (nvc0->cond_query && info->render_condition_enable)
156372283020093525ca5248d7f408e88b0bcba1e52bIlia Mirkin      IMMED_NVC0(push, NVC0_2D(COND_MODE), NV50_2D_COND_MODE_ALWAYS);
15647744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
156536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
156636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillerstatic void
156736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blit(struct pipe_context *pipe, const struct pipe_blit_info *info)
156836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller{
156936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_context *nvc0 = nvc0_context(pipe);
157038c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
1571cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   bool eng3d = false;
157236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
157336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (util_format_is_depth_or_stencil(info->dst.resource->format)) {
157436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (!(info->mask & PIPE_MASK_ZS))
157536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         return;
157636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT ||
157736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller          info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
1578cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset         eng3d = true;
157936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (info->filter != PIPE_TEX_FILTER_NEAREST)
1580cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset         eng3d = true;
158136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   } else {
158236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (!(info->mask & PIPE_MASK_RGBA))
158336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         return;
158436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      if (info->mask != PIPE_MASK_RGBA)
1585cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset         eng3d = true;
158636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
158736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
158836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (nv50_miptree(info->src.resource)->layout_3d) {
1589cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      eng3d = true;
159036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   } else
159136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->src.box.depth != info->dst.box.depth) {
1592cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      eng3d = true;
159336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      debug_printf("blit: cannot filter array or cube textures in z direction");
159436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
159536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1596443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   if (!eng3d && info->dst.format != info->src.format) {
1597d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      if (!nv50_2d_dst_format_faithful(info->dst.format)) {
1598cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset         eng3d = true;
1599d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      } else
1600d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      if (!nv50_2d_src_format_faithful(info->src.format)) {
1601d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         if (!util_format_is_luminance(info->src.format)) {
160268e4f3f572b1e629f32a73ae618dc0b016f631b1Ilia Mirkin            if (!nv50_2d_dst_format_ops_supported(info->dst.format))
1603cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset               eng3d = true;
160468e4f3f572b1e629f32a73ae618dc0b016f631b1Ilia Mirkin            else
1605d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller            if (util_format_is_intensity(info->src.format))
1606d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller               eng3d = info->src.format != PIPE_FORMAT_I8_UNORM;
1607d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller            else
160868e4f3f572b1e629f32a73ae618dc0b016f631b1Ilia Mirkin            if (util_format_is_alpha(info->src.format))
160968e4f3f572b1e629f32a73ae618dc0b016f631b1Ilia Mirkin               eng3d = info->src.format != PIPE_FORMAT_A8_UNORM;
1610d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller            else
1611d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller               eng3d = !nv50_2d_format_supported(info->src.format);
1612d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller         }
1613d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      } else
1614d801545964f8183bb17ed913b7c19482dad43b6cChristoph Bumiller      if (util_format_is_luminance_alpha(info->src.format))
1615cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset         eng3d = true;
1616443b247878edd6a67adc073b0c36e2941436b9a0Christoph Bumiller   }
161736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
161836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->src.resource->nr_samples == 8 &&
161936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller       info->dst.resource->nr_samples <= 1)
1620cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      eng3d = true;
162136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller#if 0
162236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   /* FIXME: can't make this work with eng2d anymore, at least not on nv50 */
162336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (info->src.resource->nr_samples > 1 ||
162436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller       info->dst.resource->nr_samples > 1)
1625cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      eng3d = true;
1626de80660c2bd43db112b6c82d970660ed9806cd33Marek Olšák#endif
162736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   /* FIXME: find correct src coordinates adjustments */
162836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if ((info->src.box.width !=  info->dst.box.width &&
162936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller        info->src.box.width != -info->dst.box.width) ||
163036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller       (info->src.box.height !=  info->dst.box.height &&
163136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller        info->src.box.height != -info->dst.box.height))
1632cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      eng3d = true;
163336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
1634b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin   if (info->num_window_rectangles > 0 || info->window_rectangle_include)
1635b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin      eng3d = true;
1636b21a00d1295d721275946e045e92920acdcb1a85Ilia Mirkin
163738c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin   if (nvc0->screen->num_occlusion_queries_active)
163838c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(SAMPLECNT_ENABLE), 0);
163938c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin
164036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (!eng3d)
164136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      nvc0_blit_eng2d(nvc0, info);
164236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   else
164336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      nvc0_blit_3d(nvc0, info);
1644198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller
164538c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin   if (nvc0->screen->num_occlusion_queries_active)
164638c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin      IMMED_NVC0(push, NVC0_3D(SAMPLECNT_ENABLE), 1);
164738c2ec5ff0bf626578db7b84387279342aa48844Ilia Mirkin
1648198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller   NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_blit_count, 1);
164936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller}
16507744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1651419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšákstatic void
1652419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšáknvc0_flush_resource(struct pipe_context *ctx,
1653419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšák                    struct pipe_resource *resource)
1654419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšák{
1655419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšák}
1656419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšák
1657cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetbool
165836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitter_create(struct nvc0_screen *screen)
16597744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller{
166036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   screen->blitter = CALLOC_STRUCT(nvc0_blitter);
166136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (!screen->blitter) {
166236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      NOUVEAU_ERR("failed to allocate blitter struct\n");
1663cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
16647744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller   }
166536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   screen->blitter->screen = screen;
16667744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
166736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   pipe_mutex_init(screen->blitter->mutex);
16687744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
166936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0_blitter_make_vp(screen->blitter);
167036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0_blitter_make_sampler(screen->blitter);
16717744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1672cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   return true;
167336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller}
16747744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
167536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillervoid
167636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitter_destroy(struct nvc0_screen *screen)
167736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller{
167836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   struct nvc0_blitter *blitter = screen->blitter;
167936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   unsigned i, m;
168036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
168136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   for (i = 0; i < NV50_BLIT_MAX_TEXTURE_TYPES; ++i) {
168236ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      for (m = 0; m < NV50_BLIT_MODES; ++m) {
168336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         struct nvc0_program *prog = blitter->fp[i][m];
168436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         if (prog) {
168536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            nvc0_program_destroy(NULL, prog);
168636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            FREE((void *)prog->pipe.tokens);
168736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller            FREE(prog);
168836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller         }
168936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      }
169036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
16917744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1692752769e0531bc9b15e95a2bb2250dc0970b1a421Samuel Pitoiset   pipe_mutex_destroy(blitter->mutex);
169336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   FREE(blitter);
16947744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller}
16957744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
1696cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetbool
169736ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumillernvc0_blitctx_create(struct nvc0_context *nvc0)
169836ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller{
169936ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->blit = CALLOC_STRUCT(nvc0_blitctx);
170036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   if (!nvc0->blit) {
170136ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller      NOUVEAU_ERR("failed to allocate blit context\n");
1702cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
170336ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   }
170436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
170536ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   nvc0->blit->nvc0 = nvc0;
170636ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller
17072737abb44efebfa10ac84b183c20fc5818d1514eJosé Fonseca   nvc0->blit->rast.pipe.half_pixel_center = 1;
17088aa8b0539eda00b9bafacb69774b26975db66ae4Christoph Bumiller
1709cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   return true;
171036ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller}
17117744e867b7f45f3f9d763b61d7219dc28ca39c45Christoph Bumiller
17124c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillervoid
17132a7762bdb62faa1d54c445fcec7733cf6f690ac5Joakim Sindholtnvc0_blitctx_destroy(struct nvc0_context *nvc0)
17142a7762bdb62faa1d54c445fcec7733cf6f690ac5Joakim Sindholt{
17159019e5e19532fe214fc6e45b9ee1f60bbe332456Matt Turner   FREE(nvc0->blit);
17162a7762bdb62faa1d54c445fcec7733cf6f690ac5Joakim Sindholt}
17172a7762bdb62faa1d54c445fcec7733cf6f690ac5Joakim Sindholt
17182a7762bdb62faa1d54c445fcec7733cf6f690ac5Joakim Sindholtvoid
17194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_init_surface_functions(struct nvc0_context *nvc0)
17204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
17211ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   struct pipe_context *pipe = &nvc0->base.pipe;
17221ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs
17231ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   pipe->resource_copy_region = nvc0_resource_copy_region;
172436ea744f58e5b436c45ed857bd92bbb472e641d5Christoph Bumiller   pipe->blit = nvc0_blit;
1725419cd5f2a24b87d31a2fd9dd9b7d0025b4f5515cMarek Olšák   pipe->flush_resource = nvc0_flush_resource;
17261ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   pipe->clear_render_target = nvc0_clear_render_target;
17271ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   pipe->clear_depth_stencil = nvc0_clear_depth_stencil;
1728c4182bb9b0897b4a4ac4f06b54fc7f6a2ddeb105Ilia Mirkin   pipe->clear_texture = nv50_clear_texture;
1729a26e2bc2e3578b50bd581c8f8d8e3c428c85158fTobias Klausmann   pipe->clear_buffer = nvc0_clear_buffer;
17304c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1731