brw_blorp_blit.cpp revision f91b4d92b97664e6354f66138705e93bec363ba0
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"
25506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
26506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "glsl/ralloc.h"
27506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
28506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "intel_fbo.h"
29506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
30506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_blorp.h"
31506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_context.h"
32506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_eu.h"
33506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#include "brw_state.h"
34506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
35506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
36506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
37506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Helper function for handling mirror image blits.
38506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
39506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * If coord0 > coord1, swap them and invert the "mirror" boolean.
40506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
41506d70be21cd3469118de89297cba0c0f709c1aePaul Berrystatic inline void
42506d70be21cd3469118de89297cba0c0f709c1aePaul Berryfixup_mirroring(bool &mirror, GLint &coord0, GLint &coord1)
43506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
44506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (coord0 > coord1) {
45506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mirror = !mirror;
46506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GLint tmp = coord0;
47506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      coord0 = coord1;
48506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      coord1 = tmp;
49506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
50506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
51506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
52506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
5347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry/**
5447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * Adjust {src,dst}_x{0,1} to account for clipping and scissoring of
5547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * destination coordinates.
5647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry *
5747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * Return true if there is still blitting to do, false if all pixels got
5847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * rejected by the clip and/or scissor.
5947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry *
6047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * For clarity, the nomenclature of this function assumes we are clipping and
6147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * scissoring the X coordinate; the exact same logic applies for Y
6247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry * coordinates.
6375f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry *
6475f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry * Note: this function may also be used to account for clipping of source
6575f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry * coordinates, by swapping the roles of src and dst.
6647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry */
6747b64c9290d54f78e5a20e378593977cd47e285fPaul Berrystatic inline bool
6847b64c9290d54f78e5a20e378593977cd47e285fPaul Berryclip_or_scissor(bool mirror, GLint &src_x0, GLint &src_x1, GLint &dst_x0,
6947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                GLint &dst_x1, GLint fb_xmin, GLint fb_xmax)
7047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry{
7147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If we are going to scissor everything away, stop. */
7247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (!(fb_xmin < fb_xmax &&
7347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         dst_x0 < fb_xmax &&
7447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         fb_xmin < dst_x1 &&
7547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         dst_x0 < dst_x1)) {
7647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      return false;
7747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
7847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
7947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* Clip the destination rectangle, and keep track of how many pixels we
8047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * clipped off of the left and right sides of it.
8147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
8247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   GLint pixels_clipped_left = 0;
8347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   GLint pixels_clipped_right = 0;
8447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (dst_x0 < fb_xmin) {
8547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_left = fb_xmin - dst_x0;
8647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      dst_x0 = fb_xmin;
8747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
8847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (fb_xmax < dst_x1) {
8947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_right = dst_x1 - fb_xmax;
9047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      dst_x1 = fb_xmax;
9147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
9247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
9347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If we are mirrored, then before applying pixels_clipped_{left,right} to
9447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * the source coordinates, we need to flip them to account for the
9547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * mirroring.
9647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
9747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (mirror) {
9847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      GLint tmp = pixels_clipped_left;
9947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_left = pixels_clipped_right;
10047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      pixels_clipped_right = tmp;
10147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
10247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
10347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* Adjust the source rectangle to remove the pixels corresponding to those
10447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    * that were clipped/scissored out of the destination rectangle.
10547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry    */
10647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   src_x0 += pixels_clipped_left;
10747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   src_x1 -= pixels_clipped_right;
10847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
10947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   return true;
11047b64c9290d54f78e5a20e378593977cd47e285fPaul Berry}
11147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
11247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
113506d70be21cd3469118de89297cba0c0f709c1aePaul Berrystatic bool
114506d70be21cd3469118de89297cba0c0f709c1aePaul Berrytry_blorp_blit(struct intel_context *intel,
115506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
116506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
117506d70be21cd3469118de89297cba0c0f709c1aePaul Berry               GLenum filter, GLbitfield buffer_bit)
118506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
119506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct gl_context *ctx = &intel->ctx;
120506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
121506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Sync up the state of window system buffers.  We need to do this before
122506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * we go looking for the buffers.
123506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
124506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_prepare_render(intel);
125506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
126506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Find buffers */
127506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *read_fb = ctx->ReadBuffer;
128506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const struct gl_framebuffer *draw_fb = ctx->DrawBuffer;
129506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct gl_renderbuffer *src_rb;
130506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct gl_renderbuffer *dst_rb;
131506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   switch (buffer_bit) {
132506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   case GL_COLOR_BUFFER_BIT:
133506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      src_rb = read_fb->_ColorReadBuffer;
134506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dst_rb =
135506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         draw_fb->Attachment[
136506d70be21cd3469118de89297cba0c0f709c1aePaul Berry            draw_fb->_ColorDrawBufferIndexes[0]].Renderbuffer;
137506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      break;
138506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   case GL_DEPTH_BUFFER_BIT:
139506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      src_rb = read_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
140506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dst_rb = draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
141506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      break;
142506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   case GL_STENCIL_BUFFER_BIT:
143506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      src_rb = read_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
144506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dst_rb = draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
145506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      break;
146506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   default:
147506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      assert(false);
148506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
149506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
150506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Validate source */
151506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!src_rb) return false;
152506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
153506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct intel_mipmap_tree *src_mt = src_irb->mt;
154506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!src_mt) return false;
155506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (buffer_bit == GL_STENCIL_BUFFER_BIT && src_mt->stencil_mt)
156506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      src_mt = src_mt->stencil_mt;
157506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
158506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Validate destination */
159506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!dst_rb) return false;
160506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct intel_renderbuffer *dst_irb = intel_renderbuffer(dst_rb);
161506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct intel_mipmap_tree *dst_mt = dst_irb->mt;
162506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!dst_mt) return false;
163506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (buffer_bit == GL_STENCIL_BUFFER_BIT && dst_mt->stencil_mt)
164506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dst_mt = dst_mt->stencil_mt;
1659fd0e76a196656f2f14115444f99ec1121879766Paul Berry
1669fd0e76a196656f2f14115444f99ec1121879766Paul Berry   /* Blorp blits can't translate from one format to another.  For that we'll
1679fd0e76a196656f2f14115444f99ec1121879766Paul Berry    * have to fall back to the meta-op blit.  Note: the meta-op blit doesn't
1689fd0e76a196656f2f14115444f99ec1121879766Paul Berry    * support multisampled blits, but fortunately this is ok because
1699fd0e76a196656f2f14115444f99ec1121879766Paul Berry    * multisampled blits require identical source and destination formats.
1709fd0e76a196656f2f14115444f99ec1121879766Paul Berry    */
1719fd0e76a196656f2f14115444f99ec1121879766Paul Berry   if (src_mt->format != dst_mt->format)
172506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return false;
173506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
174506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Account for the fact that in the system framebuffer, the origin is at
175506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * the lower left.
176506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
177506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (read_fb->Name == 0) {
178506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      srcY0 = read_fb->Height - srcY0;
179506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      srcY1 = read_fb->Height - srcY1;
180506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
181506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (draw_fb->Name == 0) {
182506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dstY0 = draw_fb->Height - dstY0;
183506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      dstY1 = draw_fb->Height - dstY1;
184506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
185506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
186506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Detect if the blit needs to be mirrored */
187506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool mirror_x = false, mirror_y = false;
188506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, srcX0, srcX1);
189506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_x, dstX0, dstX1);
190506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, srcY0, srcY1);
191506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   fixup_mirroring(mirror_y, dstY0, dstY1);
192506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
193506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Make sure width and height match */
194506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   GLsizei width = srcX1 - srcX0;
195506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   GLsizei height = srcY1 - srcY0;
196506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (width != dstX1 - dstX0) return false;
197506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (height != dstY1 - dstY0) return false;
198506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
19947b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   /* If the destination rectangle needs to be clipped or scissored, do so.
200506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
20147b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   if (!(clip_or_scissor(mirror_x, srcX0, srcX1, dstX0, dstX1,
20247b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Xmin, draw_fb->_Xmax) &&
20347b64c9290d54f78e5a20e378593977cd47e285fPaul Berry         clip_or_scissor(mirror_y, srcY0, srcY1, dstY0, dstY1,
20447b64c9290d54f78e5a20e378593977cd47e285fPaul Berry                         draw_fb->_Ymin, draw_fb->_Ymax))) {
20547b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
20647b64c9290d54f78e5a20e378593977cd47e285fPaul Berry      return true;
20747b64c9290d54f78e5a20e378593977cd47e285fPaul Berry   }
20847b64c9290d54f78e5a20e378593977cd47e285fPaul Berry
20975f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   /* If the source rectangle needs to be clipped or scissored, do so. */
21075f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   if (!(clip_or_scissor(mirror_x, dstX0, dstX1, srcX0, srcX1,
21175f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Width) &&
21275f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry         clip_or_scissor(mirror_y, dstY0, dstY1, srcY0, srcY1,
21375f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry                         0, read_fb->Height))) {
21475f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      /* Everything got clipped/scissored away, so the blit was successful. */
21575f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry      return true;
21675f409d75cacf90df2d6f1d718251a5d5cd92f7fPaul Berry   }
217506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
218506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Get ready to blit.  This includes depth resolving the src and dst
219506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * buffers if necessary.
220506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
221506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_renderbuffer_resolve_depth(intel, src_irb);
222506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_renderbuffer_resolve_depth(intel, dst_irb);
223506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
224506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Do the blit */
2258b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   brw_blorp_blit_params params(brw_context(ctx), src_mt, dst_mt,
226506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                srcX0, srcY0, dstX0, dstY0, dstX1, dstY1,
227506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                mirror_x, mirror_y);
2286335e0b0738a6e466f0b712e30ad9fe506f67a6cPaul Berry   brw_blorp_exec(intel, &params);
229506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
230506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Mark the dst buffer as needing a HiZ resolve if necessary. */
231506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   intel_renderbuffer_set_needs_hiz_resolve(dst_irb);
232506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
233506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return true;
234506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
235506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
236506d70be21cd3469118de89297cba0c0f709c1aePaul BerryGLbitfield
237506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_framebuffer(struct intel_context *intel,
238506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
239506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
240506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      GLbitfield mask, GLenum filter)
241506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
242b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   /* BLORP is not supported before Gen6. */
243b08545199ac8a01392a805f158d22cc03060a6fbPaul Berry   if (intel->gen < 6)
244506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return mask;
245506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
246506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   static GLbitfield buffer_bits[] = {
247506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_COLOR_BUFFER_BIT,
248506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_DEPTH_BUFFER_BIT,
249506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GL_STENCIL_BUFFER_BIT,
250506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   };
251506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
252506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
253506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      if ((mask & buffer_bits[i]) &&
254506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       try_blorp_blit(intel,
255506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      srcX0, srcY0, srcX1, srcY1,
256506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      dstX0, dstY0, dstX1, dstY1,
257506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                      filter, buffer_bits[i])) {
258506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         mask &= ~buffer_bits[i];
259506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      }
260506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
261506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
262506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return mask;
263506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
264506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
265665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
266665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry/**
267665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry * Enum to specify the order of arguments in a sampler message
268665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry */
269665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryenum sampler_message_arg
270665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
271665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_FLOAT,
272665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_FLOAT,
273665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_U_INT,
274665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   SAMPLER_MESSAGE_ARG_V_INT,
275233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_SI_INT,
2764ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   SAMPLER_MESSAGE_ARG_MCS_INT,
277233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   SAMPLER_MESSAGE_ARG_ZERO_INT,
278665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry};
279665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
280506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
281506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Generator for WM programs used in BLORP blits.
282506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
283506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * The bulk of the work done by the WM program is to wrap and unwrap the
284506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinate transformations used by the hardware to store surfaces in
28519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * memory.  The hardware transforms a pixel location (X, Y, S) (where S is the
28619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * sample index for a multisampled surface) to a memory offset by the
28719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * following formulas:
288506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
2898b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
2908b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
29119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
2921bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a single-sampled surface, or for a multisampled surface using
2931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
2941bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * function:
29519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
2961bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
2971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
2981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
2991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
30019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
3021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * embeds the sample number into bit 1 of the X and Y coordinates:
30319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
3041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
30519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
30619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
3071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry *   decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
30819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *     where X' = (X & ~0b11) >> 1 | (X & 0b1)
30919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
31019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *           S = (Y & 0b10) | (X & 0b10) >> 1
311506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
312506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For X tiling, tile() combines together the low-order bits of the X and Y
313506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
314506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 8 rows high:
315506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3168b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(x_tiled, X, Y, S) = A
317506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3188b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
3198b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           offset = (Y' & 0b111) << 9
320506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X & 0b111111111)
321506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3228b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3238b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(x_tiled, A) = (X, Y, S)
324506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
3258b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
3268b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
3278b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 3
3288b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111000000000) >> 9
329506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 9
330506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111111111)
331506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
332506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In all tiling formulas, cpp is the number of bytes occupied by a single
3338b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
3348b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * to fill the width of the surface, and qpitch is the spacing (in rows)
3358b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * between array slices).
336506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
337506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For Y tiling, tile() combines together the low-order bits of the X and Y
338506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
339506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 32 rows high:
340506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3418b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(y_tiled, X, Y, S) = A
342506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3438b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
344506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b1110000) << 5
345506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (Y' & 0b11111) << 4
346506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1111)
347506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3488b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3498b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(y_tiled, A) = (X, Y, S)
350506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
3518b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
3528b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
3538b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 5
3548b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111110000) >> 4
355506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 7
356506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 5
357506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1111)
358506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
359506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * For W tiling, tile() combines together the low-order bits of the X and Y
360506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
361506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * bytes wide and 64 rows high (note that W tiling is only used for stencil
3628b1f467cce34340637e9baca4847fc5273cf7541Paul Berry * buffers, which always have cpp = 1 and S=0):
363506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3648b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(w_tiled, X, Y, S) = A
365506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = tile_num << 12 | offset
3668b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
367506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           offset = (X' & 0b111000) << 6
3688b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b111100) << 3
369506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b100) << 2
3708b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b10) << 2
371506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b10) << 1
3728b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                    | (Y' & 0b1) << 1
373506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                    | (X' & 0b1)
374506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp = X
3758b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3768b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(w_tiled, A) = (X, Y, S)
377506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp = X'
3788b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch = Y'
3798b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y / qpitch = 0
3808b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = (tile_num / tile_pitch) << 6
3818b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b111100000) >> 3
3828b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b1000) >> 2
3838b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *                | (A & 0b10) >> 1
384506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = (tile_num % tile_pitch) << 6
385506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b111000000000) >> 6
386506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b10000) >> 2
387506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b100) >> 1
388506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *                | (A & 0b1)
389506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
390506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Finally, for a non-tiled surface, tile() simply combines together the X and
391506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Y coordinates in the natural way:
392506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
3938b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   tile(untiled, X, Y, S) = A
394506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where A = Y * pitch + X'
395506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = X * cpp
3968b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = Y + S * qpitch
3978b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   detile(untiled, A) = (X, Y, S)
398506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *     where X = X' / cpp
3998b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y = Y' % qpitch
4008b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           S = Y' / qpitch
401506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *           X' = A % pitch
4028b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *           Y' = A / pitch
403506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
404506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (In these formulas, pitch is the number of bytes occupied by a single row
40519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * of samples).
406506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
407506d70be21cd3469118de89297cba0c0f709c1aePaul Berryclass brw_blorp_blit_program
408506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
409506d70be21cd3469118de89297cba0c0f709c1aePaul Berrypublic:
410506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_blit_program(struct brw_context *brw,
411506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                          const brw_blorp_blit_prog_key *key);
412506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ~brw_blorp_blit_program();
413506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
414506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const GLuint *compile(struct brw_context *brw, GLuint *program_size);
415506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
416506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_blorp_prog_data prog_data;
417506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
418506d70be21cd3469118de89297cba0c0f709c1aePaul Berryprivate:
419506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_regs();
420506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void alloc_push_const_regs(int base_reg);
421506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void compute_frag_coords();
422506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_tiling(bool old_tiled_w, bool new_tiled_w);
4231bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void encode_msaa(unsigned num_samples, intel_msaa_layout layout);
4241bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   void decode_msaa(unsigned num_samples, intel_msaa_layout layout);
425506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void kill_if_outside_dst_rect();
426506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void translate_dst_to_src();
42719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   void single_to_blend();
4284725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void manual_blend();
4294725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void sample(struct brw_reg dst);
4304725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texel_fetch(struct brw_reg dst);
4314ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   void mcs_fetch();
432665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   void expand_to_32_bits(struct brw_reg src, struct brw_reg dst);
4334725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   void texture_lookup(struct brw_reg dst, GLuint msg_type,
4344725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                       const sampler_message_arg *args, int num_args);
435506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void render_target_write();
436506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
437b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /**
438b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Base-2 logarithm of the maximum number of samples that can be blended.
439b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
440b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   static const unsigned LOG2_MAX_BLEND_SAMPLES = 2;
441b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
442506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   void *mem_ctx;
443506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_context *brw;
444506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const brw_blorp_blit_prog_key *key;
445506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_compile func;
446506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
447506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Thread dispatch header */
448506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R0;
449506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
450506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Pixel X/Y coordinates (always in R1). */
451506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg R1;
452506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
453506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Push constants */
454506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x0;
455506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_x1;
456506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y0;
457506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg dst_y1;
458506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct {
459506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg multiplier;
460506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_reg offset;
461506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } x_transform, y_transform;
462506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
463b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Data read from texture (4 vec16's per array element) */
464b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   struct brw_reg texture_data[LOG2_MAX_BLEND_SAMPLES + 1];
465506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
4664ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   /* Auxiliary storage for the contents of the MCS surface.
4674ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    *
4684ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * Since the sampler always returns 8 registers worth of data, this is 8
4694ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    * registers wide, even though we only use the first 2 registers of it.
4704ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry    */
4714ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   struct brw_reg mcs_data;
4724ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
473506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* X coordinates.  We have two of them so that we can perform coordinate
474506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
475506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
476506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg x_coords[2];
477506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
478506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Y coordinates.  We have two of them so that we can perform coordinate
479506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * transformations easily.
480506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
481506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg y_coords[2];
482506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
483506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Which element of x_coords and y_coords is currently in use.
484506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
485506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int xy_coord_index;
486506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
48719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* True if, at the point in the program currently being compiled, the
48819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * sample index is known to be zero.
48919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
49019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   bool s_is_zero;
49119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
49219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Register storing the sample index when s_is_zero is false. */
49319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct brw_reg sample_index;
49419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
495506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Temporaries */
496506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t1;
497506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg t2;
498506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
499665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   /* MRF used for sampling and render target writes */
500506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   GLuint base_mrf;
501506d70be21cd3469118de89297cba0c0f709c1aePaul Berry};
502506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
503506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::brw_blorp_blit_program(
504506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      struct brw_context *brw,
505506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const brw_blorp_blit_prog_key *key)
506506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   : mem_ctx(ralloc_context(NULL)),
507506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     brw(brw),
508506d70be21cd3469118de89297cba0c0f709c1aePaul Berry     key(key)
509506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
510506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_init_compile(brw, &func, mem_ctx);
511506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
512506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
513506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::~brw_blorp_blit_program()
514506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
515506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ralloc_free(mem_ctx);
516506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
517506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
518506d70be21cd3469118de89297cba0c0f709c1aePaul Berryconst GLuint *
519506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compile(struct brw_context *brw,
520506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                GLuint *program_size)
521506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
52219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Sanity checks */
52334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->dst_tiled_w && key->rt_samples > 0) {
52434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination image is W tiled and multisampled, then the thread
52534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * must be dispatched once per sample, not once per pixel.  This is
52634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * necessary because after conversion between W and Y tiling, there's no
52719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * guarantee that all samples corresponding to a single pixel will still
52819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * be together.
52919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
53034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->persample_msaa_dispatch);
53119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
53219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
53319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
5344725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      /* We are blending, which means we won't have an opportunity to
5354725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry       * translate the tiling and sample count for the texture surface.  So
53619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the surface state for the texture must be configured with the correct
53719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * tiling and sample count.
53819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
53919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(!key->src_tiled_w);
54019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples == key->src_samples);
5411bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(key->tex_layout == key->src_layout);
54219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      assert(key->tex_samples > 0);
54319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
54419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
54534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
54634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* It only makes sense to do persample dispatch if the render target is
54734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * configured as multisampled.
54834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
54934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      assert(key->rt_samples > 0);
55034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
55134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
5521bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Make sure layout is consistent with sample count */
5531bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
5541bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->tex_samples == 0));
5551bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
5561bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->rt_samples == 0));
5571bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
5581bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->src_samples == 0));
5591bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
5601bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          (key->dst_samples == 0));
5618b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
56234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   /* Set up prog_data */
56334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   memset(&prog_data, 0, sizeof(prog_data));
56434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   prog_data.persample_msaa_dispatch = key->persample_msaa_dispatch;
56534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry
566506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
567506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_regs();
569506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   compute_frag_coords();
570506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
571506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Render target and texture hardware don't support W tiling. */
572506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool rt_tiled_w = false;
573506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   const bool tex_tiled_w = false;
574506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
575506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* The address that data will be written to is determined by the
57619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates supplied to the WM thread and the tiling and sample count of
57719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the render target, according to the formula:
578506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
57919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
580506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
58119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * If the actual tiling and sample count of the destination surface are not
58219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the same as the configuration of the render target, then these
58319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * coordinates are wrong and we have to adjust them to compensate for the
58419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * difference.
585506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
58619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (rt_tiled_w != key->dst_tiled_w ||
5878b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       key->rt_samples != key->dst_samples ||
5881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       key->rt_layout != key->dst_layout) {
5891bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      encode_msaa(key->rt_samples, key->rt_layout);
5908b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(rt_tiling, offset) */
591506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      translate_tiling(rt_tiled_w, key->dst_tiled_w);
5928b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* Now (X, Y, S) = detile(dst_tiling, offset) */
5931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      decode_msaa(key->dst_samples, key->dst_layout);
59419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
595506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
59619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
597506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
59819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * That is: X, Y and S now contain the true coordinates and sample index of
59919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the data that the WM thread should output.
600506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
601506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * If we need to kill pixels that are outside the destination rectangle,
602506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * now is the time to do it.
603506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
604506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
605506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (key->use_kill)
606506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      kill_if_outside_dst_rect();
607506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
608506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Next, apply a translation to obtain coordinates in the source image. */
609506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   translate_dst_to_src();
610506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
61119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* If the source image is not multisampled, then we want to fetch sample
61219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * number 0, because that's the only sample there is.
613506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
61419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->src_samples == 0)
61519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = true;
616506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
61719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* X, Y, and S are now the coordinates of the pixel in the source image
61819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that we want to texture from.  Exception: if we are blending, then S is
61919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * irrelevant, because we are going to fetch all samples.
620506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
62119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (key->blend) {
6224725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      if (brw->intel.gen == 6) {
6234725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen6 hardware an automatically blend using the SAMPLE message */
6244725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         single_to_blend();
625b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         sample(texture_data[0]);
6264725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      } else {
6274725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         /* Gen7+ hardware doesn't automaticaly blend. */
6284725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         manual_blend();
6294725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
63019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   } else {
63119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We aren't blending, which means we just want to fetch a single sample
63219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * from the source surface.  The address that we want to fetch from is
63319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * related to the X, Y and S values according to the formula:
63419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
63519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
63619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
63719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * If the actual tiling and sample count of the source surface are not
63819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the same as the configuration of the texture, then we need to adjust
63919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the coordinates to compensate for the difference.
64019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
64119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      if (tex_tiled_w != key->src_tiled_w ||
6428b1f467cce34340637e9baca4847fc5273cf7541Paul Berry          key->tex_samples != key->src_samples ||
6431bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          key->tex_layout != key->src_layout) {
6441bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         encode_msaa(key->src_samples, key->src_layout);
6458b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(src_tiling, offset) */
64619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         translate_tiling(key->src_tiled_w, tex_tiled_w);
6478b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         /* Now (X, Y, S) = detile(tex_tiling, offset) */
6481bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         decode_msaa(key->tex_samples, key->tex_layout);
64919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
650506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
65119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
65219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *
65319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * In other words: X, Y, and S now contain values which, when passed to
65419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * the texturing unit, will cause data to be read from the correct
65519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * memory location.  So we can fetch the texel now.
65619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
6574ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
6584ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         mcs_fetch();
659b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[0]);
66019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
66119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
66219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Finally, write the fetched (or blended) value to the render target and
66319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * terminate the thread.
664506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
665506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   render_target_write();
666506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return brw_get_program(&func, program_size);
667506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
668506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
669506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
670506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_push_const_regs(int base_reg)
671506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
672506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
673506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define ALLOC_REG(name) \
674506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->name = \
675506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, base_reg, CONST_LOC(name) / 2)
676506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
677506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x0);
678506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_x1);
679506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y0);
680506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(dst_y1);
681506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.multiplier);
682506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(x_transform.offset);
683506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.multiplier);
684506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   ALLOC_REG(y_transform.offset);
685506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef CONST_LOC
686506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef ALLOC_REG
687506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
688506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
689506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
690506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::alloc_regs()
691506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
692506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int reg = 0;
693506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
694506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
695506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   prog_data.first_curbe_grf = reg;
696506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   alloc_push_const_regs(reg);
697506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
698b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   for (unsigned i = 0; i < ARRAY_SIZE(texture_data); ++i) {
699e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      this->texture_data[i] =
700e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         retype(vec16(brw_vec8_grf(reg, 0)), key->texture_data_type);
701e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      reg += 8;
702b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   }
7034ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   this->mcs_data =
7044ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD); reg += 8;
705506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 2; ++i) {
706506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->x_coords[i]
707506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
708506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      this->y_coords[i]
709506d70be21cd3469118de89297cba0c0f709c1aePaul Berry         = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
710506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
711506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->xy_coord_index = 0;
71219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   this->sample_index
71319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
714506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t1 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
715506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->t2 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
716506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
717b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* Make sure we didn't run out of registers */
718b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(reg <= GEN7_MRF_HACK_START);
719b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
720506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf = 2;
721506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   this->base_mrf = mrf;
722506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
723506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
724506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* In the code that follows, X and Y can be used to quickly refer to the
725506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
726506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * prime") to the inactive elements.
72719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
72819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * S can be used to quickly refer to sample_index.
729506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
730506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define X x_coords[xy_coord_index]
731506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Y y_coords[xy_coord_index]
732506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Xp x_coords[!xy_coord_index]
733506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define Yp y_coords[!xy_coord_index]
73419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#define S sample_index
735506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
736506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/* Quickly swap the roles of (X, Y) and (Xp, Yp).  Saves us from having to do
737506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
738506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
739506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
740506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
741506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
742506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compute the X and Y coordinates of the pixels being rendered
743506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * by this WM invocation.
744506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
745506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Assuming the render target is set up for Y tiling, these (X, Y) values are
746506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * related to the address offset where outputs will be written by the formula:
747506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
748506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *   (X, Y, S) = decode_msaa(detile(offset)).
749506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
750506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
751506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
752506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
753506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::compute_frag_coords()
754506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
755506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
756506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
757506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
758506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
759506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
760506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Pixels within a subspan are laid out in this arrangement:
761506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 0 1
762506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 2 3
763506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
764506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * So, to compute the coordinates of each pixel, we need to read every 2nd
765506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
766506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
767506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * In other words, the data we want to access is R1.4<2;4,0>UW.
768506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
769506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
770506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * result, since pixels n+1 and n+3 are in the right half of the subspan.
771506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
772506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, X, stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
773506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
774506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
775506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
776506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
777506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * R1.4<2;4,0>UW).
778506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    *
779506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
780506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * pixels n+2 and n+3 are in the bottom half of the subspan.
781506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
782506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Y, stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
78319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
78434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (key->persample_msaa_dispatch) {
78534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples > 0.
78634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * Therefore, subspan 0 will represent sample 0, subspan 1 will
78734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * represent sample 1, and so on.
78834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
78934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1, 1,
79034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * 2, 2, 2, 2, 3, 3, 3, 3).  The easiest way to do this is to populate a
79134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * temporary variable with the sequence (0, 1, 2, 3), and then copy from
79234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * it using vstride=1, width=4, hstride=0.
79334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
79434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * TODO: implement the necessary calculation for 8x multisampling.
79534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
79634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      brw_MOV(&func, t1, brw_imm_v(0x3210));
79734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      brw_MOV(&func, S, stride(t1, 1, 4, 0));
79834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = false;
79934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   } else {
80034a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* Either the destination surface is single-sampled, or the WM will be
80134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
80234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel).  In either case, it's not meaningful to compute a sample
80334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * value.  Just set it to 0.
80434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       */
80534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      s_is_zero = true;
80634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   }
807506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
808506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
809506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
810506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to compensate for the difference between Y and W tiling.
811506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
812506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * This code modifies the X and Y coordinates according to the formula:
813506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
8148b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
815506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
816506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (See brw_blorp_blit_program).
817506d70be21cd3469118de89297cba0c0f709c1aePaul Berry *
818506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * It can only translate between W and Y tiling, so new_tiling and old_tiling
819506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * are booleans where true represents W tiling and false represents Y tiling.
820506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
821506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
822506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
823506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
824506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (old_tiled_w == new_tiled_w)
825506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      return;
826506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
8278b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   /* In the code that follows, we can safely assume that S = 0, because W
8281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * tiling formats always use IMS layout.
8298b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
8308b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   assert(s_is_zero);
8318b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
832506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (new_tiled_w) {
833506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Given X and Y coordinates that describe an address using Y tiling,
834506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * translate to the X and Y coordinates that describe the same address
835506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * using W tiling.
836506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
837506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we break down the low order bits of X and Y, using a
838506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * single letter to represent each low-order bit:
839506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
840506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X = A << 7 | 0bBCDEFGH
841506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y = J << 5 | 0bKLMNP                                       (1)
842506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
843506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Then we can apply the Y tiling formula to see the memory offset being
844506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * addressed:
845506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
846506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH       (2)
847506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
848506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * If we apply the W detiling formula to this memory location, that the
849506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * corresponding X' and Y' coordinates are:
850506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
851506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = A << 6 | 0bBCDPFH                                     (3)
852506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = J << 6 | 0bKLMNEG
853506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
854506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
855506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * we need to make the following computation:
856506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
857506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1         (4)
858506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
859506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
860506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
861506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
862506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
863506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
864506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
865506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
866506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
867506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
868506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
869506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(8)); /* X & 0b1000 */
870506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
871506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
872506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
873506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
874506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
875506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
876506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
877506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Applying the same logic as above, but in reverse, we obtain the
878506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * formulas:
879506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *
880506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
881506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
882506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
883506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
884506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
885506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
886506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
887506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
888506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
889506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
890506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
891506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                    | (Y & 0b1) << 1 */
892506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
893506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Xp, t1, t2);
894506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
895506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
896506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_AND(&func, t2, X, brw_imm_uw(4)); /* X & 0b100 */
897506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
898506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_OR(&func, Yp, t1, t2);
899506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      SWAP_XY_AND_XPYP();
900506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
901506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
902506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
903506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
90419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
90519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
90619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
90719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
90819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
9098b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X', Y', S') = encode_msaa_4x(X, Y, S)
91019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
91119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
91219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
91319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
9141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::encode_msaa(unsigned num_samples,
9151bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
91619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
9171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
9181bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
9198b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
9208b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
9211bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9221bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
9231bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
9241bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we haven't read from the MCS buffer.
9251bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
9261bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
9271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
92919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
9301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
9321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
93319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *   where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
93419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *         Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
93519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
93619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
93719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      if (!s_is_zero) {
93819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
93919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         brw_OR(&func, t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
94019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
94119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
94219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                                | (S & 0b1) << 1 */
94319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
94419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_OR(&func, Xp, t1, t2);
94519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
94619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
94719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      if (!s_is_zero) {
94819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
94919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
95019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      }
95119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1));
95219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_OR(&func, Yp, t1, t2);
95319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
9548b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      s_is_zero = true;
9551bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
95619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
95719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
95819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
95919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
96019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to compensate for the difference between MSAA and non-MSAA
96119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * surfaces.
96219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
96319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * This code modifies the X and Y coordinates according to the formula:
96419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
9658b1f467cce34340637e9baca4847fc5273cf7541Paul Berry *   (X', Y', S) = decode_msaa(num_samples, X, Y, S)
96619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
96719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * (See brw_blorp_blit_program).
96819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
96919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
9701bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrybrw_blorp_blit_program::decode_msaa(unsigned num_samples,
9711bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                    intel_msaa_layout layout)
97219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
9731bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (layout) {
9741bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
9758b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      /* No translation necessary, and S should already be zero. */
9768b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
9771bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9781bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
9791bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* We can't compensate for compressed layout since at this point in the
9801bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * program we don't have access to the MCS buffer.
9811bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
9821bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(!"Bad layout in encode_msaa");
9831bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9841bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
98519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* No translation necessary. */
9861bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
9871bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
9881bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
98919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *   where X' = (X & ~0b11) >> 1 | (X & 0b1)
99019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *         Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
99119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       *         S = (Y & 0b10) | (X & 0b10) >> 1
99219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
9938b1f467cce34340637e9baca4847fc5273cf7541Paul Berry      assert(s_is_zero);
99419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
99519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
99619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
99719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_OR(&func, Xp, t1, t2);
99819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
99919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
100019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
100119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_OR(&func, Yp, t1, t2);
100219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
100319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
100419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
100519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      brw_OR(&func, S, t1, t2);
100619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      s_is_zero = false;
100719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      SWAP_XY_AND_XPYP();
10081bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
100919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
101019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
101119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
101219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1013506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code that kills pixels whose X and Y coordinates are outside the
1014506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
1015506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * dst_x1, dst_y1).
1016506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1017506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1018506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::kill_if_outside_dst_rect()
1019506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1020506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg f0 = brw_flag_reg();
1021506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1022506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   struct brw_reg null16 = vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
1023506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1024506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, X, dst_x0);
1025506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_GE, Y, dst_y0);
1026506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, X, dst_x1);
1027506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_CMP(&func, null16, BRW_CONDITIONAL_L, Y, dst_y1);
1028506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1029506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_predicate_control(&func, BRW_PREDICATE_NONE);
1030506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_push_insn_state(&func);
1031506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_mask_control(&func, BRW_MASK_DISABLE);
1032506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_AND(&func, g1, f0, g1);
1033506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_pop_insn_state(&func);
1034506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1035506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1036506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
1037506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1038506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * coordinates.
1039506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1040506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1041506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::translate_dst_to_src()
1042506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1043506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Xp, X, x_transform.multiplier);
1044506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_MUL(&func, Yp, Y, y_transform.multiplier);
1045506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Xp, Xp, x_transform.offset);
1046506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_ADD(&func, Yp, Yp, y_transform.offset);
1047506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   SWAP_XY_AND_XPYP();
1048506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1049506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1050506d70be21cd3469118de89297cba0c0f709c1aePaul Berry/**
105119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to transform the X and Y coordinates as needed for blending
105219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * together the different samples in an MSAA texture.
105319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
105419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
105519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrybrw_blorp_blit_program::single_to_blend()
105619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
105719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* When looking up samples in an MSAA texture using the SAMPLE message,
105819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * Gen6 requires the texture coordinates to be odd integers (so that they
105919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * correspond to the center of a 2x2 block representing the four samples
106019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * that maxe up a pixel).  So we need to multiply our X and Y coordinates
106119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * each by 2 and then add 1.
106219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
106319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t1, X, brw_imm_w(1));
106419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_SHL(&func, t2, Y, brw_imm_w(1));
106519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Xp, t1, brw_imm_w(1));
106619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   brw_ADD(&func, Yp, t2, brw_imm_w(1));
106719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   SWAP_XY_AND_XPYP();
106819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
106919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1070b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1071b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry/**
1072b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * Count the number of trailing 1 bits in the given value.  For example:
1073b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry *
1074b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(0) == 0
1075b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(7) == 3
1076b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry * count_trailing_one_bits(11) == 2
1077b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry */
1078b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berryinline int count_trailing_one_bits(unsigned value)
1079b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry{
1080b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1081b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return __builtin_ctz(~value);
1082b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#else
1083b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   return _mesa_bitcount(value & ~(value + 1));
1084b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry#endif
1085b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry}
1086b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1087b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
10884725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berryvoid
10894725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::manual_blend()
10904725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry{
10914725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   /* TODO: support num_samples != 4 */
10924725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   const int num_samples = 4;
10934725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
10944ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
10954ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      mcs_fetch();
10964ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
1097b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1098b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1099b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *   result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1100b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1101b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * This ensures that when all samples have the same value, no numerical
1102b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision is lost, since each addition operation always adds two equal
1103b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * values, and summing two equal floating point values does not lose
1104b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * precision.
1105b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1106b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * We perform this computation by treating the texture_data array as a
1107b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * stack and performing the following operations:
1108b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1109b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 0 onto stack
1110b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 1 onto stack
1111b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1112b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 2 onto stack
1113b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - push sample 3 onto stack
1114b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1115b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - add top two stack entries
1116b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * - divide top stack entry by 4
1117b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    *
1118b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * Note that after pushing sample i onto the stack, the number of add
1119b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * operations we do is equal to the number of trailing 1 bits in i.  This
1120b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * works provided the total number of samples is a power of two, which it
1121b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    * always is for i965.
1122e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    *
1123e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * For integer formats, we replace the add operations with average
1124e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * operations and skip the final division.
1125b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry    */
1126e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   typedef struct brw_instruction *(*brw_op2_ptr)(struct brw_compile *,
1127e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1128e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg,
1129e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                                                  struct brw_reg);
1130e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   brw_op2_ptr combine_op =
1131e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      key->texture_data_type == BRW_REGISTER_TYPE_F ? brw_ADD : brw_AVG;
1132b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   unsigned stack_depth = 0;
1133b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   for (int i = 0; i < num_samples; ++i) {
1134b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
1135b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1136b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Push sample i onto the stack */
1137b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      assert(stack_depth < ARRAY_SIZE(texture_data));
1138b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      if (i == 0) {
1139b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = true;
1140b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      } else {
1141b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         s_is_zero = false;
1142b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         brw_MOV(&func, S, brw_imm_uw(i));
1143b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      }
1144b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      texel_fetch(texture_data[stack_depth++]);
1145b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1146f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      if (i == 0 && key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
1147f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1148f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * suggests an optimization:
1149f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1150f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     "A simple optimization with probable large return in
1151f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     performance is to compare the MCS value to zero (indicating
1152f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     all samples are on sample slice 0), and sample only from
1153f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *     sample slice 0 using ld2dss if MCS is zero."
1154f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          *
1155f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Note that in the case where the MCS value is zero, sampling from
1156f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * sample slice 0 using ld2dss and sampling from sample 0 using
1157f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * ld2dms are equivalent (since all samples are on sample slice 0).
1158f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * Since we have already sampled from sample 0, all we need to do is
1159f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          * skip the remaining fetches and averaging if MCS is zero.
1160f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry          */
1161f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_CMP(&func, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ,
1162f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry                 mcs_data, brw_imm_ud(0));
1163f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry         brw_IF(&func, BRW_EXECUTE_16);
1164f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      }
1165f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1166b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      /* Do count_trailing_one_bits(i) times */
1167b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry      for (int j = count_trailing_one_bits(i); j-- > 0; ) {
1168b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         assert(stack_depth >= 2);
1169b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         --stack_depth;
1170b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1171b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         /* TODO: should use a smaller loop bound for non_RGBA formats */
1172b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         for (int k = 0; k < 4; ++k) {
1173e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry            combine_op(&func, offset(texture_data[stack_depth - 1], 2*k),
1174e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth - 1]), 2*k),
1175e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                       offset(vec8(texture_data[stack_depth]), 2*k));
1176b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry         }
11774725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      }
11784725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
11794725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
1180b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   /* We should have just 1 sample on the stack now. */
1181b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry   assert(stack_depth == 1);
1182b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry
1183e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   if (key->texture_data_type == BRW_REGISTER_TYPE_F) {
1184e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* Scale the result down by a factor of num_samples */
1185e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      /* TODO: should use a smaller loop bound for non-RGBA formats */
1186e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      for (int j = 0; j < 4; ++j) {
1187e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         brw_MUL(&func, offset(texture_data[0], 2*j),
1188e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 offset(vec8(texture_data[0]), 2*j),
1189e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry                 brw_imm_f(1.0/num_samples));
1190e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
11914725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   }
1192f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry
1193f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry   if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1194f91b4d92b97664e6354f66138705e93bec363ba0Paul Berry      brw_ENDIF(&func);
11954725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry}
11964725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry
119719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
119819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Emit code to look up a value in the texture using the SAMPLE message (which
119919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * does blending of MSAA surfaces).
120019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
120119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
12024725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::sample(struct brw_reg dst)
120319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
1204665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   static const sampler_message_arg args[2] = {
1205665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_FLOAT,
1206665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_V_FLOAT
1207665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
1208665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
12094725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry   texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE, args, ARRAY_SIZE(args));
121019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
121119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
121219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
1213506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * Emit code to look up a value in the texture using the SAMPLE_LD message
1214506d70be21cd3469118de89297cba0c0f709c1aePaul Berry * (which does a simple texel fetch).
1215506d70be21cd3469118de89297cba0c0f709c1aePaul Berry */
1216506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
12174725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texel_fetch(struct brw_reg dst)
1218506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
12191c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen6_args[5] = {
1220665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
1221233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_V_INT,
1222233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* R */
1223233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
1224233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT
1225665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   };
12261c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld_args[3] = {
12271c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
12281c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
12291c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
12301c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
12311c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   static const sampler_message_arg gen7_ld2dss_args[3] = {
12321c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
12331c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
12341c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
12351c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
12364ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld2dms_args[4] = {
12374ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_SI_INT,
12384ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_MCS_INT,
12394ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
12404ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
12414ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
1242665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
12431c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   switch (brw->intel.gen) {
12441c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 6:
12454725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen6_args,
12461c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                     s_is_zero ? 2 : 5);
12471c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
12481c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   case 7:
12491c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      if (key->tex_samples > 0) {
12504ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
12514ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry            texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS,
12524ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry                           gen7_ld2dms_args, ARRAY_SIZE(gen7_ld2dms_args));
12534ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         } else {
12544ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry            texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS,
12554ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry                           gen7_ld2dss_args, ARRAY_SIZE(gen7_ld2dss_args));
12564ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         }
12571c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      } else {
12581c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry         assert(s_is_zero);
12594725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry         texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen7_ld_args,
12601c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry                        ARRAY_SIZE(gen7_ld_args));
12611c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      }
12621c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
12631c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   default:
12641c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      assert(!"Should not get here.");
12651c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry      break;
12661c73c705fadf164d61003415e3380f2d06f2e7b3Paul Berry   };
1267506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1268506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1269506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
12704ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berrybrw_blorp_blit_program::mcs_fetch()
12714ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry{
12724ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   static const sampler_message_arg gen7_ld_mcs_args[2] = {
12734ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_U_INT,
12744ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      SAMPLER_MESSAGE_ARG_V_INT
12754ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   };
12764ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry   texture_lookup(vec16(mcs_data), GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS,
12774ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry                  gen7_ld_mcs_args, ARRAY_SIZE(gen7_ld_mcs_args));
12784ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry}
12794ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry
12804ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berryvoid
1281665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berrybrw_blorp_blit_program::expand_to_32_bits(struct brw_reg src,
1282665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                          struct brw_reg dst)
1283506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1284665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, vec8(dst), vec8(src));
1285506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
1286665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   brw_MOV(&func, offset(vec8(dst), 1), suboffset(vec8(src), 8));
1287506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
1288665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry}
1289665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry
1290665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berryvoid
12914725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berrybrw_blorp_blit_program::texture_lookup(struct brw_reg dst,
12924725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry                                       GLuint msg_type,
1293665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       const sampler_message_arg *args,
1294665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry                                       int num_args)
1295665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry{
1296665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   struct brw_reg mrf =
1297665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_UD);
1298665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   for (int arg = 0; arg < num_args; ++arg) {
1299665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      switch (args[arg]) {
1300665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_FLOAT:
1301665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, retype(mrf, BRW_REGISTER_TYPE_F));
1302665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1303665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_FLOAT:
1304665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, retype(mrf, BRW_REGISTER_TYPE_F));
1305665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1306665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_U_INT:
1307665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(X, mrf);
1308665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1309665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      case SAMPLER_MESSAGE_ARG_V_INT:
1310665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         expand_to_32_bits(Y, mrf);
1311665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry         break;
1312233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_SI_INT:
1313233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         /* Note: on Gen7, this code may be reached with s_is_zero==true
1314233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * because in Gen7's ld2dss message, the sample index is the first
1315233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * argument.  When this happens, we need to move a 0 into the
1316233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          * appropriate message register.
1317233c207e9e477b6b0a5c6705e727129b92989073Paul Berry          */
1318233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         if (s_is_zero)
1319233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            brw_MOV(&func, mrf, brw_imm_ud(0));
1320233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         else
1321233c207e9e477b6b0a5c6705e727129b92989073Paul Berry            expand_to_32_bits(S, mrf);
1322233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
13234ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry      case SAMPLER_MESSAGE_ARG_MCS_INT:
13244ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         brw_MOV(&func, mrf, mcs_data);
13254ebbc766210190cb1f03fa4fc762bf7ecc0c7f90Paul Berry         break;
1326233c207e9e477b6b0a5c6705e727129b92989073Paul Berry      case SAMPLER_MESSAGE_ARG_ZERO_INT:
1327233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         brw_MOV(&func, mrf, brw_imm_ud(0));
1328233c207e9e477b6b0a5c6705e727129b92989073Paul Berry         break;
1329665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      }
1330665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry      mrf.nr += 2;
1331665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry   }
1332506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1333506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_SAMPLE(&func,
13344725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry              retype(dst, BRW_REGISTER_TYPE_UW) /* dest */,
1335506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              base_mrf /* msg_reg_nr */,
1336665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              brw_message_reg(base_mrf) /* src0 */,
1337506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX,
1338665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              0 /* sampler */,
1339506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              WRITEMASK_XYZW,
1340506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              msg_type,
1341506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              8 /* response_length.  TODO: should be smaller for non-RGBA formats? */,
1342665dc82bdc0e83854dd0f700ec264021bfb5cb39Paul Berry              mrf.nr - base_mrf /* msg_length */,
1343506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              0 /* header_present */,
1344506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_SIMD_MODE_SIMD16,
1345506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              BRW_SAMPLER_RETURN_FORMAT_FLOAT32);
1346506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1347506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1348506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef X
1349506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef Y
1350506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef U
1351506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef V
1352506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef S
1353506d70be21cd3469118de89297cba0c0f709c1aePaul Berry#undef SWAP_XY_AND_XPYP
1354506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1355506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1356506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_program::render_target_write()
1357506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1358e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   struct brw_reg mrf_rt_write =
1359e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      retype(vec16(brw_message_reg(base_mrf)), key->texture_data_type);
1360506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   int mrf_offset = 0;
1361506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1362506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* If we may have killed pixels, then we need to send R0 and R1 in a header
1363506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    * so that the render target knows which pixels we killed.
1364506d70be21cd3469118de89297cba0c0f709c1aePaul Berry    */
1365506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   bool use_header = key->use_kill;
1366506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (use_header) {
1367506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* Copy R0/1 to MRF */
1368506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_MOV(&func, retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
1369506d70be21cd3469118de89297cba0c0f709c1aePaul Berry              retype(R0, BRW_REGISTER_TYPE_UD));
1370506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1371506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1372506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1373506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Copy texture data to MRFs */
1374506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   for (int i = 0; i < 4; ++i) {
1375506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
13764725ba03cae87ddbf1fa10feaca3d42f24115f91Paul Berry      brw_MOV(&func, offset(mrf_rt_write, mrf_offset),
1377b961d37e613b8b14927c42e09d16d09d70ebcb77Paul Berry              offset(vec8(texture_data[0]), 2*i));
1378506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      mrf_offset += 2;
1379506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1380506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1381506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   /* Now write to the render target and terminate the thread */
1382506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   brw_fb_WRITE(&func,
1383506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                16 /* dispatch_width */,
1384506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                base_mrf /* msg_reg_nr */,
1385506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_rt_write /* src0 */,
138629362875f2613ad87abe7725ce3c56c36d16cf9bEric Anholt                BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
1387506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
1388506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                mrf_offset /* msg_length.  TODO: Should be smaller for non-RGBA formats. */,
1389506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                0 /* response_length */,
1390506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                true /* eot */,
1391506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                use_header);
1392506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1393506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1394506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1395506d70be21cd3469118de89297cba0c0f709c1aePaul Berryvoid
1396506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_coord_transform_params::setup(GLuint src0, GLuint dst0, GLuint dst1,
1397506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                        bool mirror)
1398506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1399506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!mirror) {
1400506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When not mirroring a coordinate (say, X), we need:
1401506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = x - dst_x0
1402506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1403506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = 1*x + (src_x0 - dst_x0)
1404506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1405506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = 1;
1406506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 - dst0;
1407506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   } else {
1408506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* When mirroring X we need:
1409506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' - src_x0 = dst_x1 - x - 1
1410506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * Therefore:
1411506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       *   x' = -1*x + (src_x0 + dst_x1 - 1)
1412506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
1413506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      multiplier = -1;
1414506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      offset = src0 + dst1 - 1;
1415506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1416506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1417506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1418506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
14191bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry/**
14201bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * Determine which MSAA layout the GPU pipeline should be configured for,
14211bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * based on the chip generation, the number of samples, and the true layout of
14221bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * the image in memory.
14231bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry */
14241bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berryinline intel_msaa_layout
14251bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrycompute_msaa_layout_for_pipeline(struct brw_context *brw, unsigned num_samples,
14261bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                 intel_msaa_layout true_layout)
14271bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry{
14281bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   if (num_samples == 0) {
14291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* When configuring the GPU for non-MSAA, we can still accommodate IMS
14301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * format buffers, by transforming coordinates appropriately.
14311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       */
14321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_NONE ||
14331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry             true_layout == INTEL_MSAA_LAYOUT_IMS);
14341bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_NONE;
14351bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
14361bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
14371bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Prior to Gen7, all MSAA surfaces use IMS layout. */
14381bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   if (brw->intel.gen == 6) {
14391bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(true_layout == INTEL_MSAA_LAYOUT_IMS);
14401bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_IMS;
14411bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
14421bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
14431bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Since blorp uses color textures and render targets to do all its work
14441bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * (even when blitting stencil and depth data), we always have to configure
14451bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * the Gen7 GPU to use UMS or CMS layout on Gen7.
14461bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    */
14471bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   assert(true_layout == INTEL_MSAA_LAYOUT_UMS ||
14481bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry          true_layout == INTEL_MSAA_LAYOUT_CMS);
14491bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   return true_layout;
14501bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry}
14511bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
14521bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry
14538b1f467cce34340637e9baca4847fc5273cf7541Paul Berrybrw_blorp_blit_params::brw_blorp_blit_params(struct brw_context *brw,
14548b1f467cce34340637e9baca4847fc5273cf7541Paul Berry                                             struct intel_mipmap_tree *src_mt,
1455506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             struct intel_mipmap_tree *dst_mt,
1456506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint src_x0, GLuint src_y0,
1457506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x0, GLuint dst_y0,
1458506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             GLuint dst_x1, GLuint dst_y1,
1459506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                             bool mirror_x, bool mirror_y)
1460506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1461530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   src.set(brw, src_mt, 0, 0);
1462530bda2aacf77b1e4661e5e5dd05cf108640e657Paul Berry   dst.set(brw, dst_mt, 0, 0);
1463506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1464506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   use_wm_prog = true;
1465506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1466506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1467e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   /* texture_data_type indicates the register type that should be used to
1468e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    * manipulate texture data.
1469e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry    */
1470e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   switch (_mesa_get_format_datatype(src_mt->format)) {
1471e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_NORMALIZED:
1472e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_SIGNED_NORMALIZED:
1473e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_FLOAT:
1474e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1475e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1476e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_UNSIGNED_INT:
1477e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      if (src_mt->format == MESA_FORMAT_S8) {
1478e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         /* We process stencil as though it's an unsigned normalized color */
1479e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1480e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      } else {
1481e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry         wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_UD;
1482e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      }
1483e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1484e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   case GL_INT:
1485e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_D;
1486e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1487e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   default:
1488e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      assert(!"Unrecognized blorp format");
1489e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry      break;
1490e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry   }
1491e5d983267a98bf9f73f0ea981eaca339b975a8dbPaul Berry
14928b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   if (brw->intel.gen > 6) {
14931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* Gen7's texturing hardware only supports the IMS layout with the
14948b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       * ld2dms instruction (which blorp doesn't use).  So if the source is
14951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * IMS, we'll have to map it as a single-sampled texture and
14961bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * de-interleave the samples ourselves.
14978b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       */
14981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (src_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
14998b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         src.num_samples = 0;
15008b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
15011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* Similarly, Gen7's rendering hardware only supports the IMS layout for
15021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * depth and stencil render targets.  Blorp always maps its destination
15031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * surface as a color render target (even if it's actually a depth or
15041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * stencil buffer).  So if the destination is IMS, we'll have to map it
15051bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * as a single-sampled texture and interleave the samples ourselves.
15068b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       */
15071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
15088b1f467cce34340637e9baca4847fc5273cf7541Paul Berry         dst.num_samples = 0;
15098b1f467cce34340637e9baca4847fc5273cf7541Paul Berry   }
15108b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
151134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (dst.map_stencil_as_y_tiled && dst.num_samples > 0) {
151234a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      /* If the destination surface is a W-tiled multisampled stencil buffer
151334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * that we're mapping as Y tiled, then we need to arrange for the WM
151434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * program to run once per sample rather than once per pixel, because
151534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * the memory layout of related samples doesn't match between W and Y
151634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * tiling.
1517233c207e9e477b6b0a5c6705e727129b92989073Paul Berry       */
151834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
1519233c207e9e477b6b0a5c6705e727129b92989073Paul Berry   }
1520233c207e9e477b6b0a5c6705e727129b92989073Paul Berry
152134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry   if (src.num_samples > 0 && dst.num_samples > 0) {
152219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are blitting from a multisample buffer to a multisample buffer, so
152319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * we must preserve samples within a pixel.  This means we have to
152434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * arrange for the WM program to run once per sample rather than once
152534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       * per pixel.
152619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
152734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      wm_prog_key.persample_msaa_dispatch = true;
152819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
152919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
153019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* The render path must be configured to use the same number of samples as
153119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * the destination buffer.
153219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
153319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   num_samples = dst.num_samples;
153419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
153519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   GLenum base_format = _mesa_get_format_base_format(src_mt->format);
153619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
153719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       base_format != GL_STENCIL_INDEX &&
153819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       src_mt->num_samples > 0 && dst_mt->num_samples == 0) {
153919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We are downsampling a color buffer, so blend. */
154019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.blend = true;
154119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
154219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
154319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* src_samples and dst_samples are the true sample counts */
154419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.src_samples = src_mt->num_samples;
154519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.dst_samples = dst_mt->num_samples;
154619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
154719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* tex_samples and rt_samples are the sample counts that are set up in
154819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    * SURFACE_STATE.
154919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry    */
155019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.tex_samples = src.num_samples;
155119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   wm_prog_key.rt_samples  = dst.num_samples;
155219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
15531bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
15541bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * use to access the source and destination surfaces.
155567b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry    */
15561bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.tex_layout =
15571bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, src.num_samples, src.msaa_layout);
15581bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.rt_layout =
15591bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      compute_msaa_layout_for_pipeline(brw, dst.num_samples, dst.msaa_layout);
156067b0f7c7dddeb92ee4d24ed3977e20b70f5674f6Paul Berry
15611bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* src_layout and dst_layout indicate the true MSAA layout used by src and
15621bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * dst.
15638b1f467cce34340637e9baca4847fc5273cf7541Paul Berry    */
15641bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.src_layout = src_mt->msaa_layout;
15651bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   wm_prog_key.dst_layout = dst_mt->msaa_layout;
15668b1f467cce34340637e9baca4847fc5273cf7541Paul Berry
1567506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.src_tiled_w = src.map_stencil_as_y_tiled;
1568506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_prog_key.dst_tiled_w = dst.map_stencil_as_y_tiled;
1569506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x0 = wm_push_consts.dst_x0 = dst_x0;
1570506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y0 = wm_push_consts.dst_y0 = dst_y0;
1571506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   x1 = wm_push_consts.dst_x1 = dst_x1;
1572506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   y1 = wm_push_consts.dst_y1 = dst_y1;
1573506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.x_transform.setup(src_x0, dst_x0, dst_x1, mirror_x);
1574506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   wm_push_consts.y_transform.setup(src_y0, dst_y0, dst_y1, mirror_y);
1575506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
157619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (dst.num_samples == 0 && dst_mt->num_samples > 0) {
157719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      /* We must expand the rectangle we send through the rendering pipeline,
157819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * to account for the fact that we are mapping the destination region as
157919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * single-sampled when it is in fact multisampled.  We must also align
158019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * it to a multiple of the multisampling pattern, because the
158119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * differences between multisampled and single-sampled surface formats
158219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * will mean that pixels are scrambled within the multisampling pattern.
158319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       * TODO: what if this makes the coordinates too large?
15848b1f467cce34340637e9baca4847fc5273cf7541Paul Berry       *
15851bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * Note: this only works if the destination surface uses the IMS layout.
15861bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * If it's UMS, then we have no choice but to set up the rendering
15871bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * pipeline as multisampled.
158819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry       */
15891bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      assert(dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS);
159019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      x0 = (x0 * 2) & ~3;
159119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      y0 = (y0 * 2) & ~3;
159219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      x1 = ALIGN(x1 * 2, 4);
159319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      y1 = ALIGN(y1 * 2, 4);
159419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      wm_prog_key.use_kill = true;
159519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
159619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
1597506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (dst.map_stencil_as_y_tiled) {
1598506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      /* We must modify the rectangle we send through the rendering pipeline,
1599506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * to account for the fact that we are mapping it as Y-tiled when it is
1600506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * in fact W-tiled.  Y tiles have dimensions 128x32 whereas W tiles have
1601506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * dimensions 64x64.  We must also align it to a multiple of the tile
1602506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * size, because the differences between W and Y tiling formats will
1603506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * mean that pixels are scrambled within the tile.
160434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
16051bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * Note: if the destination surface configured to use IMS layout, then
16061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * the effective tile size we need to align it to is smaller, because
16071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry       * each pixel covers a 2x2 or a 4x2 block of samples.
160834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry       *
1609506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       * TODO: what if this makes the coordinates too large?
1610506d70be21cd3469118de89297cba0c0f709c1aePaul Berry       */
161134a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      unsigned x_align = 64, y_align = 64;
16121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
161334a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry         x_align /= (dst_mt->num_samples == 4 ? 2 : 4);
161434a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry         y_align /= 2;
161534a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      }
161634a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      x0 = (x0 & ~(x_align - 1)) * 2;
161734a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      y0 = (y0 & ~(y_align - 1)) / 2;
161834a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      x1 = ALIGN(x1, x_align) * 2;
161934a5f12e35dd4a5aff6683a8286d4582ba17df14Paul Berry      y1 = ALIGN(y1, y_align) / 2;
1620506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      wm_prog_key.use_kill = true;
1621506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1622506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1623506d70be21cd3469118de89297cba0c0f709c1aePaul Berry
1624506d70be21cd3469118de89297cba0c0f709c1aePaul Berryuint32_t
1625506d70be21cd3469118de89297cba0c0f709c1aePaul Berrybrw_blorp_blit_params::get_wm_prog(struct brw_context *brw,
1626506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                                   brw_blorp_prog_data **prog_data) const
1627506d70be21cd3469118de89297cba0c0f709c1aePaul Berry{
1628506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   uint32_t prog_offset;
1629506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   if (!brw_search_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1630506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &this->wm_prog_key, sizeof(this->wm_prog_key),
1631506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                         &prog_offset, prog_data)) {
1632506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_blorp_blit_program prog(brw, &this->wm_prog_key);
1633506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      GLuint program_size;
1634506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      const GLuint *program = prog.compile(brw, &program_size);
1635506d70be21cd3469118de89297cba0c0f709c1aePaul Berry      brw_upload_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1636506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &this->wm_prog_key, sizeof(this->wm_prog_key),
1637506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       program, program_size,
1638506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog.prog_data, sizeof(prog.prog_data),
1639506d70be21cd3469118de89297cba0c0f709c1aePaul Berry                       &prog_offset, prog_data);
1640506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   }
1641506d70be21cd3469118de89297cba0c0f709c1aePaul Berry   return prog_offset;
1642506d70be21cd3469118de89297cba0c0f709c1aePaul Berry}
1643