1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics 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 <keithw@vmware.com>
30 */
31
32 #include "main/macros.h"
33 #include "main/enums.h"
34 #include "main/transformfeedback.h"
35
36 #include "intel_batchbuffer.h"
37
38 #include "brw_defines.h"
39 #include "brw_context.h"
40 #include "brw_util.h"
41 #include "brw_state.h"
42 #include "brw_ff_gs.h"
43
44 #include "util/ralloc.h"
45
46 void
brw_codegen_ff_gs_prog(struct brw_context * brw,struct brw_ff_gs_prog_key * key)47 brw_codegen_ff_gs_prog(struct brw_context *brw,
48 struct brw_ff_gs_prog_key *key)
49 {
50 const struct gen_device_info *devinfo = &brw->screen->devinfo;
51 struct brw_ff_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 c.vue_map = brw_vue_prog_data(brw->vs.base.prog_data)->vue_map;
60 c.nr_regs = (c.vue_map.num_slots + 1)/2;
61
62 mem_ctx = ralloc_context(NULL);
63
64 /* Begin the compilation:
65 */
66 brw_init_codegen(&brw->screen->devinfo, &c.func, mem_ctx);
67
68 c.func.single_program_flow = 1;
69
70 /* For some reason the thread is spawned with only 4 channels
71 * unmasked.
72 */
73 brw_set_default_mask_control(&c.func, BRW_MASK_DISABLE);
74
75 if (devinfo->gen >= 6) {
76 unsigned num_verts;
77 bool check_edge_flag;
78 /* On Sandybridge, we use the GS for implementing transform feedback
79 * (called "Stream Out" in the PRM).
80 */
81 switch (key->primitive) {
82 case _3DPRIM_POINTLIST:
83 num_verts = 1;
84 check_edge_flag = false;
85 break;
86 case _3DPRIM_LINELIST:
87 case _3DPRIM_LINESTRIP:
88 case _3DPRIM_LINELOOP:
89 num_verts = 2;
90 check_edge_flag = false;
91 break;
92 case _3DPRIM_TRILIST:
93 case _3DPRIM_TRIFAN:
94 case _3DPRIM_TRISTRIP:
95 case _3DPRIM_RECTLIST:
96 num_verts = 3;
97 check_edge_flag = false;
98 break;
99 case _3DPRIM_QUADLIST:
100 case _3DPRIM_QUADSTRIP:
101 case _3DPRIM_POLYGON:
102 num_verts = 3;
103 check_edge_flag = true;
104 break;
105 default:
106 unreachable("Unexpected primitive type in Gen6 SOL program.");
107 }
108 gen6_sol_program(&c, key, num_verts, check_edge_flag);
109 } else {
110 /* On Gen4-5, we use the GS to decompose certain types of primitives.
111 * Note that primitives which don't require a GS program have already
112 * been weeded out by now.
113 */
114 switch (key->primitive) {
115 case _3DPRIM_QUADLIST:
116 brw_ff_gs_quads( &c, key );
117 break;
118 case _3DPRIM_QUADSTRIP:
119 brw_ff_gs_quad_strip( &c, key );
120 break;
121 case _3DPRIM_LINELOOP:
122 brw_ff_gs_lines( &c );
123 break;
124 default:
125 ralloc_free(mem_ctx);
126 return;
127 }
128 }
129
130 brw_compact_instructions(&c.func, 0, NULL);
131
132 /* get the program
133 */
134 program = brw_get_program(&c.func, &program_size);
135
136 if (INTEL_DEBUG & DEBUG_GS) {
137 fprintf(stderr, "gs:\n");
138 brw_disassemble_with_labels(&brw->screen->devinfo, c.func.store,
139 0, program_size, stderr);
140 fprintf(stderr, "\n");
141 }
142
143 brw_upload_cache(&brw->cache, BRW_CACHE_FF_GS_PROG,
144 &c.key, sizeof(c.key),
145 program, program_size,
146 &c.prog_data, sizeof(c.prog_data),
147 &brw->ff_gs.prog_offset, &brw->ff_gs.prog_data);
148 ralloc_free(mem_ctx);
149 }
150
151 static bool
brw_ff_gs_state_dirty(const struct brw_context * brw)152 brw_ff_gs_state_dirty(const struct brw_context *brw)
153 {
154 return brw_state_dirty(brw,
155 _NEW_LIGHT,
156 BRW_NEW_PRIMITIVE |
157 BRW_NEW_TRANSFORM_FEEDBACK |
158 BRW_NEW_VS_PROG_DATA);
159 }
160
161 static void
brw_ff_gs_populate_key(struct brw_context * brw,struct brw_ff_gs_prog_key * key)162 brw_ff_gs_populate_key(struct brw_context *brw,
163 struct brw_ff_gs_prog_key *key)
164 {
165 const struct gen_device_info *devinfo = &brw->screen->devinfo;
166 static const unsigned swizzle_for_offset[4] = {
167 BRW_SWIZZLE4(0, 1, 2, 3),
168 BRW_SWIZZLE4(1, 2, 3, 3),
169 BRW_SWIZZLE4(2, 3, 3, 3),
170 BRW_SWIZZLE4(3, 3, 3, 3)
171 };
172
173 struct gl_context *ctx = &brw->ctx;
174
175 assert(devinfo->gen < 7);
176
177 memset(key, 0, sizeof(*key));
178
179 /* BRW_NEW_VS_PROG_DATA (part of VUE map) */
180 key->attrs = brw_vue_prog_data(brw->vs.base.prog_data)->vue_map.slots_valid;
181
182 /* BRW_NEW_PRIMITIVE */
183 key->primitive = brw->primitive;
184
185 /* _NEW_LIGHT */
186 key->pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
187 if (key->primitive == _3DPRIM_QUADLIST && 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 = true;
192 }
193
194 if (devinfo->gen == 6) {
195 /* On Gen6, GS is used for transform feedback. */
196 /* BRW_NEW_TRANSFORM_FEEDBACK */
197 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
198 const struct gl_program *prog =
199 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
200 const struct gl_transform_feedback_info *linked_xfb_info =
201 prog->sh.LinkedTransformFeedback;
202 int i;
203
204 /* Make sure that the VUE slots won't overflow the unsigned chars in
205 * key->transform_feedback_bindings[].
206 */
207 STATIC_ASSERT(BRW_VARYING_SLOT_COUNT <= 256);
208
209 /* Make sure that we don't need more binding table entries than we've
210 * set aside for use in transform feedback. (We shouldn't, since we
211 * set aside enough binding table entries to have one per component).
212 */
213 assert(linked_xfb_info->NumOutputs <= BRW_MAX_SOL_BINDINGS);
214
215 key->need_gs_prog = true;
216 key->num_transform_feedback_bindings = linked_xfb_info->NumOutputs;
217 for (i = 0; i < key->num_transform_feedback_bindings; ++i) {
218 key->transform_feedback_bindings[i] =
219 linked_xfb_info->Outputs[i].OutputRegister;
220 key->transform_feedback_swizzles[i] =
221 swizzle_for_offset[linked_xfb_info->Outputs[i].ComponentOffset];
222 }
223 }
224 } else {
225 /* Pre-gen6, GS is used to transform QUADLIST, QUADSTRIP, and LINELOOP
226 * into simpler primitives.
227 */
228 key->need_gs_prog = (brw->primitive == _3DPRIM_QUADLIST ||
229 brw->primitive == _3DPRIM_QUADSTRIP ||
230 brw->primitive == _3DPRIM_LINELOOP);
231 }
232 }
233
234 /* Calculate interpolants for triangle and line rasterization.
235 */
236 void
brw_upload_ff_gs_prog(struct brw_context * brw)237 brw_upload_ff_gs_prog(struct brw_context *brw)
238 {
239 struct brw_ff_gs_prog_key key;
240
241 if (!brw_ff_gs_state_dirty(brw))
242 return;
243
244 /* Populate the key:
245 */
246 brw_ff_gs_populate_key(brw, &key);
247
248 if (brw->ff_gs.prog_active != key.need_gs_prog) {
249 brw->ctx.NewDriverState |= BRW_NEW_FF_GS_PROG_DATA;
250 brw->ff_gs.prog_active = key.need_gs_prog;
251 }
252
253 if (brw->ff_gs.prog_active) {
254 if (!brw_search_cache(&brw->cache, BRW_CACHE_FF_GS_PROG, &key,
255 sizeof(key), &brw->ff_gs.prog_offset,
256 &brw->ff_gs.prog_data, true)) {
257 brw_codegen_ff_gs_prog(brw, &key);
258 }
259 }
260 }
261