i965g: add new state flag tracking fs signature changes
[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 #include "pipe/p_state.h"
33
34 #include "brw_batchbuffer.h"
35 #include "brw_defines.h"
36 #include "brw_context.h"
37 #include "brw_pipe_rast.h"
38 #include "brw_eu.h"
39 #include "brw_util.h"
40 #include "brw_sf.h"
41 #include "brw_state.h"
42
43 static enum pipe_error compile_sf_prog( struct brw_context *brw,
44 struct brw_sf_prog_key *key,
45 struct brw_winsys_buffer **bo_out )
46 {
47 enum pipe_error ret;
48 struct brw_sf_compile c;
49 const GLuint *program;
50 GLuint program_size;
51
52 memset(&c, 0, sizeof(c));
53
54 /* Begin the compilation:
55 */
56 brw_init_compile(brw, &c.func);
57
58 c.key = *key;
59 c.nr_attrs = c.key.nr_attrs;
60 c.nr_attr_regs = (c.nr_attrs+1)/2;
61 c.nr_setup_attrs = c.key.nr_attrs;
62 c.nr_setup_regs = (c.nr_setup_attrs+1)/2;
63
64 c.prog_data.urb_read_length = c.nr_attr_regs;
65 c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
66
67
68 /* Which primitive? Or all three?
69 */
70 switch (key->primitive) {
71 case SF_TRIANGLES:
72 c.nr_verts = 3;
73 brw_emit_tri_setup( &c, GL_TRUE );
74 break;
75 case SF_LINES:
76 c.nr_verts = 2;
77 brw_emit_line_setup( &c, GL_TRUE );
78 break;
79 case SF_POINTS:
80 c.nr_verts = 1;
81 if (key->do_point_sprite)
82 brw_emit_point_sprite_setup( &c, GL_TRUE );
83 else
84 brw_emit_point_setup( &c, GL_TRUE );
85 break;
86 case SF_UNFILLED_TRIS:
87 c.nr_verts = 3;
88 brw_emit_anyprim_setup( &c );
89 break;
90 default:
91 assert(0);
92 return PIPE_ERROR_BAD_INPUT;
93 }
94
95 /* get the program
96 */
97 ret = brw_get_program(&c.func, &program, &program_size);
98 if (ret)
99 return ret;
100
101 /* Upload
102 */
103 ret = brw_upload_cache( &brw->cache, BRW_SF_PROG,
104 &c.key, sizeof(c.key),
105 NULL, 0,
106 program, program_size,
107 &c.prog_data,
108 &brw->sf.prog_data,
109 bo_out);
110 if (ret)
111 return ret;
112
113 return PIPE_OK;
114 }
115
116 /* Calculate interpolants for triangle and line rasterization.
117 */
118 static enum pipe_error upload_sf_prog(struct brw_context *brw)
119 {
120 enum pipe_error ret;
121 struct brw_sf_prog_key key;
122
123 memset(&key, 0, sizeof(key));
124
125 /* Populate the key, noting state dependencies:
126 */
127
128 /* XXX: Add one to account for the position input.
129 */
130 /* PIPE_NEW_FRAGMENT_SIGNATURE */
131 key.nr_attrs = brw->curr.fragment_shader->signature.nr_inputs + 1;
132
133
134 /* XXX: this is probably where the mapping between vertex shader
135 * outputs and fragment shader inputs should be handled. Assume
136 * for now 1:1 correspondance.
137 *
138 * XXX: scan frag shader inputs to work out linear vs. perspective
139 * interpolation below.
140 *
141 * XXX: as long as we're hard-wiring, is eg. position required to
142 * be linear?
143 */
144 //key.linear_attrs = 0;
145 //key.persp_attrs = (1 << key.nr_attrs) - 1;
146
147 key.linear_attrs = (1 << key.nr_attrs) - 1;
148 key.persp_attrs = 0;
149
150 /* BRW_NEW_REDUCED_PRIMITIVE */
151 switch (brw->reduced_primitive) {
152 case PIPE_PRIM_TRIANGLES:
153 /* PIPE_NEW_RAST
154 */
155 if (brw->curr.rast->templ.fill_cw != PIPE_POLYGON_MODE_FILL ||
156 brw->curr.rast->templ.fill_ccw != PIPE_POLYGON_MODE_FILL)
157 key.primitive = SF_UNFILLED_TRIS;
158 else
159 key.primitive = SF_TRIANGLES;
160 break;
161 case PIPE_PRIM_LINES:
162 key.primitive = SF_LINES;
163 break;
164 case PIPE_PRIM_POINTS:
165 key.primitive = SF_POINTS;
166 break;
167 }
168
169 key.do_point_sprite = brw->curr.rast->templ.point_sprite;
170 key.sprite_origin_lower_left = 0; /* XXX: ctx->Point.SpriteOrigin - fix rast state */
171 key.do_flat_shading = brw->curr.rast->templ.flatshade;
172 key.do_twoside_color = brw->curr.rast->templ.light_twoside;
173
174 if (key.do_twoside_color) {
175 key.frontface_ccw = (brw->curr.rast->templ.front_winding ==
176 PIPE_WINDING_CCW);
177 }
178
179 if (brw_search_cache(&brw->cache, BRW_SF_PROG,
180 &key, sizeof(key),
181 NULL, 0,
182 &brw->sf.prog_data,
183 &brw->sf.prog_bo))
184 return PIPE_OK;
185
186 ret = compile_sf_prog( brw, &key, &brw->sf.prog_bo );
187 if (ret)
188 return ret;
189
190 return PIPE_OK;
191 }
192
193
194 const struct brw_tracked_state brw_sf_prog = {
195 .dirty = {
196 .mesa = (PIPE_NEW_RAST | PIPE_NEW_FRAGMENT_SIGNATURE),
197 .brw = (BRW_NEW_REDUCED_PRIMITIVE),
198 .cache = 0
199 },
200 .prepare = upload_sf_prog
201 };
202