brw_gs.c revision 7d2ff0bf0b7422c34676c2f47dbe754f57edb51e
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
47static 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   brw_compute_vue_map(&c.vue_map, intel, c.key.userclip_active, c.key.attrs);
61   c.nr_regs = (c.vue_map.num_slots + 1)/2;
62
63   mem_ctx = NULL;
64
65   /* Begin the compilation:
66    */
67   brw_init_compile(brw, &c.func, mem_ctx);
68
69   c.func.single_program_flow = 1;
70
71   /* For some reason the thread is spawned with only 4 channels
72    * unmasked.
73    */
74   brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
75
76   if (intel->gen >= 6) {
77      unsigned num_verts;
78      bool check_edge_flag;
79      /* On Sandybridge, we use the GS for implementing transform feedback
80       * (called "Stream Out" in the PRM).
81       */
82      switch (key->primitive) {
83      case _3DPRIM_POINTLIST:
84         num_verts = 1;
85         check_edge_flag = false;
86	 break;
87      case _3DPRIM_LINELIST:
88      case _3DPRIM_LINESTRIP:
89      case _3DPRIM_LINELOOP:
90         num_verts = 2;
91         check_edge_flag = false;
92	 break;
93      case _3DPRIM_TRILIST:
94      case _3DPRIM_TRIFAN:
95      case _3DPRIM_TRISTRIP:
96      case _3DPRIM_RECTLIST:
97	 num_verts = 3;
98         check_edge_flag = false;
99         break;
100      case _3DPRIM_QUADLIST:
101      case _3DPRIM_QUADSTRIP:
102      case _3DPRIM_POLYGON:
103         num_verts = 3;
104         check_edge_flag = true;
105         break;
106      default:
107	 assert(!"Unexpected primitive type in Gen6 SOL program.");
108	 return;
109      }
110      gen6_sol_program(&c, key, num_verts, check_edge_flag);
111   } else {
112      /* On Gen4-5, we use the GS to decompose certain types of primitives.
113       * Note that primitives which don't require a GS program have already
114       * been weeded out by now.
115       */
116      switch (key->primitive) {
117      case _3DPRIM_QUADLIST:
118	 brw_gs_quads( &c, key );
119	 break;
120      case _3DPRIM_QUADSTRIP:
121	 brw_gs_quad_strip( &c, key );
122	 break;
123      case _3DPRIM_LINELOOP:
124	 brw_gs_lines( &c );
125	 break;
126      default:
127	 ralloc_free(mem_ctx);
128	 return;
129      }
130   }
131
132   /* get the program
133    */
134   program = brw_get_program(&c.func, &program_size);
135
136   if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
137      int i;
138
139      printf("gs:\n");
140      for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
141	 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
142		    intel->gen);
143      printf("\n");
144    }
145
146   brw_upload_cache(&brw->cache, BRW_GS_PROG,
147		    &c.key, sizeof(c.key),
148		    program, program_size,
149		    &c.prog_data, sizeof(c.prog_data),
150		    &brw->gs.prog_offset, &brw->gs.prog_data);
151   ralloc_free(mem_ctx);
152}
153
154static void populate_key( struct brw_context *brw,
155			  struct brw_gs_prog_key *key )
156{
157   struct gl_context *ctx = &brw->intel.ctx;
158   struct intel_context *intel = &brw->intel;
159
160   memset(key, 0, sizeof(*key));
161
162   /* CACHE_NEW_VS_PROG */
163   key->attrs = brw->vs.prog_data->outputs_written;
164
165   /* BRW_NEW_PRIMITIVE */
166   key->primitive = brw->primitive;
167
168   /* _NEW_LIGHT */
169   key->pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
170   if (key->primitive == _3DPRIM_QUADLIST && ctx->Light.ShadeModel != GL_FLAT) {
171      /* Provide consistent primitive order with brw_set_prim's
172       * optimization of single quads to trifans.
173       */
174      key->pv_first = true;
175   }
176
177   /* _NEW_TRANSFORM */
178   key->userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
179
180   if (intel->gen >= 7) {
181      /* On Gen7 and later, we don't use GS (yet). */
182      key->need_gs_prog = false;
183   } else if (intel->gen == 6) {
184      /* On Gen6, GS is used for transform feedback. */
185      /* _NEW_TRANSFORM_FEEDBACK */
186      if (ctx->TransformFeedback.CurrentObject->Active) {
187         const struct gl_shader_program *shaderprog =
188            ctx->Shader.CurrentVertexProgram;
189         const struct gl_transform_feedback_info *linked_xfb_info =
190            &shaderprog->LinkedTransformFeedback;
191         int i;
192
193         /* Make sure that the VUE slots won't overflow the unsigned chars in
194          * key->transform_feedback_bindings[].
195          */
196         STATIC_ASSERT(BRW_VERT_RESULT_MAX <= 256);
197
198         /* Make sure that we don't need more binding table entries than we've
199          * set aside for use in transform feedback.  (We shouldn't, since we
200          * set aside enough binding table entries to have one per component).
201          */
202         assert(linked_xfb_info->NumOutputs <= BRW_MAX_SOL_BINDINGS);
203
204         key->need_gs_prog = true;
205         key->num_transform_feedback_bindings = linked_xfb_info->NumOutputs;
206         for (i = 0; i < key->num_transform_feedback_bindings; ++i) {
207            key->transform_feedback_bindings[i] =
208               linked_xfb_info->Outputs[i].OutputRegister;
209         }
210      }
211      /* On Gen6, GS is also used for rasterizer discard. */
212      /* _NEW_TRANSFORM_FEEDBACK */
213      if (ctx->TransformFeedback.RasterDiscard) {
214         key->need_gs_prog = true;
215         key->rasterizer_discard = true;
216      }
217   } else {
218      /* Pre-gen6, GS is used to transform QUADLIST, QUADSTRIP, and LINELOOP
219       * into simpler primitives.
220       */
221      key->need_gs_prog = (brw->primitive == _3DPRIM_QUADLIST ||
222                           brw->primitive == _3DPRIM_QUADSTRIP ||
223                           brw->primitive == _3DPRIM_LINELOOP);
224   }
225   /* For testing, the environment variable INTEL_FORCE_GS can be used to
226    * force a GS program to be used, even if it's not necessary.
227    */
228   if (getenv("INTEL_FORCE_GS"))
229      key->need_gs_prog = true;
230}
231
232/* Calculate interpolants for triangle and line rasterization.
233 */
234static void
235brw_upload_gs_prog(struct brw_context *brw)
236{
237   struct brw_gs_prog_key key;
238   /* Populate the key:
239    */
240   populate_key(brw, &key);
241
242   if (brw->gs.prog_active != key.need_gs_prog) {
243      brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
244      brw->gs.prog_active = key.need_gs_prog;
245   }
246
247   if (brw->gs.prog_active) {
248      if (!brw_search_cache(&brw->cache, BRW_GS_PROG,
249			    &key, sizeof(key),
250			    &brw->gs.prog_offset, &brw->gs.prog_data)) {
251	 compile_gs_prog( brw, &key );
252      }
253   }
254}
255
256
257const struct brw_tracked_state brw_gs_prog = {
258   .dirty = {
259      .mesa  = (_NEW_LIGHT |
260                _NEW_TRANSFORM |
261                _NEW_TRANSFORM_FEEDBACK),
262      .brw   = BRW_NEW_PRIMITIVE,
263      .cache = CACHE_NEW_VS_PROG
264   },
265   .emit = brw_upload_gs_prog
266};
267