brw_blorp_blit.cpp revision c738ea1191cd1b5a0dc60b0e6d05fd918083e961
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 *
115fa1d267beb4adb542ea90b805306599f602c38d2Paul Berryfind_miptree(GLbitfield buffer_bit, struct gl_renderbuffer *rb)
116fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
117fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct intel_renderbuffer *irb = intel_renderbuffer(rb);
118fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct intel_mipmap_tree *mt = irb->mt;
119fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
120fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      mt = mt->stencil_mt;
121fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   return mt;
122fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
123fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
124fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
125fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrystatic void
126fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrydo_blorp_blit(struct intel_context *intel, GLbitfield buffer_bit,
127fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              struct gl_renderbuffer *src_rb, struct gl_renderbuffer *dst_rb,
128fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              GLint srcX0, GLint srcY0,
129fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
130fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              bool mirror_x, bool mirror_y)
131fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
132fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct gl_context *ctx = &intel->ctx;
133fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
134fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Find source/dst miptrees */
135fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_rb);
136fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_rb);
137fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
138fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Get ready to blit.  This includes depth resolving the src and dst
139fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * buffers if necessary.
140fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    */
141fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   intel_renderbuffer_resolve_depth(intel, intel_renderbuffer(src_rb));
142fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   intel_renderbuffer_resolve_depth(intel, intel_renderbuffer(dst_rb));
143fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
144fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Do the blit */
145fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   brw_blorp_blit_params params(brw_context(ctx), src_mt, dst_mt,
146fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                                srcX0, srcY0, dstX0, dstY0, dstX1, dstY1,
147fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                                mirror_x, mirror_y);
148fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   brw_blorp_exec(intel, &params);
149fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
150fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Mark the dst buffer as needing a HiZ resolve if necessary. */
151fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   intel_renderbuffer_set_needs_hiz_resolve(intel_renderbuffer(dst_rb));
152fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
153fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
154fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
155fa1d267beb4adb542ea90b805306599f602c38d2Paul Berrystatic bool
156fa1d267beb4adb542ea90b805306599f602c38d2Paul Berryformats_match(GLbitfield buffer_bit, struct gl_renderbuffer *src_rb,
157fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry              struct gl_renderbuffer *dst_rb)
158fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry{
159fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Note: don't just check gl_renderbuffer::Format, because in some cases
160fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * multiple gl_formats resolve to the same native type in the miptree (for
161fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * example MESA_FORMAT_X8_Z24 and MESA_FORMAT_S8_Z24), and we can blit
162fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    * between those formats.
163fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry    */
164fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   return find_miptree(buffer_bit, src_rb)->format ==
165fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      find_miptree(buffer_bit, dst_rb)->format;
166fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry}
167fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
168fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry
169506d70be21cd3469118de89297cba0c0f709c1aePaul Berrystatic bool
170506d70be21cd3469118de89297cba0c0f709c1aePaul Berrytry_blorp_blit(struct intel_context *intel,
171506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
172506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
173506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLenum filter, GLbitfield buffer_bit)
174506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
175506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct gl_context *ctx = &intel->ctx;
176506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
177506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Sync up the state of window system buffers.  We need to do this before
178506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * we go looking for the buffers.
179506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
180506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_prepare_render(intel);
181506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
182506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *read_fb = ctx->ReadBuffer;
183506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *draw_fb = ctx->DrawBuffer;
184506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
185506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Detect if the blit needs to be mirrored */
186506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool mirror_x = false, mirror_y = false;
187506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, srcX0, srcX1);
188506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, dstX0, dstX1);
189506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, srcY0, srcY1);
190506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, dstY0, dstY1);
191506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
192506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Make sure width and height match */
193da54d2e576426122009be083ecbfb9eefd8a3799Paul Berry   if (srcX1 - srcX0 != dstX1 - dstX0) return false;
194da54d2e576426122009be083ecbfb9eefd8a3799Paul Berry   if (srcY1 - srcY0 != dstY1 - dstY0) return false;
195506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
19647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If the destination rectangle needs to be clipped or scissored, do so.
197506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
19847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (!(clip_or_scissor(mirror_x, srcX0, srcX1, dstX0, dstX1,
19947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Xmin, draw_fb->_Xmax) &&
20047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         clip_or_scissor(mirror_y, srcY0, srcY1, dstY0, dstY1,
20147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Ymin, draw_fb->_Ymax))) {
20247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
20347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      return true;
20447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
20547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
20675f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   /* If the source rectangle needs to be clipped or scissored, do so. */
20775f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   if (!(clip_or_scissor(mirror_x, dstX0, dstX1, srcX0, srcX1,
20875f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Width) &&
20975f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry         clip_or_scissor(mirror_y, dstY0, dstY1, srcY0, srcY1,
21075f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Height))) {
21175f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
21275f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      return true;
21375f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   }
214506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
2150dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   /* Account for the fact that in the system framebuffer, the origin is at
2160dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry    * the lower left.
2170dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry    */
218c738ea1191cd1b5a0dc60b0e6d05fd918083e961Paul Berry   if (_mesa_is_winsys_fbo(read_fb)) {
2190dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      GLint tmp = read_fb->Height - srcY0;
2200dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      srcY0 = read_fb->Height - srcY1;
2210dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      srcY1 = tmp;
2220dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      mirror_y = !mirror_y;
2230dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   }
224c738ea1191cd1b5a0dc60b0e6d05fd918083e961Paul Berry   if (_mesa_is_winsys_fbo(draw_fb)) {
2250dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      GLint tmp = draw_fb->Height - dstY0;
2260dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      dstY0 = draw_fb->Height - dstY1;
2270dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      dstY1 = tmp;
2280dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry      mirror_y = !mirror_y;
2290dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry   }
2300dbec6ae07e7b3d566cc397ab09caa413e412846Paul Berry
231fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   /* Find buffers */
232fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct gl_renderbuffer *src_rb;
233fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   struct gl_renderbuffer *dst_rb;
234fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   switch (buffer_bit) {
235fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_COLOR_BUFFER_BIT:
236fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      src_rb = read_fb->_ColorReadBuffer;
237ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
238ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry         dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
239ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry         if (dst_rb && !formats_match(buffer_bit, src_rb, dst_rb))
240ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry            return false;
241ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      }
242ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
243ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry         dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
244ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry         do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
245ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry                       dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
246ff9313fac70fa85d051dd4d2b9d3402d39f67ceaPaul Berry      }
247fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
248fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_DEPTH_BUFFER_BIT:
249fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      src_rb = read_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
250fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      dst_rb = draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
251fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      if (!formats_match(buffer_bit, src_rb, dst_rb))
252fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry         return false;
253fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
254fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                    dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
255fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
256fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   case GL_STENCIL_BUFFER_BIT:
257fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      src_rb = read_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
258fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      dst_rb = draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
259fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      if (!formats_match(buffer_bit, src_rb, dst_rb))
260fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry         return false;
261fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
262fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry                    dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
263fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      break;
264fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   default:
265fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry      assert(false);
266fa1d267beb4adb542ea90b805306599f602c38d2Paul Berry   }
267506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
268506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return true;
269506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
270506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
271506d70be21cd3469118de89297cba0c0f709c1aePaul BerryGLbitfield
272506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_framebuffer(struct intel_context *intel,
273506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
274506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
275506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLbitfield mask, GLenum filter)
276506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
277b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   /* BLORP is not supported before Gen6. */
278b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   if (intel->gen < 6)
279506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return mask;
280506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
281506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   static GLbitfield buffer_bits[] = {
282506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_COLOR_BUFFER_BIT,
283506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_DEPTH_BUFFER_BIT,
284506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_STENCIL_BUFFER_BIT,
285506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   };
286506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
287506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
288506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      if ((mask & buffer_bits[i]) &&
289506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       try_blorp_blit(intel,
290506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      srcX0, srcY0, srcX1, srcY1,
291506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      dstX0, dstY0, dstX1, dstY1,
292506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      filter, buffer_bits[i])) {
293506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         mask &= ~buffer_bits[i];
294506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      }
295506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
296506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
297506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return mask;
298506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
299506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
300665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
301665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry/**
302665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry * Enum to specify the order of arguments in a sampler message
303665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry */
304665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryenum sampler_message_arg
305665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
306665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_FLOAT,
307665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_FLOAT,
308665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_INT,
309665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_INT,
310233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_SI_INT,
3114ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   SAMPLER_MESSAGE_ARG_MCS_INT,
312233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_ZERO_INT,
313665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry};
314665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
315506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
316506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Generator for WM programs used in BLORP blits.
317506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
318506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * The bulk of the work done by the WM program is to wrap and unwrap the
319506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinate transformations used by the hardware to store surfaces in
32019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * memory.  The hardware transforms a pixel location (X, Y, S) (where S is the
32119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * sample index for a multisampled surface) to a memory offset by the
32219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * following formulas:
323506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3248b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
3258b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
32619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a single-sampled surface, or for a multisampled surface using
3281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
3291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * function:
33019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
3321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
3331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
3341bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
33519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3361bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
3371bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * embeds the sample number into bit 1 of the X and Y coordinates:
33819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3391bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
34019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
34119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
3421bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
34319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b11) >> 1 | (X & 0b1)
34419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
34519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           S = (Y & 0b10) | (X & 0b10) >> 1
346506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3477fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
3487fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
3497fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry * the Y coordinate:
3507fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *
3517fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
3527fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *     where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
3537fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
3547fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
3557fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *     where X' = (X & ~0b111) >> 2 | (X & 0b1)
3567fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
3577fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *           S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
3587fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *
359506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For X tiling, tile() combines together the low-order bits of the X and Y
360506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
361506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 8 rows high:
362506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3638b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(x_tiled, X, Y, S) = A
364506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3658b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
3668b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           offset = (Y' & 0b111) << 9
367506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X & 0b111111111)
368506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3698b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3708b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(x_tiled, A) = (X, Y, S)
371506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
3728b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
3738b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
3748b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 3
3758b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111000000000) >> 9
376506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 9
377506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111111111)
378506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
379506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In all tiling formulas, cpp is the number of bytes occupied by a single
3808b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
3818b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * to fill the width of the surface, and qpitch is the spacing (in rows)
3828b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * between array slices).
383506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
384506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For Y tiling, tile() combines together the low-order bits of the X and Y
385506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
386506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 32 rows high:
387506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3888b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(y_tiled, X, Y, S) = A
389506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3908b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
391506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b1110000) << 5
392506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (Y' & 0b11111) << 4
393506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1111)
394506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3958b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3968b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(y_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) << 5
4018b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111110000) >> 4
402506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 7
403506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 5
404506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1111)
405506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
406506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For W tiling, tile() combines together the low-order bits of the X and Y
407506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
408506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 64 rows high (note that W tiling is only used for stencil
4098b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * buffers, which always have cpp = 1 and S=0):
410506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4118b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(w_tiled, X, Y, S) = A
412506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
4138b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
414506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b111000) << 6
4158b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b111100) << 3
416506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b100) << 2
4178b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b10) << 2
418506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b10) << 1
4198b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b1) << 1
420506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1)
421506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp = X
4228b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
4238b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(w_tiled, A) = (X, Y, S)
424506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp = X'
4258b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch = Y'
4268b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y / qpitch = 0
4278b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 6
4288b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111100000) >> 3
4298b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b1000) >> 2
4308b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b10) >> 1
431506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 6
432506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 6
433506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b10000) >> 2
434506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b100) >> 1
435506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1)
436506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
437506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Finally, for a non-tiled surface, tile() simply combines together the X and
438506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Y coordinates in the natural way:
439506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
4408b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(untiled, X, Y, S) = A
441506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = Y * pitch + X'
442506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
4438b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
4448b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(untiled, A) = (X, Y, S)
445506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
4468b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
4478b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
448506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = A % pitch
4498b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = A / pitch
450506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
451506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In these formulas, pitch is the number of bytes occupied by a single row
45219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * of samples).
453506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
454506d70be21cd3469118de89297cba0c0f709c1aePaul Berryclass brw_blorp_blit_program
455506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
456506d70be21cd3469118de89297cba0c0f709c1aePaul Berrypublic:
457506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_blit_program(struct brw_context *brw,
458506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                          const brw_blorp_blit_prog_key *key);
459506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ~brw_blorp_blit_program();
460506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
461506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const GLuint *compile(struct brw_context *brw, GLuint *program_size);
462506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
463506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_prog_data prog_data;
464506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
465506d70be21cd3469118de89297cba0c0f709c1aePaul Berryprivate:
466506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_regs();
467506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_push_const_regs(int base_reg);
468506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void compute_frag_coords();
469506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_tiling(bool old_tiled_w, bool new_tiled_w);
4701bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void encode_msaa(unsigned num_samples, intel_msaa_layout layout);
4711bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void decode_msaa(unsigned num_samples, intel_msaa_layout layout);
472506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void kill_if_outside_dst_rect();
473506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_dst_to_src();
47419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   void single_to_blend();
47517eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   void manual_blend(unsigned num_samples);
4764725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void sample(struct brw_reg dst);
4774725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texel_fetch(struct brw_reg dst);
4784ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   void mcs_fetch();
479665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   void expand_to_32_bits(struct brw_reg src, struct brw_reg dst);
4804725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texture_lookup(struct brw_reg dst, GLuint msg_type,
4814725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                       const sampler_message_arg *args, int num_args);
482506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void render_target_write();
483506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
484b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /**
485b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Base-2 logarithm of the maximum number of samples that can be blended.
486b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
48717eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   static const unsigned LOG2_MAX_BLEND_SAMPLES = 3;
488b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
489506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void *mem_ctx;
490506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_context *brw;
491506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const brw_blorp_blit_prog_key *key;
492506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_compile func;
493506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
494506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Thread dispatch header */
495506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R0;
496506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
497506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Pixel X/Y coordinates (always in R1). */
498506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R1;
499506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
500506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Push constants */
501506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x0;
502506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x1;
503506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y0;
504506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y1;
505506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct {
506506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg multiplier;
507506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg offset;
508506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } x_transform, y_transform;
509506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
510b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Data read from texture (4 vec16's per array element) */
511b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   struct brw_reg texture_data[LOG2_MAX_BLEND_SAMPLES + 1];
512506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
5134ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   /* Auxiliary storage for the contents of the MCS surface.
5144ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    *
5154ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * Since the sampler always returns 8 registers worth of data, this is 8
5164ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * registers wide, even though we only use the first 2 registers of it.
5174ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    */
5184ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   struct brw_reg mcs_data;
5194ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
520506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* X coordinates.  We have two of them so that we can perform coordinate
521506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
522506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
523506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg x_coords[2];
524506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
525506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Y coordinates.  We have two of them so that we can perform coordinate
526506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
527506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
528506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg y_coords[2];
529506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
530506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Which element of x_coords and y_coords is currently in use.
531506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
532506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int xy_coord_index;
533506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
53419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* True if, at the point in the program currently being compiled, the
53519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * sample index is known to be zero.
53619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
53719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   bool s_is_zero;
53819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
53919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Register storing the sample index when s_is_zero is false. */
54019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct brw_reg sample_index;
54119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
542506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Temporaries */
543506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t1;
544506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t2;
545506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
546665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   /* MRF used for sampling and render target writes */
547506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   GLuint base_mrf;
548506d70be21cd3469118de89297cba0c0f709c1aePaul Berry};
549506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
550506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::brw_blorp_blit_program(
551506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_context *brw,
552506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const brw_blorp_blit_prog_key *key)
553506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   : mem_ctx(ralloc_context(NULL)),
554506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     brw(brw),
555506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     key(key)
556506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
557506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_init_compile(brw, &func, mem_ctx);
558506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
559506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
560506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::~brw_blorp_blit_program()
561506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
562506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ralloc_free(mem_ctx);
563506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
564506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
565506d70be21cd3469118de89297cba0c0f709c1aePaul Berryconst GLuint *
566506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compile(struct brw_context *brw,
567506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                GLuint *program_size)
568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
56919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Sanity checks */
57034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->dst_tiled_w && key->rt_samples > 0) {
57134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination image is W tiled and multisampled, then the thread
57234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * must be dispatched once per sample, not once per pixel.  This is
57334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * necessary because after conversion between W and Y tiling, there's no
57419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * guarantee that all samples corresponding to a single pixel will still
57519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * be together.
57619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
57734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->persample_msaa_dispatch);
57819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
57919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
58019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
5814725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      /* We are blending, which means we won't have an opportunity to
5824725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry       * translate the tiling and sample count for the texture surface.  So
58319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the surface state for the texture must be configured with the correct
58419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * tiling and sample count.
58519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
58619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(!key->src_tiled_w);
58719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples == key->src_samples);
5881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(key->tex_layout == key->src_layout);
58919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples > 0);
59019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
59119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
59234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
59334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* It only makes sense to do persample dispatch if the render target is
59434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * configured as multisampled.
59534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
59634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->rt_samples > 0);
59734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
59834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
5991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Make sure layout is consistent with sample count */
6001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
6011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->tex_samples == 0));
6021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
6031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->rt_samples == 0));
6041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
6051bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->src_samples == 0));
6061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
6071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->dst_samples == 0));
6088b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
60934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   /* Set up prog_data */
61034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   memset(&prog_data, 0, sizeof(prog_data));
61134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   prog_data.persample_msaa_dispatch = key->persample_msaa_dispatch;
61234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
613506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
614506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
615506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_regs();
616506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   compute_frag_coords();
617506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
618506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Render target and texture hardware don't support W tiling. */
619506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool rt_tiled_w = false;
620506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool tex_tiled_w = false;
621506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
622506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* The address that data will be written to is determined by the
62319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates supplied to the WM thread and the tiling and sample count of
62419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the render target, according to the formula:
625506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
62619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
627506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
62819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * If the actual tiling and sample count of the destination surface are not
62919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the same as the configuration of the render target, then these
63019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates are wrong and we have to adjust them to compensate for the
63119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * difference.
632506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
63319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (rt_tiled_w != key->dst_tiled_w ||
6348b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       key->rt_samples != key->dst_samples ||
6351bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       key->rt_layout != key->dst_layout) {
6361bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      encode_msaa(key->rt_samples, key->rt_layout);
6378b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(rt_tiling, offset) */
638506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      translate_tiling(rt_tiled_w, key->dst_tiled_w);
6398b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(dst_tiling, offset) */
6401bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      decode_msaa(key->dst_samples, key->dst_layout);
64119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
642506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
64319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
644506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
64519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * That is: X, Y and S now contain the true coordinates and sample index of
64619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the data that the WM thread should output.
647506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
648506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * If we need to kill pixels that are outside the destination rectangle,
649506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * now is the time to do it.
650506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
651506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
652506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (key->use_kill)
653506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      kill_if_outside_dst_rect();
654506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
655506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Next, apply a translation to obtain coordinates in the source image. */
656506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   translate_dst_to_src();
657506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
65819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* If the source image is not multisampled, then we want to fetch sample
65919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * number 0, because that's the only sample there is.
660506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
66119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->src_samples == 0)
66219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = true;
663506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
66419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* X, Y, and S are now the coordinates of the pixel in the source image
66519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that we want to texture from.  Exception: if we are blending, then S is
66619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * irrelevant, because we are going to fetch all samples.
667506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
66819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
6694725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      if (brw->intel.gen == 6) {
6704725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen6 hardware an automatically blend using the SAMPLE message */
6714725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         single_to_blend();
672b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         sample(texture_data[0]);
6734725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      } else {
6744725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen7+ hardware doesn't automaticaly blend. */
67517eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry         manual_blend(key->src_samples);
6764725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
67719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   } else {
67819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We aren't blending, which means we just want to fetch a single sample
67919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * from the source surface.  The address that we want to fetch from is
68019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * related to the X, Y and S values according to the formula:
68119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
68219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
68319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
68419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * If the actual tiling and sample count of the source surface are not
68519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the same as the configuration of the texture, then we need to adjust
68619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the coordinates to compensate for the difference.
68719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
68819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      if (tex_tiled_w != key->src_tiled_w ||
6898b1f467cce34340637e9baca4847fc5273cf7541Paul Berry          key->tex_samples != key->src_samples ||
6901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          key->tex_layout != key->src_layout) {
6911bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         encode_msaa(key->src_samples, key->src_layout);
6928b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(src_tiling, offset) */
69319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         translate_tiling(key->src_tiled_w, tex_tiled_w);
6948b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(tex_tiling, offset) */
6951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         decode_msaa(key->tex_samples, key->tex_layout);
69619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
697506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
69819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
69919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
70019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * In other words: X, Y, and S now contain values which, when passed to
70119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the texturing unit, will cause data to be read from the correct
70219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * memory location.  So we can fetch the texel now.
70319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
7044ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
7054ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         mcs_fetch();
706b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[0]);
70719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
70819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
70919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Finally, write the fetched (or blended) value to the render target and
71019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * terminate the thread.
711506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
712506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   render_target_write();
713506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return brw_get_program(&func, program_size);
714506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
715506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
716506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
717506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_push_const_regs(int base_reg)
718506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
719506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
720506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define ALLOC_REG(name) \
721506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->name = \
722506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, base_reg, CONST_LOC(name) / 2)
723506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
724506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x0);
725506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x1);
726506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y0);
727506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y1);
728506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.multiplier);
729506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.offset);
730506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.multiplier);
731506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.offset);
732506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef CONST_LOC
733506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef ALLOC_REG
734506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
735506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
736506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
737506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_regs()
738506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
739506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int reg = 0;
740506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
741506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
742506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   prog_data.first_curbe_grf = reg;
743506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_push_const_regs(reg);
744506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
745b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   for (unsigned i = 0; i < ARRAY_SIZE(texture_data); ++i) {
746e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      this->texture_data[i] =
747e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         retype(vec16(brw_vec8_grf(reg, 0)), key->texture_data_type);
748e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      reg += 8;
749b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   }
7504ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   this->mcs_data =
7514ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD); reg += 8;
752506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 2; ++i) {
753506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->x_coords[i]
754506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
755506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->y_coords[i]
756506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
757506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
758506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->xy_coord_index = 0;
75919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   this->sample_index
76019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
761506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t1 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
762506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t2 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
763506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
764b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Make sure we didn't run out of registers */
765b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(reg <= GEN7_MRF_HACK_START);
766b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
767506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf = 2;
768506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->base_mrf = mrf;
769506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
770506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
771506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* In the code that follows, X and Y can be used to quickly refer to the
772506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
773506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * prime") to the inactive elements.
77419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
77519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * S can be used to quickly refer to sample_index.
776506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
777506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define X x_coords[xy_coord_index]
778506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Y y_coords[xy_coord_index]
779506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Xp x_coords[!xy_coord_index]
780506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Yp y_coords[!xy_coord_index]
78119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#define S sample_index
782506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
783506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* Quickly swap the roles of (X, Y) and (Xp, Yp).  Saves us from having to do
784506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
785506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
786506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
787506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
788506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
789506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compute the X and Y coordinates of the pixels being rendered
790506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * by this WM invocation.
791506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
792506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Assuming the render target is set up for Y tiling, these (X, Y) values are
793506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * related to the address offset where outputs will be written by the formula:
794506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
795506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *   (X, Y, S) = decode_msaa(detile(offset)).
796506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
797506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
798506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
799506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
800506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compute_frag_coords()
801506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
802506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
803506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
804506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
805506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
806506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
807506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Pixels within a subspan are laid out in this arrangement:
808506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 0 1
809506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 2 3
810506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
811506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * So, to compute the coordinates of each pixel, we need to read every 2nd
812506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
813506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
814506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * In other words, the data we want to access is R1.4<2;4,0>UW.
815506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
816506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
817506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * result, since pixels n+1 and n+3 are in the right half of the subspan.
818506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
819506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, X, stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
820506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
821506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
822506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
823506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
824506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4<2;4,0>UW).
825506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
826506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
827506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * pixels n+2 and n+3 are in the bottom half of the subspan.
828506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
829506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Y, stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
83019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
83134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
832619471dc322de80942f7dbb29a437890e48155c6Paul Berry      switch (key->rt_samples) {
833619471dc322de80942f7dbb29a437890e48155c6Paul Berry      case 4:
834619471dc322de80942f7dbb29a437890e48155c6Paul Berry         /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
835619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Therefore, subspan 0 will represent sample 0, subspan 1 will
836619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * represent sample 1, and so on.
837619471dc322de80942f7dbb29a437890e48155c6Paul Berry          *
838619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
839619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 1, 2, 2, 2, 2, 3, 3, 3, 3).  The easiest way to do this is to
840619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * populate a temporary variable with the sequence (0, 1, 2, 3), and
841619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * then copy from it using vstride=1, width=4, hstride=0.
842619471dc322de80942f7dbb29a437890e48155c6Paul Berry          */
843619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, t1, brw_imm_v(0x3210));
844619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, S, stride(t1, 1, 4, 0));
845619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
846619471dc322de80942f7dbb29a437890e48155c6Paul Berry      case 8: {
847619471dc322de80942f7dbb29a437890e48155c6Paul Berry         /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
848619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
849619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * subspan 1 will represent sample 1, and so on.  We can find the
850619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
851619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Index") and multiplying by two (since samples are always delivered
852619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * in pairs).  That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
853619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 0xc0) >> 5.
854619471dc322de80942f7dbb29a437890e48155c6Paul Berry          *
855619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
856619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
857619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * variable with the sequence (0, 1, 2, 3), and then reading from it
858619471dc322de80942f7dbb29a437890e48155c6Paul Berry          * using vstride=1, width=4, hstride=0.
859619471dc322de80942f7dbb29a437890e48155c6Paul Berry          */
860619471dc322de80942f7dbb29a437890e48155c6Paul Berry         struct brw_reg t1_ud1 = vec1(retype(t1, BRW_REGISTER_TYPE_UD));
861619471dc322de80942f7dbb29a437890e48155c6Paul Berry         struct brw_reg r0_ud1 = vec1(retype(R0, BRW_REGISTER_TYPE_UD));
862619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_AND(&func, t1_ud1, r0_ud1, brw_imm_ud(0xc0));
863619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_SHR(&func, t1_ud1, t1_ud1, brw_imm_ud(5));
864619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_MOV(&func, t2, brw_imm_v(0x3210));
865619471dc322de80942f7dbb29a437890e48155c6Paul Berry         brw_ADD(&func, S, retype(t1_ud1, BRW_REGISTER_TYPE_UW),
866619471dc322de80942f7dbb29a437890e48155c6Paul Berry                 stride(t2, 1, 4, 0));
867619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
868619471dc322de80942f7dbb29a437890e48155c6Paul Berry      }
869619471dc322de80942f7dbb29a437890e48155c6Paul Berry      default:
870619471dc322de80942f7dbb29a437890e48155c6Paul Berry         assert(!"Unrecognized sample count in "
871619471dc322de80942f7dbb29a437890e48155c6Paul Berry                "brw_blorp_blit_program::compute_frag_coords()");
872619471dc322de80942f7dbb29a437890e48155c6Paul Berry         break;
873619471dc322de80942f7dbb29a437890e48155c6Paul Berry      }
87434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = false;
87534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   } else {
87634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* Either the destination surface is single-sampled, or the WM will be
87734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
87834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel).  In either case, it's not meaningful to compute a sample
87934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * value.  Just set it to 0.
88034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
88134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = true;
88234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
883506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
884506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
885506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
886506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compensate for the difference between Y and W tiling.
887506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
888506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * This code modifies the X and Y coordinates according to the formula:
889506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
8908b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
891506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
892506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
893506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
894506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * It can only translate between W and Y tiling, so new_tiling and old_tiling
895506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * are booleans where true represents W tiling and false represents Y tiling.
896506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
897506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
898506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
899506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
900506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (old_tiled_w == new_tiled_w)
901506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return;
902506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
9038b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   /* In the code that follows, we can safely assume that S = 0, because W
9041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * tiling formats always use IMS layout.
9058b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
9068b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   assert(s_is_zero);
9078b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
908506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (new_tiled_w) {
909506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Given X and Y coordinates that describe an address using Y tiling,
910506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * translate to the X and Y coordinates that describe the same address
911506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * using W tiling.
912506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
913506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we break down the low order bits of X and Y, using a
914506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * single letter to represent each low-order bit:
915506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
916506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X = A << 7 | 0bBCDEFGH
917506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y = J << 5 | 0bKLMNP                                       (1)
918506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
919506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Then we can apply the Y tiling formula to see the memory offset being
920506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * addressed:
921506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
922506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH       (2)
923506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
924506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we apply the W detiling formula to this memory location, that the
925506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * corresponding X' and Y' coordinates are:
926506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
927506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = A << 6 | 0bBCDPFH                                     (3)
928506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = J << 6 | 0bKLMNEG
929506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
930506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
931506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * we need to make the following computation:
932506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
933506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1         (4)
934506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
935506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
936506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
937506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
938506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
939506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
940506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
941506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
942506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
943506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
944506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
945506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(8)); /* X & 0b1000 */
946506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
947506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
948506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
949506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
950506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
951506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
952506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
953506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Applying the same logic as above, but in reverse, we obtain the
954506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * formulas:
955506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
956506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
957506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
958506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
959506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
960506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
961506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
962506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
963506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
964506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
965506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
966506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
967506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                    | (Y & 0b1) << 1 */
968506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
969506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
970506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
971506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
972506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(4)); /* X & 0b100 */
973506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
974506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
975506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
976506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
977506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
978506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
979506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
98019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
98119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
98219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
98319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
98419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
9857fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
98619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
98719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
98819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
98919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
9901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::encode_msaa(unsigned num_samples,
9911bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
99219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
9931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
9941bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
9958b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
9968b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
9971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
9991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
10001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we haven't read from the MCS buffer.
10011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
10021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
10031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
100519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
10061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
10087fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      switch (num_samples) {
10097fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 4:
10107fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
10117fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
10127fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
10137fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
10147fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
10157fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10167fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
10177fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
10187fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10197fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
10207fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry                                                   | (S & 0b1) << 1 */
10217fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
10227fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
10237fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
10247fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
10257fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10267fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
10277fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
10287fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10297fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
10307fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
10317fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
10327fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 8:
10337fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
10347fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
10357fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *              | (X & 0b1)
10367fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
10377fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
10387fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
10397fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
10407fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10417fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(4)); /* S & 0b100 */
10427fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100) */
10437fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
10447fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (S & 0b1) << 1 */
10457fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100)
10467fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry                                          | (S & 0b1) << 1 */
10477fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10487fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
10497fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
10507fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
10517fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
10527fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         if (!s_is_zero) {
10537fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
10547fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry            brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
10557fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         }
10567fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
10577fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
10587fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
105919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
106019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
10618b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      s_is_zero = true;
10621bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
106319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
106419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
106519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
106619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
106719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
106819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
106919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
107019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
107119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
10727fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry *   (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
107319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
107419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
107519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
107619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
10771bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::decode_msaa(unsigned num_samples,
10781bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
107919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
10801bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
10811bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
10828b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
10838b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
10841bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10851bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
10861bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
10871bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we don't have access to the MCS buffer.
10881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
10891bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
10901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10911bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
109219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
10931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
10941bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
10958b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
10967fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      switch (num_samples) {
10977fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 4:
10987fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
10997fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b11) >> 1 | (X & 0b1)
11007fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
11017fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         S = (Y & 0b10) | (X & 0b10) >> 1
11027fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
11037fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
11047fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
11057fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
11067fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
11077fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
11087fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
11097fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
11107fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
11117fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
11127fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
11137fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
11147fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, S, t1, t2);
11157fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
11167fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      case 8:
11177fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
11187fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *   where X' = (X & ~0b111) >> 2 | (X & 0b1)
11197fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
11207fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          *         S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
11217fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry          */
11227fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(0xfff8)); /* X & ~0b111 */
11237fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
11247fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
11257fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Xp, t1, t2);
11267fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
11277fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
11287fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
11297fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, Yp, t1, t2);
11307fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t1, X, brw_imm_uw(4)); /* X & 0b100 */
11317fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
11327fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, t1, t1, t2); /* (X & 0b100) | (Y & 0b10) */
11337fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
11347fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
11357fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         brw_OR(&func, S, t1, t2);
11367fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry         break;
11377fae97c98bfa13bff0e9da857e86eefdb625584cPaul Berry      }
113819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = false;
113919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
11401bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
114119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
114219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
114319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
114419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1145506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code that kills pixels whose X and Y coordinates are outside the
1146506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
1147506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * dst_x1, dst_y1).
1148506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1149506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1150506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::kill_if_outside_dst_rect()
1151506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1152506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg f0 = brw_flag_reg();
1153506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1154506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg null16 = vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
1155506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1156506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, X, dst_x0);
1157506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, Y, dst_y0);
1158506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, X, dst_x1);
1159506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, Y, dst_y1);
1160506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1161506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_predicate_control(&func, BRW_PREDICATE_NONE);
1162506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_push_insn_state(&func);
1163506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_mask_control(&func, BRW_MASK_DISABLE);
1164506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_AND(&func, g1, f0, g1);
1165506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_pop_insn_state(&func);
1166506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1167506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1168506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
1169506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1170506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates.
1171506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1172506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1173506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_dst_to_src()
1174506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1175506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Xp, X, x_transform.multiplier);
1176506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Yp, Y, y_transform.multiplier);
1177506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Xp, Xp, x_transform.offset);
1178506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Yp, Yp, y_transform.offset);
1179506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   SWAP_XY_AND_XPYP();
1180506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1181506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1182506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
118319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to transform the X and Y coordinates as needed for blending
118419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * together the different samples in an MSAA texture.
118519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
118619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
118719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrybrw_blorp_blit_program::single_to_blend()
118819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
118919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* When looking up samples in an MSAA texture using the SAMPLE message,
119019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * Gen6 requires the texture coordinates to be odd integers (so that they
119119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * correspond to the center of a 2x2 block representing the four samples
119219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that maxe up a pixel).  So we need to multiply our X and Y coordinates
119319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * each by 2 and then add 1.
119419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
119519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t1, X, brw_imm_w(1));
119619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t2, Y, brw_imm_w(1));
119719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Xp, t1, brw_imm_w(1));
119819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Yp, t2, brw_imm_w(1));
119919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   SWAP_XY_AND_XPYP();
120019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
120119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1202b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1203b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry/**
1204b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * Count the number of trailing 1 bits in the given value.  For example:
1205b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry *
1206b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(0) == 0
1207b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(7) == 3
1208b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(11) == 2
1209b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry */
1210b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berryinline int count_trailing_one_bits(unsigned value)
1211b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry{
1212b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1213b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return __builtin_ctz(~value);
1214b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#else
1215b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return _mesa_bitcount(value & ~(value + 1));
1216b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#endif
1217b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry}
1218b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1219b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
12204725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berryvoid
122117eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berrybrw_blorp_blit_program::manual_blend(unsigned num_samples)
12224725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry{
12234ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
12244ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      mcs_fetch();
12254ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
1226b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1227b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1228b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *   result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1229b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1230b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * This ensures that when all samples have the same value, no numerical
1231b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision is lost, since each addition operation always adds two equal
1232b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * values, and summing two equal floating point values does not lose
1233b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision.
1234b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1235b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * We perform this computation by treating the texture_data array as a
1236b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * stack and performing the following operations:
1237b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1238b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 0 onto stack
1239b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 1 onto stack
1240b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1241b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 2 onto stack
1242b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 3 onto stack
1243b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1244b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1245b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - divide top stack entry by 4
1246b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1247b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Note that after pushing sample i onto the stack, the number of add
1248b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * operations we do is equal to the number of trailing 1 bits in i.  This
1249b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * works provided the total number of samples is a power of two, which it
1250b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * always is for i965.
1251e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    *
1252e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * For integer formats, we replace the add operations with average
1253e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * operations and skip the final division.
1254b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
1255e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   typedef struct brw_instruction *(*brw_op2_ptr)(struct brw_compile *,
1256e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1257e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1258e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg);
1259e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   brw_op2_ptr combine_op =
1260e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      key->texture_data_type == BRW_REGISTER_TYPE_F ? brw_ADD : brw_AVG;
1261b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   unsigned stack_depth = 0;
126217eae9762cdd6cfa69a060001e26113dfc0d7c86Paul Berry   for (unsigned i = 0; i < num_samples; ++i) {
1263b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
1264b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1265b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Push sample i onto the stack */
1266b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth < ARRAY_SIZE(texture_data));
1267b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      if (i == 0) {
1268b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = true;
1269b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      } else {
1270b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = false;
1271b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         brw_MOV(&func, S, brw_imm_uw(i));
1272b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      }
1273b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[stack_depth++]);
1274b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1275f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      if (i == 0 && key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
1276f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1277f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * suggests an optimization:
1278f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1279f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     "A simple optimization with probable large return in
1280f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     performance is to compare the MCS value to zero (indicating
1281f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     all samples are on sample slice 0), and sample only from
1282f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     sample slice 0 using ld2dss if MCS is zero."
1283f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1284f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Note that in the case where the MCS value is zero, sampling from
1285f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * sample slice 0 using ld2dss and sampling from sample 0 using
1286f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * ld2dms are equivalent (since all samples are on sample slice 0).
1287f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Since we have already sampled from sample 0, all we need to do is
1288f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * skip the remaining fetches and averaging if MCS is zero.
1289f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          */
1290f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_CMP(&func, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ,
1291f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry                 mcs_data, brw_imm_ud(0));
1292f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_IF(&func, BRW_EXECUTE_16);
1293f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      }
1294f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1295b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Do count_trailing_one_bits(i) times */
1296b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      for (int j = count_trailing_one_bits(i); j-- > 0; ) {
1297b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         assert(stack_depth >= 2);
1298b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         --stack_depth;
1299b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1300b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         /* TODO: should use a smaller loop bound for non_RGBA formats */
1301b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         for (int k = 0; k < 4; ++k) {
1302e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry            combine_op(&func, offset(texture_data[stack_depth - 1], 2*k),
1303e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth - 1]), 2*k),
1304e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth]), 2*k));
1305b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         }
13064725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
13074725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
13084725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
1309b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We should have just 1 sample on the stack now. */
1310b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(stack_depth == 1);
1311b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1312e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   if (key->texture_data_type == BRW_REGISTER_TYPE_F) {
1313e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* Scale the result down by a factor of num_samples */
1314e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* TODO: should use a smaller loop bound for non-RGBA formats */
1315e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      for (int j = 0; j < 4; ++j) {
1316e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         brw_MUL(&func, offset(texture_data[0], 2*j),
1317e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 offset(vec8(texture_data[0]), 2*j),
1318e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 brw_imm_f(1.0/num_samples));
1319e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
13204725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
1321f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1322f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1323f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      brw_ENDIF(&func);
13244725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry}
13254725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
132619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
132719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to look up a value in the texture using the SAMPLE message (which
132819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * does blending of MSAA surfaces).
132919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
133019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
13314725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::sample(struct brw_reg dst)
133219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
1333665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   static const sampler_message_arg args[2] = {
1334665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_FLOAT,
1335665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_V_FLOAT
1336665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
1337665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
13384725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE, args, ARRAY_SIZE(args));
133919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
134019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
134119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1342506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to look up a value in the texture using the SAMPLE_LD message
1343506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (which does a simple texel fetch).
1344506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1345506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
13464725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texel_fetch(struct brw_reg dst)
1347506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
13481c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen6_args[5] = {
1349665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
1350233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_V_INT,
1351233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* R */
1352233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
1353233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT
1354665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
13551c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld_args[3] = {
13561c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13571c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
13581c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13591c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
13601c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld2dss_args[3] = {
13611c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
13621c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13631c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13641c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
13654ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld2dms_args[4] = {
13664ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
13674ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_MCS_INT,
13684ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
13694ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
13704ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
1371665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
13721c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   switch (brw->intel.gen) {
13731c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 6:
13744725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen6_args,
13751c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                     s_is_zero ? 2 : 5);
13761c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
13771c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 7:
137860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      switch (key->tex_layout) {
137960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_IMS:
138060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
138160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * Format):
138260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *
138360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     If this field is MSFMT_DEPTH_STENCIL
138460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
138560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *     messages allowed are "ld2dms", "resinfo", and "sampleinfo".
138660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          *
138760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * So fall through to emit the same message as we use for
138860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          * INTEL_MSAA_LAYOUT_CMS.
138960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry          */
139060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_CMS:
139160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS,
139260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry                        gen7_ld2dms_args, ARRAY_SIZE(gen7_ld2dms_args));
139360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
139460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_UMS:
139560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS,
139660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry                        gen7_ld2dss_args, ARRAY_SIZE(gen7_ld2dss_args));
139760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
139860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      case INTEL_MSAA_LAYOUT_NONE:
13991c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry         assert(s_is_zero);
14004725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen7_ld_args,
14011c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                        ARRAY_SIZE(gen7_ld_args));
140260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         break;
14031c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      }
14041c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
14051c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   default:
14061c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      assert(!"Should not get here.");
14071c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
14081c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
1409506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1410506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1411506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
14124ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berrybrw_blorp_blit_program::mcs_fetch()
14134ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry{
14144ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld_mcs_args[2] = {
14154ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
14164ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
14174ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
14184ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   texture_lookup(vec16(mcs_data), GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS,
14194ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry                  gen7_ld_mcs_args, ARRAY_SIZE(gen7_ld_mcs_args));
14204ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry}
14214ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
14224ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berryvoid
1423665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berrybrw_blorp_blit_program::expand_to_32_bits(struct brw_reg src,
1424665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                          struct brw_reg dst)
1425506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1426665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, vec8(dst), vec8(src));
1427506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
1428665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, offset(vec8(dst), 1), suboffset(vec8(src), 8));
1429506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
1430665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry}
1431665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
1432665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryvoid
14334725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texture_lookup(struct brw_reg dst,
14344725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                                       GLuint msg_type,
1435665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       const sampler_message_arg *args,
1436665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       int num_args)
1437665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
1438665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   struct brw_reg mrf =
1439665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_UD);
1440665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   for (int arg = 0; arg < num_args; ++arg) {
1441665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      switch (args[arg]) {
1442665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_FLOAT:
1443665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, retype(mrf, BRW_REGISTER_TYPE_F));
1444665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1445665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_FLOAT:
1446665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, retype(mrf, BRW_REGISTER_TYPE_F));
1447665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1448665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_INT:
1449665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, mrf);
1450665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1451665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_INT:
1452665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, mrf);
1453665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1454233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_SI_INT:
1455233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         /* Note: on Gen7, this code may be reached with s_is_zero==true
1456233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * because in Gen7's ld2dss message, the sample index is the first
1457233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * argument.  When this happens, we need to move a 0 into the
1458233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * appropriate message register.
1459233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          */
1460233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         if (s_is_zero)
1461233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            brw_MOV(&func, mrf, brw_imm_ud(0));
1462233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         else
1463233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            expand_to_32_bits(S, mrf);
1464233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
14654ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      case SAMPLER_MESSAGE_ARG_MCS_INT:
146660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         switch (key->tex_layout) {
146760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         case INTEL_MSAA_LAYOUT_CMS:
146860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            brw_MOV(&func, mrf, mcs_data);
146960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
147060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         case INTEL_MSAA_LAYOUT_IMS:
147160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            /* When sampling from an IMS surface, MCS data is not relevant,
147260c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             * and the hardware ignores it.  So don't bother populating it.
147360c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             */
147460c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
147560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         default:
147660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            /* We shouldn't be trying to send MCS data with any other
147760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             * layouts.
147860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry             */
147960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            assert (!"Unsupported layout for MCS data");
148060c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry            break;
148160c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry         }
14824ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         break;
1483233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_ZERO_INT:
1484233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         brw_MOV(&func, mrf, brw_imm_ud(0));
1485233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
1486665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      }
1487665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      mrf.nr += 2;
1488665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   }
1489506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1490506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_SAMPLE(&func,
14914725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry              retype(dst, BRW_REGISTER_TYPE_UW) /* dest */,
1492506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              base_mrf /* msg_reg_nr */,
1493665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              brw_message_reg(base_mrf) /* src0 */,
1494506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX,
1495665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              0 /* sampler */,
1496506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              WRITEMASK_XYZW,
1497506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              msg_type,
1498506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              8 /* response_length.  TODO: should be smaller for non-RGBA formats? */,
1499665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              mrf.nr - base_mrf /* msg_length */,
1500506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              0 /* header_present */,
1501506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_SIMD_MODE_SIMD16,
1502506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_RETURN_FORMAT_FLOAT32);
1503506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1504506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1505506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef X
1506506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef Y
1507506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef U
1508506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef V
1509506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef S
1510506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef SWAP_XY_AND_XPYP
1511506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1512506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1513506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::render_target_write()
1514506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1515e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   struct brw_reg mrf_rt_write =
1516e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      retype(vec16(brw_message_reg(base_mrf)), key->texture_data_type);
1517506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf_offset = 0;
1518506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1519506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* If we may have killed pixels, then we need to send R0 and R1 in a header
1520506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * so that the render target knows which pixels we killed.
1521506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
1522506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool use_header = key->use_kill;
1523506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (use_header) {
1524506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Copy R0/1 to MRF */
1525506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_MOV(&func, retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
1526506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              retype(R0, BRW_REGISTER_TYPE_UD));
1527506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1528506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1529506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1530506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Copy texture data to MRFs */
1531506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 4; ++i) {
1532506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
15334725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      brw_MOV(&func, offset(mrf_rt_write, mrf_offset),
1534b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry              offset(vec8(texture_data[0]), 2*i));
1535506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1536506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1537506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1538506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Now write to the render target and terminate the thread */
1539506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_fb_WRITE(&func,
1540506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                16 /* dispatch_width */,
1541506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                base_mrf /* msg_reg_nr */,
1542506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_rt_write /* src0 */,
154329362875f2613ad87abe7725ce3c56c36d16cf9bEric Anholt                BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
1544506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
1545506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_offset /* msg_length.  TODO: Should be smaller for non-RGBA formats. */,
1546506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                0 /* response_length */,
1547506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                true /* eot */,
1548506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                use_header);
1549506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1550506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1551506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1552506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1553506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_coord_transform_params::setup(GLuint src0, GLuint dst0, GLuint dst1,
1554506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                        bool mirror)
1555506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1556506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!mirror) {
1557506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When not mirroring a coordinate (say, X), we need:
1558506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = x - dst_x0
1559506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1560506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = 1*x + (src_x0 - dst_x0)
1561506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1562506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = 1;
1563506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 - dst0;
1564506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
1565506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When mirroring X we need:
1566506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = dst_x1 - x - 1
1567506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = -1*x + (src_x0 + dst_x1 - 1)
1569506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1570506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = -1;
1571506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 + dst1 - 1;
1572506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1573506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1574506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1575506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
15761bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry/**
15771bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * Determine which MSAA layout the GPU pipeline should be configured for,
15781bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * based on the chip generation, the number of samples, and the true layout of
15791bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * the image in memory.
15801bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry */
15811bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berryinline intel_msaa_layout
15821bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrycompute_msaa_layout_for_pipeline(struct brw_context *brw, unsigned num_samples,
15831bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                 intel_msaa_layout true_layout)
15841bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry{
15851bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   if (num_samples == 0) {
15861bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* When configuring the GPU for non-MSAA, we can still accommodate IMS
15871bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * format buffers, by transforming coordinates appropriately.
15881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
15891bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_NONE ||
15901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry             true_layout == INTEL_MSAA_LAYOUT_IMS);
15911bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_NONE;
15920dd5e98aa5c146ef21ab44b34fb7714206d5ec08Paul Berry   } else {
15930dd5e98aa5c146ef21ab44b34fb7714206d5ec08Paul Berry      assert(true_layout != INTEL_MSAA_LAYOUT_NONE);
15941bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
15951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
15961bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Prior to Gen7, all MSAA surfaces use IMS layout. */
15971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   if (brw->intel.gen == 6) {
15981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_IMS);
15991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
16001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   return true_layout;
16021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry}
16031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
16058b1f467cce34340637e9baca4847fc5273cf7541Paul Berrybrw_blorp_blit_params::brw_blorp_blit_params(struct brw_context *brw,
16068b1f467cce34340637e9baca4847fc5273cf7541Paul Berry                                             struct intel_mipmap_tree *src_mt,
1607506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             struct intel_mipmap_tree *dst_mt,
1608506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint src_x0, GLuint src_y0,
1609506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x0, GLuint dst_y0,
1610506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x1, GLuint dst_y1,
1611506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             bool mirror_x, bool mirror_y)
1612506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1613530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   src.set(brw, src_mt, 0, 0);
1614530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   dst.set(brw, dst_mt, 0, 0);
1615506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1616506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   use_wm_prog = true;
1617506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1618506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1619e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   /* texture_data_type indicates the register type that should be used to
1620e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * manipulate texture data.
1621e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    */
1622e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   switch (_mesa_get_format_datatype(src_mt->format)) {
1623e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_NORMALIZED:
1624e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_SIGNED_NORMALIZED:
1625e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_FLOAT:
1626e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1627e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1628e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_INT:
1629e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      if (src_mt->format == MESA_FORMAT_S8) {
1630e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         /* We process stencil as though it's an unsigned normalized color */
1631e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1632e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      } else {
1633e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_UD;
1634e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
1635e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1636e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_INT:
1637e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_D;
1638e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1639e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   default:
1640e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      assert(!"Unrecognized blorp format");
1641e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1642e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   }
1643e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry
16448b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   if (brw->intel.gen > 6) {
164560c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry      /* Gen7's rendering hardware only supports the IMS layout for depth and
164660c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * stencil render targets.  Blorp always maps its destination surface as
164760c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * a color render target (even if it's actually a depth or stencil
164860c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * buffer).  So if the destination is IMS, we'll have to map it as a
164960c3e69dbf297426c42e4b8f94c5f0493bd9be5fPaul Berry       * single-sampled texture and interleave the samples ourselves.
16508b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       */
16511bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
16528b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         dst.num_samples = 0;
16538b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   }
16548b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
165534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (dst.map_stencil_as_y_tiled && dst.num_samples > 0) {
165634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination surface is a W-tiled multisampled stencil buffer
165734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * that we're mapping as Y tiled, then we need to arrange for the WM
165834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * program to run once per sample rather than once per pixel, because
165934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * the memory layout of related samples doesn't match between W and Y
166034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * tiling.
1661233c207e9e477b6b0a5c6705e727129b92989073Paul Berry       */
166234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
1663233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   }
1664233c207e9e477b6b0a5c6705e727129b92989073Paul Berry
166534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (src.num_samples > 0 && dst.num_samples > 0) {
166619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are blitting from a multisample buffer to a multisample buffer, so
166719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * we must preserve samples within a pixel.  This means we have to
166834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * arrange for the WM program to run once per sample rather than once
166934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel.
167019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
167134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
167219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
167319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
167419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* The render path must be configured to use the same number of samples as
167519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the destination buffer.
167619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
167719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   num_samples = dst.num_samples;
167819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
167919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   GLenum base_format = _mesa_get_format_base_format(src_mt->format);
168019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
168119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       base_format != GL_STENCIL_INDEX &&
168219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       src_mt->num_samples > 0 && dst_mt->num_samples == 0) {
168319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are downsampling a color buffer, so blend. */
168419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.blend = true;
168519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
168619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
168719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* src_samples and dst_samples are the true sample counts */
168819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.src_samples = src_mt->num_samples;
168919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.dst_samples = dst_mt->num_samples;
169019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
169119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* tex_samples and rt_samples are the sample counts that are set up in
169219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * SURFACE_STATE.
169319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
169419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.tex_samples = src.num_samples;
169519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.rt_samples  = dst.num_samples;
169619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
16971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
16981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * use to access the source and destination surfaces.
169967b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry    */
17001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.tex_layout =
17011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, src.num_samples, src.msaa_layout);
17021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.rt_layout =
17031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, dst.num_samples, dst.msaa_layout);
170467b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry
17051bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* src_layout and dst_layout indicate the true MSAA layout used by src and
17061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * dst.
17078b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
17081bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.src_layout = src_mt->msaa_layout;
17091bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.dst_layout = dst_mt->msaa_layout;
17108b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
1711506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.src_tiled_w = src.map_stencil_as_y_tiled;
1712506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.dst_tiled_w = dst.map_stencil_as_y_tiled;
1713506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x0 = wm_push_consts.dst_x0 = dst_x0;
1714506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y0 = wm_push_consts.dst_y0 = dst_y0;
1715506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x1 = wm_push_consts.dst_x1 = dst_x1;
1716506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y1 = wm_push_consts.dst_y1 = dst_y1;
1717506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.x_transform.setup(src_x0, dst_x0, dst_x1, mirror_x);
1718506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.y_transform.setup(src_y0, dst_y0, dst_y1, mirror_y);
1719506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
172019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (dst.num_samples == 0 && dst_mt->num_samples > 0) {
172119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We must expand the rectangle we send through the rendering pipeline,
172219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * to account for the fact that we are mapping the destination region as
172319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * single-sampled when it is in fact multisampled.  We must also align
172419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * it to a multiple of the multisampling pattern, because the
172519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * differences between multisampled and single-sampled surface formats
172619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * will mean that pixels are scrambled within the multisampling pattern.
172719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * TODO: what if this makes the coordinates too large?
17288b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       *
17291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * Note: this only works if the destination surface uses the IMS layout.
17301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * If it's UMS, then we have no choice but to set up the rendering
17311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * pipeline as multisampled.
173219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
17331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS);
1734082874e3891e588f674508be6578f600b35852c4Paul Berry      switch (dst_mt->num_samples) {
1735082874e3891e588f674508be6578f600b35852c4Paul Berry      case 4:
1736082874e3891e588f674508be6578f600b35852c4Paul Berry         x0 = (x0 * 2) & ~3;
1737082874e3891e588f674508be6578f600b35852c4Paul Berry         y0 = (y0 * 2) & ~3;
1738082874e3891e588f674508be6578f600b35852c4Paul Berry         x1 = ALIGN(x1 * 2, 4);
1739082874e3891e588f674508be6578f600b35852c4Paul Berry         y1 = ALIGN(y1 * 2, 4);
1740082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1741082874e3891e588f674508be6578f600b35852c4Paul Berry      case 8:
1742082874e3891e588f674508be6578f600b35852c4Paul Berry         x0 = (x0 * 4) & ~7;
1743082874e3891e588f674508be6578f600b35852c4Paul Berry         y0 = (y0 * 2) & ~3;
1744082874e3891e588f674508be6578f600b35852c4Paul Berry         x1 = ALIGN(x1 * 4, 8);
1745082874e3891e588f674508be6578f600b35852c4Paul Berry         y1 = ALIGN(y1 * 2, 4);
1746082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1747082874e3891e588f674508be6578f600b35852c4Paul Berry      default:
1748082874e3891e588f674508be6578f600b35852c4Paul Berry         assert(!"Unrecognized sample count in brw_blorp_blit_params ctor");
1749082874e3891e588f674508be6578f600b35852c4Paul Berry         break;
1750082874e3891e588f674508be6578f600b35852c4Paul Berry      }
175119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.use_kill = true;
175219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
175319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1754506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (dst.map_stencil_as_y_tiled) {
1755506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* We must modify the rectangle we send through the rendering pipeline,
1756506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * to account for the fact that we are mapping it as Y-tiled when it is
1757506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * in fact W-tiled.  Y tiles have dimensions 128x32 whereas W tiles have
1758506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * dimensions 64x64.  We must also align it to a multiple of the tile
1759506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * size, because the differences between W and Y tiling formats will
1760506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * mean that pixels are scrambled within the tile.
176134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
17621bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * Note: if the destination surface configured to use IMS layout, then
17631bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * the effective tile size we need to align it to is smaller, because
17641bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * each pixel covers a 2x2 or a 4x2 block of samples.
176534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
1766506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * TODO: what if this makes the coordinates too large?
1767506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
176834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      unsigned x_align = 64, y_align = 64;
17691bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
177034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry         x_align /= (dst_mt->num_samples == 4 ? 2 : 4);
177134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry         y_align /= 2;
177234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      }
177334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      x0 = (x0 & ~(x_align - 1)) * 2;
177434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      y0 = (y0 & ~(y_align - 1)) / 2;
177534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      x1 = ALIGN(x1, x_align) * 2;
177634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      y1 = ALIGN(y1, y_align) / 2;
1777506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      wm_prog_key.use_kill = true;
1778506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1779506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1780506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1781506d70be21cd3469118de89297cba0c0f709c1aePaul Berryuint32_t
1782506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_params::get_wm_prog(struct brw_context *brw,
1783506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                   brw_blorp_prog_data **prog_data) const
1784506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1785506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   uint32_t prog_offset;
1786506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!brw_search_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1787506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &this->wm_prog_key, sizeof(this->wm_prog_key),
1788506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &prog_offset, prog_data)) {
1789506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_blorp_blit_program prog(brw, &this->wm_prog_key);
1790506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GLuint program_size;
1791506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const GLuint *program = prog.compile(brw, &program_size);
1792506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_upload_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1793506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &this->wm_prog_key, sizeof(this->wm_prog_key),
1794506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       program, program_size,
1795506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog.prog_data, sizeof(prog.prog_data),
1796506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog_offset, prog_data);
1797506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1798506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return prog_offset;
1799506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1800