i965/vs: Implement vec4_visitor::generate_tex().
[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 #include "glsl/ralloc.h"
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 void *mem_ctx;
54 GLuint program_size;
55
56 memset(&c, 0, sizeof(c));
57
58 c.key = *key;
59 /* The geometry shader needs to access the entire VUE. */
60 struct brw_vue_map vue_map;
61 brw_compute_vue_map(&vue_map, intel, c.key.userclip_active, c.key.attrs);
62 c.nr_regs = (vue_map.num_slots + 1)/2;
63
64 mem_ctx = NULL;
65
66 /* Begin the compilation:
67 */
68 brw_init_compile(brw, &c.func, mem_ctx);
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 if (intel->gen >= 6) {
78 unsigned num_verts;
79 bool check_edge_flag;
80 /* On Sandybridge, we use the GS for implementing transform feedback
81 * (called "Stream Out" in the PRM).
82 */
83 switch (key->primitive) {
84 case _3DPRIM_POINTLIST:
85 num_verts = 1;
86 check_edge_flag = false;
87 break;
88 case _3DPRIM_LINELIST:
89 case _3DPRIM_LINESTRIP:
90 case _3DPRIM_LINELOOP:
91 num_verts = 2;
92 check_edge_flag = false;
93 break;
94 case _3DPRIM_TRILIST:
95 case _3DPRIM_TRIFAN:
96 case _3DPRIM_TRISTRIP:
97 case _3DPRIM_RECTLIST:
98 num_verts = 3;
99 check_edge_flag = false;
100 break;
101 case _3DPRIM_QUADLIST:
102 case _3DPRIM_QUADSTRIP:
103 case _3DPRIM_POLYGON:
104 num_verts = 3;
105 check_edge_flag = true;
106 break;
107 default:
108 assert(!"Unexpected primitive type in Gen6 SOL program.");
109 return;
110 }
111 gen6_sol_program(&c, key, num_verts, check_edge_flag);
112 } else {
113 /* On Gen4-5, we use the GS to decompose certain types of primitives.
114 * Note that primitives which don't require a GS program have already
115 * been weeded out by now.
116 */
117 switch (key->primitive) {
118 case _3DPRIM_QUADLIST:
119 brw_gs_quads( &c, key );
120 break;
121 case _3DPRIM_QUADSTRIP:
122 brw_gs_quad_strip( &c, key );
123 break;
124 case _3DPRIM_LINELOOP:
125 brw_gs_lines( &c );
126 break;
127 default:
128 ralloc_free(mem_ctx);
129 return;
130 }
131 }
132
133 /* get the program
134 */
135 program = brw_get_program(&c.func, &program_size);
136
137 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
138 int i;
139
140 printf("gs:\n");
141 for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
142 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
143 intel->gen);
144 printf("\n");
145 }
146
147 brw_upload_cache(&brw->cache, BRW_GS_PROG,
148 &c.key, sizeof(c.key),
149 program, program_size,
150 &c.prog_data, sizeof(c.prog_data),
151 &brw->gs.prog_offset, &brw->gs.prog_data);
152 ralloc_free(mem_ctx);
153 }
154
155 static void populate_key( struct brw_context *brw,
156 struct brw_gs_prog_key *key )
157 {
158 struct gl_context *ctx = &brw->intel.ctx;
159 struct intel_context *intel = &brw->intel;
160
161 memset(key, 0, sizeof(*key));
162
163 /* CACHE_NEW_VS_PROG */
164 key->attrs = brw->vs.prog_data->outputs_written;
165
166 /* BRW_NEW_PRIMITIVE */
167 key->primitive = brw->primitive;
168
169 /* _NEW_LIGHT */
170 key->pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
171 if (key->primitive == _3DPRIM_QUADLIST && ctx->Light.ShadeModel != GL_FLAT) {
172 /* Provide consistent primitive order with brw_set_prim's
173 * optimization of single quads to trifans.
174 */
175 key->pv_first = true;
176 }
177
178 /* _NEW_TRANSFORM */
179 key->userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
180
181 if (intel->gen >= 7) {
182 /* On Gen7 and later, we don't use GS (yet). */
183 key->need_gs_prog = false;
184 } else if (intel->gen == 6) {
185 /* On Gen6, GS is used for transform feedback. */
186 /* _NEW_TRANSFORM_FEEDBACK */
187 key->need_gs_prog = ctx->TransformFeedback.CurrentObject->Active;
188 } else {
189 /* Pre-gen6, GS is used to transform QUADLIST, QUADSTRIP, and LINELOOP
190 * into simpler primitives.
191 */
192 key->need_gs_prog = (brw->primitive == _3DPRIM_QUADLIST ||
193 brw->primitive == _3DPRIM_QUADSTRIP ||
194 brw->primitive == _3DPRIM_LINELOOP);
195 }
196 /* For testing, the environment variable INTEL_FORCE_GS can be used to
197 * force a GS program to be used, even if it's not necessary.
198 */
199 if (getenv("INTEL_FORCE_GS"))
200 key->need_gs_prog = true;
201 }
202
203 /* Calculate interpolants for triangle and line rasterization.
204 */
205 static void
206 brw_upload_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 if (brw->gs.prog_active) {
219 if (!brw_search_cache(&brw->cache, BRW_GS_PROG,
220 &key, sizeof(key),
221 &brw->gs.prog_offset, &brw->gs.prog_data)) {
222 compile_gs_prog( brw, &key );
223 }
224 }
225 }
226
227
228 const struct brw_tracked_state brw_gs_prog = {
229 .dirty = {
230 .mesa = (_NEW_LIGHT |
231 _NEW_TRANSFORM |
232 _NEW_TRANSFORM_FEEDBACK),
233 .brw = BRW_NEW_PRIMITIVE,
234 .cache = CACHE_NEW_VS_PROG
235 },
236 .emit = brw_upload_gs_prog
237 };