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