brw_gs.c revision fa4b23581b4ee8a07400364dccbd61b749c2d1d1
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   /* Gen6: VF has already converted into polygon, and LINELOOP is
57    * converted to LINESTRIP at the beginning of the 3D pipeline.
58    */
59   if (intel->gen >= 6)
60      return;
61
62   memset(&c, 0, sizeof(c));
63
64   c.key = *key;
65   /* Need to locate the two positions present in vertex + header.
66    * These are currently hardcoded:
67    */
68   c.nr_attrs = brw_count_bits(c.key.attrs);
69
70   if (intel->gen >= 5)
71       c.nr_regs = (c.nr_attrs + 1) / 2 + 3;  /* are vertices packed, or reg-aligned? */
72   else
73       c.nr_regs = (c.nr_attrs + 1) / 2 + 1;  /* are vertices packed, or reg-aligned? */
74
75   c.nr_bytes = c.nr_regs * REG_SIZE;
76
77   mem_ctx = NULL;
78
79   /* Begin the compilation:
80    */
81   brw_init_compile(brw, &c.func, mem_ctx);
82
83   c.func.single_program_flow = 1;
84
85   /* For some reason the thread is spawned with only 4 channels
86    * unmasked.
87    */
88   brw_set_mask_control(&c.func, BRW_MASK_DISABLE);
89
90
91   /* Note that primitives which don't require a GS program have
92    * already been weeded out by this stage:
93    */
94
95   switch (key->primitive) {
96   case GL_QUADS:
97      brw_gs_quads( &c, key );
98      break;
99   case GL_QUAD_STRIP:
100      brw_gs_quad_strip( &c, key );
101      break;
102   case GL_LINE_LOOP:
103      brw_gs_lines( &c );
104      break;
105   default:
106      ralloc_free(mem_ctx);
107      return;
108   }
109
110   /* get the program
111    */
112   program = brw_get_program(&c.func, &program_size);
113
114   if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
115      int i;
116
117      printf("gs:\n");
118      for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
119	 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
120		    intel->gen);
121      printf("\n");
122    }
123
124   /* Upload
125    */
126   drm_intel_bo_unreference(brw->gs.prog_bo);
127   brw->gs.prog_bo = brw_upload_cache(&brw->cache, BRW_GS_PROG,
128				      &c.key, sizeof(c.key),
129				      program, program_size,
130				      &c.prog_data, sizeof(c.prog_data),
131				      &brw->gs.prog_data);
132   ralloc_free(mem_ctx);
133}
134
135static const GLenum gs_prim[GL_POLYGON+1] = {
136   GL_POINTS,
137   GL_LINES,
138   GL_LINE_LOOP,
139   GL_LINES,
140   GL_TRIANGLES,
141   GL_TRIANGLES,
142   GL_TRIANGLES,
143   GL_QUADS,
144   GL_QUAD_STRIP,
145   GL_TRIANGLES
146};
147
148static void populate_key( struct brw_context *brw,
149			  struct brw_gs_prog_key *key )
150{
151   struct gl_context *ctx = &brw->intel.ctx;
152   struct intel_context *intel = &brw->intel;
153
154   memset(key, 0, sizeof(*key));
155
156   /* CACHE_NEW_VS_PROG */
157   key->attrs = brw->vs.prog_data->outputs_written;
158
159   /* BRW_NEW_PRIMITIVE */
160   key->primitive = gs_prim[brw->primitive];
161
162   /* _NEW_LIGHT */
163   key->pv_first = (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION);
164   if (key->primitive == GL_QUADS && ctx->Light.ShadeModel != GL_FLAT) {
165      /* Provide consistent primitive order with brw_set_prim's
166       * optimization of single quads to trifans.
167       */
168      key->pv_first = GL_TRUE;
169   }
170
171   key->need_gs_prog = (intel->gen >= 6)
172      ? 0
173      : (brw->primitive == GL_QUADS ||
174	 brw->primitive == GL_QUAD_STRIP ||
175	 brw->primitive == GL_LINE_LOOP);
176}
177
178/* Calculate interpolants for triangle and line rasterization.
179 */
180static void prepare_gs_prog(struct brw_context *brw)
181{
182   struct brw_gs_prog_key key;
183   /* Populate the key:
184    */
185   populate_key(brw, &key);
186
187   if (brw->gs.prog_active != key.need_gs_prog) {
188      brw->state.dirty.cache |= CACHE_NEW_GS_PROG;
189      brw->gs.prog_active = key.need_gs_prog;
190   }
191
192   drm_intel_bo_unreference(brw->gs.prog_bo);
193   brw->gs.prog_bo = NULL;
194
195   if (brw->gs.prog_active) {
196      brw->gs.prog_bo = brw_search_cache(&brw->cache, BRW_GS_PROG,
197					 &key, sizeof(key),
198					 &brw->gs.prog_data);
199      if (brw->gs.prog_bo == NULL)
200	 compile_gs_prog( brw, &key );
201   }
202}
203
204
205const struct brw_tracked_state brw_gs_prog = {
206   .dirty = {
207      .mesa  = _NEW_LIGHT,
208      .brw   = BRW_NEW_PRIMITIVE,
209      .cache = CACHE_NEW_VS_PROG
210   },
211   .prepare = prepare_gs_prog
212};
213