i965: Move clip program compilation to the compiler
[mesa.git] / src / mesa / drivers / dri / i965 / brw_clip.c
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
45 static 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 */
69 void
70 brw_upload_clip_prog(struct brw_context *brw)
71 {
72 struct gl_context *ctx = &brw->ctx;
73 struct brw_clip_prog_key key;
74
75 if (!brw_state_dirty(brw,
76 _NEW_BUFFERS |
77 _NEW_LIGHT |
78 _NEW_POLYGON |
79 _NEW_TRANSFORM,
80 BRW_NEW_BLORP |
81 BRW_NEW_FS_PROG_DATA |
82 BRW_NEW_REDUCED_PRIMITIVE |
83 BRW_NEW_VUE_MAP_GEOM_OUT))
84 return;
85
86 memset(&key, 0, sizeof(key));
87
88 /* Populate the key:
89 */
90
91 /* BRW_NEW_FS_PROG_DATA */
92 const struct brw_wm_prog_data *wm_prog_data =
93 brw_wm_prog_data(brw->wm.base.prog_data);
94 if (wm_prog_data) {
95 key.contains_flat_varying = wm_prog_data->contains_flat_varying;
96 key.contains_noperspective_varying =
97 wm_prog_data->contains_noperspective_varying;
98
99 STATIC_ASSERT(sizeof(key.interp_mode) ==
100 sizeof(wm_prog_data->interp_mode));
101 memcpy(key.interp_mode, wm_prog_data->interp_mode,
102 sizeof(key.interp_mode));
103 }
104
105 /* BRW_NEW_REDUCED_PRIMITIVE */
106 key.primitive = brw->reduced_primitive;
107 /* BRW_NEW_VUE_MAP_GEOM_OUT */
108 key.attrs = brw->vue_map_geom_out.slots_valid;
109
110 /* _NEW_LIGHT */
111 key.pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
112 /* _NEW_TRANSFORM (also part of VUE map)*/
113 if (ctx->Transform.ClipPlanesEnabled)
114 key.nr_userclip = _mesa_logbase2(ctx->Transform.ClipPlanesEnabled) + 1;
115
116 if (brw->gen == 5)
117 key.clip_mode = BRW_CLIP_MODE_KERNEL_CLIP;
118 else
119 key.clip_mode = BRW_CLIP_MODE_NORMAL;
120
121 /* _NEW_POLYGON */
122 if (key.primitive == GL_TRIANGLES) {
123 if (ctx->Polygon.CullFlag &&
124 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
125 key.clip_mode = BRW_CLIP_MODE_REJECT_ALL;
126 else {
127 GLuint fill_front = BRW_CLIP_FILL_MODE_CULL;
128 GLuint fill_back = BRW_CLIP_FILL_MODE_CULL;
129 GLuint offset_front = 0;
130 GLuint offset_back = 0;
131
132 if (!ctx->Polygon.CullFlag ||
133 ctx->Polygon.CullFaceMode != GL_FRONT) {
134 switch (ctx->Polygon.FrontMode) {
135 case GL_FILL:
136 fill_front = BRW_CLIP_FILL_MODE_FILL;
137 offset_front = 0;
138 break;
139 case GL_LINE:
140 fill_front = BRW_CLIP_FILL_MODE_LINE;
141 offset_front = ctx->Polygon.OffsetLine;
142 break;
143 case GL_POINT:
144 fill_front = BRW_CLIP_FILL_MODE_POINT;
145 offset_front = ctx->Polygon.OffsetPoint;
146 break;
147 }
148 }
149
150 if (!ctx->Polygon.CullFlag ||
151 ctx->Polygon.CullFaceMode != GL_BACK) {
152 switch (ctx->Polygon.BackMode) {
153 case GL_FILL:
154 fill_back = BRW_CLIP_FILL_MODE_FILL;
155 offset_back = 0;
156 break;
157 case GL_LINE:
158 fill_back = BRW_CLIP_FILL_MODE_LINE;
159 offset_back = ctx->Polygon.OffsetLine;
160 break;
161 case GL_POINT:
162 fill_back = BRW_CLIP_FILL_MODE_POINT;
163 offset_back = ctx->Polygon.OffsetPoint;
164 break;
165 }
166 }
167
168 if (ctx->Polygon.BackMode != GL_FILL ||
169 ctx->Polygon.FrontMode != GL_FILL) {
170 key.do_unfilled = 1;
171
172 /* Most cases the fixed function units will handle. Cases where
173 * one or more polygon faces are unfilled will require help:
174 */
175 key.clip_mode = BRW_CLIP_MODE_CLIP_NON_REJECTED;
176
177 if (offset_back || offset_front) {
178 /* _NEW_POLYGON, _NEW_BUFFERS */
179 key.offset_units = ctx->Polygon.OffsetUnits * ctx->DrawBuffer->_MRD * 2;
180 key.offset_factor = ctx->Polygon.OffsetFactor * ctx->DrawBuffer->_MRD;
181 key.offset_clamp = ctx->Polygon.OffsetClamp * ctx->DrawBuffer->_MRD;
182 }
183
184 if (!ctx->Polygon._FrontBit) {
185 key.fill_ccw = fill_front;
186 key.fill_cw = fill_back;
187 key.offset_ccw = offset_front;
188 key.offset_cw = offset_back;
189 if (ctx->Light.Model.TwoSide &&
190 key.fill_cw != BRW_CLIP_FILL_MODE_CULL)
191 key.copy_bfc_cw = 1;
192 } else {
193 key.fill_cw = fill_front;
194 key.fill_ccw = fill_back;
195 key.offset_cw = offset_front;
196 key.offset_ccw = offset_back;
197 if (ctx->Light.Model.TwoSide &&
198 key.fill_ccw != BRW_CLIP_FILL_MODE_CULL)
199 key.copy_bfc_ccw = 1;
200 }
201 }
202 }
203 }
204
205 if (!brw_search_cache(&brw->cache, BRW_CACHE_CLIP_PROG,
206 &key, sizeof(key),
207 &brw->clip.prog_offset, &brw->clip.prog_data)) {
208 compile_clip_prog( brw, &key );
209 }
210 }