brw_wm.c revision 5aa96286e7e1a5380673eb75e8653616b48751fd
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"
361f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt#include "main/samplerobj.h"
378752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt#include "program/prog_parameter.h"
389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
392f0edc60f4bd2ae5999a6afa656e3bb3f181bf0fChad Versace#include "glsl/ralloc.h"
40774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke
4132e03c4a2ff5ef07de892dcd26f6be3b82ab3ba1Brian Paul/** Return number of src args for given instruction */
429f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_nr_args( GLuint opcode )
439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
45699db6d842c52d0b3b98b320f8ef1104a65fa783Eric Anholt   case WM_FRONTFACING:
469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELXY:
4708687c8b402f42eda5e0061112382528836b0fe9Eric Anholt      return 0;
48046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_CINTERP:
49046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_WPOSXY:
5008687c8b402f42eda5e0061112382528836b0fe9Eric Anholt   case WM_DELTAXY:
519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
52046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_LINTERP:
539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELW:
549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 2;
559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_FB_WRITE:
56046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_PINTERP:
579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 3;
589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
59046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      assert(opcode < MAX_OPCODE);
60046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      return _mesa_num_inst_src_regs(opcode);
619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
659f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_is_scalar_result( GLuint opcode )
669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_COS:
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_EX2:
709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_LG2:
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_POW:
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RCP:
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RSQ:
749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_SIN:
75dd5ef33e3c2ac7886ca71344e41201d0be2062c0Eric Anholt   case OPCODE_DP2:
769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP3:
779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP4:
789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DPH:
799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DST:
809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 0;
849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
882f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
892f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Do GPU code generation for non-GLSL shader.  non-GLSL shaders have
902f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * no flow control instructions so we can more readily do SSA-style
912f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * optimizations.
922f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
932f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulstatic void
942f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulbrw_wm_non_glsl_emit(struct brw_context *brw, struct brw_wm_compile *c)
952f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul{
962f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Augment fragment program.  Add instructions for pre- and
972f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * post-fragment-program tasks such as interpolation and fogging.
982f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
992f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass_fp(c);
1002f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1012f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Translate to intermediate representation.  Build register usage
1022f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * chains.
1032f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1042f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass0(c);
1052f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1062f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Dead code removal.
1072f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1082f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass1(c);
1092f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1102f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Register allocation.
1115f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * Divide by two because we operate on 16 pixels at a time and require
1125f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * two GRF entries for each logical shader register.
1132f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1142f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   c->grf_limit = BRW_WM_MAX_GRF / 2;
1152f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1162f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass2(c);
1172f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1185f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul   /* how many general-purpose registers are used */
1191791857d7d950d3d2834bbb09b495f51f43ef7c1Eric Anholt   c->prog_data.reg_blocks = brw_register_blocks(c->max_wm_grf);
1205f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul
1212f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Emit GEN4 code.
1222f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1232f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_emit(c);
1242f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul}
1252f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
126e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
127e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry/**
128e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry * Return a bitfield where bit n is set if barycentric interpolation mode n
129e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
130e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry */
131e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berryunsigned
1325aa96286e7e1a5380673eb75e8653616b48751fdPaul Berrybrw_compute_barycentric_interp_modes(bool shade_model_flat,
1335aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                                     const struct gl_fragment_program *fprog)
134e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry{
1355aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   unsigned barycentric_interp_modes = 0;
1365aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   int attr;
1375aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1385aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   /* Loop through all fragment shader inputs to figure out what interpolation
1395aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    * modes are in use, and set the appropriate bits in
1405aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    * barycentric_interp_modes.
1415aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry    */
1425aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   for (attr = 0; attr < FRAG_ATTRIB_MAX; ++attr) {
1435aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      enum glsl_interp_qualifier interp_qualifier =
1445aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         fprog->InterpQualifier[attr];
1455aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
1465aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1475aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      /* Ignore unused inputs. */
1485aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (!(fprog->Base.InputsRead & BITFIELD64_BIT(attr)))
1495aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         continue;
1505aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1515aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      /* Ignore WPOS and FACE, because they don't require interpolation. */
1525aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (attr == FRAG_ATTRIB_WPOS || attr == FRAG_ATTRIB_FACE)
1535aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         continue;
1545aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1555aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      if (interp_qualifier == INTERP_QUALIFIER_NOPERSPECTIVE) {
1565aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         barycentric_interp_modes |=
1575aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry            1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
1585aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      } else if (interp_qualifier == INTERP_QUALIFIER_SMOOTH ||
1595aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                 (!(shade_model_flat && is_gl_Color) &&
1605aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                  interp_qualifier == INTERP_QUALIFIER_NONE)) {
1615aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry         barycentric_interp_modes |=
1625aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry            1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
1635aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      }
1645aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   }
1655aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry
1665aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry   return barycentric_interp_modes;
167e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry}
168e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
169e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
170662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholtvoid
17116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholtbrw_wm_payload_setup(struct brw_context *brw,
17216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		     struct brw_wm_compile *c)
17316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt{
17416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   struct intel_context *intel = &brw->intel;
17516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   bool uses_depth = (c->fp->program.Base.InputsRead &
17616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		      (1 << FRAG_ATTRIB_WPOS)) != 0;
177e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry   unsigned barycentric_interp_modes =
1785aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry      brw_compute_barycentric_interp_modes(c->key.flat_shade,
1795aa96286e7e1a5380673eb75e8653616b48751fdPaul Berry                                           &c->fp->program);
180e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry   int i;
18116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
18216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   if (intel->gen >= 6) {
18316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R0-1: masks, pixel X/Y coordinates. */
18416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs = 2;
18516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R2: only for 32-pixel dispatch.*/
186e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry
187e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry      /* R3-26: barycentric interpolation coordinates.  These appear in the
188e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * same order that they appear in the brw_wm_barycentric_interp_mode
189e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * enum.  Each set of coordinates occupies 2 registers if dispatch width
190e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * == 8 and 4 registers if dispatch width == 16.  Coordinates only
191e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * appear if they were enabled using the "Barycentric Interpolation
192e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       * Mode" bits in WM_STATE.
193e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry       */
194e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry      for (i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
195e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry         if (barycentric_interp_modes & (1 << i)) {
196e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            c->barycentric_coord_reg[i] = c->nr_payload_regs;
197e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            c->nr_payload_regs += 2;
198e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            if (c->dispatch_width == 16) {
199e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry               c->nr_payload_regs += 2;
200e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry            }
201e04bdeae82797dbdcf6f544a997a4626fdfd4aeePaul Berry         }
20216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
20316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
20416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R27: interpolated depth if uses source depth */
20516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
20616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_depth_reg = c->nr_payload_regs;
20716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
2085ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
20916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R28: interpolated depth if not 8-wide. */
21016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
21116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
21216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
21316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W.
21416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt       */
21516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
21616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_w_reg = c->nr_payload_regs;
21716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
2185ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
21916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R30: interpolated W if not 8-wide. */
22016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
22116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
22216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
22316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R31: MSAA position offsets. */
22416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R32-: bary for 32-pixel. */
22516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R58-59: interp W for 32-pixel. */
22616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
22716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (c->fp->program.Base.OutputsWritten &
22816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	  BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
2292e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->source_depth_to_render_target = true;
2302e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->computes_depth = true;
23116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
23216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   } else {
23316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      brw_wm_lookup_iz(intel, c);
23416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   }
23516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt}
2362f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
2372f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
2382f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * All Mesa program -> GPU code generation goes through this function.
2392f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Depending on the instructions used (i.e. flow control instructions)
2402f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * we'll use one of two code generators.
2412f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
2428752764076e5b3f052a57e0134424a37bf2e9164Eric Anholtbool do_wm_prog(struct brw_context *brw,
2438752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct gl_shader_program *prog,
2448752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_fragment_program *fp,
2458752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_wm_prog_key *key)
2469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
24759c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt   struct intel_context *intel = &brw->intel;
248d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   struct brw_wm_compile *c;
2499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   const GLuint *program;
2509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint program_size;
2519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
252d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c = brw->wm.compile_data;
253d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   if (c == NULL) {
254774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      brw->wm.compile_data = rzalloc(NULL, struct brw_wm_compile);
25514dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      c = brw->wm.compile_data;
25644a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      if (c == NULL) {
25744a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison         /* Ouch - big out of memory problem.  Can't continue
25844a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * without triggering a segfault, no way to signal,
25944a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * so just return.
26044a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          */
2618752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt         return false;
26244a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      }
263d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   } else {
264c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *instruction = c->instruction;
265c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *prog_instructions = c->prog_instructions;
266c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *vreg = c->vreg;
267c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *refs = c->refs;
26814dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      memset(c, 0, sizeof(*brw->wm.compile_data));
269c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->instruction = instruction;
270c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->prog_instructions = prog_instructions;
271c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->vreg = vreg;
272c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->refs = refs;
273d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
274d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   memcpy(&c->key, key, sizeof(*key));
2759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
276d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->fp = fp;
277d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->env_param = brw->intel.ctx.FragmentProgram.Parameters;
2789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
279774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke   brw_init_compile(brw, &c->func, c);
28014dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul
281de772c402215b956ab3aa0875330fc1bf7cdf95bIan Romanick   if (prog && prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
2828752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      if (!brw_wm_fs_emit(brw, c, prog))
2838752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt	 return false;
2848752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   } else {
2850722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      if (!c->instruction) {
2860722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->instruction = rzalloc_array(c, struct brw_wm_instruction, BRW_WM_MAX_INSN);
2870722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->prog_instructions = rzalloc_array(c, struct prog_instruction, BRW_WM_MAX_INSN);
2880722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->vreg = rzalloc_array(c, struct brw_wm_value, BRW_WM_MAX_VREG);
2890722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->refs = rzalloc_array(c, struct brw_wm_ref, BRW_WM_MAX_REF);
2900722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      }
2910722edc59cd526437c2d4bad474b934dad84d789Eric Anholt
292662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      /* Fallback for fixed function and ARB_fp shaders. */
2935ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      c->dispatch_width = 16;
2945ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_payload_setup(brw, c);
2955ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_non_glsl_emit(brw, c);
296662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      c->prog_data.dispatch_width = 16;
297d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
2982f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
2994e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   /* Scratch space is used for register spilling */
3004e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   if (c->last_scratch) {
3012b224d66a01f3ce867fb05558b25749705bbfe7aEric Anholt      c->prog_data.total_scratch = brw_get_scratch_size(c->last_scratch);
30259c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt
303abbb8fc3a7d49066ecca10cb9db0b4756a1bbef0Eric Anholt      brw_get_scratch_bo(intel, &brw->wm.scratch_bo,
304db6dd6d88fdc4361193dd063e4f150f01a104faaKenneth Graunke			 c->prog_data.total_scratch * brw->max_wm_threads);
3054e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   }
3064e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt
307bb1540835056cdea5db6f55b19c0c87358f14cd1Eric Anholt   if (unlikely(INTEL_DEBUG & DEBUG_WM))
308fc3971d80051b34836716579fd060dbb122d036bEric Anholt      fprintf(stderr, "\n");
309fc3971d80051b34836716579fd060dbb122d036bEric Anholt
3109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* get the program
3119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
312d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   program = brw_get_program(&c->func, &program_size);
3139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
314c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   brw_upload_cache(&brw->cache, BRW_WM_PROG,
315c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->key, sizeof(c->key),
316c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    program, program_size,
317c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->prog_data, sizeof(c->prog_data),
318c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &brw->wm.prog_offset, &brw->wm.prog_data);
3198752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt
3208752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   return true;
3219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
3229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic void brw_wm_populate_key( struct brw_context *brw,
3269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				 struct brw_wm_prog_key *key )
3279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
328f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
3299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_FRAGMENT_PROGRAM */
33055d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   const struct brw_fragment_program *fp =
3319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      (struct brw_fragment_program *)brw->fragment_program;
3329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint lookup = 0;
3339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint line_aa;
3349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint i;
3359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   memset(key, 0, sizeof(*key));
3379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Build the index for table lookup
3399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
3409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_COLOR */
341a99447314ca1cfce60f2a22285398fb222b2a440Eric Anholt   key->alpha_test = ctx->Color.AlphaEnabled;
3429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   if (fp->program.UsesKill ||
343052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Color.AlphaEnabled)
3449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_KILL_ALPHATEST_BIT;
3459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3465606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
3479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
3489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_DEPTH */
350052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test)
3519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
3529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
353052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test &&
354052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Depth.Mask) /* ?? */
3559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
3569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_STENCIL */
35891e61f435a71436c209934a0ece165b540aba3e0Brian Paul   if (ctx->Stencil._Enabled) {
3599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
3609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
361052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      if (ctx->Stencil.WriteMask[0] ||
362052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	  ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
3639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
3649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   line_aa = AA_NEVER;
3679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
369052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Line.SmoothFlag) {
3709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (brw->intel.reduced_primitive == GL_LINES) {
3719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 line_aa = AA_ALWAYS;
3729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      else if (brw->intel.reduced_primitive == GL_TRIANGLES) {
374052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 if (ctx->Polygon.FrontMode == GL_LINE) {
3759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
377052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if (ctx->Polygon.BackMode == GL_LINE ||
378052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		(ctx->Polygon.CullFlag &&
379052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_BACK))
3809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
382052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 else if (ctx->Polygon.BackMode == GL_LINE) {
3839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
385052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if ((ctx->Polygon.CullFlag &&
386052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_FRONT))
3879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
3899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
39216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->iz_lookup = lookup;
39316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->line_aa = line_aa;
39416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->stats_wm = brw->intel.stats_wm;
3959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_WM_INPUT_DIMENSIONS */
3976b917d0b1787280f976c2f0d1ead0e5d7587a3e9Brian Paul   key->proj_attrib_mask = brw->wm.input_size_masks[4-1];
3989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LIGHT */
400052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
4019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
402d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
403d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
404d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt
4059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_TEXTURE */
4069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
407052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      const struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
4089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (unit->_ReallyEnabled) {
41089fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_object *t = unit->_Current;
41189fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
4121f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, i);
41314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 int swizzles[SWIZZLE_NIL + 1] = {
41414bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_X,
41514bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Y,
41614bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Z,
41714bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_W,
41814bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ZERO,
41914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ONE,
42014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_NIL
42114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 };
422a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
423a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 /* GL_DEPTH_TEXTURE_MODE is normally handled through
424a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * brw_wm_surface_state, but it applies to shadow compares as
425a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * well and our shadow compares always return the result in
426a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * all 4 channels.
427a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  */
4281f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 if (sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
4296430df37736d71dd2bd6f1fe447d39f0b68cb567Kenneth Graunke	    key->compare_funcs[i] = sampler->CompareFunc;
4306430df37736d71dd2bd6f1fe447d39f0b68cb567Kenneth Graunke
4311f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    if (sampler->DepthMode == GL_ALPHA) {
43214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[0] = SWIZZLE_ZERO;
43314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
43414bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
4351f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_LUMINANCE) {
43614bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[3] = SWIZZLE_ONE;
4371f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_RED) {
4381842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       /* See table 3.23 of the GL 3.0 spec. */
4393fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
4403fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
4411842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       swizzles[3] = SWIZZLE_ONE;
442a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	    }
443a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 }
444a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
44589fddf978c9d2ab5042f89110015234e979c2686Brian Paul	 if (img->InternalFormat == GL_YCBCR_MESA) {
44689fddf978c9d2ab5042f89110015234e979c2686Brian Paul	    key->yuvtex_mask |= 1 << i;
4471f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul	    if (img->TexFormat == MESA_FORMAT_YCBCR)
44889fddf978c9d2ab5042f89110015234e979c2686Brian Paul		key->yuvtex_swap_mask |= 1 << i;
4497676980d38cff417015bca8d23549d567d74228bZou Nan hai	 }
450c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul
45114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 key->tex_swizzles[i] =
45214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    MAKE_SWIZZLE4(swizzles[GET_SWZ(t->_Swizzle, 0)],
45314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 1)],
45414bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 2)],
45514bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 3)]);
456b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt
457b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 if (sampler->MinFilter != GL_NEAREST &&
458b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	     sampler->MagFilter != GL_NEAREST) {
459b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapS == GL_CLAMP)
460b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[0] |= 1 << i;
461b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapT == GL_CLAMP)
462b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[1] |= 1 << i;
463b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapR == GL_CLAMP)
464b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[2] |= 1 << i;
465b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 }
466c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      }
467c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      else {
468c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul         key->tex_swizzles[i] = SWIZZLE_NOOP;
4699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
4709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
4719c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt
4729c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /* _NEW_BUFFERS */
4739c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /*
4749c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * Include the draw buffer origin and height so that we can calculate
4759c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment position values relative to the bottom left of the drawable,
4769c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * from the incoming screen origin relative position we get as part of our
4779c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * payload.
4789c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
479861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * This is only needed for the WM_WPOSXY opcode when the fragment program
480861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * uses the gl_FragCoord input.
481861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    *
4829c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * We could avoid recompiling by including this as a constant referenced by
4839c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * our program, but if we were to do that it would also be nice to handle
4849c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * getting that constant updated at batchbuffer submit time (when we
4859c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * hold the lock and know where the buffer really is) rather than at emit
4869c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * time when we don't hold the lock and are just guessing.  We could also
4879c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * just avoid using this as key data if the program doesn't use
4889c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment.position.
4899c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
490861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * For DRI2 the origin_x/y will always be (0,0) but we still need the
491861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * drawable height in order to invert the Y axis.
4929c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    */
493861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul   if (fp->program.Base.InputsRead & FRAG_BIT_WPOS) {
494f62c2a0bb89041567467a6c01cf1eb27cec01e9eEric Anholt      key->drawable_height = ctx->DrawBuffer->Height;
4953b337f5cd94384d2d5918fb630aa8089e49b1d8dEric Anholt      key->render_to_fbo = ctx->DrawBuffer->Name != 0;
4969c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   }
4979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4981b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   /* _NEW_BUFFERS */
4991b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
5009ef33b86855c4d000271774030bd1b19b6d79687Brian Paul
5010f5113deed91611ecdda6596542530b1849bb161Eric Anholt   /* CACHE_NEW_VS_PROG */
5025606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   key->vp_outputs_written = brw->vs.prog_data->outputs_written;
5030f5113deed91611ecdda6596542530b1849bb161Eric Anholt
50455d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   /* The unique fragment program ID */
5059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   key->program_string_id = fp->id;
5069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
5079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
509f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void brw_prepare_wm_prog(struct brw_context *brw)
5109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
5118752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct intel_context *intel = &brw->intel;
5128752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct gl_context *ctx = &intel->ctx;
5139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_wm_prog_key key;
5149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_fragment_program *fp = (struct brw_fragment_program *)
5159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->fragment_program;
516662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt
5179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   brw_wm_populate_key(brw, &key);
5189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
519c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   if (!brw_search_cache(&brw->cache, BRW_WM_PROG,
520c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &key, sizeof(key),
521c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &brw->wm.prog_offset, &brw->wm.prog_data)) {
5228752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      bool success = do_wm_prog(brw, ctx->Shader.CurrentFragmentProgram, fp,
5238752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt				&key);
5244170227407eea7fd8287b17480a37309bf73f4e4Brian Paul      (void) success;
5258752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      assert(success);
5268752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   }
5279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
5289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
5309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_prog = {
5319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
5329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa  = (_NEW_COLOR |
5339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_DEPTH |
5349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_STENCIL |
5359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_POLYGON |
5369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LINE |
5379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LIGHT |
538d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt		_NEW_FRAG_CLAMP |
5399c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt		_NEW_BUFFERS |
5409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_TEXTURE),
5419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw   = (BRW_NEW_FRAGMENT_PROGRAM |
5429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_WM_INPUT_DIMENSIONS |
5439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_REDUCED_PRIMITIVE),
5440f5113deed91611ecdda6596542530b1849bb161Eric Anholt      .cache = CACHE_NEW_VS_PROG,
5459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
546008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie   .prepare = brw_prepare_wm_prog
5479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
5489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
549