brw_wm.c revision 8313f44409ceb733e9f8835926364164237b3111
19f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/*
29f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Copyright (C) Intel Corp.  2006.  All Rights Reserved.
39f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
49f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt develop this 3D driver.
59f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
69f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Permission is hereby granted, free of charge, to any person obtaining
79f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt a copy of this software and associated documentation files (the
89f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt "Software"), to deal in the Software without restriction, including
99f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt without limitation the rights to use, copy, modify, merge, publish,
109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt distribute, sublicense, and/or sell copies of the Software, and to
119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt permit persons to whom the Software is furnished to do so, subject to
129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt the following conditions:
139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt The above copyright notice and this permission notice (including the
159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt next paragraph) shall be included in all copies or substantial
169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt portions of the Software.
179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt **********************************************************************/
279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /*
289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  * Authors:
299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  *   Keith Whitwell <keith@tungstengraphics.com>
309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  */
319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_context.h"
339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_wm.h"
349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_state.h"
35c374487a54aca2dd1053645092367c1cf0414ef7Eric Anholt#include "main/formats.h"
364433b0302d0aa9dc61002e8bb4fd1b752b0be338Brian Paul#include "main/fbobject.h"
371f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt#include "main/samplerobj.h"
388752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt#include "program/prog_parameter.h"
399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
402f0edc60f4bd2ae5999a6afa656e3bb3f181bf0fChad Versace#include "glsl/ralloc.h"
41774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke
4232e03c4a2ff5ef07de892dcd26f6be3b82ab3ba1Brian Paul/** Return number of src args for given instruction */
439f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_nr_args( GLuint opcode )
449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
46699db6d842c52d0b3b98b320f8ef1104a65fa783Eric Anholt   case WM_FRONTFACING:
479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELXY:
4808687c8b402f42eda5e0061112382528836b0fe9Eric Anholt      return 0;
49046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_CINTERP:
50046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_WPOSXY:
5108687c8b402f42eda5e0061112382528836b0fe9Eric Anholt   case WM_DELTAXY:
529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
53046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_LINTERP:
549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELW:
559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 2;
569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_FB_WRITE:
57046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_PINTERP:
589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 3;
599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
60046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      assert(opcode < MAX_OPCODE);
61046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      return _mesa_num_inst_src_regs(opcode);
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
669f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_is_scalar_result( GLuint opcode )
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_COS:
709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_EX2:
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_LG2:
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_POW:
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RCP:
749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RSQ:
759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_SIN:
76dd5ef33e3c2ac7886ca71344e41201d0be2062c0Eric Anholt   case OPCODE_DP2:
779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP3:
789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP4:
799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DPH:
809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DST:
819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 0;
859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
892f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
902f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Do GPU code generation for non-GLSL shader.  non-GLSL shaders have
912f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * no flow control instructions so we can more readily do SSA-style
922f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * optimizations.
932f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
942f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulstatic void
952f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulbrw_wm_non_glsl_emit(struct brw_context *brw, struct brw_wm_compile *c)
962f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul{
972f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Augment fragment program.  Add instructions for pre- and
982f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * post-fragment-program tasks such as interpolation and fogging.
992f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1002f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass_fp(c);
1012f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1022f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Translate to intermediate representation.  Build register usage
1032f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * chains.
1042f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1052f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass0(c);
1062f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1072f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Dead code removal.
1082f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1092f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass1(c);
1102f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1112f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Register allocation.
1125f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * Divide by two because we operate on 16 pixels at a time and require
1135f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * two GRF entries for each logical shader register.
1142f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1152f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   c->grf_limit = BRW_WM_MAX_GRF / 2;
1162f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1172f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass2(c);
1182f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1195f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul   /* how many general-purpose registers are used */
1201791857d7d950d3d2834bbb09b495f51f43ef7c1Eric Anholt   c->prog_data.reg_blocks = brw_register_blocks(c->max_wm_grf);
1215f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul
1222f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Emit GEN4 code.
1232f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1242f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_emit(c);
1252f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul}
1262f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
127e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
128e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry/**
129e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry * Return a bitfield where bit n is set if barycentric interpolation mode n
130e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
131e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry */
13271d71d5e891570e8516c65471939a2ebdc07282aEric Anholtstatic unsigned
1338313f44409ceb733e9f8835926364164237b3111Paul Berrybrw_compute_barycentric_interp_modes(struct brw_context *brw,
1348313f44409ceb733e9f8835926364164237b3111Paul Berry                                     bool shade_model_flat,
1355aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                                     const struct gl_fragment_program *fprog)
136e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry{
1375aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   unsigned barycentric_interp_modes = 0;
1385aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   int attr;
1395aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1405aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   /* Loop through all fragment shader inputs to figure out what interpolation
1415aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    * modes are in use, and set the appropriate bits in
1425aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    * barycentric_interp_modes.
1435aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    */
1445aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   for (attr = 0; attr < FRAG_ATTRIB_MAX; ++attr) {
1455aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      enum glsl_interp_qualifier interp_qualifier =
1465aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         fprog->InterpQualifier[attr];
147d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry      bool is_centroid = fprog->IsCentroid & BITFIELD64_BIT(attr);
1485aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
1495aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1505aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      /* Ignore unused inputs. */
1515aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (!(fprog->Base.InputsRead & BITFIELD64_BIT(attr)))
1525aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         continue;
1535aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1545aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      /* Ignore WPOS and FACE, because they don't require interpolation. */
1555aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (attr == FRAG_ATTRIB_WPOS || attr == FRAG_ATTRIB_FACE)
1565aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         continue;
1575aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1588313f44409ceb733e9f8835926364164237b3111Paul Berry      /* Determine the set (or sets) of barycentric coordinates needed to
1598313f44409ceb733e9f8835926364164237b3111Paul Berry       * interpolate this variable.  Note that when
1608313f44409ceb733e9f8835926364164237b3111Paul Berry       * brw->needs_unlit_centroid_workaround is set, centroid interpolation
1618313f44409ceb733e9f8835926364164237b3111Paul Berry       * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
1628313f44409ceb733e9f8835926364164237b3111Paul Berry       * for lit pixels, so we need both sets of barycentric coordinates.
1638313f44409ceb733e9f8835926364164237b3111Paul Berry       */
1645aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (interp_qualifier == INTERP_QUALIFIER_NOPERSPECTIVE) {
165d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry         if (is_centroid) {
166d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry            barycentric_interp_modes |=
167d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry               1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC;
1688313f44409ceb733e9f8835926364164237b3111Paul Berry         }
1698313f44409ceb733e9f8835926364164237b3111Paul Berry         if (!is_centroid || brw->needs_unlit_centroid_workaround) {
170d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry            barycentric_interp_modes |=
171d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry               1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
172d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry         }
1735aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      } else if (interp_qualifier == INTERP_QUALIFIER_SMOOTH ||
1745aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                 (!(shade_model_flat && is_gl_Color) &&
1755aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                  interp_qualifier == INTERP_QUALIFIER_NONE)) {
176d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry         if (is_centroid) {
177d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry            barycentric_interp_modes |=
178d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry               1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC;
1798313f44409ceb733e9f8835926364164237b3111Paul Berry         }
1808313f44409ceb733e9f8835926364164237b3111Paul Berry         if (!is_centroid || brw->needs_unlit_centroid_workaround) {
181d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry            barycentric_interp_modes |=
182d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry               1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
183d1056541e239dfcee0ad6af2fd2d9fab37dbf025Paul Berry         }
1845aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      }
1855aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   }
1865aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1875aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   return barycentric_interp_modes;
188e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry}
189e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
190e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
191662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholtvoid
19216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholtbrw_wm_payload_setup(struct brw_context *brw,
19316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		     struct brw_wm_compile *c)
19416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt{
19516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   struct intel_context *intel = &brw->intel;
19616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   bool uses_depth = (c->fp->program.Base.InputsRead &
19716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		      (1 << FRAG_ATTRIB_WPOS)) != 0;
19871d71d5e891570e8516c65471939a2ebdc07282aEric Anholt   unsigned barycentric_interp_modes = c->prog_data.barycentric_interp_modes;
199e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry   int i;
20016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
20116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   if (intel->gen >= 6) {
20216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R0-1: masks, pixel X/Y coordinates. */
20316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs = 2;
20416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R2: only for 32-pixel dispatch.*/
205e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
206e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry      /* R3-26: barycentric interpolation coordinates.  These appear in the
207e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * same order that they appear in the brw_wm_barycentric_interp_mode
208e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * enum.  Each set of coordinates occupies 2 registers if dispatch width
209e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * == 8 and 4 registers if dispatch width == 16.  Coordinates only
210e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * appear if they were enabled using the "Barycentric Interpolation
211e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * Mode" bits in WM_STATE.
212e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       */
213e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry      for (i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
214e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry         if (barycentric_interp_modes & (1 << i)) {
215e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            c->barycentric_coord_reg[i] = c->nr_payload_regs;
216e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            c->nr_payload_regs += 2;
217e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            if (c->dispatch_width == 16) {
218e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry               c->nr_payload_regs += 2;
219e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            }
220e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry         }
22116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
22216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
22316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R27: interpolated depth if uses source depth */
22416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
22516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_depth_reg = c->nr_payload_regs;
22616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
2275ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
22816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R28: interpolated depth if not 8-wide. */
22916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
23016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
23116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
23216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W.
23316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt       */
23416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
23516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_w_reg = c->nr_payload_regs;
23616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
2375ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
23816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R30: interpolated W if not 8-wide. */
23916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
24016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
24116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
24216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R31: MSAA position offsets. */
24316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R32-: bary for 32-pixel. */
24416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R58-59: interp W for 32-pixel. */
24516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
24616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (c->fp->program.Base.OutputsWritten &
24716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	  BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
2482e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->source_depth_to_render_target = true;
2492e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->computes_depth = true;
25016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
25116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   } else {
25216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      brw_wm_lookup_iz(intel, c);
25316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   }
25416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt}
2552f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
2562f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
2572f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * All Mesa program -> GPU code generation goes through this function.
2582f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Depending on the instructions used (i.e. flow control instructions)
2592f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * we'll use one of two code generators.
2602f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
2618752764076e5b3f052a57e0134424a37bf2e9164Eric Anholtbool do_wm_prog(struct brw_context *brw,
2628752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct gl_shader_program *prog,
2638752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_fragment_program *fp,
2648752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_wm_prog_key *key)
2659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
26659c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt   struct intel_context *intel = &brw->intel;
267d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   struct brw_wm_compile *c;
2689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   const GLuint *program;
2699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint program_size;
2709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
271d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c = brw->wm.compile_data;
272d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   if (c == NULL) {
273774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      brw->wm.compile_data = rzalloc(NULL, struct brw_wm_compile);
27414dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      c = brw->wm.compile_data;
27544a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      if (c == NULL) {
27644a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison         /* Ouch - big out of memory problem.  Can't continue
27744a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * without triggering a segfault, no way to signal,
27844a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * so just return.
27944a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          */
2808752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt         return false;
28144a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      }
282d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   } else {
283c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *instruction = c->instruction;
284c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *prog_instructions = c->prog_instructions;
285c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *vreg = c->vreg;
286c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *refs = c->refs;
28714dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      memset(c, 0, sizeof(*brw->wm.compile_data));
288c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->instruction = instruction;
289c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->prog_instructions = prog_instructions;
290c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->vreg = vreg;
291c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->refs = refs;
292d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
293d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   memcpy(&c->key, key, sizeof(*key));
2949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
295d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->fp = fp;
296d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->env_param = brw->intel.ctx.FragmentProgram.Parameters;
2979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
298774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke   brw_init_compile(brw, &c->func, c);
29914dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul
30071d71d5e891570e8516c65471939a2ebdc07282aEric Anholt   c->prog_data.barycentric_interp_modes =
3018313f44409ceb733e9f8835926364164237b3111Paul Berry      brw_compute_barycentric_interp_modes(brw, c->key.flat_shade,
3028313f44409ceb733e9f8835926364164237b3111Paul Berry                                           &fp->program);
30371d71d5e891570e8516c65471939a2ebdc07282aEric Anholt
304de772c402215b956ab3aa0875330fc1bf7cdf95bIan Romanick   if (prog && prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
3058752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      if (!brw_wm_fs_emit(brw, c, prog))
3068752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt	 return false;
3078752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   } else {
3080722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      if (!c->instruction) {
3090722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->instruction = rzalloc_array(c, struct brw_wm_instruction, BRW_WM_MAX_INSN);
3100722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->prog_instructions = rzalloc_array(c, struct prog_instruction, BRW_WM_MAX_INSN);
3110722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->vreg = rzalloc_array(c, struct brw_wm_value, BRW_WM_MAX_VREG);
3120722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->refs = rzalloc_array(c, struct brw_wm_ref, BRW_WM_MAX_REF);
3130722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      }
3140722edc59cd526437c2d4bad474b934dad84d789Eric Anholt
315662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      /* Fallback for fixed function and ARB_fp shaders. */
3165ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      c->dispatch_width = 16;
3175ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_payload_setup(brw, c);
3185ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_non_glsl_emit(brw, c);
319662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      c->prog_data.dispatch_width = 16;
320d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
3212f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
3224e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   /* Scratch space is used for register spilling */
3234e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   if (c->last_scratch) {
3242b224d66a01f3ce867fb05558b25749705bbfe7aEric Anholt      c->prog_data.total_scratch = brw_get_scratch_size(c->last_scratch);
32559c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt
326abbb8fc3a7d49066ecca10cb9db0b4756a1bbef0Eric Anholt      brw_get_scratch_bo(intel, &brw->wm.scratch_bo,
327db6dd6d88fdc4361193dd063e4f150f01a104faaKenneth Graunke			 c->prog_data.total_scratch * brw->max_wm_threads);
3284e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   }
3294e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt
330bb1540835056cdea5db6f55b19c0c87358f14cd1Eric Anholt   if (unlikely(INTEL_DEBUG & DEBUG_WM))
331fc3971d80051b34836716579fd060dbb122d036bEric Anholt      fprintf(stderr, "\n");
332fc3971d80051b34836716579fd060dbb122d036bEric Anholt
3339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* get the program
3349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
335d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   program = brw_get_program(&c->func, &program_size);
3369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
337c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   brw_upload_cache(&brw->cache, BRW_WM_PROG,
338c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->key, sizeof(c->key),
339c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    program, program_size,
340c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->prog_data, sizeof(c->prog_data),
341c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &brw->wm.prog_offset, &brw->wm.prog_data);
3428752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt
3438752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   return true;
3449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
3459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3461b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunkevoid
3471b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunkebrw_populate_sampler_prog_key_data(struct gl_context *ctx,
3481b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke				   struct brw_sampler_prog_key_data *key,
3491b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke				   int i)
3501b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke{
3511b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke   const struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
3521b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke
353a07cf3397e332388d3599c83e50ac45511972890Eric Anholt   if (unit->_ReallyEnabled && unit->_Current->Target != GL_TEXTURE_BUFFER) {
3541b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      const struct gl_texture_object *t = unit->_Current;
3551b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3561b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, i);
3571b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      int swizzles[SWIZZLE_NIL + 1] = {
3581b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_X,
3591b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_Y,
3601b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_Z,
3611b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_W,
3621b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_ZERO,
3631b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_ONE,
3641b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 SWIZZLE_NIL
3651b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      };
3661b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke
3671b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      if (img->_BaseFormat == GL_DEPTH_COMPONENT ||
3681b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	  img->_BaseFormat == GL_DEPTH_STENCIL) {
3691b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 if (sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB)
3701b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    key->compare_funcs[i] = sampler->CompareFunc;
3711b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke
3721b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 /* We handle GL_DEPTH_TEXTURE_MODE here instead of as surface format
3731b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	  * overrides because shadow comparison always returns the result of
3741b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	  * the comparison in all channels anyway.
3751b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	  */
3761b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 switch (sampler->DepthMode) {
3771b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 case GL_ALPHA:
3781b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[0] = SWIZZLE_ZERO;
3791b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[1] = SWIZZLE_ZERO;
3801b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[2] = SWIZZLE_ZERO;
3811b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[3] = SWIZZLE_X;
3821b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    break;
3831b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 case GL_LUMINANCE:
3841b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[0] = SWIZZLE_X;
3851b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[1] = SWIZZLE_X;
3861b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[2] = SWIZZLE_X;
3871b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[3] = SWIZZLE_ONE;
3881b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    break;
3891b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 case GL_INTENSITY:
3901b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[0] = SWIZZLE_X;
3911b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[1] = SWIZZLE_X;
3921b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[2] = SWIZZLE_X;
3931b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[3] = SWIZZLE_X;
3941b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    break;
3951b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 case GL_RED:
3961b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[0] = SWIZZLE_X;
3971b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[1] = SWIZZLE_ZERO;
3981b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[2] = SWIZZLE_ZERO;
3991b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    swizzles[3] = SWIZZLE_ONE;
4001b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    break;
4011b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 }
4021b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      }
4039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4041b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      if (img->InternalFormat == GL_YCBCR_MESA) {
4051b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 key->yuvtex_mask |= 1 << i;
4061b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 if (img->TexFormat == MESA_FORMAT_YCBCR)
4071b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	     key->yuvtex_swap_mask |= 1 << i;
4081b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      }
4091b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke
4101b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      key->swizzles[i] =
4111b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 MAKE_SWIZZLE4(swizzles[GET_SWZ(t->_Swizzle, 0)],
4121b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke		       swizzles[GET_SWZ(t->_Swizzle, 1)],
4131b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke		       swizzles[GET_SWZ(t->_Swizzle, 2)],
4141b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke		       swizzles[GET_SWZ(t->_Swizzle, 3)]);
4151b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke
4161b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      if (sampler->MinFilter != GL_NEAREST &&
4171b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	  sampler->MagFilter != GL_NEAREST) {
4181b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 if (sampler->WrapS == GL_CLAMP)
4191b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    key->gl_clamp_mask[0] |= 1 << i;
4201b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 if (sampler->WrapT == GL_CLAMP)
4211b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    key->gl_clamp_mask[1] |= 1 << i;
4221b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	 if (sampler->WrapR == GL_CLAMP)
4231b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke	    key->gl_clamp_mask[2] |= 1 << i;
4241b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      }
4251b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke   }
4261b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke   else {
4271b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke      key->swizzles[i] = SWIZZLE_NOOP;
4281b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke   }
4291b05fc7cdd0e5d77b50bc8ee2f2c851da5884d72Kenneth Graunke}
4309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic void brw_wm_populate_key( struct brw_context *brw,
4329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				 struct brw_wm_prog_key *key )
4339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
434f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
4352f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke   struct intel_context *intel = &brw->intel;
4369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_FRAGMENT_PROGRAM */
43755d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   const struct brw_fragment_program *fp =
4389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      (struct brw_fragment_program *)brw->fragment_program;
439387a3d43d11cbd3eb1bd8ce787a2c693d575300cKenneth Graunke   const struct gl_program *prog = (struct gl_program *) brw->fragment_program;
4409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint lookup = 0;
4419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint line_aa;
4429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint i;
4439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
44482d25963a838cfebdeb9b080169979329ee850eaPaul Berry   /* As a temporary measure we assume that all programs use dFdy() (and hence
44582d25963a838cfebdeb9b080169979329ee850eaPaul Berry    * need to be compiled differently depending on whether we're rendering to
44682d25963a838cfebdeb9b080169979329ee850eaPaul Berry    * an FBO).  FIXME: set this bool correctly based on the contents of the
44782d25963a838cfebdeb9b080169979329ee850eaPaul Berry    * program.
44882d25963a838cfebdeb9b080169979329ee850eaPaul Berry    */
44982d25963a838cfebdeb9b080169979329ee850eaPaul Berry   bool program_uses_dfdy = true;
45082d25963a838cfebdeb9b080169979329ee850eaPaul Berry
4519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   memset(key, 0, sizeof(*key));
4529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Build the index for table lookup
4549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
4559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_COLOR */
456a99447314ca1cfce60f2a22285398fb222b2a440Eric Anholt   key->alpha_test = ctx->Color.AlphaEnabled;
4579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4582f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke   if (intel->gen < 6) {
4592f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      if (fp->program.UsesKill || ctx->Color.AlphaEnabled)
4602f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 lookup |= IZ_PS_KILL_ALPHATEST_BIT;
4612f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke
4622f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
4632f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
4649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4652f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      /* _NEW_DEPTH */
4662f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      if (ctx->Depth.Test)
4672f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
4689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4692f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      if (ctx->Depth.Test && ctx->Depth.Mask) /* ?? */
4702f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
4719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4722f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      /* _NEW_STENCIL */
4732f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      if (ctx->Stencil._Enabled) {
4742f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
4759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4762f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	 if (ctx->Stencil.WriteMask[0] ||
4772f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	     ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
4782f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke	    lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
4792f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      }
4802f8351a5ac7bb04482eebaa73d967f7527df4d18Kenneth Graunke      key->iz_lookup = lookup;
4819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
4829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   line_aa = AA_NEVER;
4849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
486052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Line.SmoothFlag) {
4879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (brw->intel.reduced_primitive == GL_LINES) {
4889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 line_aa = AA_ALWAYS;
4899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
4909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      else if (brw->intel.reduced_primitive == GL_TRIANGLES) {
491052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 if (ctx->Polygon.FrontMode == GL_LINE) {
4929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
4939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
494052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if (ctx->Polygon.BackMode == GL_LINE ||
495052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		(ctx->Polygon.CullFlag &&
496052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_BACK))
4979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
4989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
499052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 else if (ctx->Polygon.BackMode == GL_LINE) {
5009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
5019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
502052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if ((ctx->Polygon.CullFlag &&
503052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_FRONT))
5049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
5059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
5069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
5079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
5089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
50916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->line_aa = line_aa;
51016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->stats_wm = brw->intel.stats_wm;
5119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_WM_INPUT_DIMENSIONS */
5136b917d0b1787280f976c2f0d1ead0e5d7587a3e9Brian Paul   key->proj_attrib_mask = brw->wm.input_size_masks[4-1];
5149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LIGHT */
516052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
5179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
518d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
519d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
520d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt
5219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_TEXTURE */
5229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
523387a3d43d11cbd3eb1bd8ce787a2c693d575300cKenneth Graunke      if (prog->TexturesUsed[i])
524387a3d43d11cbd3eb1bd8ce787a2c693d575300cKenneth Graunke	 brw_populate_sampler_prog_key_data(ctx, &key->tex, i);
5259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
5269c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt
5279c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /* _NEW_BUFFERS */
5289c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /*
5299c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * Include the draw buffer origin and height so that we can calculate
5309c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment position values relative to the bottom left of the drawable,
5319c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * from the incoming screen origin relative position we get as part of our
5329c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * payload.
5339c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
534861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * This is only needed for the WM_WPOSXY opcode when the fragment program
535861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * uses the gl_FragCoord input.
536861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    *
5379c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * We could avoid recompiling by including this as a constant referenced by
5389c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * our program, but if we were to do that it would also be nice to handle
5399c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * getting that constant updated at batchbuffer submit time (when we
5409c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * hold the lock and know where the buffer really is) rather than at emit
5419c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * time when we don't hold the lock and are just guessing.  We could also
5429c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * just avoid using this as key data if the program doesn't use
5439c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment.position.
5449c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
545861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * For DRI2 the origin_x/y will always be (0,0) but we still need the
546861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * drawable height in order to invert the Y axis.
5479c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    */
548861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul   if (fp->program.Base.InputsRead & FRAG_BIT_WPOS) {
549f62c2a0bb89041567467a6c01cf1eb27cec01e9eEric Anholt      key->drawable_height = ctx->DrawBuffer->Height;
55082d25963a838cfebdeb9b080169979329ee850eaPaul Berry   }
55182d25963a838cfebdeb9b080169979329ee850eaPaul Berry
55282d25963a838cfebdeb9b080169979329ee850eaPaul Berry   if ((fp->program.Base.InputsRead & FRAG_BIT_WPOS) || program_uses_dfdy) {
5534433b0302d0aa9dc61002e8bb4fd1b752b0be338Brian Paul      key->render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
5549c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   }
5559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5561b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   /* _NEW_BUFFERS */
5571b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
5589ef33b86855c4d000271774030bd1b19b6d79687Brian Paul
5590f5113deed91611ecdda6596542530b1849bb161Eric Anholt   /* CACHE_NEW_VS_PROG */
5605606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   key->vp_outputs_written = brw->vs.prog_data->outputs_written;
5610f5113deed91611ecdda6596542530b1849bb161Eric Anholt
56255d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   /* The unique fragment program ID */
5639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   key->program_string_id = fp->id;
5649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
5659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
567dc9a753f6687133d2d057597e5af86abcdc56781Eric Anholtstatic void
568dc9a753f6687133d2d057597e5af86abcdc56781Eric Anholtbrw_upload_wm_prog(struct brw_context *brw)
5699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
5708752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct intel_context *intel = &brw->intel;
5718752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct gl_context *ctx = &intel->ctx;
5729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_wm_prog_key key;
5739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_fragment_program *fp = (struct brw_fragment_program *)
5749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->fragment_program;
575662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt
5769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   brw_wm_populate_key(brw, &key);
5779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
578c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   if (!brw_search_cache(&brw->cache, BRW_WM_PROG,
579c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &key, sizeof(key),
580c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &brw->wm.prog_offset, &brw->wm.prog_data)) {
581b527dd65c830a2b008816cf390d5be906e29bb23Ian Romanick      bool success = do_wm_prog(brw, ctx->Shader._CurrentFragmentProgram, fp,
5828752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt				&key);
5834170227407eea7fd8287b17480a37309bf73f4e4Brian Paul      (void) success;
5848752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      assert(success);
5858752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   }
5869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
5879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_prog = {
5909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
5919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa  = (_NEW_COLOR |
5929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_DEPTH |
5939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_STENCIL |
5949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_POLYGON |
5959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LINE |
5969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LIGHT |
597d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt		_NEW_FRAG_CLAMP |
5989c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt		_NEW_BUFFERS |
5999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_TEXTURE),
6009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw   = (BRW_NEW_FRAGMENT_PROGRAM |
6019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_WM_INPUT_DIMENSIONS |
6029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_REDUCED_PRIMITIVE),
6030f5113deed91611ecdda6596542530b1849bb161Eric Anholt      .cache = CACHE_NEW_VS_PROG,
6049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
605dc9a753f6687133d2d057597e5af86abcdc56781Eric Anholt   .emit = brw_upload_wm_prog
6069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
6079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
608