fd7fb9659f7d3902a99f2c8b1e83020431ed54b2
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sf.c
1
2 /*
3 Copyright (C) Intel Corp. 2006. All Rights Reserved.
4 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
5 develop this 3D driver.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice (including the
16 next paragraph) shall be included in all copies or substantial
17 portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 **********************************************************************/
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "glheader.h"
35 #include "macros.h"
36 #include "enums.h"
37
38 #include "intel_batchbuffer.h"
39
40 #include "brw_defines.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_util.h"
44 #include "brw_sf.h"
45 #include "brw_state.h"
46
47 #define DO_SETUP_BITS ((1<<(FRAG_ATTRIB_MAX)) - 1)
48
49 static void compile_sf_prog( struct brw_context *brw,
50 struct brw_sf_prog_key *key )
51 {
52 struct brw_sf_compile c;
53 const GLuint *program;
54 GLuint program_size;
55 GLuint i, idx;
56
57 memset(&c, 0, sizeof(c));
58
59 /* Begin the compilation:
60 */
61 brw_init_compile(&c.func);
62
63 c.key = *key;
64 c.nr_attrs = brw_count_bits(c.key.attrs);
65 c.nr_attr_regs = (c.nr_attrs+1)/2;
66 c.nr_setup_attrs = brw_count_bits(c.key.attrs & DO_SETUP_BITS);
67 c.nr_setup_regs = (c.nr_setup_attrs+1)/2;
68
69 c.prog_data.urb_read_length = c.nr_attr_regs;
70 c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
71
72 /* Construct map from attribute number to position in the vertex.
73 */
74 for (i = idx = 0; i < VERT_RESULT_MAX; i++)
75 if (c.key.attrs & (1<<i)) {
76 c.attr_to_idx[i] = idx;
77 c.idx_to_attr[idx] = i;
78 if (i >= VERT_RESULT_TEX0 && i <= VERT_RESULT_TEX7) {
79 c.point_attrs[i].CoordReplace =
80 brw->attribs.Point->CoordReplace[i - VERT_RESULT_TEX0];
81 } else
82 c.point_attrs[i].CoordReplace = GL_FALSE;
83 idx++;
84 }
85
86 /* Which primitive? Or all three?
87 */
88 switch (key->primitive) {
89 case SF_TRIANGLES:
90 c.nr_verts = 3;
91 brw_emit_tri_setup( &c );
92 break;
93 case SF_LINES:
94 c.nr_verts = 2;
95 brw_emit_line_setup( &c );
96 break;
97 case SF_POINTS:
98 c.nr_verts = 1;
99 if (key->do_point_sprite)
100 brw_emit_point_sprite_setup( &c );
101 else
102 brw_emit_point_setup( &c );
103 break;
104 case SF_UNFILLED_TRIS:
105 c.nr_verts = 3;
106 brw_emit_anyprim_setup( &c );
107 break;
108 default:
109 assert(0);
110 return;
111 }
112
113
114 /* get the program
115 */
116 program = brw_get_program(&c.func, &program_size);
117
118 /* Upload
119 */
120 brw->sf.prog_gs_offset = brw_upload_cache( &brw->cache[BRW_SF_PROG],
121 &c.key,
122 sizeof(c.key),
123 program,
124 program_size,
125 &c.prog_data,
126 &brw->sf.prog_data );
127 }
128
129 /* Calculate interpolants for triangle and line rasterization.
130 */
131 static void upload_sf_prog( struct brw_context *brw )
132 {
133 struct brw_sf_prog_key key;
134 struct brw_sf_prog_data *prog_data;
135 uint32_t offset;
136
137 memset(&key, 0, sizeof(key));
138
139 /* Populate the key, noting state dependencies:
140 */
141 /* CACHE_NEW_VS_PROG */
142 key.attrs = brw->vs.prog_data->outputs_written;
143
144 /* BRW_NEW_REDUCED_PRIMITIVE */
145 switch (brw->intel.reduced_primitive) {
146 case GL_TRIANGLES:
147 /* NOTE: We just use the edgeflag attribute as an indicator that
148 * unfilled triangles are active. We don't actually do the
149 * edgeflag testing here, it is already done in the clip
150 * program.
151 */
152 if (key.attrs & (1<<VERT_RESULT_EDGE))
153 key.primitive = SF_UNFILLED_TRIS;
154 else
155 key.primitive = SF_TRIANGLES;
156 break;
157 case GL_LINES:
158 key.primitive = SF_LINES;
159 break;
160 case GL_POINTS:
161 key.primitive = SF_POINTS;
162 break;
163 }
164
165 key.do_point_sprite = brw->attribs.Point->PointSprite;
166 key.SpriteOrigin = brw->attribs.Point->SpriteOrigin;
167 /* _NEW_LIGHT */
168 key.do_flat_shading = (brw->attribs.Light->ShadeModel == GL_FLAT);
169 key.do_twoside_color = (brw->attribs.Light->Enabled && brw->attribs.Light->Model.TwoSide);
170
171 /* _NEW_POLYGON */
172 if (key.do_twoside_color)
173 key.frontface_ccw = (brw->attribs.Polygon->FrontFace == GL_CCW);
174
175 if (brw_search_cache(&brw->cache[BRW_SF_PROG],
176 &key, sizeof(key),
177 &prog_data,
178 &offset)) {
179 if (offset != brw->sf.prog_gs_offset ||
180 !brw->sf.prog_data ||
181 memcmp(prog_data, &brw->sf.prog_data,
182 sizeof(*brw->sf.prog_data)) != 0)
183 {
184 brw->sf.prog_gs_offset = offset;
185 brw->sf.prog_data = prog_data;
186 brw->state.dirty.cache |= CACHE_NEW_SF_PROG;
187 }
188 } else {
189 compile_sf_prog( brw, &key );
190 brw->state.dirty.cache |= CACHE_NEW_SF_PROG;
191 }
192 }
193
194
195 const struct brw_tracked_state brw_sf_prog = {
196 .dirty = {
197 .mesa = (_NEW_LIGHT|_NEW_POLYGON|_NEW_POINT),
198 .brw = (BRW_NEW_REDUCED_PRIMITIVE),
199 .cache = CACHE_NEW_VS_PROG
200 },
201 .update = upload_sf_prog
202 };
203