12c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry/*
22c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * Copyright © 2012 Intel Corporation
32c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry *
42c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * Permission is hereby granted, free of charge, to any person obtaining a
52c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * copy of this software and associated documentation files (the "Software"),
62c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * to deal in the Software without restriction, including without limitation
72c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * and/or sell copies of the Software, and to permit persons to whom the
92c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * Software is furnished to do so, subject to the following conditions:
102c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry *
112c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * The above copyright notice and this permission notice (including the next
122c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * paragraph) shall be included in all copies or substantial portions of the
132c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * Software.
142c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry *
152c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
212c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry * IN THE SOFTWARE.
222c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry */
232c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
242c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry#include "intel_fbo.h"
252c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
262c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry#include "brw_blorp.h"
272c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry#include "brw_defines.h"
282c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry#include "gen6_blorp.h"
292c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry#include "gen7_blorp.h"
302c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
312c5510b71b6348b686e76ecc2c34195080d566f4Paul Berrybrw_blorp_mip_info::brw_blorp_mip_info()
322c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   : mt(NULL),
33602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry     width(0),
34127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry     height(0),
35127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry     x_offset(0),
36127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry     y_offset(0)
372c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry{
382c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry}
392c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
40506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_surface_info::brw_blorp_surface_info()
4119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   : map_stencil_as_y_tiled(false),
4219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry     num_samples(0)
43506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
44506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
45506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
462c5510b71b6348b686e76ecc2c34195080d566f4Paul Berryvoid
472c5510b71b6348b686e76ecc2c34195080d566f4Paul Berrybrw_blorp_mip_info::set(struct intel_mipmap_tree *mt,
482c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry                        unsigned int level, unsigned int layer)
492c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry{
502c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   intel_miptree_check_level_layer(mt, level, layer);
512c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
522c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   this->mt = mt;
53602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   this->width = mt->level[level].width;
54602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   this->height = mt->level[level].height;
55127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry
56127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   /* Construct a dummy renderbuffer just to extract tile offsets. */
57127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   struct intel_renderbuffer rb;
58127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   rb.mt = mt;
59127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   rb.mt_level = level;
60127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   rb.mt_layer = layer;
61127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   intel_renderbuffer_set_draw_offset(&rb);
62127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   x_offset = rb.draw_x;
63127dc6d136db64fcf9448d66cb4c86db3bb11226Paul Berry   y_offset = rb.draw_y;
642c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry}
652c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
662c5510b71b6348b686e76ecc2c34195080d566f4Paul Berryvoid
67530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berrybrw_blorp_surface_info::set(struct brw_context *brw,
68530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry                            struct intel_mipmap_tree *mt,
69506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                            unsigned int level, unsigned int layer)
70506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
71506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_mip_info::set(mt, level, layer);
72233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   this->num_samples = mt->num_samples;
73455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   this->array_spacing_lod0 = mt->array_spacing_lod0;
74530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   this->map_stencil_as_y_tiled = false;
751bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   this->msaa_layout = mt->msaa_layout;
76506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
77530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   switch (mt->format) {
78530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   case MESA_FORMAT_S8:
79506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* The miptree is a W-tiled stencil buffer.  Surface states can't be set
80506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * up for W tiling, so we'll need to use Y tiling and have the WM
81233c207e9e477b6b0a5c6705e727129b92989073Paul Berry       * program swizzle the coordinates.
82506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
83506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->map_stencil_as_y_tiled = true;
84040d0157341381708c35c2f27721ebffa2ee1db2Paul Berry      this->brw_surfaceformat = BRW_SURFACEFORMAT_R8_UNORM;
85530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      break;
86530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   case MESA_FORMAT_X8_Z24:
87530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   case MESA_FORMAT_Z32_FLOAT:
88530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      /* The miptree consists of 32 bits per pixel, arranged either as 24-bit
89530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * depth values interleaved with 8 "don't care" bits, or as 32-bit
90530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * floating point depth values.  Since depth values don't require any
91530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * blending, it doesn't matter how we interpret the bit pattern as long
92530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * as we copy the right amount of data, so just map it as 8-bit BGRA.
93530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       */
94040d0157341381708c35c2f27721ebffa2ee1db2Paul Berry      this->brw_surfaceformat = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
95530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      break;
96530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   case MESA_FORMAT_Z16:
97530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      /* The miptree consists of 16 bits per pixel of depth data.  Since depth
98530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * values don't require any blending, it doesn't matter how we interpret
99530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * the bit pattern as long as we copy the right amount of data, so just
100530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       * map is as 8-bit RG.
101530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       */
102530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      this->brw_surfaceformat = BRW_SURFACEFORMAT_R8G8_UNORM;
103530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      break;
104530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   default:
1058c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke      /* Blorp blits don't support any sort of format conversion (except
1068c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke       * between sRGB and linear), so we can safely assume that the format is
1078c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke       * supported as a render target, even if this is the source image.  So
1088c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke       * we can convert to a surface format using brw->render_target_format.
109530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry       */
110530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      assert(brw->format_supported_as_render_target[mt->format]);
111530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      this->brw_surfaceformat = brw->render_target_format[mt->format];
112530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry      break;
113506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
114506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
115506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
116239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
117239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry/**
118239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry * Split x_offset and y_offset into a base offset (in bytes) and a remaining
119239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry * x/y offset (in pixels).  Note: we can't do this by calling
120239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry * intel_renderbuffer_tile_offsets(), because the offsets may have been
121239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry * adjusted to account for Y vs. W tiling differences.  So we compute it
122239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry * directly from the adjusted offsets.
123239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry */
124239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berryuint32_t
125239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berrybrw_blorp_surface_info::compute_tile_offsets(uint32_t *tile_x,
126239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry                                             uint32_t *tile_y) const
127239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry{
128239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry   struct intel_region *region = mt->region;
129239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry   uint32_t mask_x, mask_y;
130239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
13196fd94ba9421c7c3072988f999ee869534f2bc2aPaul Berry   intel_region_get_tile_masks(region, &mask_x, &mask_y,
13296fd94ba9421c7c3072988f999ee869534f2bc2aPaul Berry                               map_stencil_as_y_tiled);
133239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
134239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry   *tile_x = x_offset & mask_x;
135239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry   *tile_y = y_offset & mask_y;
136239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
137239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry   return intel_region_get_aligned_offset(region, x_offset & ~mask_x,
13868da5dfc2c2e9c0aca47431076be0cd43406d4aaPaul Berry                                          y_offset & ~mask_y,
13968da5dfc2c2e9c0aca47431076be0cd43406d4aaPaul Berry                                          map_stencil_as_y_tiled);
140239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry}
141239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
142239e9bef92bd4602bc30e05177fa85a6e5b69fe0Paul Berry
1432c5510b71b6348b686e76ecc2c34195080d566f4Paul Berrybrw_blorp_params::brw_blorp_params()
1442c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   : x0(0),
1452c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry     y0(0),
1462c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry     x1(0),
1472c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry     y1(0),
1482c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry     depth_format(0),
149506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     hiz_op(GEN6_HIZ_OP_NONE),
15019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry     num_samples(0),
151506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     use_wm_prog(false)
1522c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry{
1532c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry}
1542c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
1555b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholtextern "C" {
1565b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholtvoid
1575b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholtintel_hiz_exec(struct intel_context *intel, struct intel_mipmap_tree *mt,
1585b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt	       unsigned int level, unsigned int layer, gen6_hiz_op op)
1595b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt{
1605b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt   brw_hiz_op_params params(mt, level, layer, op);
1615b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt   brw_blorp_exec(intel, &params);
1625b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt}
1635b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt
1645b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt} /* extern "C" */
1655b226ad603302554f38e6b12a93bd2cf443d4b56Eric Anholt
1662c5510b71b6348b686e76ecc2c34195080d566f4Paul Berryvoid
1676335e0b0738a6e466f0b712e30ad9fe506f67a6cPaul Berrybrw_blorp_exec(struct intel_context *intel, const brw_blorp_params *params)
1682c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry{
1692c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   switch (intel->gen) {
1702c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   case 6:
1716335e0b0738a6e466f0b712e30ad9fe506f67a6cPaul Berry      gen6_blorp_exec(intel, params);
1722c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry      break;
1732c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   case 7:
1746335e0b0738a6e466f0b712e30ad9fe506f67a6cPaul Berry      gen7_blorp_exec(intel, params);
1752c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry      break;
1762c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   default:
1772c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry      /* BLORP is not supported before Gen6. */
1782c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry      assert(false);
1792c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry      break;
1802c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   }
1812c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry}
1822c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
1832c5510b71b6348b686e76ecc2c34195080d566f4Paul Berrybrw_hiz_op_params::brw_hiz_op_params(struct intel_mipmap_tree *mt,
1842c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry                                     unsigned int level,
1852c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry                                     unsigned int layer,
1862c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry                                     gen6_hiz_op op)
1872c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry{
1882c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   this->hiz_op = op;
1892c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
1902c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   depth.set(mt, level, layer);
191602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   x1 = depth.width;
192602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   y1 = depth.height;
1932c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
1942c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   assert(mt->hiz_mt != NULL);
1952c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry
1962c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   switch (mt->format) {
1972c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   case MESA_FORMAT_Z16:       depth_format = BRW_DEPTHFORMAT_D16_UNORM; break;
1982c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   case MESA_FORMAT_Z32_FLOAT: depth_format = BRW_DEPTHFORMAT_D32_FLOAT; break;
1992c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   case MESA_FORMAT_X8_Z24:    depth_format = BRW_DEPTHFORMAT_D24_UNORM_X8_UINT; break;
2002c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   default:                    assert(0); break;
2012c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry   }
2022c5510b71b6348b686e76ecc2c34195080d566f4Paul Berry}
203506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
204506d70be21cd3469118de89297cba0c0f709c1aePaul Berryuint32_t
205506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_hiz_op_params::get_wm_prog(struct brw_context *brw,
206506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                               brw_blorp_prog_data **prog_data) const
207506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
208506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return 0;
209506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
210