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