brw_wm.c revision 774fb90db3e83d5e7326b7a72e05ce805c306b24
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"
379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
38774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke#include "../glsl/ralloc.h"
39774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke
4032e03c4a2ff5ef07de892dcd26f6be3b82ab3ba1Brian Paul/** Return number of src args for given instruction */
419f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_nr_args( GLuint opcode )
429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
44699db6d842c52d0b3b98b320f8ef1104a65fa783Eric Anholt   case WM_FRONTFACING:
459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELXY:
4608687c8b402f42eda5e0061112382528836b0fe9Eric Anholt      return 0;
47046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_CINTERP:
48046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_WPOSXY:
4908687c8b402f42eda5e0061112382528836b0fe9Eric Anholt   case WM_DELTAXY:
509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
51046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_LINTERP:
529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_PIXELW:
539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 2;
549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case WM_FB_WRITE:
55046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt   case WM_PINTERP:
569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 3;
579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
58046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      assert(opcode < MAX_OPCODE);
59046e88fc0be37d5a3dfbfa9fb8033b549604c74cEric Anholt      return _mesa_num_inst_src_regs(opcode);
609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
649f344b3e7d6e23674dd4747faec253f103563b36Eric AnholtGLuint brw_wm_is_scalar_result( GLuint opcode )
659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (opcode) {
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_COS:
689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_EX2:
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_LG2:
709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_POW:
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RCP:
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_RSQ:
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_SIN:
74dd5ef33e3c2ac7886ca71344e41201d0be2062c0Eric Anholt   case OPCODE_DP2:
759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP3:
769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DP4:
779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DPH:
789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case OPCODE_DST:
799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 1;
809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return 0;
839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
872f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
882f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Do GPU code generation for non-GLSL shader.  non-GLSL shaders have
892f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * no flow control instructions so we can more readily do SSA-style
902f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * optimizations.
912f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
922f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulstatic void
932f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paulbrw_wm_non_glsl_emit(struct brw_context *brw, struct brw_wm_compile *c)
942f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul{
952f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Augment fragment program.  Add instructions for pre- and
962f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * post-fragment-program tasks such as interpolation and fogging.
972f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
982f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass_fp(c);
992f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1002f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Translate to intermediate representation.  Build register usage
1012f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    * chains.
1022f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1032f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass0(c);
1042f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1052f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Dead code removal.
1062f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1072f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass1(c);
1082f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1092f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Register allocation.
1105f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * Divide by two because we operate on 16 pixels at a time and require
1115f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul    * two GRF entries for each logical shader register.
1122f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1132f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   c->grf_limit = BRW_WM_MAX_GRF / 2;
1142f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1152f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_pass2(c);
1162f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1175f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul   /* how many general-purpose registers are used */
1182f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   c->prog_data.total_grf = c->max_wm_grf;
1195f1ce6b87e837b9f6bc2a4f3e81cf8feea4af2dfBrian Paul
1202f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   /* Emit GEN4 code.
1212f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul    */
1222f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul   brw_wm_emit(c);
1232f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul}
1242f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
125662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholtvoid
12616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholtbrw_wm_payload_setup(struct brw_context *brw,
12716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		     struct brw_wm_compile *c)
12816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt{
12916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   struct intel_context *intel = &brw->intel;
13016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   bool uses_depth = (c->fp->program.Base.InputsRead &
13116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt		      (1 << FRAG_ATTRIB_WPOS)) != 0;
13216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
13316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   if (intel->gen >= 6) {
13416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R0-1: masks, pixel X/Y coordinates. */
13516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs = 2;
13616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R2: only for 32-pixel dispatch.*/
13716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R3-4: perspective pixel location barycentric */
13816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      c->nr_payload_regs += 2;
13916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R5-6: perspective pixel location bary for dispatch width != 8 */
1405ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      if (c->dispatch_width == 16) {
14116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs += 2;
14216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
14316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R7-10: perspective centroid barycentric */
14416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R11-14: perspective sample barycentric */
14516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R15-18: linear pixel location barycentric */
14616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R19-22: linear centroid barycentric */
14716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R23-26: linear sample barycentric */
14816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
14916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R27: interpolated depth if uses source depth */
15016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
15116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_depth_reg = c->nr_payload_regs;
15216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
1535ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
15416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R28: interpolated depth if not 8-wide. */
15516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
15616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
15716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
15816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W.
15916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt       */
16016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (uses_depth) {
16116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_w_reg = c->nr_payload_regs;
16216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->nr_payload_regs++;
1635ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt	 if (c->dispatch_width == 16) {
16416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    /* R30: interpolated W if not 8-wide. */
16516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	    c->nr_payload_regs++;
16616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 }
16716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
16816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R31: MSAA position offsets. */
16916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R32-: bary for 32-pixel. */
17016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      /* R58-59: interp W for 32-pixel. */
17116f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt
17216f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      if (c->fp->program.Base.OutputsWritten &
17316f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	  BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
17416f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->source_depth_to_render_target = GL_TRUE;
17516f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt	 c->computes_depth = GL_TRUE;
17616f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      }
17716f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   } else {
17816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt      brw_wm_lookup_iz(intel, c);
17916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   }
18016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt}
1812f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
1822f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul/**
1832f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * All Mesa program -> GPU code generation goes through this function.
1842f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * Depending on the instructions used (i.e. flow control instructions)
1852f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul * we'll use one of two code generators.
1862f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul */
1879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic void do_wm_prog( struct brw_context *brw,
1889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt			struct brw_fragment_program *fp,
1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt			struct brw_wm_prog_key *key)
1909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
19159c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt   struct intel_context *intel = &brw->intel;
192d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   struct brw_wm_compile *c;
1939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   const GLuint *program;
1949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint program_size;
1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
196d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c = brw->wm.compile_data;
197d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   if (c == NULL) {
198774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      brw->wm.compile_data = rzalloc(NULL, struct brw_wm_compile);
19914dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      c = brw->wm.compile_data;
20044a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      if (c == NULL) {
20144a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison         /* Ouch - big out of memory problem.  Can't continue
20244a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * without triggering a segfault, no way to signal,
20344a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          * so just return.
20444a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison          */
20544a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison         return;
20644a4abfd4f8695809eaec07df8eeb191d6e017d7Robert Ellison      }
207774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      c->instruction = rzalloc_array(c, struct brw_wm_instruction, BRW_WM_MAX_INSN);
208774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      c->prog_instructions = rzalloc_array(c, struct prog_instruction, BRW_WM_MAX_INSN);
209774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      c->vreg = rzalloc_array(c, struct brw_wm_value, BRW_WM_MAX_VREG);
210774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke      c->refs = rzalloc_array(c, struct brw_wm_ref, BRW_WM_MAX_REF);
211d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   } else {
212c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *instruction = c->instruction;
213c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *prog_instructions = c->prog_instructions;
214c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *vreg = c->vreg;
215c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      void *refs = c->refs;
21614dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul      memset(c, 0, sizeof(*brw->wm.compile_data));
217c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->instruction = instruction;
218c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->prog_instructions = prog_instructions;
219c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->vreg = vreg;
220c5413839b3e99c7b162f1260142f3c175502b0ceEric Anholt      c->refs = refs;
221d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
222d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   memcpy(&c->key, key, sizeof(*key));
2239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
224d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->fp = fp;
225d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   c->env_param = brw->intel.ctx.FragmentProgram.Parameters;
2269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
227774fb90db3e83d5e7326b7a72e05ce805c306b24Kenneth Graunke   brw_init_compile(brw, &c->func, c);
22814dc4937336061c4c8d51c75d96fa216d9edcf2aBrian Paul
2299763d0a82a1ee605a8794f199d432824fb972b6aEric Anholt   if (!brw_wm_fs_emit(brw, c)) {
230662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      /* Fallback for fixed function and ARB_fp shaders. */
2315ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      c->dispatch_width = 16;
2325ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_payload_setup(brw, c);
2335ba517baa22b05d594b8839ac06fe45b81c1d09fEric Anholt      brw_wm_non_glsl_emit(brw, c);
234662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt      c->prog_data.dispatch_width = 16;
235d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   }
2362f78d4a2cd009d8d6a5f470d5738586b7f89f3d9Brian Paul
2374e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   /* Scratch space is used for register spilling */
2384e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   if (c->last_scratch) {
23959c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      uint32_t total_scratch;
24059c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt
2414e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt      /* Per-thread scratch space is power-of-two sized. */
2424e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt      for (c->prog_data.total_scratch = 1024;
2434e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt	   c->prog_data.total_scratch <= c->last_scratch;
2444e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt	   c->prog_data.total_scratch *= 2) {
2454e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt	 /* empty */
2464e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt      }
24759c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      total_scratch = c->prog_data.total_scratch * brw->wm_max_threads;
24859c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt
24959c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      if (brw->wm.scratch_bo && total_scratch > brw->wm.scratch_bo->size) {
25059c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt	 drm_intel_bo_unreference(brw->wm.scratch_bo);
25159c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt	 brw->wm.scratch_bo = NULL;
25259c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      }
25359c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      if (brw->wm.scratch_bo == NULL) {
25459c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt	 brw->wm.scratch_bo = drm_intel_bo_alloc(intel->bufmgr,
25559c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt						 "wm scratch",
25659c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt						 total_scratch,
25759c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt						 4096);
25859c6b775a6aacfe03c84dae62c2fd45d4af9d70bEric Anholt      }
2594e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   }
2604e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   else {
2614e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt      c->prog_data.total_scratch = 0;
2624e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt   }
2634e7252510976d8d3ff12437ea8842129f24d88f5Eric Anholt
264bb1540835056cdea5db6f55b19c0c87358f14cd1Eric Anholt   if (unlikely(INTEL_DEBUG & DEBUG_WM))
265fc3971d80051b34836716579fd060dbb122d036bEric Anholt      fprintf(stderr, "\n");
266fc3971d80051b34836716579fd060dbb122d036bEric Anholt
2679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* get the program
2689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
269d7b24fec245f90db4b8c66f4f7c167b8f20a9b9eEric Anholt   program = brw_get_program(&c->func, &program_size);
2709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
27134474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unreference(brw->wm.prog_bo);
2723032582d032a28381dd4c2f4093d82c79e73129eEric Anholt   brw->wm.prog_bo = brw_upload_cache(&brw->cache, BRW_WM_PROG,
2733032582d032a28381dd4c2f4093d82c79e73129eEric Anholt				      &c->key, sizeof(c->key),
2743032582d032a28381dd4c2f4093d82c79e73129eEric Anholt				      program, program_size,
2753032582d032a28381dd4c2f4093d82c79e73129eEric Anholt				      &c->prog_data, sizeof(c->prog_data),
2763032582d032a28381dd4c2f4093d82c79e73129eEric Anholt				      &brw->wm.prog_data);
2779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
2789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic void brw_wm_populate_key( struct brw_context *brw,
2829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				 struct brw_wm_prog_key *key )
2839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
284f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
2859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_FRAGMENT_PROGRAM */
28655d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   const struct brw_fragment_program *fp =
2879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      (struct brw_fragment_program *)brw->fragment_program;
2889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint lookup = 0;
2899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint line_aa;
2909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   GLuint i;
2919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   memset(key, 0, sizeof(*key));
2939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Build the index for table lookup
2959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_COLOR */
297a99447314ca1cfce60f2a22285398fb222b2a440Eric Anholt   key->alpha_test = ctx->Color.AlphaEnabled;
2989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   if (fp->program.UsesKill ||
299052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Color.AlphaEnabled)
3009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_KILL_ALPHATEST_BIT;
3019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3025606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
3039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
3049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_DEPTH */
306052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test)
3079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
3089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
309052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Depth.Test &&
310052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt       ctx->Depth.Mask) /* ?? */
3119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
3129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_STENCIL */
31491e61f435a71436c209934a0ece165b540aba3e0Brian Paul   if (ctx->Stencil._Enabled) {
3159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      lookup |= IZ_STENCIL_TEST_ENABLE_BIT;
3169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
317052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      if (ctx->Stencil.WriteMask[0] ||
318052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	  ctx->Stencil.WriteMask[ctx->Stencil._BackFace])
3199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;
3209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   line_aa = AA_NEVER;
3239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LINE, _NEW_POLYGON, BRW_NEW_REDUCED_PRIMITIVE */
325052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   if (ctx->Line.SmoothFlag) {
3269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (brw->intel.reduced_primitive == GL_LINES) {
3279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 line_aa = AA_ALWAYS;
3289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      else if (brw->intel.reduced_primitive == GL_TRIANGLES) {
330052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 if (ctx->Polygon.FrontMode == GL_LINE) {
3319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
333052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if (ctx->Polygon.BackMode == GL_LINE ||
334052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		(ctx->Polygon.CullFlag &&
335052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_BACK))
3369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
338052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 else if (ctx->Polygon.BackMode == GL_LINE) {
3399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    line_aa = AA_SOMETIMES;
3409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
341052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	    if ((ctx->Polygon.CullFlag &&
342052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt		 ctx->Polygon.CullFaceMode == GL_FRONT))
3439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       line_aa = AA_ALWAYS;
3449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
3459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
34816f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->iz_lookup = lookup;
34916f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->line_aa = line_aa;
35016f8c823898fd71a3545457eacd2dc31ddeb3592Eric Anholt   key->stats_wm = brw->intel.stats_wm;
3519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_WM_INPUT_DIMENSIONS */
3536b917d0b1787280f976c2f0d1ead0e5d7587a3e9Brian Paul   key->proj_attrib_mask = brw->wm.input_size_masks[4-1];
3549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_LIGHT */
356052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt   key->flat_shade = (ctx->Light.ShadeModel == GL_FLAT);
3579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
35818af7c384cf663533f210d95d074c244d4214f29Brian Paul   /* _NEW_HINT */
35918af7c384cf663533f210d95d074c244d4214f29Brian Paul   key->linear_color = (ctx->Hint.PerspectiveCorrection == GL_FASTEST);
36018af7c384cf663533f210d95d074c244d4214f29Brian Paul
361d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   /* _NEW_FRAG_CLAMP | _NEW_BUFFERS */
362d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt   key->clamp_fragment_color = ctx->Color._ClampFragmentColor;
363d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt
3649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* _NEW_TEXTURE */
3659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
366052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      const struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
3679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      if (unit->_ReallyEnabled) {
36989fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_object *t = unit->_Current;
37089fddf978c9d2ab5042f89110015234e979c2686Brian Paul         const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
3711f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, i);
37214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 int swizzles[SWIZZLE_NIL + 1] = {
37314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_X,
37414bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Y,
37514bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_Z,
37614bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_W,
37714bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ZERO,
37814bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_ONE,
37914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    SWIZZLE_NIL
38014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 };
381a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
382a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 /* GL_DEPTH_TEXTURE_MODE is normally handled through
383a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * brw_wm_surface_state, but it applies to shadow compares as
384a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * well and our shadow compares always return the result in
385a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  * all 4 channels.
386a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	  */
3871f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 if (sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
3881f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    if (sampler->DepthMode == GL_ALPHA) {
38914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[0] = SWIZZLE_ZERO;
39014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
39114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
3921f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_LUMINANCE) {
39314bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	       swizzles[3] = SWIZZLE_ONE;
3941f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	    } else if (sampler->DepthMode == GL_RED) {
3951842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       /* See table 3.23 of the GL 3.0 spec. */
3963fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[1] = SWIZZLE_ZERO;
3973fb18d67753fec8a21461266246ff6949fc0fe81Eric Anholt	       swizzles[2] = SWIZZLE_ZERO;
3981842b89f77bb7bd283b61e27cd69c643f2a60a22Chad Versace	       swizzles[3] = SWIZZLE_ONE;
399a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	    }
400a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt	 }
401a7fa00dfc5ab5782b2e497fbf4ca292dde69cdbdEric Anholt
40289fddf978c9d2ab5042f89110015234e979c2686Brian Paul	 if (img->InternalFormat == GL_YCBCR_MESA) {
40389fddf978c9d2ab5042f89110015234e979c2686Brian Paul	    key->yuvtex_mask |= 1 << i;
4041f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul	    if (img->TexFormat == MESA_FORMAT_YCBCR)
40589fddf978c9d2ab5042f89110015234e979c2686Brian Paul		key->yuvtex_swap_mask |= 1 << i;
4067676980d38cff417015bca8d23549d567d74228bZou Nan hai	 }
407c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul
40814bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	 key->tex_swizzles[i] =
40914bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt	    MAKE_SWIZZLE4(swizzles[GET_SWZ(t->_Swizzle, 0)],
41014bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 1)],
41114bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 2)],
41214bf92ba19373d54e9909bbdda5e430e0affea37Eric Anholt			  swizzles[GET_SWZ(t->_Swizzle, 3)]);
413c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      }
414c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul      else {
415c0d3b7679aa90e1a0dca2db152205efaec088b90Brian Paul         key->tex_swizzles[i] = SWIZZLE_NOOP;
4169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
4179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
4189c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt
419b17b110716936c32d20910cb9589038062b4f527Xiang, Haihao   /* Shadow */
420b17b110716936c32d20910cb9589038062b4f527Xiang, Haihao   key->shadowtex_mask = fp->program.Base.ShadowSamplers;
421b17b110716936c32d20910cb9589038062b4f527Xiang, Haihao
4229c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /* _NEW_BUFFERS */
4239c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   /*
4249c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * Include the draw buffer origin and height so that we can calculate
4259c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment position values relative to the bottom left of the drawable,
4269c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * from the incoming screen origin relative position we get as part of our
4279c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * payload.
4289c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
429861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * This is only needed for the WM_WPOSXY opcode when the fragment program
430861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * uses the gl_FragCoord input.
431861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    *
4329c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * We could avoid recompiling by including this as a constant referenced by
4339c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * our program, but if we were to do that it would also be nice to handle
4349c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * getting that constant updated at batchbuffer submit time (when we
4359c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * hold the lock and know where the buffer really is) rather than at emit
4369c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * time when we don't hold the lock and are just guessing.  We could also
4379c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * just avoid using this as key data if the program doesn't use
4389c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    * fragment.position.
4399c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    *
440861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * For DRI2 the origin_x/y will always be (0,0) but we still need the
441861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul    * drawable height in order to invert the Y axis.
4429c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt    */
443861fec163c1ae7e431956db0a08989d841e2b74eBrian Paul   if (fp->program.Base.InputsRead & FRAG_BIT_WPOS) {
444f62c2a0bb89041567467a6c01cf1eb27cec01e9eEric Anholt      key->drawable_height = ctx->DrawBuffer->Height;
4453b337f5cd94384d2d5918fb630aa8089e49b1d8dEric Anholt      key->render_to_fbo = ctx->DrawBuffer->Name != 0;
4469c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt   }
4479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4481b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   /* _NEW_BUFFERS */
4491b80622c4e94e8c59eb2f7ee9989d99712baff8fEric Anholt   key->nr_color_regions = ctx->DrawBuffer->_NumColorDrawBuffers;
4509ef33b86855c4d000271774030bd1b19b6d79687Brian Paul
4510f5113deed91611ecdda6596542530b1849bb161Eric Anholt   /* CACHE_NEW_VS_PROG */
4525606dfb572bf4b89b4882265924705bacc8c182bIan Romanick   key->vp_outputs_written = brw->vs.prog_data->outputs_written;
4530f5113deed91611ecdda6596542530b1849bb161Eric Anholt
45455d33e1fa7d231a0cdfce9b9650ae9e136e6c63cBrian Paul   /* The unique fragment program ID */
4559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   key->program_string_id = fp->id;
4569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
4579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
459f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void brw_prepare_wm_prog(struct brw_context *brw)
4609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
4619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_wm_prog_key key;
4629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_fragment_program *fp = (struct brw_fragment_program *)
4639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->fragment_program;
464662f1b48bd1a02907bb42ecda889a3aa52a5755dEric Anholt
4659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   brw_wm_populate_key(brw, &key);
4669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Make an early check for the key.
4689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
46934474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unreference(brw->wm.prog_bo);
47038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   brw->wm.prog_bo = brw_search_cache(&brw->cache, BRW_WM_PROG,
47138bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt				      &key, sizeof(key),
47238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt				      &brw->wm.prog_data);
47338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   if (brw->wm.prog_bo == NULL)
47438bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt      do_wm_prog(brw, fp, &key);
4759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
4769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_prog = {
4799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
4809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa  = (_NEW_COLOR |
4819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_DEPTH |
48218af7c384cf663533f210d95d074c244d4214f29Brian Paul                _NEW_HINT |
4839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_STENCIL |
4849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_POLYGON |
4859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LINE |
4869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_LIGHT |
487d22e2ebe35ef9d33ec5f7a67f903f36bcd9fbc91Eric Anholt		_NEW_FRAG_CLAMP |
4889c8f27ba1366da07e20e86a0d48341ea97f5cda4Eric Anholt		_NEW_BUFFERS |
4899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		_NEW_TEXTURE),
4909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw   = (BRW_NEW_FRAGMENT_PROGRAM |
4919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_WM_INPUT_DIMENSIONS |
4929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_REDUCED_PRIMITIVE),
4930f5113deed91611ecdda6596542530b1849bb161Eric Anholt      .cache = CACHE_NEW_VS_PROG,
4949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
495008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie   .prepare = brw_prepare_wm_prog
4969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
4979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
498