1/*
2 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28  * Authors:
29  *   Keith Whitwell <keithw@vmware.com>
30  */
31
32#include "main/macros.h"
33#include "main/enums.h"
34
35#include "intel_batchbuffer.h"
36
37#include "brw_defines.h"
38#include "brw_context.h"
39#include "brw_util.h"
40#include "brw_state.h"
41#include "compiler/brw_eu.h"
42
43#include "util/ralloc.h"
44
45static void compile_clip_prog( struct brw_context *brw,
46			     struct brw_clip_prog_key *key )
47{
48   const unsigned *program;
49   void *mem_ctx;
50   unsigned program_size;
51
52   mem_ctx = ralloc_context(NULL);
53
54   struct brw_clip_prog_data prog_data;
55   program = brw_compile_clip(brw->screen->compiler, mem_ctx, key, &prog_data,
56                              &brw->vue_map_geom_out, &program_size);
57
58   brw_upload_cache(&brw->cache,
59		    BRW_CACHE_CLIP_PROG,
60		    key, sizeof(*key),
61		    program, program_size,
62		    &prog_data, sizeof(prog_data),
63		    &brw->clip.prog_offset, &brw->clip.prog_data);
64   ralloc_free(mem_ctx);
65}
66
67/* Calculate interpolants for triangle and line rasterization.
68 */
69void
70brw_upload_clip_prog(struct brw_context *brw)
71{
72   const struct gen_device_info *devinfo = &brw->screen->devinfo;
73   struct gl_context *ctx = &brw->ctx;
74   struct brw_clip_prog_key key;
75
76   if (!brw_state_dirty(brw,
77                        _NEW_BUFFERS |
78                        _NEW_LIGHT |
79                        _NEW_POLYGON |
80                        _NEW_TRANSFORM,
81                        BRW_NEW_BLORP |
82                        BRW_NEW_FS_PROG_DATA |
83                        BRW_NEW_REDUCED_PRIMITIVE |
84                        BRW_NEW_VUE_MAP_GEOM_OUT))
85      return;
86
87   memset(&key, 0, sizeof(key));
88
89   /* Populate the key:
90    */
91
92   /* BRW_NEW_FS_PROG_DATA */
93   const struct brw_wm_prog_data *wm_prog_data =
94      brw_wm_prog_data(brw->wm.base.prog_data);
95   if (wm_prog_data) {
96      key.contains_flat_varying = wm_prog_data->contains_flat_varying;
97      key.contains_noperspective_varying =
98         wm_prog_data->contains_noperspective_varying;
99
100      STATIC_ASSERT(sizeof(key.interp_mode) ==
101                    sizeof(wm_prog_data->interp_mode));
102      memcpy(key.interp_mode, wm_prog_data->interp_mode,
103             sizeof(key.interp_mode));
104   }
105
106   /* BRW_NEW_REDUCED_PRIMITIVE */
107   key.primitive = brw->reduced_primitive;
108   /* BRW_NEW_VUE_MAP_GEOM_OUT */
109   key.attrs = brw->vue_map_geom_out.slots_valid;
110
111   /* _NEW_LIGHT */
112   key.pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
113   /* _NEW_TRANSFORM (also part of VUE map)*/
114   if (ctx->Transform.ClipPlanesEnabled)
115      key.nr_userclip = _mesa_logbase2(ctx->Transform.ClipPlanesEnabled) + 1;
116
117   if (devinfo->gen == 5)
118       key.clip_mode = BRW_CLIP_MODE_KERNEL_CLIP;
119   else
120       key.clip_mode = BRW_CLIP_MODE_NORMAL;
121
122   /* _NEW_POLYGON */
123   if (key.primitive == GL_TRIANGLES) {
124      if (ctx->Polygon.CullFlag &&
125	  ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
126	 key.clip_mode = BRW_CLIP_MODE_REJECT_ALL;
127      else {
128	 GLuint fill_front = BRW_CLIP_FILL_MODE_CULL;
129	 GLuint fill_back = BRW_CLIP_FILL_MODE_CULL;
130	 GLuint offset_front = 0;
131	 GLuint offset_back = 0;
132
133	 if (!ctx->Polygon.CullFlag ||
134	     ctx->Polygon.CullFaceMode != GL_FRONT) {
135	    switch (ctx->Polygon.FrontMode) {
136	    case GL_FILL:
137	       fill_front = BRW_CLIP_FILL_MODE_FILL;
138	       offset_front = 0;
139	       break;
140	    case GL_LINE:
141	       fill_front = BRW_CLIP_FILL_MODE_LINE;
142	       offset_front = ctx->Polygon.OffsetLine;
143	       break;
144	    case GL_POINT:
145	       fill_front = BRW_CLIP_FILL_MODE_POINT;
146	       offset_front = ctx->Polygon.OffsetPoint;
147	       break;
148	    }
149	 }
150
151	 if (!ctx->Polygon.CullFlag ||
152	     ctx->Polygon.CullFaceMode != GL_BACK) {
153	    switch (ctx->Polygon.BackMode) {
154	    case GL_FILL:
155	       fill_back = BRW_CLIP_FILL_MODE_FILL;
156	       offset_back = 0;
157	       break;
158	    case GL_LINE:
159	       fill_back = BRW_CLIP_FILL_MODE_LINE;
160	       offset_back = ctx->Polygon.OffsetLine;
161	       break;
162	    case GL_POINT:
163	       fill_back = BRW_CLIP_FILL_MODE_POINT;
164	       offset_back = ctx->Polygon.OffsetPoint;
165	       break;
166	    }
167	 }
168
169	 if (ctx->Polygon.BackMode != GL_FILL ||
170	     ctx->Polygon.FrontMode != GL_FILL) {
171	    key.do_unfilled = 1;
172
173	    /* Most cases the fixed function units will handle.  Cases where
174	     * one or more polygon faces are unfilled will require help:
175	     */
176	    key.clip_mode = BRW_CLIP_MODE_CLIP_NON_REJECTED;
177
178	    if (offset_back || offset_front) {
179	       /* _NEW_POLYGON, _NEW_BUFFERS */
180	       key.offset_units = ctx->Polygon.OffsetUnits * ctx->DrawBuffer->_MRD * 2;
181	       key.offset_factor = ctx->Polygon.OffsetFactor * ctx->DrawBuffer->_MRD;
182	       key.offset_clamp = ctx->Polygon.OffsetClamp * ctx->DrawBuffer->_MRD;
183	    }
184
185	    if (!brw->polygon_front_bit) {
186	       key.fill_ccw = fill_front;
187	       key.fill_cw = fill_back;
188	       key.offset_ccw = offset_front;
189	       key.offset_cw = offset_back;
190	       if (ctx->Light.Model.TwoSide &&
191		   key.fill_cw != BRW_CLIP_FILL_MODE_CULL)
192		  key.copy_bfc_cw = 1;
193	    } else {
194	       key.fill_cw = fill_front;
195	       key.fill_ccw = fill_back;
196	       key.offset_cw = offset_front;
197	       key.offset_ccw = offset_back;
198	       if (ctx->Light.Model.TwoSide &&
199		   key.fill_ccw != BRW_CLIP_FILL_MODE_CULL)
200		  key.copy_bfc_ccw = 1;
201	    }
202	 }
203      }
204   }
205
206   if (!brw_search_cache(&brw->cache, BRW_CACHE_CLIP_PROG, &key, sizeof(key),
207                         &brw->clip.prog_offset, &brw->clip.prog_data, true)) {
208      compile_clip_prog( brw, &key );
209   }
210}
211