brw_sf.c revision 08f030baec3d13805c5800183550d8ad88ed8881
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
33#include "main/glheader.h"
34#include "main/macros.h"
35#include "main/enums.h"
36
37#include "intel_batchbuffer.h"
38
39#include "brw_defines.h"
40#include "brw_context.h"
41#include "brw_eu.h"
42#include "brw_util.h"
43#include "brw_sf.h"
44#include "brw_state.h"
45
46#include "glsl/ralloc.h"
47
48static void compile_sf_prog( struct brw_context *brw,
49			     struct brw_sf_prog_key *key )
50{
51   struct intel_context *intel = &brw->intel;
52   struct brw_sf_compile c;
53   const GLuint *program;
54   void *mem_ctx;
55   GLuint program_size;
56   GLuint i;
57
58   memset(&c, 0, sizeof(c));
59
60   mem_ctx = ralloc_context(NULL);
61   /* Begin the compilation:
62    */
63   brw_init_compile(brw, &c.func, mem_ctx);
64
65   c.key = *key;
66   c.nr_attrs = brw_count_bits(c.key.attrs);
67   c.nr_attr_regs = (c.nr_attrs+1)/2;
68   c.nr_setup_attrs = brw_count_bits(c.key.attrs);
69   c.nr_setup_regs = (c.nr_setup_attrs+1)/2;
70   brw_compute_vue_map(&c.vue_map, intel, c.key.nr_userclip,
71                       c.key.do_twoside_color, c.key.attrs);
72   c.urb_entry_read_offset = brw_sf_compute_urb_entry_read_offset(intel);
73
74   c.prog_data.urb_read_length = c.nr_attr_regs;
75   c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
76
77   /* Which primitive?  Or all three?
78    */
79   switch (key->primitive) {
80   case SF_TRIANGLES:
81      c.nr_verts = 3;
82      brw_emit_tri_setup( &c, GL_TRUE );
83      break;
84   case SF_LINES:
85      c.nr_verts = 2;
86      brw_emit_line_setup( &c, GL_TRUE );
87      break;
88   case SF_POINTS:
89      c.nr_verts = 1;
90      if (key->do_point_sprite)
91	  brw_emit_point_sprite_setup( &c, GL_TRUE );
92      else
93	  brw_emit_point_setup( &c, GL_TRUE );
94      break;
95   case SF_UNFILLED_TRIS:
96      c.nr_verts = 3;
97      brw_emit_anyprim_setup( &c );
98      break;
99   default:
100      assert(0);
101      return;
102   }
103
104   /* get the program
105    */
106   program = brw_get_program(&c.func, &program_size);
107
108   if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
109      printf("sf:\n");
110      for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
111	 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
112		    intel->gen);
113      printf("\n");
114   }
115
116   brw_upload_cache(&brw->cache, BRW_SF_PROG,
117		    &c.key, sizeof(c.key),
118		    program, program_size,
119		    &c.prog_data, sizeof(c.prog_data),
120		    &brw->sf.prog_offset, &brw->sf.prog_data);
121   ralloc_free(mem_ctx);
122}
123
124/* Calculate interpolants for triangle and line rasterization.
125 */
126static void upload_sf_prog(struct brw_context *brw)
127{
128   struct gl_context *ctx = &brw->intel.ctx;
129   struct brw_sf_prog_key key;
130
131   memset(&key, 0, sizeof(key));
132
133   /* Populate the key, noting state dependencies:
134    */
135   /* CACHE_NEW_VS_PROG */
136   key.attrs = brw->vs.prog_data->outputs_written;
137
138   /* BRW_NEW_REDUCED_PRIMITIVE */
139   switch (brw->intel.reduced_primitive) {
140   case GL_TRIANGLES:
141      /* NOTE: We just use the edgeflag attribute as an indicator that
142       * unfilled triangles are active.  We don't actually do the
143       * edgeflag testing here, it is already done in the clip
144       * program.
145       */
146      if (key.attrs & BITFIELD64_BIT(VERT_RESULT_EDGE))
147	 key.primitive = SF_UNFILLED_TRIS;
148      else
149	 key.primitive = SF_TRIANGLES;
150      break;
151   case GL_LINES:
152      key.primitive = SF_LINES;
153      break;
154   case GL_POINTS:
155      key.primitive = SF_POINTS;
156      break;
157   }
158
159   /* _NEW_TRANSFORM */
160   key.nr_userclip = brw_count_bits(ctx->Transform.ClipPlanesEnabled);
161
162   /* _NEW_POINT */
163   key.do_point_sprite = ctx->Point.PointSprite;
164   if (key.do_point_sprite) {
165      int i;
166
167      for (i = 0; i < 8; i++) {
168	 if (ctx->Point.CoordReplace[i])
169	    key.point_sprite_coord_replace |= (1 << i);
170      }
171   }
172   key.sprite_origin_lower_left = (ctx->Point.SpriteOrigin == GL_LOWER_LEFT);
173   /* _NEW_LIGHT */
174   key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT);
175   key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
176
177   /* _NEW_POLYGON */
178   if (key.do_twoside_color) {
179      /* If we're rendering to a FBO, we have to invert the polygon
180       * face orientation, just as we invert the viewport in
181       * sf_unit_create_from_key().  ctx->DrawBuffer->Name will be
182       * nonzero if we're rendering to such an FBO.
183       */
184      key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) ^ (ctx->DrawBuffer->Name != 0);
185   }
186
187   if (!brw_search_cache(&brw->cache, BRW_SF_PROG,
188			 &key, sizeof(key),
189			 &brw->sf.prog_offset, &brw->sf.prog_data)) {
190      compile_sf_prog( brw, &key );
191   }
192}
193
194
195const struct brw_tracked_state brw_sf_prog = {
196   .dirty = {
197      .mesa  = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT | _NEW_TRANSFORM),
198      .brw   = (BRW_NEW_REDUCED_PRIMITIVE),
199      .cache = CACHE_NEW_VS_PROG
200   },
201   .prepare = upload_sf_prog
202};
203
204