Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[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 brw_gs_compile c;
51 const GLuint *program;
52 GLuint program_size;
53
54 memset(&c, 0, sizeof(c));
55
56 c.key = *key;
57 c.need_ff_sync = BRW_IS_IGDNG(brw);
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 (BRW_IS_IGDNG(brw))
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 brw_gs_quads( &c );
89 break;
90 case GL_QUAD_STRIP:
91 brw_gs_quad_strip( &c );
92 break;
93 case GL_LINE_LOOP:
94 brw_gs_lines( &c );
95 break;
96 case GL_LINES:
97 if (key->hint_gs_always)
98 brw_gs_lines( &c );
99 else {
100 return;
101 }
102 break;
103 case GL_TRIANGLES:
104 if (key->hint_gs_always)
105 brw_gs_tris( &c );
106 else {
107 return;
108 }
109 break;
110 case GL_POINTS:
111 if (key->hint_gs_always)
112 brw_gs_points( &c );
113 else {
114 return;
115 }
116 break;
117 default:
118 return;
119 }
120
121 /* get the program
122 */
123 program = brw_get_program(&c.func, &program_size);
124
125 /* Upload
126 */
127 dri_bo_unreference(brw->gs.prog_bo);
128 brw->gs.prog_bo = 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 }
135
136 static const GLenum gs_prim[GL_POLYGON+1] = {
137 GL_POINTS,
138 GL_LINES,
139 GL_LINE_LOOP,
140 GL_LINES,
141 GL_TRIANGLES,
142 GL_TRIANGLES,
143 GL_TRIANGLES,
144 GL_QUADS,
145 GL_QUAD_STRIP,
146 GL_TRIANGLES
147 };
148
149 static void populate_key( struct brw_context *brw,
150 struct brw_gs_prog_key *key )
151 {
152 memset(key, 0, sizeof(*key));
153
154 /* CACHE_NEW_VS_PROG */
155 key->attrs = brw->vs.prog_data->outputs_written;
156
157 /* BRW_NEW_PRIMITIVE */
158 key->primitive = gs_prim[brw->primitive];
159
160 key->hint_gs_always = 0; /* debug code? */
161
162 key->need_gs_prog = (key->hint_gs_always ||
163 brw->primitive == GL_QUADS ||
164 brw->primitive == GL_QUAD_STRIP ||
165 brw->primitive == GL_LINE_LOOP);
166 }
167
168 /* Calculate interpolants for triangle and line rasterization.
169 */
170 static void prepare_gs_prog(struct brw_context *brw)
171 {
172 struct brw_gs_prog_key key;
173 /* Populate the key:
174 */
175 populate_key(brw, &key);
176
177 if (brw->gs.prog_active != key.need_gs_prog) {
178 brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
179 brw->gs.prog_active = key.need_gs_prog;
180 }
181
182 if (brw->gs.prog_active) {
183 dri_bo_unreference(brw->gs.prog_bo);
184 brw->gs.prog_bo = brw_search_cache(&brw->cache, BRW_GS_PROG,
185 &key, sizeof(key),
186 NULL, 0,
187 &brw->gs.prog_data);
188 if (brw->gs.prog_bo == NULL)
189 compile_gs_prog( brw, &key );
190 }
191 }
192
193
194 const struct brw_tracked_state brw_gs_prog = {
195 .dirty = {
196 .mesa = 0,
197 .brw = BRW_NEW_PRIMITIVE,
198 .cache = CACHE_NEW_VS_PROG
199 },
200 .prepare = prepare_gs_prog
201 };