i965g: hardwire linear interpolation for now
[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 /* CACHE_NEW_VS_PROG */
128 key.nr_attrs = brw->curr.vertex_shader->info.file_max[TGSI_FILE_OUTPUT] + 1;
129
130
131 /* XXX: this is probably where the mapping between vertex shader
132 * outputs and fragment shader inputs should be handled. Assume
133 * for now 1:1 correspondance.
134 *
135 * XXX: scan frag shader inputs to work out linear vs. perspective
136 * interpolation below.
137 *
138 * XXX: as long as we're hard-wiring, is eg. position required to
139 * be linear?
140 */
141 //key.linear_attrs = 0;
142 //key.persp_attrs = (1 << key.nr_attrs) - 1;
143
144 key.linear_attrs = (1 << key.nr_attrs) - 1;
145 key.persp_attrs = 0;
146
147 /* BRW_NEW_REDUCED_PRIMITIVE */
148 switch (brw->reduced_primitive) {
149 case PIPE_PRIM_TRIANGLES:
150 /* PIPE_NEW_RAST
151 */
152 if (brw->curr.rast->templ.fill_cw != PIPE_POLYGON_MODE_FILL ||
153 brw->curr.rast->templ.fill_ccw != PIPE_POLYGON_MODE_FILL)
154 key.primitive = SF_UNFILLED_TRIS;
155 else
156 key.primitive = SF_TRIANGLES;
157 break;
158 case PIPE_PRIM_LINES:
159 key.primitive = SF_LINES;
160 break;
161 case PIPE_PRIM_POINTS:
162 key.primitive = SF_POINTS;
163 break;
164 }
165
166 key.do_point_sprite = brw->curr.rast->templ.point_sprite;
167 key.sprite_origin_lower_left = 0; /* XXX: ctx->Point.SpriteOrigin - fix rast state */
168 key.do_flat_shading = brw->curr.rast->templ.flatshade;
169 key.do_twoside_color = brw->curr.rast->templ.light_twoside;
170
171 if (key.do_twoside_color) {
172 key.frontface_ccw = (brw->curr.rast->templ.front_winding ==
173 PIPE_WINDING_CCW);
174 }
175
176 if (brw_search_cache(&brw->cache, BRW_SF_PROG,
177 &key, sizeof(key),
178 NULL, 0,
179 &brw->sf.prog_data,
180 &brw->sf.prog_bo))
181 return PIPE_OK;
182
183 ret = compile_sf_prog( brw, &key, &brw->sf.prog_bo );
184 if (ret)
185 return ret;
186
187 return PIPE_OK;
188 }
189
190
191 const struct brw_tracked_state brw_sf_prog = {
192 .dirty = {
193 .mesa = (PIPE_NEW_RAST | PIPE_NEW_VERTEX_SHADER),
194 .brw = (BRW_NEW_REDUCED_PRIMITIVE),
195 .cache = 0
196 },
197 .prepare = upload_sf_prog
198 };
199