brw_sf.c revision 639314d40e78b5b56c3fc840b2f416e7fc519a4d
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
33#include "main/glheader.h"
34#include "main/macros.h"
35#include "main/mtypes.h"
36#include "main/enums.h"
37#include "main/fbobject.h"
38
39#include "intel_batchbuffer.h"
40
41#include "brw_defines.h"
42#include "brw_context.h"
43#include "brw_eu.h"
44#include "brw_util.h"
45#include "brw_sf.h"
46#include "brw_state.h"
47
48#include "util/ralloc.h"
49
50static void compile_sf_prog( struct brw_context *brw,
51			     struct brw_sf_prog_key *key )
52{
53   struct brw_sf_compile c;
54   const GLuint *program;
55   void *mem_ctx;
56   GLuint program_size;
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.vue_map = brw->vue_map_geom_out;
67   if (c.key.do_point_coord) {
68      /*
69       * gl_PointCoord is a FS instead of VS builtin variable, thus it's
70       * not included in c.vue_map generated in VS stage. Here we add
71       * it manually to let SF shader generate the needed interpolation
72       * coefficient for FS shader.
73       */
74      c.vue_map.varying_to_slot[BRW_VARYING_SLOT_PNTC] = c.vue_map.num_slots;
75      c.vue_map.slot_to_varying[c.vue_map.num_slots++] = BRW_VARYING_SLOT_PNTC;
76   }
77   c.urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
78   c.nr_attr_regs = (c.vue_map.num_slots + 1)/2 - c.urb_entry_read_offset;
79   c.nr_setup_regs = c.nr_attr_regs;
80
81   c.prog_data.urb_read_length = c.nr_attr_regs;
82   c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
83   c.has_flat_shading = brw_any_flat_varyings(&key->interpolation_mode);
84
85   /* Which primitive?  Or all three?
86    */
87   switch (key->primitive) {
88   case SF_TRIANGLES:
89      c.nr_verts = 3;
90      brw_emit_tri_setup( &c, true );
91      break;
92   case SF_LINES:
93      c.nr_verts = 2;
94      brw_emit_line_setup( &c, true );
95      break;
96   case SF_POINTS:
97      c.nr_verts = 1;
98      if (key->do_point_sprite)
99	  brw_emit_point_sprite_setup( &c, true );
100      else
101	  brw_emit_point_setup( &c, true );
102      break;
103   case SF_UNFILLED_TRIS:
104      c.nr_verts = 3;
105      brw_emit_anyprim_setup( &c );
106      break;
107   default:
108      unreachable("not reached");
109   }
110
111   /* FINISHME: SF programs use calculated jumps (i.e., JMPI with a register
112    * source). Compacting would be difficult.
113    */
114   /* brw_compact_instructions(&c.func, 0, 0, NULL); */
115
116   /* get the program
117    */
118   program = brw_get_program(&c.func, &program_size);
119
120   if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
121      fprintf(stderr, "sf:\n");
122      brw_disassemble(brw->intelScreen->devinfo,
123                      c.func.store, 0, program_size, stderr);
124      fprintf(stderr, "\n");
125   }
126
127   brw_upload_cache(&brw->cache, BRW_CACHE_SF_PROG,
128		    &c.key, sizeof(c.key),
129		    program, program_size,
130		    &c.prog_data, sizeof(c.prog_data),
131		    &brw->sf.prog_offset, &brw->sf.prog_data);
132   ralloc_free(mem_ctx);
133}
134
135/* Calculate interpolants for triangle and line rasterization.
136 */
137static void
138brw_upload_sf_prog(struct brw_context *brw)
139{
140   struct gl_context *ctx = &brw->ctx;
141   struct brw_sf_prog_key key;
142   /* _NEW_BUFFERS */
143   bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
144
145   memset(&key, 0, sizeof(key));
146
147   /* Populate the key, noting state dependencies:
148    */
149   /* BRW_NEW_VUE_MAP_GEOM_OUT */
150   key.attrs = brw->vue_map_geom_out.slots_valid;
151
152   /* BRW_NEW_REDUCED_PRIMITIVE */
153   switch (brw->reduced_primitive) {
154   case GL_TRIANGLES:
155      /* NOTE: We just use the edgeflag attribute as an indicator that
156       * unfilled triangles are active.  We don't actually do the
157       * edgeflag testing here, it is already done in the clip
158       * program.
159       */
160      if (key.attrs & BITFIELD64_BIT(VARYING_SLOT_EDGE))
161	 key.primitive = SF_UNFILLED_TRIS;
162      else
163	 key.primitive = SF_TRIANGLES;
164      break;
165   case GL_LINES:
166      key.primitive = SF_LINES;
167      break;
168   case GL_POINTS:
169      key.primitive = SF_POINTS;
170      break;
171   }
172
173   /* _NEW_TRANSFORM */
174   key.userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
175
176   /* _NEW_POINT */
177   key.do_point_sprite = ctx->Point.PointSprite;
178   if (key.do_point_sprite) {
179      int i;
180
181      for (i = 0; i < 8; i++) {
182	 if (ctx->Point.CoordReplace[i])
183	    key.point_sprite_coord_replace |= (1 << i);
184      }
185   }
186   if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(VARYING_SLOT_PNTC))
187      key.do_point_coord = 1;
188   /*
189    * Window coordinates in a FBO are inverted, which means point
190    * sprite origin must be inverted, too.
191    */
192   if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
193      key.sprite_origin_lower_left = true;
194
195   /* BRW_NEW_INTERPOLATION_MAP */
196   key.interpolation_mode = brw->interpolation_mode;
197
198   /* _NEW_LIGHT | _NEW_PROGRAM */
199   key.do_twoside_color = ((ctx->Light.Enabled && ctx->Light.Model.TwoSide) ||
200                           ctx->VertexProgram._TwoSideEnabled);
201
202   /* _NEW_POLYGON */
203   if (key.do_twoside_color) {
204      /* If we're rendering to a FBO, we have to invert the polygon
205       * face orientation, just as we invert the viewport in
206       * sf_unit_create_from_key().
207       */
208      key.frontface_ccw = ctx->Polygon._FrontBit == render_to_fbo;
209   }
210
211   if (!brw_search_cache(&brw->cache, BRW_CACHE_SF_PROG,
212			 &key, sizeof(key),
213			 &brw->sf.prog_offset, &brw->sf.prog_data)) {
214      compile_sf_prog( brw, &key );
215   }
216}
217
218
219const struct brw_tracked_state brw_sf_prog = {
220   .dirty = {
221      .mesa  = _NEW_BUFFERS |
222               _NEW_HINT |
223               _NEW_LIGHT |
224               _NEW_POINT |
225               _NEW_POLYGON |
226               _NEW_PROGRAM |
227               _NEW_TRANSFORM,
228      .brw   = BRW_NEW_INTERPOLATION_MAP |
229               BRW_NEW_REDUCED_PRIMITIVE |
230               BRW_NEW_VUE_MAP_GEOM_OUT,
231   },
232   .emit = brw_upload_sf_prog
233};
234
235