i965g: more work on compiling
[mesa.git] / src / gallium / drivers / i965 / brw_sf.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) 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 <keith@tungstengraphics.com>
30 */
31
32
33 #include "brw_batchbuffer.h"
34
35 #include "brw_defines.h"
36 #include "brw_context.h"
37 #include "brw_eu.h"
38 #include "brw_util.h"
39 #include "brw_sf.h"
40 #include "brw_state.h"
41
42 static void compile_sf_prog( struct brw_context *brw,
43 struct brw_sf_prog_key *key )
44 {
45 struct brw_sf_compile c;
46 const GLuint *program;
47 GLuint program_size;
48 GLuint i, idx;
49
50 memset(&c, 0, sizeof(c));
51
52 /* Begin the compilation:
53 */
54 brw_init_compile(brw, &c.func);
55
56 c.key = *key;
57 c.nr_attrs = util_count_bits(c.key.attrs);
58 c.nr_attr_regs = (c.nr_attrs+1)/2;
59 c.nr_setup_attrs = c.key.nr_attrs;
60 c.nr_setup_regs = (c.nr_setup_attrs+1)/2;
61
62 c.prog_data.urb_read_length = c.nr_attr_regs;
63 c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
64
65 /* Construct map from attribute number to position in the vertex.
66 */
67 for (i = idx = 0; i < VERT_RESULT_MAX; i++)
68 if (c.key.attrs & (1<<i)) {
69 c.attr_to_idx[i] = idx;
70 c.idx_to_attr[idx] = i;
71 if (i >= VERT_RESULT_TEX0 && i <= VERT_RESULT_TEX7) {
72 c.point_attrs[i].CoordReplace =
73 ctx->Point.CoordReplace[i - VERT_RESULT_TEX0];
74 }
75 else {
76 c.point_attrs[i].CoordReplace = GL_FALSE;
77 }
78 idx++;
79 }
80
81 /* Which primitive? Or all three?
82 */
83 switch (key->primitive) {
84 case SF_TRIANGLES:
85 c.nr_verts = 3;
86 brw_emit_tri_setup( &c, GL_TRUE );
87 break;
88 case SF_LINES:
89 c.nr_verts = 2;
90 brw_emit_line_setup( &c, GL_TRUE );
91 break;
92 case SF_POINTS:
93 c.nr_verts = 1;
94 if (key->do_point_sprite)
95 brw_emit_point_sprite_setup( &c, GL_TRUE );
96 else
97 brw_emit_point_setup( &c, GL_TRUE );
98 break;
99 case SF_UNFILLED_TRIS:
100 c.nr_verts = 3;
101 brw_emit_anyprim_setup( &c );
102 break;
103 default:
104 assert(0);
105 return;
106 }
107
108 /* get the program
109 */
110 program = brw_get_program(&c.func, &program_size);
111
112 /* Upload
113 */
114 brw->sws->bo_unreference(brw->sf.prog_bo);
115 brw->sf.prog_bo = brw_upload_cache( &brw->cache, BRW_SF_PROG,
116 &c.key, sizeof(c.key),
117 NULL, 0,
118 program, program_size,
119 &c.prog_data,
120 &brw->sf.prog_data );
121 }
122
123 /* Calculate interpolants for triangle and line rasterization.
124 */
125 static void upload_sf_prog(struct brw_context *brw)
126 {
127 struct brw_sf_prog_key key;
128
129 memset(&key, 0, sizeof(key));
130
131 /* Populate the key, noting state dependencies:
132 */
133 /* CACHE_NEW_VS_PROG */
134 key.attrs = brw->vs.prog_data->outputs_written;
135
136 /* BRW_NEW_REDUCED_PRIMITIVE */
137 switch (brw->intel.reduced_primitive) {
138 case GL_TRIANGLES:
139 /* NOTE: We just use the edgeflag attribute as an indicator that
140 * unfilled triangles are active. We don't actually do the
141 * edgeflag testing here, it is already done in the clip
142 * program.
143 */
144 if (key.attrs & (1<<VERT_RESULT_EDGE))
145 key.primitive = SF_UNFILLED_TRIS;
146 else
147 key.primitive = SF_TRIANGLES;
148 break;
149 case GL_LINES:
150 key.primitive = SF_LINES;
151 break;
152 case GL_POINTS:
153 key.primitive = SF_POINTS;
154 break;
155 }
156
157 key.do_point_sprite = ctx->Point.PointSprite;
158 key.SpriteOrigin = ctx->Point.SpriteOrigin;
159 /* _NEW_LIGHT */
160 key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT);
161 key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
162
163 /* _NEW_HINT */
164 key.linear_color = 0;
165
166 /* _NEW_POLYGON */
167 if (key.do_twoside_color) {
168 /* If we're rendering to a FBO, we have to invert the polygon
169 * face orientation, just as we invert the viewport in
170 * sf_unit_create_from_key(). ctx->DrawBuffer->Name will be
171 * nonzero if we're rendering to such an FBO.
172 */
173 key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) ^ (ctx->DrawBuffer->Name != 0);
174 }
175
176 brw->sws->bo_unreference(brw->sf.prog_bo);
177 brw->sf.prog_bo = brw_search_cache(&brw->cache, BRW_SF_PROG,
178 &key, sizeof(key),
179 NULL, 0,
180 &brw->sf.prog_data);
181 if (brw->sf.prog_bo == NULL)
182 compile_sf_prog( brw, &key );
183 }
184
185
186 const struct brw_tracked_state brw_sf_prog = {
187 .dirty = {
188 .mesa = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT),
189 .brw = (BRW_NEW_REDUCED_PRIMITIVE),
190 .cache = CACHE_NEW_VS_PROG
191 },
192 .prepare = upload_sf_prog
193 };
194