brw_clip_state.c revision 82eb7c235db9939d067c4d64e32df96caef939ab
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_state.h"
349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_defines.h"
35ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/macros.h"
369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
37114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholtstruct brw_clip_unit_key {
38114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int total_grf;
39114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int urb_entry_read_length;
40114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int curb_entry_read_length;
41114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int clip_mode;
429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
43114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int curbe_offset;
449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
45114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   unsigned int nr_urb_entries, urb_size;
46114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt};
479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
48114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholtstatic void
49114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholtclip_unit_populate_key(struct brw_context *brw, struct brw_clip_unit_key *key)
50114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt{
51114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   memset(key, 0, sizeof(*key));
529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* CACHE_NEW_CLIP_PROG */
54114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->total_grf = brw->clip.prog_data->total_grf;
55114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->urb_entry_read_length = brw->clip.prog_data->urb_read_length;
56114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->curb_entry_read_length = brw->clip.prog_data->curb_read_length;
57114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->clip_mode = brw->clip.prog_data->clip_mode;
589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_CURBE_OFFSETS */
60114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->curbe_offset = brw->curbe.clip_start;
619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* BRW_NEW_URB_FENCE */
63114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->nr_urb_entries = brw->urb.nr_clip_entries;
64114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   key->urb_size = brw->urb.vsize;
65114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt}
669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
67114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholtstatic dri_bo *
68114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholtclip_unit_create_from_key(struct brw_context *brw,
69114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt			  struct brw_clip_unit_key *key)
70114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt{
71114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   struct brw_clip_unit_state clip;
728abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt   dri_bo *bo;
73114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
74114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   memset(&clip, 0, sizeof(clip));
75114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
76114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread0.grf_reg_count = ALIGN(key->total_grf, 16) / 16 - 1;
77114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   /* reloc */
78114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread0.kernel_start_pointer = brw->clip.prog_bo->offset >> 6;
799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.thread1.floating_point_mode = BRW_FLOATING_POINT_NON_IEEE_754;
819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.thread1.single_program_flow = 1;
82114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
83114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread3.urb_entry_read_length = key->urb_entry_read_length;
84114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread3.const_urb_entry_read_length = key->curb_entry_read_length;
85114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread3.const_urb_entry_read_offset = key->curbe_offset * 2;
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.thread3.dispatch_grf_start_reg = 1;
879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.thread3.urb_entry_read_offset = 0;
88114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
89114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread4.nr_urb_entries = key->nr_urb_entries;
90114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.thread4.urb_entry_allocation_size = key->urb_size - 1;
9182eb7c235db9939d067c4d64e32df96caef939abEric Anholt   /* If we have enough clip URB entries to run two threads, do so.
9282eb7c235db9939d067c4d64e32df96caef939abEric Anholt    */
9382eb7c235db9939d067c4d64e32df96caef939abEric Anholt   if (key->nr_urb_entries >= 10) {
9482eb7c235db9939d067c4d64e32df96caef939abEric Anholt      /* Half of the URB entries go to each thread, and it has to be an
9582eb7c235db9939d067c4d64e32df96caef939abEric Anholt       * even number.
9682eb7c235db9939d067c4d64e32df96caef939abEric Anholt       */
9782eb7c235db9939d067c4d64e32df96caef939abEric Anholt      assert(key->nr_urb_entries % 2 == 0);
9882eb7c235db9939d067c4d64e32df96caef939abEric Anholt      clip.thread4.max_threads = 2 - 1;
9982eb7c235db9939d067c4d64e32df96caef939abEric Anholt   } else {
10082eb7c235db9939d067c4d64e32df96caef939abEric Anholt      assert(key->nr_urb_entries >= 5);
10182eb7c235db9939d067c4d64e32df96caef939abEric Anholt      clip.thread4.max_threads = 1 - 1;
10282eb7c235db9939d067c4d64e32df96caef939abEric Anholt   }
10382eb7c235db9939d067c4d64e32df96caef939abEric Anholt
10482eb7c235db9939d067c4d64e32df96caef939abEric Anholt   if (INTEL_DEBUG & DEBUG_SINGLE_THREAD)
10582eb7c235db9939d067c4d64e32df96caef939abEric Anholt      clip.thread4.max_threads = 0;
106114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
107114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   if (INTEL_DEBUG & DEBUG_STATS)
108114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt      clip.thread4.stats_enable = 1;
109114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
1109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.userclip_enable_flags = 0x7f;
1119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.userclip_must_clip = 1;
1129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.guard_band_enable = 0;
1139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.viewport_z_clip_enable = 1;
1149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.viewport_xy_clip_enable = 1;
1159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip5.vertex_position_space = BRW_CLIP_NDCSPACE;
116114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.clip5.api_mode = BRW_CLIP_API_OGL;
117114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip.clip5.clip_mode = key->clip_mode;
1188e444fb9e2685e3eac42beb848b08e91dc20c88aXiang, Haihao
11934b1776e8d965605d12807884c9c447214d57281Eric Anholt   if (BRW_IS_G4X(brw))
1208e444fb9e2685e3eac42beb848b08e91dc20c88aXiang, Haihao      clip.clip5.negative_w_clip_test = 1;
1218e444fb9e2685e3eac42beb848b08e91dc20c88aXiang, Haihao
1229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.clip6.clipper_viewport_state_ptr = 0;
1239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.viewport_xmin = -1;
1249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.viewport_xmax = 1;
1259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.viewport_ymin = -1;
1269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   clip.viewport_ymax = 1;
1279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1288abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt   bo = brw_upload_cache(&brw->cache, BRW_CLIP_UNIT,
1298abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt			 key, sizeof(*key),
1308abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt			 &brw->clip.prog_bo, 1,
1318abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt			 &clip, sizeof(clip),
1328abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt			 NULL, NULL);
1338abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt
134c9b1fef0c9c5018efd825c42782f19ad0618696aXiang, Haihao   /* Emit clip program relocation */
135c9b1fef0c9c5018efd825c42782f19ad0618696aXiang, Haihao   assert(brw->clip.prog_bo);
1363628185f566e178a12b493fb89abf52b4b281f99Eric Anholt   dri_bo_emit_reloc(bo,
1373628185f566e178a12b493fb89abf52b4b281f99Eric Anholt		     I915_GEM_DOMAIN_INSTRUCTION,
1383628185f566e178a12b493fb89abf52b4b281f99Eric Anholt		     0,
1393628185f566e178a12b493fb89abf52b4b281f99Eric Anholt		     clip.thread0.grf_reg_count << 1,
1403628185f566e178a12b493fb89abf52b4b281f99Eric Anholt		     offsetof(struct brw_clip_unit_state, thread0),
1413628185f566e178a12b493fb89abf52b4b281f99Eric Anholt		     brw->clip.prog_bo);
1428abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt
1438abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt   return bo;
144114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt}
145114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
146f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void upload_clip_unit( struct brw_context *brw )
147114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt{
148114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   struct brw_clip_unit_key key;
149114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt
150114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   clip_unit_populate_key(brw, &key);
15138bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt
15238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   dri_bo_unreference(brw->clip.state_bo);
153114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   brw->clip.state_bo = brw_search_cache(&brw->cache, BRW_CLIP_UNIT,
154114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt					 &key, sizeof(key),
155114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt					 &brw->clip.prog_bo, 1,
156114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt					 NULL);
157114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   if (brw->clip.state_bo == NULL) {
158114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt      brw->clip.state_bo = clip_unit_create_from_key(brw, &key);
159114b802e87bca5edbc2c59fc2c5caad3ec90bca6Eric Anholt   }
1609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
1619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_clip_unit = {
1639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
1649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa  = 0,
1659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw   = (BRW_NEW_CURBE_OFFSETS |
1669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt		BRW_NEW_URB_FENCE),
1679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .cache = CACHE_NEW_CLIP_PROG
1689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
169008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie   .prepare = upload_clip_unit,
1709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
171