Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / i965 / brw_gs.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 "brw_batchbuffer.h"
33
34 #include "brw_defines.h"
35 #include "brw_context.h"
36 #include "brw_eu.h"
37 #include "brw_util.h"
38 #include "brw_state.h"
39 #include "brw_gs.h"
40
41
42
43 static enum pipe_error compile_gs_prog( struct brw_context *brw,
44 struct brw_gs_prog_key *key,
45 struct brw_winsys_buffer **bo_out )
46 {
47 struct brw_gs_compile c;
48 enum pipe_error ret;
49 const GLuint *program;
50 GLuint program_size;
51
52 memset(&c, 0, sizeof(c));
53
54 c.key = *key;
55 c.need_ff_sync = BRW_IS_IGDNG(brw);
56 /* Need to locate the two positions present in vertex + header.
57 * These are currently hardcoded:
58 */
59 c.nr_attrs = c.key.nr_attrs;
60
61 if (BRW_IS_IGDNG(brw))
62 c.nr_regs = (c.nr_attrs + 1) / 2 + 3; /* are vertices packed, or reg-aligned? */
63 else
64 c.nr_regs = (c.nr_attrs + 1) / 2 + 1; /* are vertices packed, or reg-aligned? */
65
66 c.nr_bytes = c.nr_regs * REG_SIZE;
67
68
69 /* Begin the compilation:
70 */
71 brw_init_compile(brw, &c.func);
72
73 c.func.single_program_flow = 1;
74
75 /* For some reason the thread is spawned with only 4 channels
76 * unmasked.
77 */
78 brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
79
80
81 /* Note that primitives which don't require a GS program have
82 * already been weeded out by this stage:
83 */
84 switch (key->primitive) {
85 case PIPE_PRIM_QUADS:
86 brw_gs_quads( &c );
87 break;
88 case PIPE_PRIM_QUAD_STRIP:
89 brw_gs_quad_strip( &c );
90 break;
91 case PIPE_PRIM_LINE_LOOP:
92 brw_gs_lines( &c );
93 break;
94 case PIPE_PRIM_LINES:
95 if (key->hint_gs_always)
96 brw_gs_lines( &c );
97 else {
98 return PIPE_OK;
99 }
100 break;
101 case PIPE_PRIM_TRIANGLES:
102 if (key->hint_gs_always)
103 brw_gs_tris( &c );
104 else {
105 return PIPE_OK;
106 }
107 break;
108 case PIPE_PRIM_POINTS:
109 if (key->hint_gs_always)
110 brw_gs_points( &c );
111 else {
112 return PIPE_OK;
113 }
114 break;
115 default:
116 assert(0);
117 return PIPE_ERROR_BAD_INPUT;
118 }
119
120 /* get the program
121 */
122 ret = brw_get_program(&c.func, &program, &program_size);
123 if (ret)
124 return ret;
125
126 /* Upload
127 */
128 ret = brw_upload_cache( &brw->cache, BRW_GS_PROG,
129 &c.key, sizeof(c.key),
130 NULL, 0,
131 program, program_size,
132 &c.prog_data,
133 &brw->gs.prog_data,
134 bo_out );
135 if (ret)
136 return ret;
137
138 return PIPE_OK;
139 }
140
141 static const unsigned gs_prim[PIPE_PRIM_MAX] = {
142 PIPE_PRIM_POINTS,
143 PIPE_PRIM_LINES,
144 PIPE_PRIM_LINE_LOOP,
145 PIPE_PRIM_LINES,
146 PIPE_PRIM_TRIANGLES,
147 PIPE_PRIM_TRIANGLES,
148 PIPE_PRIM_TRIANGLES,
149 PIPE_PRIM_QUADS,
150 PIPE_PRIM_QUAD_STRIP,
151 PIPE_PRIM_TRIANGLES
152 };
153
154 static void populate_key( struct brw_context *brw,
155 struct brw_gs_prog_key *key )
156 {
157 const struct brw_fs_signature *sig = &brw->curr.fragment_shader->signature;
158
159 memset(key, 0, sizeof(*key));
160
161 /* PIPE_NEW_FRAGMENT_SIGNATURE */
162 key->nr_attrs = sig->nr_inputs + 1;
163
164 /* BRW_NEW_PRIMITIVE */
165 key->primitive = gs_prim[brw->primitive];
166
167 key->hint_gs_always = 0; /* debug code? */
168
169 key->need_gs_prog = (key->hint_gs_always ||
170 brw->primitive == PIPE_PRIM_QUADS ||
171 brw->primitive == PIPE_PRIM_QUAD_STRIP ||
172 brw->primitive == PIPE_PRIM_LINE_LOOP);
173 }
174
175 /* Calculate interpolants for triangle and line rasterization.
176 */
177 static int prepare_gs_prog(struct brw_context *brw)
178 {
179 struct brw_gs_prog_key key;
180 enum pipe_error ret;
181
182 /* Populate the key:
183 */
184 populate_key(brw, &key);
185
186 if (brw->gs.prog_active != key.need_gs_prog) {
187 brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
188 brw->gs.prog_active = key.need_gs_prog;
189 }
190
191 if (!brw->gs.prog_active)
192 return PIPE_OK;
193
194 if (brw_search_cache(&brw->cache, BRW_GS_PROG,
195 &key, sizeof(key),
196 NULL, 0,
197 &brw->gs.prog_data,
198 &brw->gs.prog_bo))
199 return PIPE_OK;
200
201 ret = compile_gs_prog( brw, &key, &brw->gs.prog_bo );
202 if (ret)
203 return ret;
204
205 return PIPE_OK;
206 }
207
208
209 const struct brw_tracked_state brw_gs_prog = {
210 .dirty = {
211 .mesa = PIPE_NEW_FRAGMENT_SIGNATURE,
212 .brw = BRW_NEW_PRIMITIVE,
213 .cache = 0,
214 },
215 .prepare = prepare_gs_prog
216 };