i965: don't spawn GS thread for LINELOOP on Sandybridge
[mesa.git] / src / mesa / drivers / dri / 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 "main/glheader.h"
33 #include "main/macros.h"
34 #include "main/enums.h"
35
36 #include "intel_batchbuffer.h"
37
38 #include "brw_defines.h"
39 #include "brw_context.h"
40 #include "brw_eu.h"
41 #include "brw_util.h"
42 #include "brw_state.h"
43 #include "brw_gs.h"
44
45
46
47 static void compile_gs_prog( struct brw_context *brw,
48 struct brw_gs_prog_key *key )
49 {
50 struct intel_context *intel = &brw->intel;
51 struct brw_gs_compile c;
52 const GLuint *program;
53 GLuint program_size;
54
55 memset(&c, 0, sizeof(c));
56
57 c.key = *key;
58 /* Need to locate the two positions present in vertex + header.
59 * These are currently hardcoded:
60 */
61 c.nr_attrs = brw_count_bits(c.key.attrs);
62
63 if (intel->gen >= 5)
64 c.nr_regs = (c.nr_attrs + 1) / 2 + 3; /* are vertices packed, or reg-aligned? */
65 else
66 c.nr_regs = (c.nr_attrs + 1) / 2 + 1; /* are vertices packed, or reg-aligned? */
67
68 c.nr_bytes = c.nr_regs * REG_SIZE;
69
70
71 /* Begin the compilation:
72 */
73 brw_init_compile(brw, &c.func);
74
75 c.func.single_program_flow = 1;
76
77 /* For some reason the thread is spawned with only 4 channels
78 * unmasked.
79 */
80 brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
81
82
83 /* Note that primitives which don't require a GS program have
84 * already been weeded out by this stage:
85 */
86 switch (key->primitive) {
87 case GL_QUADS:
88 /* Gen6: VF has already converted into polygon. */
89 if (intel->gen == 6)
90 return;
91 brw_gs_quads( &c, key );
92 break;
93 case GL_QUAD_STRIP:
94 if (intel->gen == 6)
95 return;
96 brw_gs_quad_strip( &c, key );
97 break;
98 case GL_LINE_LOOP:
99 /* Gen6: LINELOOP is converted to LINESTRIP at the beginning of the 3D pipeline */
100 if (intel->gen == 6)
101 return;
102 brw_gs_lines( &c );
103 break;
104 case GL_LINES:
105 if (key->hint_gs_always)
106 brw_gs_lines( &c );
107 else {
108 return;
109 }
110 break;
111 case GL_TRIANGLES:
112 if (key->hint_gs_always)
113 brw_gs_tris( &c );
114 else {
115 return;
116 }
117 break;
118 case GL_POINTS:
119 if (key->hint_gs_always)
120 brw_gs_points( &c );
121 else {
122 return;
123 }
124 break;
125 default:
126 return;
127 }
128
129 /* get the program
130 */
131 program = brw_get_program(&c.func, &program_size);
132
133 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
134 int i;
135
136 printf("gs:\n");
137 for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
138 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
139 intel->gen);
140 printf("\n");
141 }
142
143 /* Upload
144 */
145 drm_intel_bo_unreference(brw->gs.prog_bo);
146 brw->gs.prog_bo = brw_upload_cache_with_auxdata(&brw->cache, BRW_GS_PROG,
147 &c.key, sizeof(c.key),
148 NULL, 0,
149 program, program_size,
150 &c.prog_data,
151 sizeof(c.prog_data),
152 &brw->gs.prog_data);
153 }
154
155 static const GLenum gs_prim[GL_POLYGON+1] = {
156 GL_POINTS,
157 GL_LINES,
158 GL_LINE_LOOP,
159 GL_LINES,
160 GL_TRIANGLES,
161 GL_TRIANGLES,
162 GL_TRIANGLES,
163 GL_QUADS,
164 GL_QUAD_STRIP,
165 GL_TRIANGLES
166 };
167
168 static void populate_key( struct brw_context *brw,
169 struct brw_gs_prog_key *key )
170 {
171 struct gl_context *ctx = &brw->intel.ctx;
172 struct intel_context *intel = &brw->intel;
173 int prim_gs_always;
174
175 memset(key, 0, sizeof(*key));
176
177 /* CACHE_NEW_VS_PROG */
178 key->attrs = brw->vs.prog_data->outputs_written;
179
180 /* BRW_NEW_PRIMITIVE */
181 key->primitive = gs_prim[brw->primitive];
182
183 key->hint_gs_always = 0; /* debug code? */
184
185 /* _NEW_LIGHT */
186 key->pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
187 if (key->primitive == GL_QUADS && ctx->Light.ShadeModel != GL_FLAT) {
188 /* Provide consistent primitive order with brw_set_prim's
189 * optimization of single quads to trifans.
190 */
191 key->pv_first = GL_TRUE;
192 }
193
194 if (intel->gen == 6)
195 prim_gs_always = 0;
196 else
197 prim_gs_always = brw->primitive == GL_QUADS ||
198 brw->primitive == GL_QUAD_STRIP ||
199 brw->primitive == GL_LINE_LOOP;
200
201 key->need_gs_prog = (key->hint_gs_always || prim_gs_always);
202 }
203
204 /* Calculate interpolants for triangle and line rasterization.
205 */
206 static void prepare_gs_prog(struct brw_context *brw)
207 {
208 struct brw_gs_prog_key key;
209 /* Populate the key:
210 */
211 populate_key(brw, &key);
212
213 if (brw->gs.prog_active != key.need_gs_prog) {
214 brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
215 brw->gs.prog_active = key.need_gs_prog;
216 }
217
218 drm_intel_bo_unreference(brw->gs.prog_bo);
219 brw->gs.prog_bo = NULL;
220
221 if (brw->gs.prog_active) {
222 brw->gs.prog_bo = brw_search_cache(&brw->cache, BRW_GS_PROG,
223 &key, sizeof(key),
224 NULL, 0,
225 &brw->gs.prog_data);
226 if (brw->gs.prog_bo == NULL)
227 compile_gs_prog( brw, &key );
228 }
229 }
230
231
232 const struct brw_tracked_state brw_gs_prog = {
233 .dirty = {
234 .mesa = _NEW_LIGHT,
235 .brw = BRW_NEW_PRIMITIVE,
236 .cache = CACHE_NEW_VS_PROG
237 },
238 .prepare = prepare_gs_prog
239 };