1506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/*
2506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Copyright © 2012 Intel Corporation
3506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Permission is hereby granted, free of charge, to any person obtaining a
5506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * copy of this software and associated documentation files (the "Software"),
6506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * to deal in the Software without restriction, including without limitation
7506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * and/or sell copies of the Software, and to permit persons to whom the
9506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Software is furnished to do so, subject to the following conditions:
10506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
11506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * The above copyright notice and this permission notice (including the next
12506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * paragraph) shall be included in all copies or substantial portions of the
13506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Software.
14506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
15506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * IN THE SOFTWARE.
22506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
23506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
24506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "main/teximage.h"
25c738ea1191cd1b5a0dc60b0e6d05fd918083e961Paul Berry#include "main/fbobject.h"
26506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
27506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "glsl/ralloc.h"
28506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
29506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "intel_fbo.h"
30506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
31506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_blorp.h"
32506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_context.h"
33506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_eu.h"
34506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_state.h"
35506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
36506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
37506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
38506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Helper function for handling mirror image blits.
39506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
40506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * If coord0 > coord1, swap them and invert the "mirror" boolean.
41506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
42506d70be21cd3469118de89297cba0c0f709c1aePaul Berrystatic inline void
43506d70be21cd3469118de89297cba0c0f709c1aePaul Berryfixup_mirroring(bool &mirror, GLint &coord0, GLint &coord1)
44506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
45506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (coord0 > coord1) {
46506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mirror = !mirror;
47506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GLint tmp = coord0;
48506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      coord0 = coord1;
49506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      coord1 = tmp;
50506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
51506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
52506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
53506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
5447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry/**
5547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * Adjust {src,dst}_x{0,1} to account for clipping and scissoring of
5647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * destination coordinates.
5747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry *
5847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * Return true if there is still blitting to do, false if all pixels got
5947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * rejected by the clip and/or scissor.
6047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry *
6147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * For clarity, the nomenclature of this function assumes we are clipping and
6247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * scissoring the X coordinate; the exact same logic applies for Y
6347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * coordinates.
6475f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry *
6575f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry * Note: this function may also be used to account for clipping of source
6675f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry * coordinates, by swapping the roles of src and dst.
6747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry */
6847b64c9290d54f78e5a20e378593977cd47e285fPaul Berrystatic inline bool
6947b64c9290d54f78e5a20e378593977cd47e285fPaul Berryclip_or_scissor(bool mirror, GLint &src_x0, GLint &src_x1, GLint &dst_x0,
7047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                GLint &dst_x1, GLint fb_xmin, GLint fb_xmax)
7147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry{
7247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If we are going to scissor everything away, stop. */
7347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (!(fb_xmin < fb_xmax &&
7447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         dst_x0 < fb_xmax &&
7547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         fb_xmin < dst_x1 &&
7647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         dst_x0 < dst_x1)) {
7747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      return false;
7847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
7947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
8047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* Clip the destination rectangle, and keep track of how many pixels we
8147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * clipped off of the left and right sides of it.
8247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
8347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   GLint pixels_clipped_left = 0;
8447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   GLint pixels_clipped_right = 0;
8547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (dst_x0 < fb_xmin) {
8647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_left = fb_xmin - dst_x0;
8747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      dst_x0 = fb_xmin;
8847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
8947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (fb_xmax < dst_x1) {
9047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_right = dst_x1 - fb_xmax;
9147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      dst_x1 = fb_xmax;
9247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
9347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
9447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If we are mirrored, then before applying pixels_clipped_{left,right} to
9547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * the source coordinates, we need to flip them to account for the
9647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * mirroring.
9747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
9847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (mirror) {
9947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      GLint tmp = pixels_clipped_left;
10047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_left = pixels_clipped_right;
10147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_right = tmp;
10247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
10347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
10447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* Adjust the source rectangle to remove the pixels corresponding to those
10547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * that were clipped/scissored out of the destination rectangle.
10647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
10747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   src_x0 += pixels_clipped_left;
10847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   src_x1 -= pixels_clipped_right;
10947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
11047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   return true;
11147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry}
11247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
11347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
114fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrystatic struct intel_mipmap_tree *
1155c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berryfind_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
116fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
117fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct intel_mipmap_tree *mt = irb->mt;
118fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
119fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      mt = mt->stencil_mt;
120fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   return mt;
121fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
122fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
1236cc9df331b4799715b31d7ec606ad09fa914e260Chad Versacevoid
1246cc9df331b4799715b31d7ec606ad09fa914e260Chad Versacebrw_blorp_blit_miptrees(struct intel_context *intel,
1256cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        struct intel_mipmap_tree *src_mt,
126e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                        unsigned src_level, unsigned src_layer,
1276cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        struct intel_mipmap_tree *dst_mt,
128e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                        unsigned dst_level, unsigned dst_layer,
1296cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        int src_x0, int src_y0,
1306cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        int dst_x0, int dst_y0,
1316cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        int dst_x1, int dst_y1,
1326cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                        bool mirror_x, bool mirror_y)
1336cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace{
1346cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace   brw_blorp_blit_params params(brw_context(&intel->ctx),
135e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                                src_mt, src_level, src_layer,
136e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                                dst_mt, dst_level, dst_layer,
1376cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                                src_x0, src_y0,
1386cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                                dst_x0, dst_y0,
1396cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                                dst_x1, dst_y1,
1406cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                                mirror_x, mirror_y);
1416cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace   brw_blorp_exec(intel, &params);
1426cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace}
143fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
144fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrystatic void
145fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrydo_blorp_blit(struct intel_context *intel, GLbitfield buffer_bit,
1465c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry              struct intel_renderbuffer *src_irb,
1475c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry              struct intel_renderbuffer *dst_irb,
148fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              GLint srcX0, GLint srcY0,
149fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
150fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              bool mirror_x, bool mirror_y)
151fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
152fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Find source/dst miptrees */
1535c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
1545c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
155fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
156fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Get ready to blit.  This includes depth resolving the src and dst
157fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * buffers if necessary.
158fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    */
1595c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   intel_renderbuffer_resolve_depth(intel, src_irb);
1605c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   intel_renderbuffer_resolve_depth(intel, dst_irb);
161fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
162fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Do the blit */
163e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry   brw_blorp_blit_miptrees(intel,
164e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                           src_mt, src_irb->mt_level, src_irb->mt_layer,
165e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                           dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
1666cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                           srcX0, srcY0, dstX0, dstY0, dstX1, dstY1,
1676cc9df331b4799715b31d7ec606ad09fa914e260Chad Versace                           mirror_x, mirror_y);
168fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
1695c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   intel_renderbuffer_set_needs_hiz_resolve(dst_irb);
1705c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   intel_renderbuffer_set_needs_downsample(dst_irb);
171fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
172fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
173fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
174fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrystatic bool
1755c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berryformats_match(GLbitfield buffer_bit, struct intel_renderbuffer *src_irb,
1765c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry              struct intel_renderbuffer *dst_irb)
177fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
178fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Note: don't just check gl_renderbuffer::Format, because in some cases
179fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * multiple gl_formats resolve to the same native type in the miptree (for
180fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * example MESA_FORMAT_X8_Z24 and MESA_FORMAT_S8_Z24), and we can blit
181fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * between those formats.
182fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    */
1838c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   gl_format src_format = find_miptree(buffer_bit, src_irb)->format;
1848c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   gl_format dst_format = find_miptree(buffer_bit, dst_irb)->format;
1858c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke
1868c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   return _mesa_get_srgb_format_linear(src_format) ==
1878c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke          _mesa_get_srgb_format_linear(dst_format);
188fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
189fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
190fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
191506d70be21cd3469118de89297cba0c0f709c1aePaul Berrystatic bool
192506d70be21cd3469118de89297cba0c0f709c1aePaul Berrytry_blorp_blit(struct intel_context *intel,
193506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
194506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
195506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLenum filter, GLbitfield buffer_bit)
196506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
197506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct gl_context *ctx = &intel->ctx;
198506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
199506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Sync up the state of window system buffers.  We need to do this before
200506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * we go looking for the buffers.
201506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
202506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_prepare_render(intel);
203506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
204506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *read_fb = ctx->ReadBuffer;
205506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *draw_fb = ctx->DrawBuffer;
206506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
207506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Detect if the blit needs to be mirrored */
208506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool mirror_x = false, mirror_y = false;
209506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, srcX0, srcX1);
210506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, dstX0, dstX1);
211506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, srcY0, srcY1);
212506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, dstY0, dstY1);
213506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
214506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Make sure width and height match */
215da54d2e576426122009be083ecbfb9eefd8a3799Paul Berry   if (srcX1 - srcX0 != dstX1 - dstX0) return false;
216da54d2e576426122009be083ecbfb9eefd8a3799Paul Berry   if (srcY1 - srcY0 != dstY1 - dstY0) return false;
217506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
21847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If the destination rectangle needs to be clipped or scissored, do so.
219506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
22047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (!(clip_or_scissor(mirror_x, srcX0, srcX1, dstX0, dstX1,
22147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Xmin, draw_fb->_Xmax) &&
22247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         clip_or_scissor(mirror_y, srcY0, srcY1, dstY0, dstY1,
22347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Ymin, draw_fb->_Ymax))) {
22447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
22547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      return true;
22647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
22747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
22875f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   /* If the source rectangle needs to be clipped or scissored, do so. */
22975f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   if (!(clip_or_scissor(mirror_x, dstX0, dstX1, srcX0, srcX1,
23075f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Width) &&
23175f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry         clip_or_scissor(mirror_y, dstY0, dstY1, srcY0, srcY1,
23275f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Height))) {
23375f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
23475f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      return true;
23575f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   }
236506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
2370dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   /* Account for the fact that in the system framebuffer, the origin is at
2380dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry    * the lower left.
2390dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry    */
240c738ea1191cd1b5a0dc60b0e6d05fd918083e961Paul Berry   if (_mesa_is_winsys_fbo(read_fb)) {
2410dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      GLint tmp = read_fb->Height - srcY0;
2420dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      srcY0 = read_fb->Height - srcY1;
2430dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      srcY1 = tmp;
2440dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      mirror_y = !mirror_y;
2450dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   }
246c738ea1191cd1b5a0dc60b0e6d05fd918083e961Paul Berry   if (_mesa_is_winsys_fbo(draw_fb)) {
2470dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      GLint tmp = draw_fb->Height - dstY0;
2480dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      dstY0 = draw_fb->Height - dstY1;
2490dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      dstY1 = tmp;
2500dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      mirror_y = !mirror_y;
2510dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   }
2520dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry
253fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Find buffers */
2545c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   struct intel_renderbuffer *src_irb;
2555c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry   struct intel_renderbuffer *dst_irb;
256fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   switch (buffer_bit) {
257fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_COLOR_BUFFER_BIT:
2585c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
259ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
2605c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         dst_irb = intel_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[i]);
2615c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         if (dst_irb && !formats_match(buffer_bit, src_irb, dst_irb))
262ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry            return false;
263ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      }
264ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
2655c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         dst_irb = intel_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[i]);
2665c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         do_blorp_blit(intel, buffer_bit, src_irb, dst_irb, srcX0, srcY0,
267ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry                       dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
268ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      }
269fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
270fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_DEPTH_BUFFER_BIT:
2715c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      src_irb =
2725c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
2735c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      dst_irb =
2745c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
2755c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      if (!formats_match(buffer_bit, src_irb, dst_irb))
276fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry         return false;
2775c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      do_blorp_blit(intel, buffer_bit, src_irb, dst_irb, srcX0, srcY0,
278fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                    dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
279fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
280fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_STENCIL_BUFFER_BIT:
2815c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      src_irb =
2825c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
2835c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      dst_irb =
2845c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry         intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
2855c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      if (!formats_match(buffer_bit, src_irb, dst_irb))
286fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry         return false;
2875c66640ac7c271a96f66f4cb49adad54eb58cc47Paul Berry      do_blorp_blit(intel, buffer_bit, src_irb, dst_irb, srcX0, srcY0,
288fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                    dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
289fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
290fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   default:
291fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      assert(false);
292fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   }
293506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
294506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return true;
295506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
296506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
297506d70be21cd3469118de89297cba0c0f709c1aePaul BerryGLbitfield
298506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_framebuffer(struct intel_context *intel,
299506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
300506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
301506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLbitfield mask, GLenum filter)
302506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
303b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   /* BLORP is not supported before Gen6. */
304b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   if (intel->gen < 6)
305506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return mask;
306506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
307506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   static GLbitfield buffer_bits[] = {
308506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_COLOR_BUFFER_BIT,
309506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_DEPTH_BUFFER_BIT,
310506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_STENCIL_BUFFER_BIT,
311506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   };
312506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
313506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
314506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      if ((mask & buffer_bits[i]) &&
315506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       try_blorp_blit(intel,
316506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      srcX0, srcY0, srcX1, srcY1,
317506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      dstX0, dstY0, dstX1, dstY1,
318506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      filter, buffer_bits[i])) {
319506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         mask &= ~buffer_bits[i];
320506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      }
321506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
322506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
323506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return mask;
324506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
325506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
326665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
327665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry/**
328665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry * Enum to specify the order of arguments in a sampler message
329665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry */
330665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryenum sampler_message_arg
331665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
332665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_FLOAT,
333665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_FLOAT,
334665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_INT,
335665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_INT,
336233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_SI_INT,
3374ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   SAMPLER_MESSAGE_ARG_MCS_INT,
338233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_ZERO_INT,
339665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry};
340665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
341506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
342506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Generator for WM programs used in BLORP blits.
343506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
344506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * The bulk of the work done by the WM program is to wrap and unwrap the
345506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinate transformations used by the hardware to store surfaces in
34619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * memory.  The hardware transforms a pixel location (X, Y, S) (where S is the
34719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * sample index for a multisampled surface) to a memory offset by the
34819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * following formulas:
349506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3508b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
3518b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
35219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3531bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a single-sampled surface, or for a multisampled surface using
3541bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
3551bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * function:
35619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3571bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
3581bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
3591bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
3601bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
36119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3621bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
3631bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * embeds the sample number into bit 1 of the X and Y coordinates:
36419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3651bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
36619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
36719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
3681bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
36919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b11) >> 1 | (X & 0b1)
37019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
37119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           S = (Y & 0b10) | (X & 0b10) >> 1
372506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3737fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
3747fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
3757fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * the Y coordinate:
3767fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *
3777fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
3787fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *     where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
3797fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
3807fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
3817fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *     where X' = (X & ~0b111) >> 2 | (X & 0b1)
3827fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
3837fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
3847fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *
385506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For X tiling, tile() combines together the low-order bits of the X and Y
386506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
387506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 8 rows high:
388506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3898b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(x_tiled, X, Y, S) = A
390506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3918b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
3928b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           offset = (Y' & 0b111) << 9
393506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X & 0b111111111)
394506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3958b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3968b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(x_tiled, A) = (X, Y, S)
397506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
3988b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
3998b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
4008b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 3
4018b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111000000000) >> 9
402506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 9
403506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111111111)
404506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
405506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In all tiling formulas, cpp is the number of bytes occupied by a single
4068b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
4078b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * to fill the width of the surface, and qpitch is the spacing (in rows)
4088b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * between array slices).
409506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
410506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For Y tiling, tile() combines together the low-order bits of the X and Y
411506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
412506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 32 rows high:
413506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4148b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(y_tiled, X, Y, S) = A
415506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
4168b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
417506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b1110000) << 5
418506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (Y' & 0b11111) << 4
419506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1111)
420506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
4218b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
4228b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(y_tiled, A) = (X, Y, S)
423506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
4248b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
4258b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
4268b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 5
4278b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111110000) >> 4
428506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 7
429506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 5
430506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1111)
431506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
432506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For W tiling, tile() combines together the low-order bits of the X and Y
433506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
434506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 64 rows high (note that W tiling is only used for stencil
4358b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * buffers, which always have cpp = 1 and S=0):
436506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4378b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(w_tiled, X, Y, S) = A
438506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
4398b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
440506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b111000) << 6
4418b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b111100) << 3
442506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b100) << 2
4438b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b10) << 2
444506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b10) << 1
4458b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b1) << 1
446506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1)
447506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp = X
4488b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
4498b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(w_tiled, A) = (X, Y, S)
450506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp = X'
4518b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch = Y'
4528b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y / qpitch = 0
4538b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 6
4548b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111100000) >> 3
4558b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b1000) >> 2
4568b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b10) >> 1
457506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 6
458506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 6
459506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b10000) >> 2
460506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b100) >> 1
461506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1)
462506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
463506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Finally, for a non-tiled surface, tile() simply combines together the X and
464506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Y coordinates in the natural way:
465506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4668b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(untiled, X, Y, S) = A
467506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = Y * pitch + X'
468506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
4698b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
4708b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(untiled, A) = (X, Y, S)
471506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
4728b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
4738b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
474506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = A % pitch
4758b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = A / pitch
476506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
477506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In these formulas, pitch is the number of bytes occupied by a single row
47819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * of samples).
479506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
480506d70be21cd3469118de89297cba0c0f709c1aePaul Berryclass brw_blorp_blit_program
481506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
482506d70be21cd3469118de89297cba0c0f709c1aePaul Berrypublic:
483506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_blit_program(struct brw_context *brw,
484506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                          const brw_blorp_blit_prog_key *key);
485506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ~brw_blorp_blit_program();
486506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
487506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const GLuint *compile(struct brw_context *brw, GLuint *program_size);
488506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
489506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_prog_data prog_data;
490506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
491506d70be21cd3469118de89297cba0c0f709c1aePaul Berryprivate:
492506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_regs();
493506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_push_const_regs(int base_reg);
494506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void compute_frag_coords();
495506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_tiling(bool old_tiled_w, bool new_tiled_w);
4961bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void encode_msaa(unsigned num_samples, intel_msaa_layout layout);
4971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void decode_msaa(unsigned num_samples, intel_msaa_layout layout);
498506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void kill_if_outside_dst_rect();
499506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_dst_to_src();
50019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   void single_to_blend();
50117eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   void manual_blend(unsigned num_samples);
5024725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void sample(struct brw_reg dst);
5034725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texel_fetch(struct brw_reg dst);
5044ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   void mcs_fetch();
505665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   void expand_to_32_bits(struct brw_reg src, struct brw_reg dst);
5064725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texture_lookup(struct brw_reg dst, GLuint msg_type,
5074725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                       const sampler_message_arg *args, int num_args);
508506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void render_target_write();
509506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
510b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /**
511b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Base-2 logarithm of the maximum number of samples that can be blended.
512b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
51317eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   static const unsigned LOG2_MAX_BLEND_SAMPLES = 3;
514b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
515506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void *mem_ctx;
516506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_context *brw;
517506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const brw_blorp_blit_prog_key *key;
518506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_compile func;
519506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
520506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Thread dispatch header */
521506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R0;
522506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
523506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Pixel X/Y coordinates (always in R1). */
524506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R1;
525506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
526506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Push constants */
527506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x0;
528506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x1;
529506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y0;
530506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y1;
531506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct {
532506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg multiplier;
533506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg offset;
534506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } x_transform, y_transform;
535506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
536b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Data read from texture (4 vec16's per array element) */
537b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   struct brw_reg texture_data[LOG2_MAX_BLEND_SAMPLES + 1];
538506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
5394ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   /* Auxiliary storage for the contents of the MCS surface.
5404ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    *
5414ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * Since the sampler always returns 8 registers worth of data, this is 8
5424ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * registers wide, even though we only use the first 2 registers of it.
5434ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    */
5444ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   struct brw_reg mcs_data;
5454ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
546506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* X coordinates.  We have two of them so that we can perform coordinate
547506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
548506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
549506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg x_coords[2];
550506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
551506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Y coordinates.  We have two of them so that we can perform coordinate
552506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
553506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
554506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg y_coords[2];
555506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
556506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Which element of x_coords and y_coords is currently in use.
557506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
558506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int xy_coord_index;
559506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
56019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* True if, at the point in the program currently being compiled, the
56119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * sample index is known to be zero.
56219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
56319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   bool s_is_zero;
56419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
56519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Register storing the sample index when s_is_zero is false. */
56619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct brw_reg sample_index;
56719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Temporaries */
569506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t1;
570506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t2;
571506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
572665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   /* MRF used for sampling and render target writes */
573506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   GLuint base_mrf;
574506d70be21cd3469118de89297cba0c0f709c1aePaul Berry};
575506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
576506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::brw_blorp_blit_program(
577506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_context *brw,
578506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const brw_blorp_blit_prog_key *key)
579506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   : mem_ctx(ralloc_context(NULL)),
580506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     brw(brw),
581506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     key(key)
582506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
583506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_init_compile(brw, &func, mem_ctx);
584506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
585506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
586506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::~brw_blorp_blit_program()
587506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
588506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ralloc_free(mem_ctx);
589506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
590506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
591506d70be21cd3469118de89297cba0c0f709c1aePaul Berryconst GLuint *
592506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compile(struct brw_context *brw,
593506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                GLuint *program_size)
594506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
59519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Sanity checks */
59634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->dst_tiled_w && key->rt_samples > 0) {
59734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination image is W tiled and multisampled, then the thread
59834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * must be dispatched once per sample, not once per pixel.  This is
59934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * necessary because after conversion between W and Y tiling, there's no
60019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * guarantee that all samples corresponding to a single pixel will still
60119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * be together.
60219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
60334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->persample_msaa_dispatch);
60419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
60519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
60619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
6074725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      /* We are blending, which means we won't have an opportunity to
6084725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry       * translate the tiling and sample count for the texture surface.  So
60919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the surface state for the texture must be configured with the correct
61019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * tiling and sample count.
61119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
61219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(!key->src_tiled_w);
61319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples == key->src_samples);
6141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(key->tex_layout == key->src_layout);
61519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples > 0);
61619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
61719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
61834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
61934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* It only makes sense to do persample dispatch if the render target is
62034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * configured as multisampled.
62134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
62234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->rt_samples > 0);
62334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
62434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
6251bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Make sure layout is consistent with sample count */
6261bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
6271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->tex_samples == 0));
6281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
6291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->rt_samples == 0));
6301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
6311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->src_samples == 0));
6321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
6331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->dst_samples == 0));
6348b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
63534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   /* Set up prog_data */
63634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   memset(&prog_data, 0, sizeof(prog_data));
63734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   prog_data.persample_msaa_dispatch = key->persample_msaa_dispatch;
63834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
639506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
640506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
641506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_regs();
642506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   compute_frag_coords();
643506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
644506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Render target and texture hardware don't support W tiling. */
645506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool rt_tiled_w = false;
646506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool tex_tiled_w = false;
647506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
648506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* The address that data will be written to is determined by the
64919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates supplied to the WM thread and the tiling and sample count of
65019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the render target, according to the formula:
651506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
65219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
653506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
65419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * If the actual tiling and sample count of the destination surface are not
65519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the same as the configuration of the render target, then these
65619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates are wrong and we have to adjust them to compensate for the
65719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * difference.
658506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
65919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (rt_tiled_w != key->dst_tiled_w ||
6608b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       key->rt_samples != key->dst_samples ||
6611bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       key->rt_layout != key->dst_layout) {
6621bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      encode_msaa(key->rt_samples, key->rt_layout);
6638b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(rt_tiling, offset) */
664506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      translate_tiling(rt_tiled_w, key->dst_tiled_w);
6658b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(dst_tiling, offset) */
6661bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      decode_msaa(key->dst_samples, key->dst_layout);
66719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
668506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
66919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
670506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
67119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * That is: X, Y and S now contain the true coordinates and sample index of
67219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the data that the WM thread should output.
673506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
674506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * If we need to kill pixels that are outside the destination rectangle,
675506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * now is the time to do it.
676506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
677506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
678506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (key->use_kill)
679506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      kill_if_outside_dst_rect();
680506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
681506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Next, apply a translation to obtain coordinates in the source image. */
682506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   translate_dst_to_src();
683506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
68419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* If the source image is not multisampled, then we want to fetch sample
68519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * number 0, because that's the only sample there is.
686506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
68719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->src_samples == 0)
68819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = true;
689506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
69019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* X, Y, and S are now the coordinates of the pixel in the source image
69119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that we want to texture from.  Exception: if we are blending, then S is
69219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * irrelevant, because we are going to fetch all samples.
693506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
69419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
6954725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      if (brw->intel.gen == 6) {
6964725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen6 hardware an automatically blend using the SAMPLE message */
6974725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         single_to_blend();
698b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         sample(texture_data[0]);
6994725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      } else {
7004725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen7+ hardware doesn't automaticaly blend. */
70117eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry         manual_blend(key->src_samples);
7024725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
70319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   } else {
70419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We aren't blending, which means we just want to fetch a single sample
70519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * from the source surface.  The address that we want to fetch from is
70619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * related to the X, Y and S values according to the formula:
70719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
70819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
70919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
71019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * If the actual tiling and sample count of the source surface are not
71119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the same as the configuration of the texture, then we need to adjust
71219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the coordinates to compensate for the difference.
71319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
71419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      if (tex_tiled_w != key->src_tiled_w ||
7158b1f467cce34340637e9baca4847fc5273cf7541Paul Berry          key->tex_samples != key->src_samples ||
7161bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          key->tex_layout != key->src_layout) {
7171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         encode_msaa(key->src_samples, key->src_layout);
7188b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(src_tiling, offset) */
71919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         translate_tiling(key->src_tiled_w, tex_tiled_w);
7208b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(tex_tiling, offset) */
7211bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         decode_msaa(key->tex_samples, key->tex_layout);
72219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
723506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
72419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
72519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
72619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * In other words: X, Y, and S now contain values which, when passed to
72719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the texturing unit, will cause data to be read from the correct
72819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * memory location.  So we can fetch the texel now.
72919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
7304ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
7314ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         mcs_fetch();
732b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[0]);
73319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
73419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
73519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Finally, write the fetched (or blended) value to the render target and
73619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * terminate the thread.
737506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
738506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   render_target_write();
739506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return brw_get_program(&func, program_size);
740506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
741506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
742506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
743506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_push_const_regs(int base_reg)
744506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
745506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
746506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define ALLOC_REG(name) \
747506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->name = \
748506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, base_reg, CONST_LOC(name) / 2)
749506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
750506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x0);
751506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x1);
752506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y0);
753506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y1);
754506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.multiplier);
755506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.offset);
756506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.multiplier);
757506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.offset);
758506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef CONST_LOC
759506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef ALLOC_REG
760506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
761506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
762506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
763506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_regs()
764506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
765506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int reg = 0;
766506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
767506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
768506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   prog_data.first_curbe_grf = reg;
769506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_push_const_regs(reg);
770506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
771b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   for (unsigned i = 0; i < ARRAY_SIZE(texture_data); ++i) {
772e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      this->texture_data[i] =
773e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         retype(vec16(brw_vec8_grf(reg, 0)), key->texture_data_type);
774e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      reg += 8;
775b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   }
7764ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   this->mcs_data =
7774ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD); reg += 8;
778506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 2; ++i) {
779506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->x_coords[i]
780506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
781506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->y_coords[i]
782506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
783506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
784506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->xy_coord_index = 0;
78519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   this->sample_index
78619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
787506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t1 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
788506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t2 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
789506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
790b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Make sure we didn't run out of registers */
791b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(reg <= GEN7_MRF_HACK_START);
792b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
793506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf = 2;
794506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->base_mrf = mrf;
795506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
796506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
797506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* In the code that follows, X and Y can be used to quickly refer to the
798506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
799506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * prime") to the inactive elements.
80019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
80119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * S can be used to quickly refer to sample_index.
802506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
803506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define X x_coords[xy_coord_index]
804506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Y y_coords[xy_coord_index]
805506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Xp x_coords[!xy_coord_index]
806506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Yp y_coords[!xy_coord_index]
80719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#define S sample_index
808506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
809506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* Quickly swap the roles of (X, Y) and (Xp, Yp).  Saves us from having to do
810506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
811506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
812506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
813506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
814506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
815506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compute the X and Y coordinates of the pixels being rendered
816506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * by this WM invocation.
817506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
818506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Assuming the render target is set up for Y tiling, these (X, Y) values are
819506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * related to the address offset where outputs will be written by the formula:
820506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
821506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *   (X, Y, S) = decode_msaa(detile(offset)).
822506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
823506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
824506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
825506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
826506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compute_frag_coords()
827506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
828506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
829506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
830506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
831506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
832506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
833506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Pixels within a subspan are laid out in this arrangement:
834506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 0 1
835506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 2 3
836506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
837506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * So, to compute the coordinates of each pixel, we need to read every 2nd
838506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
839506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
840506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * In other words, the data we want to access is R1.4<2;4,0>UW.
841506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
842506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
843506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * result, since pixels n+1 and n+3 are in the right half of the subspan.
844506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
845506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, X, stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
846506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
847506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
848506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
849506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
850506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4<2;4,0>UW).
851506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
852506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
853506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * pixels n+2 and n+3 are in the bottom half of the subspan.
854506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
855506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Y, stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
85619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
85734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
858619471dc322de80942f7dbb29a437890e48155c6Paul Berry      switch (key->rt_samples) {
859619471dc322de80942f7dbb29a437890e48155c6Paul Berry      case 4:
860619471dc322de80942f7dbb29a437890e48155c6Paul Berry         /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
861619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Therefore, subspan 0 will represent sample 0, subspan 1 will
862619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * represent sample 1, and so on.
863619471dc322de80942f7dbb29a437890e48155c6Paul Berry          *
864619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
865619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 1, 2, 2, 2, 2, 3, 3, 3, 3).  The easiest way to do this is to
866619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * populate a temporary variable with the sequence (0, 1, 2, 3), and
867619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * then copy from it using vstride=1, width=4, hstride=0.
868619471dc322de80942f7dbb29a437890e48155c6Paul Berry          */
869619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, t1, brw_imm_v(0x3210));
870619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, S, stride(t1, 1, 4, 0));
871619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
872619471dc322de80942f7dbb29a437890e48155c6Paul Berry      case 8: {
873619471dc322de80942f7dbb29a437890e48155c6Paul Berry         /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
874619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
875619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * subspan 1 will represent sample 1, and so on.  We can find the
876619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
877619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Index") and multiplying by two (since samples are always delivered
878619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * in pairs).  That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
879619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 0xc0) >> 5.
880619471dc322de80942f7dbb29a437890e48155c6Paul Berry          *
881619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
882619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
883619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * variable with the sequence (0, 1, 2, 3), and then reading from it
884619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * using vstride=1, width=4, hstride=0.
885619471dc322de80942f7dbb29a437890e48155c6Paul Berry          */
886619471dc322de80942f7dbb29a437890e48155c6Paul Berry         struct brw_reg t1_ud1 = vec1(retype(t1, BRW_REGISTER_TYPE_UD));
887619471dc322de80942f7dbb29a437890e48155c6Paul Berry         struct brw_reg r0_ud1 = vec1(retype(R0, BRW_REGISTER_TYPE_UD));
888619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_AND(&func, t1_ud1, r0_ud1, brw_imm_ud(0xc0));
889619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_SHR(&func, t1_ud1, t1_ud1, brw_imm_ud(5));
890619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, t2, brw_imm_v(0x3210));
891619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_ADD(&func, S, retype(t1_ud1, BRW_REGISTER_TYPE_UW),
892619471dc322de80942f7dbb29a437890e48155c6Paul Berry                 stride(t2, 1, 4, 0));
893619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
894619471dc322de80942f7dbb29a437890e48155c6Paul Berry      }
895619471dc322de80942f7dbb29a437890e48155c6Paul Berry      default:
896619471dc322de80942f7dbb29a437890e48155c6Paul Berry         assert(!"Unrecognized sample count in "
897619471dc322de80942f7dbb29a437890e48155c6Paul Berry                "brw_blorp_blit_program::compute_frag_coords()");
898619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
899619471dc322de80942f7dbb29a437890e48155c6Paul Berry      }
90034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = false;
90134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   } else {
90234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* Either the destination surface is single-sampled, or the WM will be
90334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
90434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel).  In either case, it's not meaningful to compute a sample
90534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * value.  Just set it to 0.
90634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
90734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = true;
90834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
909506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
910506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
911506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
912506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compensate for the difference between Y and W tiling.
913506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
914506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * This code modifies the X and Y coordinates according to the formula:
915506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
9168b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
917506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
918506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
919506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
920506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * It can only translate between W and Y tiling, so new_tiling and old_tiling
921506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * are booleans where true represents W tiling and false represents Y tiling.
922506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
923506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
924506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
925506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
926506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (old_tiled_w == new_tiled_w)
927506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return;
928506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
9298b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   /* In the code that follows, we can safely assume that S = 0, because W
9301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * tiling formats always use IMS layout.
9318b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
9328b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   assert(s_is_zero);
9338b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
934506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (new_tiled_w) {
935506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Given X and Y coordinates that describe an address using Y tiling,
936506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * translate to the X and Y coordinates that describe the same address
937506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * using W tiling.
938506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
939506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we break down the low order bits of X and Y, using a
940506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * single letter to represent each low-order bit:
941506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
942506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X = A << 7 | 0bBCDEFGH
943506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y = J << 5 | 0bKLMNP                                       (1)
944506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
945506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Then we can apply the Y tiling formula to see the memory offset being
946506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * addressed:
947506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
948506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH       (2)
949506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
950506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we apply the W detiling formula to this memory location, that the
951506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * corresponding X' and Y' coordinates are:
952506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
953506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = A << 6 | 0bBCDPFH                                     (3)
954506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = J << 6 | 0bKLMNEG
955506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
956506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
957506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * we need to make the following computation:
958506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
959506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1         (4)
960506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
961506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
962506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
963506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
964506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
965506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
966506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
967506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
968506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
969506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
970506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
971506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(8)); /* X & 0b1000 */
972506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
973506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
974506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
975506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
976506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
977506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
978506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
979506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Applying the same logic as above, but in reverse, we obtain the
980506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * formulas:
981506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
982506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
983506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
984506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
985506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
986506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
987506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
988506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
989506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
990506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
991506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
992506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
993506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                    | (Y & 0b1) << 1 */
994506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
995506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
996506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
997506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
998506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(4)); /* X & 0b100 */
999506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1000506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
1001506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
1002506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1003506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1004506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1005506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
100619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
100719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
100819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
100919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
101019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
10117fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
101219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
101319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
101419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
101519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
10161bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::encode_msaa(unsigned num_samples,
10171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
101819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
10191bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
10201bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
10218b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
10228b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
10231bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10241bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
10251bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
10261bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we haven't read from the MCS buffer.
10271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
10281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
10291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
103119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
10321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
10347fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      switch (num_samples) {
10357fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 4:
10367fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
10377fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
10387fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
10397fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
10407fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
10417fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10427fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
10437fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
10447fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10457fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
10467fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry                                                   | (S & 0b1) << 1 */
10477fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
10487fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
10497fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
10507fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
10517fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10527fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
10537fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
10547fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10557fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
10567fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
10577fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
10587fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 8:
10597fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
10607fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
10617fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *              | (X & 0b1)
10627fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
10637fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
10647fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
10657fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
10667fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10677fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(4)); /* S & 0b100 */
10687fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100) */
10697fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
10707fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (S & 0b1) << 1 */
10717fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100)
10727fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry                                          | (S & 0b1) << 1 */
10737fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10747fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
10757fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
10767fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
10777fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
10787fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10797fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
10807fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
10817fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10827fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
10837fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
10847fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
108519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
108619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
10878b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      s_is_zero = true;
10881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
108919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
109019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
109119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
109219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
109319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
109419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
109519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
109619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
109719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
10987fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
109919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
110019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
110119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
110219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
11031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::decode_msaa(unsigned num_samples,
11041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
110519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
11061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
11071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
11088b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
11098b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
11101bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
11111bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
11121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
11131bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we don't have access to the MCS buffer.
11141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
11151bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
11161bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
11171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
111819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
11191bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
11201bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
11218b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
11227fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      switch (num_samples) {
11237fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 4:
11247fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
11257fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b11) >> 1 | (X & 0b1)
11267fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
11277fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         S = (Y & 0b10) | (X & 0b10) >> 1
11287fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
11297fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
11307fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
11317fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
11327fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
11337fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
11347fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
11357fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
11367fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
11377fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
11387fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
11397fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
11407fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, S, t1, t2);
11417fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
11427fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 8:
11437fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
11447fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b111) >> 2 | (X & 0b1)
11457fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
11467fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
11477fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
11487fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfff8)); /* X & ~0b111 */
11497fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
11507fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
11517fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
11527fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
11537fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
11547fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
11557fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
11567fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(4)); /* X & 0b100 */
11577fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
11587fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, t1, t1, t2); /* (X & 0b100) | (Y & 0b10) */
11597fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
11607fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
11617fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, S, t1, t2);
11627fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
11637fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      }
116419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = false;
116519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
11661bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
116719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
116819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
116919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
117019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1171506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code that kills pixels whose X and Y coordinates are outside the
1172506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
1173506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * dst_x1, dst_y1).
1174506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1175506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1176506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::kill_if_outside_dst_rect()
1177506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1178506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg f0 = brw_flag_reg();
1179506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1180506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg null16 = vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
1181506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1182506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, X, dst_x0);
1183506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, Y, dst_y0);
1184506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, X, dst_x1);
1185506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, Y, dst_y1);
1186506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1187506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_predicate_control(&func, BRW_PREDICATE_NONE);
1188506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_push_insn_state(&func);
1189506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_mask_control(&func, BRW_MASK_DISABLE);
1190506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_AND(&func, g1, f0, g1);
1191506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_pop_insn_state(&func);
1192506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1193506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1194506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
1195506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1196506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates.
1197506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1198506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1199506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_dst_to_src()
1200506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1201506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Xp, X, x_transform.multiplier);
1202506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Yp, Y, y_transform.multiplier);
1203506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Xp, Xp, x_transform.offset);
1204506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Yp, Yp, y_transform.offset);
1205506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   SWAP_XY_AND_XPYP();
1206506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1207506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1208506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
120919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to transform the X and Y coordinates as needed for blending
121019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * together the different samples in an MSAA texture.
121119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
121219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
121319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrybrw_blorp_blit_program::single_to_blend()
121419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
121519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* When looking up samples in an MSAA texture using the SAMPLE message,
121619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * Gen6 requires the texture coordinates to be odd integers (so that they
121719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * correspond to the center of a 2x2 block representing the four samples
121819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that maxe up a pixel).  So we need to multiply our X and Y coordinates
121919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * each by 2 and then add 1.
122019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
122119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t1, X, brw_imm_w(1));
122219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t2, Y, brw_imm_w(1));
122319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Xp, t1, brw_imm_w(1));
122419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Yp, t2, brw_imm_w(1));
122519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   SWAP_XY_AND_XPYP();
122619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
122719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1228b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1229b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry/**
1230b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * Count the number of trailing 1 bits in the given value.  For example:
1231b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry *
1232b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(0) == 0
1233b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(7) == 3
1234b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(11) == 2
1235b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry */
1236b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berryinline int count_trailing_one_bits(unsigned value)
1237b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry{
1238b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1239b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return __builtin_ctz(~value);
1240b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#else
1241b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return _mesa_bitcount(value & ~(value + 1));
1242b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#endif
1243b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry}
1244b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1245b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
12464725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berryvoid
124717eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berrybrw_blorp_blit_program::manual_blend(unsigned num_samples)
12484725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry{
12494ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
12504ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      mcs_fetch();
12514ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
1252b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1253b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1254b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *   result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1255b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1256b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * This ensures that when all samples have the same value, no numerical
1257b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision is lost, since each addition operation always adds two equal
1258b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * values, and summing two equal floating point values does not lose
1259b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision.
1260b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1261b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * We perform this computation by treating the texture_data array as a
1262b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * stack and performing the following operations:
1263b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1264b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 0 onto stack
1265b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 1 onto stack
1266b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1267b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 2 onto stack
1268b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 3 onto stack
1269b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1270b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1271b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - divide top stack entry by 4
1272b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1273b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Note that after pushing sample i onto the stack, the number of add
1274b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * operations we do is equal to the number of trailing 1 bits in i.  This
1275b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * works provided the total number of samples is a power of two, which it
1276b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * always is for i965.
1277e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    *
1278e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * For integer formats, we replace the add operations with average
1279e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * operations and skip the final division.
1280b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
1281e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   typedef struct brw_instruction *(*brw_op2_ptr)(struct brw_compile *,
1282e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1283e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1284e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg);
1285e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   brw_op2_ptr combine_op =
1286e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      key->texture_data_type == BRW_REGISTER_TYPE_F ? brw_ADD : brw_AVG;
1287b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   unsigned stack_depth = 0;
128817eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   for (unsigned i = 0; i < num_samples; ++i) {
1289b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
1290b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1291b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Push sample i onto the stack */
1292b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth < ARRAY_SIZE(texture_data));
1293b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      if (i == 0) {
1294b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = true;
1295b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      } else {
1296b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = false;
1297b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         brw_MOV(&func, S, brw_imm_uw(i));
1298b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      }
1299b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[stack_depth++]);
1300b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1301f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      if (i == 0 && key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
1302f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1303f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * suggests an optimization:
1304f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1305f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     "A simple optimization with probable large return in
1306f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     performance is to compare the MCS value to zero (indicating
1307f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     all samples are on sample slice 0), and sample only from
1308f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     sample slice 0 using ld2dss if MCS is zero."
1309f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1310f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Note that in the case where the MCS value is zero, sampling from
1311f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * sample slice 0 using ld2dss and sampling from sample 0 using
1312f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * ld2dms are equivalent (since all samples are on sample slice 0).
1313f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Since we have already sampled from sample 0, all we need to do is
1314f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * skip the remaining fetches and averaging if MCS is zero.
1315f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          */
1316f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_CMP(&func, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ,
1317f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry                 mcs_data, brw_imm_ud(0));
1318f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_IF(&func, BRW_EXECUTE_16);
1319f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      }
1320f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1321b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Do count_trailing_one_bits(i) times */
1322b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      for (int j = count_trailing_one_bits(i); j-- > 0; ) {
1323b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         assert(stack_depth >= 2);
1324b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         --stack_depth;
1325b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1326b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         /* TODO: should use a smaller loop bound for non_RGBA formats */
1327b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         for (int k = 0; k < 4; ++k) {
1328e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry            combine_op(&func, offset(texture_data[stack_depth - 1], 2*k),
1329e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth - 1]), 2*k),
1330e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth]), 2*k));
1331b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         }
13324725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
13334725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
13344725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
1335b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We should have just 1 sample on the stack now. */
1336b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(stack_depth == 1);
1337b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1338e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   if (key->texture_data_type == BRW_REGISTER_TYPE_F) {
1339e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* Scale the result down by a factor of num_samples */
1340e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* TODO: should use a smaller loop bound for non-RGBA formats */
1341e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      for (int j = 0; j < 4; ++j) {
1342e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         brw_MUL(&func, offset(texture_data[0], 2*j),
1343e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 offset(vec8(texture_data[0]), 2*j),
1344e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 brw_imm_f(1.0/num_samples));
1345e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
13464725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
1347f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1348f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1349f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      brw_ENDIF(&func);
13504725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry}
13514725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
135219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
135319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to look up a value in the texture using the SAMPLE message (which
135419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * does blending of MSAA surfaces).
135519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
135619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
13574725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::sample(struct brw_reg dst)
135819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
1359665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   static const sampler_message_arg args[2] = {
1360665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_FLOAT,
1361665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_V_FLOAT
1362665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
1363665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
13644725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE, args, ARRAY_SIZE(args));
136519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
136619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
136719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1368506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to look up a value in the texture using the SAMPLE_LD message
1369506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (which does a simple texel fetch).
1370506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1371506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
13724725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texel_fetch(struct brw_reg dst)
1373506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
13741c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen6_args[5] = {
1375665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
1376233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_V_INT,
1377233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* R */
1378233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
1379233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT
1380665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
13811c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld_args[3] = {
13821c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13831c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
13841c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13851c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
13861c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld2dss_args[3] = {
13871c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
13881c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13891c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13901c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
13914ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld2dms_args[4] = {
13924ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
13934ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_MCS_INT,
13944ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13954ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13964ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
1397665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
13981c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   switch (brw->intel.gen) {
13991c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 6:
14004725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen6_args,
14011c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                     s_is_zero ? 2 : 5);
14021c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
14031c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 7:
140460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      switch (key->tex_layout) {
140560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_IMS:
140660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
140760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * Format):
140860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *
140960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     If this field is MSFMT_DEPTH_STENCIL
141060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
141160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     messages allowed are "ld2dms", "resinfo", and "sampleinfo".
141260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *
141360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * So fall through to emit the same message as we use for
141460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * INTEL_MSAA_LAYOUT_CMS.
141560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          */
141660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_CMS:
141760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS,
141860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry                        gen7_ld2dms_args, ARRAY_SIZE(gen7_ld2dms_args));
141960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
142060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_UMS:
142160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS,
142260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry                        gen7_ld2dss_args, ARRAY_SIZE(gen7_ld2dss_args));
142360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
142460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_NONE:
14251c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry         assert(s_is_zero);
14264725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen7_ld_args,
14271c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                        ARRAY_SIZE(gen7_ld_args));
142860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
14291c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      }
14301c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
14311c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   default:
14321c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      assert(!"Should not get here.");
14331c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
14341c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
1435506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1436506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1437506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
14384ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berrybrw_blorp_blit_program::mcs_fetch()
14394ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry{
14404ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld_mcs_args[2] = {
14414ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
14424ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
14434ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
14444ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   texture_lookup(vec16(mcs_data), GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS,
14454ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry                  gen7_ld_mcs_args, ARRAY_SIZE(gen7_ld_mcs_args));
14464ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry}
14474ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
14484ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berryvoid
1449665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berrybrw_blorp_blit_program::expand_to_32_bits(struct brw_reg src,
1450665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                          struct brw_reg dst)
1451506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1452665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, vec8(dst), vec8(src));
1453506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
1454665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, offset(vec8(dst), 1), suboffset(vec8(src), 8));
1455506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
1456665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry}
1457665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
1458665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryvoid
14594725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texture_lookup(struct brw_reg dst,
14604725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                                       GLuint msg_type,
1461665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       const sampler_message_arg *args,
1462665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       int num_args)
1463665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
1464665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   struct brw_reg mrf =
1465665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_UD);
1466665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   for (int arg = 0; arg < num_args; ++arg) {
1467665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      switch (args[arg]) {
1468665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_FLOAT:
1469665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, retype(mrf, BRW_REGISTER_TYPE_F));
1470665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1471665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_FLOAT:
1472665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, retype(mrf, BRW_REGISTER_TYPE_F));
1473665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1474665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_INT:
1475665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, mrf);
1476665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1477665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_INT:
1478665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, mrf);
1479665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1480233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_SI_INT:
1481233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         /* Note: on Gen7, this code may be reached with s_is_zero==true
1482233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * because in Gen7's ld2dss message, the sample index is the first
1483233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * argument.  When this happens, we need to move a 0 into the
1484233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * appropriate message register.
1485233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          */
1486233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         if (s_is_zero)
1487233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            brw_MOV(&func, mrf, brw_imm_ud(0));
1488233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         else
1489233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            expand_to_32_bits(S, mrf);
1490233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
14914ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      case SAMPLER_MESSAGE_ARG_MCS_INT:
149260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         switch (key->tex_layout) {
149360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         case INTEL_MSAA_LAYOUT_CMS:
149460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            brw_MOV(&func, mrf, mcs_data);
149560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
149660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         case INTEL_MSAA_LAYOUT_IMS:
149760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            /* When sampling from an IMS surface, MCS data is not relevant,
149860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             * and the hardware ignores it.  So don't bother populating it.
149960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             */
150060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
150160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         default:
150260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            /* We shouldn't be trying to send MCS data with any other
150360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             * layouts.
150460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             */
150560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            assert (!"Unsupported layout for MCS data");
150660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
150760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         }
15084ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         break;
1509233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_ZERO_INT:
1510233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         brw_MOV(&func, mrf, brw_imm_ud(0));
1511233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
1512665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      }
1513665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      mrf.nr += 2;
1514665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   }
1515506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1516506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_SAMPLE(&func,
15174725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry              retype(dst, BRW_REGISTER_TYPE_UW) /* dest */,
1518506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              base_mrf /* msg_reg_nr */,
1519665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              brw_message_reg(base_mrf) /* src0 */,
1520506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX,
1521665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              0 /* sampler */,
1522506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              WRITEMASK_XYZW,
1523506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              msg_type,
1524506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              8 /* response_length.  TODO: should be smaller for non-RGBA formats? */,
1525665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              mrf.nr - base_mrf /* msg_length */,
1526506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              0 /* header_present */,
1527506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_SIMD_MODE_SIMD16,
1528506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_RETURN_FORMAT_FLOAT32);
1529506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1530506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1531506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef X
1532506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef Y
1533506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef U
1534506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef V
1535506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef S
1536506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef SWAP_XY_AND_XPYP
1537506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1538506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1539506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::render_target_write()
1540506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1541e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   struct brw_reg mrf_rt_write =
1542e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      retype(vec16(brw_message_reg(base_mrf)), key->texture_data_type);
1543506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf_offset = 0;
1544506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1545506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* If we may have killed pixels, then we need to send R0 and R1 in a header
1546506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * so that the render target knows which pixels we killed.
1547506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
1548506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool use_header = key->use_kill;
1549506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (use_header) {
1550506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Copy R0/1 to MRF */
1551506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_MOV(&func, retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
1552506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              retype(R0, BRW_REGISTER_TYPE_UD));
1553506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1554506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1555506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1556506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Copy texture data to MRFs */
1557506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 4; ++i) {
1558506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
15594725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      brw_MOV(&func, offset(mrf_rt_write, mrf_offset),
1560b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry              offset(vec8(texture_data[0]), 2*i));
1561506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1562506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1563506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1564506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Now write to the render target and terminate the thread */
1565506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_fb_WRITE(&func,
1566506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                16 /* dispatch_width */,
1567506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                base_mrf /* msg_reg_nr */,
1568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_rt_write /* src0 */,
156929362875f2613ad87abe7725ce3c56c36d16cf9bEric Anholt                BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
1570506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
1571506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_offset /* msg_length.  TODO: Should be smaller for non-RGBA formats. */,
1572506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                0 /* response_length */,
1573506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                true /* eot */,
1574506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                use_header);
1575506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1576506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1577506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1578506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1579506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_coord_transform_params::setup(GLuint src0, GLuint dst0, GLuint dst1,
1580506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                        bool mirror)
1581506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1582506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!mirror) {
1583506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When not mirroring a coordinate (say, X), we need:
1584506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = x - dst_x0
1585506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1586506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = 1*x + (src_x0 - dst_x0)
1587506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1588506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = 1;
1589506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 - dst0;
1590506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
1591506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When mirroring X we need:
1592506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = dst_x1 - x - 1
1593506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1594506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = -1*x + (src_x0 + dst_x1 - 1)
1595506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1596506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = -1;
1597506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 + dst1 - 1;
1598506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1599506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1600506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1601506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
16021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry/**
16031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * Determine which MSAA layout the GPU pipeline should be configured for,
16041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * based on the chip generation, the number of samples, and the true layout of
16051bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * the image in memory.
16061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry */
16071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berryinline intel_msaa_layout
16081bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrycompute_msaa_layout_for_pipeline(struct brw_context *brw, unsigned num_samples,
16091bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                 intel_msaa_layout true_layout)
16101bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry{
161197fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry   if (num_samples <= 1) {
16121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* When configuring the GPU for non-MSAA, we can still accommodate IMS
16131bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * format buffers, by transforming coordinates appropriately.
16141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
16151bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_NONE ||
16161bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry             true_layout == INTEL_MSAA_LAYOUT_IMS);
16171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_NONE;
16180dd5e98aa5c146ef21ab44b34fb7714206d5ec08Paul Berry   } else {
16190dd5e98aa5c146ef21ab44b34fb7714206d5ec08Paul Berry      assert(true_layout != INTEL_MSAA_LAYOUT_NONE);
16201bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
16211bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16221bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Prior to Gen7, all MSAA surfaces use IMS layout. */
16231bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   if (brw->intel.gen == 6) {
16241bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_IMS);
16251bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
16261bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   return true_layout;
16281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry}
16291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16318b1f467cce34340637e9baca4847fc5273cf7541Paul Berrybrw_blorp_blit_params::brw_blorp_blit_params(struct brw_context *brw,
16328b1f467cce34340637e9baca4847fc5273cf7541Paul Berry                                             struct intel_mipmap_tree *src_mt,
1633e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                                             unsigned src_level, unsigned src_layer,
1634506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             struct intel_mipmap_tree *dst_mt,
1635e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry                                             unsigned dst_level, unsigned dst_layer,
1636506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint src_x0, GLuint src_y0,
1637506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x0, GLuint dst_y0,
1638506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x1, GLuint dst_y1,
1639506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             bool mirror_x, bool mirror_y)
1640506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1641e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry   src.set(brw, src_mt, src_level, src_layer);
1642e87174cf4b499c8e9558438e70b0da5f0f38f54aPaul Berry   dst.set(brw, dst_mt, dst_level, dst_layer);
1643506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
16448c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   /* If we are blitting from sRGB to linear or vice versa, we still want the
16458c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * blit to be a direct copy, so we need source and destination to use the
16468c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * same format.  However, we want the destination sRGB/linear state to be
16478c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * correct (so that sRGB blending is used when doing an MSAA resolve to an
16488c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * sRGB surface, and linear blending is used when doing an MSAA resolve to
16498c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * a linear surface).  Since blorp blits don't support any format
16508c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * conversion (except between sRGB and linear), we can accomplish this by
16518c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    * simply setting up the source to use the same format as the destination.
16528c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke    */
16538c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   assert(_mesa_get_srgb_format_linear(src_mt->format) ==
16548c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke          _mesa_get_srgb_format_linear(dst_mt->format));
16558c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke   src.brw_surfaceformat = dst.brw_surfaceformat;
16568c1c18769ef4838b11065b353f6f62bfd1de1cd2Kenneth Graunke
1657506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   use_wm_prog = true;
1658506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1659506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1660e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   /* texture_data_type indicates the register type that should be used to
1661e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * manipulate texture data.
1662e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    */
1663e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   switch (_mesa_get_format_datatype(src_mt->format)) {
1664e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_NORMALIZED:
1665e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_SIGNED_NORMALIZED:
1666e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_FLOAT:
1667e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1668e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1669e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_INT:
1670e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      if (src_mt->format == MESA_FORMAT_S8) {
1671e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         /* We process stencil as though it's an unsigned normalized color */
1672e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1673e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      } else {
1674e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_UD;
1675e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
1676e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1677e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_INT:
1678e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_D;
1679e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1680e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   default:
1681e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      assert(!"Unrecognized blorp format");
1682e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1683e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   }
1684e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry
16858b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   if (brw->intel.gen > 6) {
168660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      /* Gen7's rendering hardware only supports the IMS layout for depth and
168760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * stencil render targets.  Blorp always maps its destination surface as
168860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * a color render target (even if it's actually a depth or stencil
168960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * buffer).  So if the destination is IMS, we'll have to map it as a
169060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * single-sampled texture and interleave the samples ourselves.
16918b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       */
16921bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
16938b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         dst.num_samples = 0;
16948b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   }
16958b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
169697fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry   if (dst.map_stencil_as_y_tiled && dst.num_samples > 1) {
169734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination surface is a W-tiled multisampled stencil buffer
169834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * that we're mapping as Y tiled, then we need to arrange for the WM
169934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * program to run once per sample rather than once per pixel, because
170034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * the memory layout of related samples doesn't match between W and Y
170134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * tiling.
1702233c207e9e477b6b0a5c6705e727129b92989073Paul Berry       */
170334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
1704233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   }
1705233c207e9e477b6b0a5c6705e727129b92989073Paul Berry
170697fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry   if (src.num_samples > 0 && dst.num_samples > 1) {
170719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are blitting from a multisample buffer to a multisample buffer, so
170819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * we must preserve samples within a pixel.  This means we have to
170934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * arrange for the WM program to run once per sample rather than once
171034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel.
171119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
171234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
171319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
171419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
171519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* The render path must be configured to use the same number of samples as
171619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the destination buffer.
171719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
171819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   num_samples = dst.num_samples;
171919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
172019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   GLenum base_format = _mesa_get_format_base_format(src_mt->format);
172119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
172219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       base_format != GL_STENCIL_INDEX &&
172397fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry       src_mt->num_samples > 1 && dst_mt->num_samples <= 1) {
172419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are downsampling a color buffer, so blend. */
172519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.blend = true;
172619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
172719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
172819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* src_samples and dst_samples are the true sample counts */
172919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.src_samples = src_mt->num_samples;
173019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.dst_samples = dst_mt->num_samples;
173119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
173219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* tex_samples and rt_samples are the sample counts that are set up in
173319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * SURFACE_STATE.
173419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
173519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.tex_samples = src.num_samples;
173619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.rt_samples  = dst.num_samples;
173719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
17381bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
17391bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * use to access the source and destination surfaces.
174067b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry    */
17411bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.tex_layout =
17421bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, src.num_samples, src.msaa_layout);
17431bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.rt_layout =
17441bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, dst.num_samples, dst.msaa_layout);
174567b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry
17461bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* src_layout and dst_layout indicate the true MSAA layout used by src and
17471bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * dst.
17488b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
17491bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.src_layout = src_mt->msaa_layout;
17501bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.dst_layout = dst_mt->msaa_layout;
17518b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
1752506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.src_tiled_w = src.map_stencil_as_y_tiled;
1753506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.dst_tiled_w = dst.map_stencil_as_y_tiled;
1754506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x0 = wm_push_consts.dst_x0 = dst_x0;
1755506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y0 = wm_push_consts.dst_y0 = dst_y0;
1756506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x1 = wm_push_consts.dst_x1 = dst_x1;
1757506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y1 = wm_push_consts.dst_y1 = dst_y1;
1758506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.x_transform.setup(src_x0, dst_x0, dst_x1, mirror_x);
1759506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.y_transform.setup(src_y0, dst_y0, dst_y1, mirror_y);
1760506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
176197fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry   if (dst.num_samples <= 1 && dst_mt->num_samples > 1) {
176219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We must expand the rectangle we send through the rendering pipeline,
176319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * to account for the fact that we are mapping the destination region as
176419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * single-sampled when it is in fact multisampled.  We must also align
176519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * it to a multiple of the multisampling pattern, because the
176619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * differences between multisampled and single-sampled surface formats
176719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * will mean that pixels are scrambled within the multisampling pattern.
176819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * TODO: what if this makes the coordinates too large?
17698b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       *
17701bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * Note: this only works if the destination surface uses the IMS layout.
17711bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * If it's UMS, then we have no choice but to set up the rendering
17721bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * pipeline as multisampled.
177319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
17741bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS);
1775082874e3891e588f674508be6578f600b35852c4Paul Berry      switch (dst_mt->num_samples) {
1776082874e3891e588f674508be6578f600b35852c4Paul Berry      case 4:
17774df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry         x0 = ROUND_DOWN_TO(x0 * 2, 4);
17784df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry         y0 = ROUND_DOWN_TO(y0 * 2, 4);
1779082874e3891e588f674508be6578f600b35852c4Paul Berry         x1 = ALIGN(x1 * 2, 4);
1780082874e3891e588f674508be6578f600b35852c4Paul Berry         y1 = ALIGN(y1 * 2, 4);
1781082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1782082874e3891e588f674508be6578f600b35852c4Paul Berry      case 8:
17834df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry         x0 = ROUND_DOWN_TO(x0 * 4, 8);
17844df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry         y0 = ROUND_DOWN_TO(y0 * 2, 4);
1785082874e3891e588f674508be6578f600b35852c4Paul Berry         x1 = ALIGN(x1 * 4, 8);
1786082874e3891e588f674508be6578f600b35852c4Paul Berry         y1 = ALIGN(y1 * 2, 4);
1787082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1788082874e3891e588f674508be6578f600b35852c4Paul Berry      default:
1789082874e3891e588f674508be6578f600b35852c4Paul Berry         assert(!"Unrecognized sample count in brw_blorp_blit_params ctor");
1790082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1791082874e3891e588f674508be6578f600b35852c4Paul Berry      }
179219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.use_kill = true;
179319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
179419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1795506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (dst.map_stencil_as_y_tiled) {
1796602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry      /* We must modify the rectangle we send through the rendering pipeline
179776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * (and the size and x/y offset of the destination surface), to account
179876c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * for the fact that we are mapping it as Y-tiled when it is in fact
179976c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * W-tiled.
180021e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       *
180121e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * Both Y tiling and W tiling can be understood as organizations of
180221e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
180321e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * is different, but the layout of the 32-byte sub-tiles within the 4k
180421e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
180521e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * column-major order).  In Y tiling, the sub-tiles are 16 bytes wide
180621e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
180721e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       *
180821e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * Therefore, to account for the layout differences within the 32-byte
180921e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * sub-tiles, we must expand the rectangle so the X coordinates of its
181021e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * edges are multiples of 8 (the W sub-tile width), and its Y
181121e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * coordinates of its edges are multiples of 4 (the W sub-tile height).
181221e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * Then we need to scale the X and Y coordinates of the rectangle to
181321e9850d5369f9757b5005df4c8af38668a3053bPaul Berry       * account for the differences in aspect ratio between the Y and W
181476c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * sub-tiles.  We need to modify the layer width and height similarly.
181576c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       *
181636bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry       * A correction needs to be applied when MSAA is in use: since
181736bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry       * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
181836bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry       * we need to align the Y coordinates to multiples of 8, so that when
181936bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry       * they are divided by two they are still multiples of 4.
182036bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry       *
182176c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * Note: Since the x/y offset of the surface will be applied using the
182276c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * SURFACE_STATE command packet, it will be invisible to the swizzling
182376c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * code in the shader; therefore it needs to be in a multiple of the
182476c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * 32-byte sub-tile size.  Fortunately it is, since the sub-tile is 8
182576c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
182676c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * buffer), and the miplevel alignment used for stencil buffers is 8
182776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * pixels horizontally and either 4 or 8 pixels vertically (see
182876c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * intel_horizontal_texture_alignment_unit() and
182976c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * intel_vertical_texture_alignment_unit()).
183076c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       *
183176c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * Note: Also, since the SURFACE_STATE command packet can only apply
183276c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * offsets that are multiples of 4 pixels horizontally and 2 pixels
183376c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * vertically, it is important that the offsets will be multiples of
183476c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * these sizes after they are converted into Y-tiled coordinates.
183576c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * Fortunately they will be, since we know from above that the offsets
183676c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * are a multiple of the 32-byte sub-tile size, and in Y-tiled
183776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
183834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
1839602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry       * TODO: what if this makes the coordinates (or the texture size) too
1840602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry       * large?
1841506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
184236bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry      const unsigned x_align = 8, y_align = dst.num_samples != 0 ? 8 : 4;
18434df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry      x0 = ROUND_DOWN_TO(x0, x_align) * 2;
18444df2848786d4778a2ce7dbf2e046e191036ccb56Paul Berry      y0 = ROUND_DOWN_TO(y0, y_align) / 2;
184534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      x1 = ALIGN(x1, x_align) * 2;
184634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      y1 = ALIGN(y1, y_align) / 2;
184776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      dst.width = ALIGN(dst.width, x_align) * 2;
184876c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      dst.height = ALIGN(dst.height, y_align) / 2;
184976c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      dst.x_offset *= 2;
185076c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      dst.y_offset /= 2;
1851506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      wm_prog_key.use_kill = true;
1852506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1853602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry
1854602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   if (src.map_stencil_as_y_tiled) {
185576c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      /* We must modify the size and x/y offset of the source surface to
185676c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * account for the fact that we are mapping it as Y-tiled when it is in
185776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * fact W tiled.
185876c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       *
185976c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * See the comments above concerning x/y offset alignment for the
186076c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry       * destination surface.
1861602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry       *
1862602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry       * TODO: what if this makes the texture size too large?
1863602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry       */
186436bc0fe4f2e90ea9efa19940f477472dad6fb18fPaul Berry      const unsigned x_align = 8, y_align = src.num_samples != 0 ? 8 : 4;
186576c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      src.width = ALIGN(src.width, x_align) * 2;
186676c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      src.height = ALIGN(src.height, y_align) / 2;
186776c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      src.x_offset *= 2;
186876c1c34c4aa2fa48126aee8d16e943bf0e3ff750Paul Berry      src.y_offset /= 2;
1869602e9a0f3727b036caf3a7b228fe90d36d832ea7Paul Berry   }
1870506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1871506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1872506d70be21cd3469118de89297cba0c0f709c1aePaul Berryuint32_t
1873506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_params::get_wm_prog(struct brw_context *brw,
1874506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                   brw_blorp_prog_data **prog_data) const
1875506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1876506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   uint32_t prog_offset;
1877506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!brw_search_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1878506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &this->wm_prog_key, sizeof(this->wm_prog_key),
1879506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &prog_offset, prog_data)) {
1880506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_blorp_blit_program prog(brw, &this->wm_prog_key);
1881506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GLuint program_size;
1882506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const GLuint *program = prog.compile(brw, &program_size);
1883506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_upload_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1884506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &this->wm_prog_key, sizeof(this->wm_prog_key),
1885506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       program, program_size,
1886506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog.prog_data, sizeof(prog.prog_data),
1887506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog_offset, prog_data);
1888506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1889506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return prog_offset;
1890506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1891