Merge branch 'origin' into glsl-compiler-1
[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 "glheader.h"
33 #include "macros.h"
34 #include "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
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 c.nr_regs = (c.nr_attrs + 1) / 2 + 1; /* are vertices packed, or reg-aligned? */
63 c.nr_bytes = c.nr_regs * REG_SIZE;
64
65
66 /* Begin the compilation:
67 */
68 brw_init_compile(&c.func);
69
70 c.func.single_program_flow = 1;
71
72 /* For some reason the thread is spawned with only 4 channels
73 * unmasked.
74 */
75 brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
76
77
78 /* Note that primitives which don't require a GS program have
79 * already been weeded out by this stage:
80 */
81 switch (key->primitive) {
82 case GL_QUADS:
83 brw_gs_quads( &c );
84 break;
85 case GL_QUAD_STRIP:
86 brw_gs_quad_strip( &c );
87 break;
88 case GL_LINE_LOOP:
89 brw_gs_lines( &c );
90 break;
91 case GL_LINES:
92 if (key->hint_gs_always)
93 brw_gs_lines( &c );
94 else {
95 return;
96 }
97 break;
98 case GL_TRIANGLES:
99 if (key->hint_gs_always)
100 brw_gs_tris( &c );
101 else {
102 return;
103 }
104 break;
105 case GL_POINTS:
106 if (key->hint_gs_always)
107 brw_gs_points( &c );
108 else {
109 return;
110 }
111 break;
112 default:
113 return;
114 }
115
116 /* get the program
117 */
118 program = brw_get_program(&c.func, &program_size);
119
120 /* Upload
121 */
122 brw->gs.prog_gs_offset = brw_upload_cache( &brw->cache[BRW_GS_PROG],
123 &c.key,
124 sizeof(c.key),
125 program,
126 program_size,
127 &c.prog_data,
128 &brw->gs.prog_data );
129 }
130
131
132 static GLboolean search_cache( struct brw_context *brw,
133 struct brw_gs_prog_key *key )
134 {
135 return brw_search_cache(&brw->cache[BRW_GS_PROG],
136 key, sizeof(*key),
137 &brw->gs.prog_data,
138 &brw->gs.prog_gs_offset);
139 }
140
141
142 static const GLenum gs_prim[GL_POLYGON+1] = {
143 GL_POINTS,
144 GL_LINES,
145 GL_LINE_LOOP,
146 GL_LINES,
147 GL_TRIANGLES,
148 GL_TRIANGLES,
149 GL_TRIANGLES,
150 GL_QUADS,
151 GL_QUAD_STRIP,
152 GL_TRIANGLES
153 };
154
155 static void populate_key( struct brw_context *brw,
156 struct brw_gs_prog_key *key )
157 {
158 memset(key, 0, sizeof(*key));
159
160 /* CACHE_NEW_VS_PROG */
161 key->attrs = brw->vs.prog_data->outputs_written;
162
163 /* BRW_NEW_PRIMITIVE */
164 key->primitive = gs_prim[brw->primitive];
165
166 key->hint_gs_always = 0; /* debug code? */
167
168 key->need_gs_prog = (key->hint_gs_always ||
169 brw->primitive == GL_QUADS ||
170 brw->primitive == GL_QUAD_STRIP ||
171 brw->primitive == GL_LINE_LOOP);
172 }
173
174 /* Calculate interpolants for triangle and line rasterization.
175 */
176 static void upload_gs_prog( struct brw_context *brw )
177 {
178 struct brw_gs_prog_key key;
179
180 /* Populate the key:
181 */
182 populate_key(brw, &key);
183
184 if (brw->gs.prog_active != key.need_gs_prog) {
185 brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
186 brw->gs.prog_active = key.need_gs_prog;
187 }
188
189 if (brw->gs.prog_active) {
190 if (!search_cache(brw, &key))
191 compile_gs_prog( brw, &key );
192 }
193 }
194
195
196 const struct brw_tracked_state brw_gs_prog = {
197 .dirty = {
198 .mesa = 0,
199 .brw = BRW_NEW_PRIMITIVE,
200 .cache = CACHE_NEW_VS_PROG
201 },
202 .update = upload_gs_prog
203 };