brw_wm.c revision db6dd6d88fdc4361193dd063e4f150f01a104faa
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
126662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholtvoid
12716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholtbrw_wm_payload_setup(struct brw_context *brw,
12816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		     struct brw_wm_compile *c)
12916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt{
13016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   struct intel_context *intel = &brw->intel;
13116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   bool uses_depth = (c->fp->program.Base.InputsRead &
13216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		      (1 << FRAG_ATTRIB_WPOS)) != 0;
13316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
13416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   if (intel->gen >= 6) {
13516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R0-1: masks, pixel X/Y coordinates. */
13616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs = 2;
13716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R2: only for 32-pixel dispatch.*/
13816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R3-4: perspective pixel location barycentric */
13916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs += 2;
14016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R5-6: perspective pixel location bary for dispatch width != 8 */
1415ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      if (c->dispatch_width == 16) {
14216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs += 2;
14316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
14416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R7-10: perspective centroid barycentric */
14516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R11-14: perspective sample barycentric */
14616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R15-18: linear pixel location barycentric */
14716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R19-22: linear centroid barycentric */
14816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R23-26: linear sample barycentric */
14916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
15016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R27: interpolated depth if uses source depth */
15116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
15216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_depth_reg = c->nr_payload_regs;
15316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
1545ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
15516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R28: interpolated depth if not 8-wide. */
15616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
15716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
15816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
15916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W.
16016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt       */
16116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
16216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_w_reg = c->nr_payload_regs;
16316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
1645ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
16516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R30: interpolated W if not 8-wide. */
16616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
16716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
16816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
16916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R31: MSAA position offsets. */
17016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R32-: bary for 32-pixel. */
17116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R58-59: interp W for 32-pixel. */
17216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
17316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (c->fp->program.Base.OutputsWritten &
17416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	  BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
1752e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->source_depth_to_render_target = true;
1762e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke	 c->computes_depth = true;
17716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
17816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   } else {
17916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      brw_wm_lookup_iz(intel, c);
18016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   }
18116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt}
1822f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1832f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
1842f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * All Mesa program -> GPU code generation goes through this function.
1852f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Depending on the instructions used (i.e. flow control instructions)
1862f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * we'll use one of two code generators.
1872f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
1888752764076e5b3f052a57e0134424a37bf2e9164Eric Anholtbool do_wm_prog(struct brw_context *brw,
1898752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct gl_shader_program *prog,
1908752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_fragment_program *fp,
1918752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt		struct brw_wm_prog_key *key)
1929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
19359c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt   struct intel_context *intel = &brw->intel;
194d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   struct brw_wm_compile *c;
1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   const GLuint *program;
1969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint program_size;
1979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
198d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c = brw->wm.compile_data;
199d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   if (c == NULL) {
200774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      brw->wm.compile_data = rzalloc(NULL, struct brw_wm_compile);
20114dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      c = brw->wm.compile_data;
20244a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      if (c == NULL) {
20344a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison         /* Ouch - big out of memory problem.  Can't continue
20444a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * without triggering a segfault, no way to signal,
20544a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * so just return.
20644a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          */
2078752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt         return false;
20844a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      }
209d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   } else {
210c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *instruction = c->instruction;
211c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *prog_instructions = c->prog_instructions;
212c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *vreg = c->vreg;
213c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *refs = c->refs;
21414dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      memset(c, 0, sizeof(*brw->wm.compile_data));
215c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->instruction = instruction;
216c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->prog_instructions = prog_instructions;
217c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->vreg = vreg;
218c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->refs = refs;
219d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
220d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   memcpy(&c->key, key, sizeof(*key));
2219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
222d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->fp = fp;
223d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->env_param = brw->intel.ctx.FragmentProgram.Parameters;
2249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
225774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke   brw_init_compile(brw, &c->func, c);
22614dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul
227de772c402215b956ab3aa0875330fc1bf7cdf95bIan Romanick   if (prog && prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
2288752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      if (!brw_wm_fs_emit(brw, c, prog))
2298752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt	 return false;
2308752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   } else {
2310722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      if (!c->instruction) {
2320722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->instruction = rzalloc_array(c, struct brw_wm_instruction, BRW_WM_MAX_INSN);
2330722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->prog_instructions = rzalloc_array(c, struct prog_instruction, BRW_WM_MAX_INSN);
2340722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->vreg = rzalloc_array(c, struct brw_wm_value, BRW_WM_MAX_VREG);
2350722edc59cd526437c2d4bad474b934dad84d789Eric Anholt	 c->refs = rzalloc_array(c, struct brw_wm_ref, BRW_WM_MAX_REF);
2360722edc59cd526437c2d4bad474b934dad84d789Eric Anholt      }
2370722edc59cd526437c2d4bad474b934dad84d789Eric Anholt
238662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      /* Fallback for fixed function and ARB_fp shaders. */
2395ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      c->dispatch_width = 16;
2405ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_payload_setup(brw, c);
2415ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_non_glsl_emit(brw, c);
242662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      c->prog_data.dispatch_width = 16;
243d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
2442f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
2454e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   /* Scratch space is used for register spilling */
2464e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   if (c->last_scratch) {
2472b224d66a01f3ce867fb05558b25749705bbfe7aEric Anholt      c->prog_data.total_scratch = brw_get_scratch_size(c->last_scratch);
24859c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt
249abbb8fc3a7d49066ecca10cb9db0b4756a1bbef0Eric Anholt      brw_get_scratch_bo(intel, &brw->wm.scratch_bo,
250db6dd6d88fdc4361193dd063e4f150f01a104faaKenneth Graunke			 c->prog_data.total_scratch * brw->max_wm_threads);
2514e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   }
2524e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt
253bb1540835056cdea5db6f55b19c0c87358f14cd1Eric Anholt   if (unlikely(INTEL_DEBUG & DEBUG_WM))
254fc3971d80051b34836716579fd060dbb122d036bEric Anholt      fprintf(stderr, "\n");
255fc3971d80051b34836716579fd060dbb122d036bEric Anholt
2569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* get the program
2579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
258d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   program = brw_get_program(&c->func, &program_size);
2599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
260c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   brw_upload_cache(&brw->cache, BRW_WM_PROG,
261c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->key, sizeof(c->key),
262c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    program, program_size,
263c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &c->prog_data, sizeof(c->prog_data),
264c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt		    &brw->wm.prog_offset, &brw->wm.prog_data);
2658752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt
2668752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   return true;
2679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
2689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic void brw_wm_populate_key( struct brw_context *brw,
2729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				 struct brw_wm_prog_key *key )
2739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
274f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
2759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_FRAGMENT_PROGRAM */
27655d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   const struct brw_fragment_program *fp =
2779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      (struct brw_fragment_program *)brw->fragment_program;
2789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint lookup = 0;
2799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint line_aa;
2809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint i;
2819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   memset(key, 0, sizeof(*key));
2839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Build the index for table lookup
2859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_COLOR */
287a99447314ca1cfce60f2a22285398fb222b2a440Eric Anholt   key->alpha_test = ctx->Color.AlphaEnabled;
2889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   if (fp->program.UsesKill ||
289052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Color.AlphaEnabled)
2909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_KILL_ALPHATEST_BIT;
2919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2925606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
2939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
2949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_DEPTH */
296052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test)
2979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
2989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
299052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test &&
300052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Depth.Mask) /* ?? */
3019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
3029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_STENCIL */
30491e61f435a71436c209934a0ece165b540aba3e0Brian Paul   if (ctx->Stencil._Enabled) {
3059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
3069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
307052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      if (ctx->Stencil.WriteMask[0] ||
308052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	  ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
3099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
3109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   line_aa = AA_NEVER;
3139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
315052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Line.SmoothFlag) {
3169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (brw->intel.reduced_primitive == GL_LINES) {
3179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 line_aa = AA_ALWAYS;
3189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      else if (brw->intel.reduced_primitive == GL_TRIANGLES) {
320052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 if (ctx->Polygon.FrontMode == GL_LINE) {
3219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
323052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if (ctx->Polygon.BackMode == GL_LINE ||
324052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		(ctx->Polygon.CullFlag &&
325052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_BACK))
3269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
328052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 else if (ctx->Polygon.BackMode == GL_LINE) {
3299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
331052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if ((ctx->Polygon.CullFlag &&
332052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_FRONT))
3339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
3359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
33816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->iz_lookup = lookup;
33916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->line_aa = line_aa;
34016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->stats_wm = brw->intel.stats_wm;
3419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_WM_INPUT_DIMENSIONS */
3436b917d0b1787280f976c2f0d1ead0e5d7587a3e9Brian Paul   key->proj_attrib_mask = brw->wm.input_size_masks[4-1];
3449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LIGHT */
346052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
3479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
348d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
349d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
350d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt
3519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_TEXTURE */
3529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
353052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      const struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
3549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (unit->_ReallyEnabled) {
35689fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_object *t = unit->_Current;
35789fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3581f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, i);
35914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 int swizzles[SWIZZLE_NIL + 1] = {
36014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_X,
36114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Y,
36214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Z,
36314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_W,
36414bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ZERO,
36514bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ONE,
36614bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_NIL
36714bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 };
368a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
369a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 /* GL_DEPTH_TEXTURE_MODE is normally handled through
370a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * brw_wm_surface_state, but it applies to shadow compares as
371a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * well and our shadow compares always return the result in
372a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * all 4 channels.
373a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  */
3741f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 if (sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
3756430df37736d71dd2bd6f1fe447d39f0b68cb567Kenneth Graunke	    key->compare_funcs[i] = sampler->CompareFunc;
3766430df37736d71dd2bd6f1fe447d39f0b68cb567Kenneth Graunke
3771f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    if (sampler->DepthMode == GL_ALPHA) {
37814bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[0] = SWIZZLE_ZERO;
37914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
38014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
3811f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_LUMINANCE) {
38214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[3] = SWIZZLE_ONE;
3831f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_RED) {
3841842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       /* See table 3.23 of the GL 3.0 spec. */
3853fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
3863fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
3871842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       swizzles[3] = SWIZZLE_ONE;
388a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	    }
389a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 }
390a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
39189fddf978c9d2ab5042f89110015234e979c2686Brian Paul	 if (img->InternalFormat == GL_YCBCR_MESA) {
39289fddf978c9d2ab5042f89110015234e979c2686Brian Paul	    key->yuvtex_mask |= 1 << i;
3931f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul	    if (img->TexFormat == MESA_FORMAT_YCBCR)
39489fddf978c9d2ab5042f89110015234e979c2686Brian Paul		key->yuvtex_swap_mask |= 1 << i;
3957676980d38cff417015bca8d23549d567d74228bZou Nan hai	 }
396c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul
39714bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 key->tex_swizzles[i] =
39814bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    MAKE_SWIZZLE4(swizzles[GET_SWZ(t->_Swizzle, 0)],
39914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 1)],
40014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 2)],
40114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 3)]);
402b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt
403b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 if (sampler->MinFilter != GL_NEAREST &&
404b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	     sampler->MagFilter != GL_NEAREST) {
405b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapS == GL_CLAMP)
406b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[0] |= 1 << i;
407b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapT == GL_CLAMP)
408b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[1] |= 1 << i;
409b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	    if (sampler->WrapR == GL_CLAMP)
410b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	       key->gl_clamp_mask[2] |= 1 << i;
411b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 }
412c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      }
413c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      else {
414c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul         key->tex_swizzles[i] = SWIZZLE_NOOP;
4159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
4169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
4179c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt
4189c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /* _NEW_BUFFERS */
4199c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /*
4209c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * Include the draw buffer origin and height so that we can calculate
4219c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment position values relative to the bottom left of the drawable,
4229c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * from the incoming screen origin relative position we get as part of our
4239c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * payload.
4249c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
425861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * This is only needed for the WM_WPOSXY opcode when the fragment program
426861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * uses the gl_FragCoord input.
427861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    *
4289c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * We could avoid recompiling by including this as a constant referenced by
4299c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * our program, but if we were to do that it would also be nice to handle
4309c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * getting that constant updated at batchbuffer submit time (when we
4319c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * hold the lock and know where the buffer really is) rather than at emit
4329c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * time when we don't hold the lock and are just guessing.  We could also
4339c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * just avoid using this as key data if the program doesn't use
4349c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment.position.
4359c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
436861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * For DRI2 the origin_x/y will always be (0,0) but we still need the
437861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * drawable height in order to invert the Y axis.
4389c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    */
439861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul   if (fp->program.Base.InputsRead & FRAG_BIT_WPOS) {
440f62c2a0bb89041567467a6c01cf1eb27cec01e9eEric Anholt      key->drawable_height = ctx->DrawBuffer->Height;
4413b337f5cd94384d2d5918fb630aa8089e49b1d8dEric Anholt      key->render_to_fbo = ctx->DrawBuffer->Name != 0;
4429c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   }
4439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4441b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   /* _NEW_BUFFERS */
4451b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
4469ef33b86855c4d000271774030bd1b19b6d79687Brian Paul
4470f5113deed91611ecdda6596542530b1849bb161Eric Anholt   /* CACHE_NEW_VS_PROG */
4485606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   key->vp_outputs_written = brw->vs.prog_data->outputs_written;
4490f5113deed91611ecdda6596542530b1849bb161Eric Anholt
45055d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   /* The unique fragment program ID */
4519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   key->program_string_id = fp->id;
4529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
4539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
455f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void brw_prepare_wm_prog(struct brw_context *brw)
4569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
4578752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct intel_context *intel = &brw->intel;
4588752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   struct gl_context *ctx = &intel->ctx;
4599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_wm_prog_key key;
4609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_fragment_program *fp = (struct brw_fragment_program *)
4619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->fragment_program;
462662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt
4639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   brw_wm_populate_key(brw, &key);
4649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
465c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt   if (!brw_search_cache(&brw->cache, BRW_WM_PROG,
466c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &key, sizeof(key),
467c173541d9769d41a85cc899bc49699a3587df4bfEric Anholt			 &brw->wm.prog_offset, &brw->wm.prog_data)) {
4688752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      bool success = do_wm_prog(brw, ctx->Shader.CurrentFragmentProgram, fp,
4698752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt				&key);
4704170227407eea7fd8287b17480a37309bf73f4e4Brian Paul      (void) success;
4718752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt      assert(success);
4728752764076e5b3f052a57e0134424a37bf2e9164Eric Anholt   }
4739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
4749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_prog = {
4779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
4789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa  = (_NEW_COLOR |
4799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_DEPTH |
4809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_STENCIL |
4819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_POLYGON |
4829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LINE |
4839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LIGHT |
484d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt		_NEW_FRAG_CLAMP |
4859c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt		_NEW_BUFFERS |
4869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_TEXTURE),
4879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw   = (BRW_NEW_FRAGMENT_PROGRAM |
4889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_WM_INPUT_DIMENSIONS |
4899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_REDUCED_PRIMITIVE),
4900f5113deed91611ecdda6596542530b1849bb161Eric Anholt      .cache = CACHE_NEW_VS_PROG,
4919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
492008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie   .prepare = brw_prepare_wm_prog
4939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
4949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
495