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/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 "glsl/ralloc.h"
49 
compile_sf_prog(struct brw_context * brw,struct brw_sf_prog_key * key)50 static void compile_sf_prog( struct brw_context *brw,
51 			     struct brw_sf_prog_key *key )
52 {
53    struct intel_context *intel = &brw->intel;
54    struct brw_sf_compile c;
55    const GLuint *program;
56    void *mem_ctx;
57    GLuint program_size;
58    GLuint i;
59 
60    memset(&c, 0, sizeof(c));
61 
62    mem_ctx = ralloc_context(NULL);
63    /* Begin the compilation:
64     */
65    brw_init_compile(brw, &c.func, mem_ctx);
66 
67    c.key = *key;
68    c.vue_map = brw->vs.prog_data->vue_map;
69    if (c.key.do_point_coord) {
70       /*
71        * gl_PointCoord is a FS instead of VS builtin variable, thus it's
72        * not included in c.vue_map generated in VS stage. Here we add
73        * it manually to let SF shader generate the needed interpolation
74        * coefficient for FS shader.
75        */
76       c.vue_map.vert_result_to_slot[BRW_VERT_RESULT_PNTC] = c.vue_map.num_slots;
77       c.vue_map.slot_to_vert_result[c.vue_map.num_slots++] = BRW_VERT_RESULT_PNTC;
78    }
79    c.urb_entry_read_offset = brw_sf_compute_urb_entry_read_offset(intel);
80    c.nr_attr_regs = (c.vue_map.num_slots + 1)/2 - c.urb_entry_read_offset;
81    c.nr_setup_regs = c.nr_attr_regs;
82 
83    c.prog_data.urb_read_length = c.nr_attr_regs;
84    c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
85 
86    /* Which primitive?  Or all three?
87     */
88    switch (key->primitive) {
89    case SF_TRIANGLES:
90       c.nr_verts = 3;
91       brw_emit_tri_setup( &c, true );
92       break;
93    case SF_LINES:
94       c.nr_verts = 2;
95       brw_emit_line_setup( &c, true );
96       break;
97    case SF_POINTS:
98       c.nr_verts = 1;
99       if (key->do_point_sprite)
100 	  brw_emit_point_sprite_setup( &c, true );
101       else
102 	  brw_emit_point_setup( &c, true );
103       break;
104    case SF_UNFILLED_TRIS:
105       c.nr_verts = 3;
106       brw_emit_anyprim_setup( &c );
107       break;
108    default:
109       assert(0);
110       return;
111    }
112 
113    /* get the program
114     */
115    program = brw_get_program(&c.func, &program_size);
116 
117    if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
118       printf("sf:\n");
119       for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
120 	 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
121 		    intel->gen);
122       printf("\n");
123    }
124 
125    brw_upload_cache(&brw->cache, BRW_SF_PROG,
126 		    &c.key, sizeof(c.key),
127 		    program, program_size,
128 		    &c.prog_data, sizeof(c.prog_data),
129 		    &brw->sf.prog_offset, &brw->sf.prog_data);
130    ralloc_free(mem_ctx);
131 }
132 
133 /* Calculate interpolants for triangle and line rasterization.
134  */
135 static void
brw_upload_sf_prog(struct brw_context * brw)136 brw_upload_sf_prog(struct brw_context *brw)
137 {
138    struct gl_context *ctx = &brw->intel.ctx;
139    struct brw_sf_prog_key key;
140    /* _NEW_BUFFERS */
141    bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
142 
143    memset(&key, 0, sizeof(key));
144 
145    /* Populate the key, noting state dependencies:
146     */
147    /* CACHE_NEW_VS_PROG */
148    key.attrs = brw->vs.prog_data->outputs_written;
149 
150    /* BRW_NEW_REDUCED_PRIMITIVE */
151    switch (brw->intel.reduced_primitive) {
152    case GL_TRIANGLES:
153       /* NOTE: We just use the edgeflag attribute as an indicator that
154        * unfilled triangles are active.  We don't actually do the
155        * edgeflag testing here, it is already done in the clip
156        * program.
157        */
158       if (key.attrs & BITFIELD64_BIT(VERT_RESULT_EDGE))
159 	 key.primitive = SF_UNFILLED_TRIS;
160       else
161 	 key.primitive = SF_TRIANGLES;
162       break;
163    case GL_LINES:
164       key.primitive = SF_LINES;
165       break;
166    case GL_POINTS:
167       key.primitive = SF_POINTS;
168       break;
169    }
170 
171    /* _NEW_TRANSFORM */
172    key.userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
173 
174    /* _NEW_POINT */
175    key.do_point_sprite = ctx->Point.PointSprite;
176    if (key.do_point_sprite) {
177       int i;
178 
179       for (i = 0; i < 8; i++) {
180 	 if (ctx->Point.CoordReplace[i])
181 	    key.point_sprite_coord_replace |= (1 << i);
182       }
183    }
184    if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(FRAG_ATTRIB_PNTC))
185       key.do_point_coord = 1;
186    /*
187     * Window coordinates in a FBO are inverted, which means point
188     * sprite origin must be inverted, too.
189     */
190    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
191       key.sprite_origin_lower_left = true;
192 
193    /* _NEW_LIGHT */
194    key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT);
195    key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
196 
197    /* _NEW_POLYGON */
198    if (key.do_twoside_color) {
199       /* If we're rendering to a FBO, we have to invert the polygon
200        * face orientation, just as we invert the viewport in
201        * sf_unit_create_from_key().
202        */
203       key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) != render_to_fbo;
204    }
205 
206    if (!brw_search_cache(&brw->cache, BRW_SF_PROG,
207 			 &key, sizeof(key),
208 			 &brw->sf.prog_offset, &brw->sf.prog_data)) {
209       compile_sf_prog( brw, &key );
210    }
211 }
212 
213 
214 const struct brw_tracked_state brw_sf_prog = {
215    .dirty = {
216       .mesa  = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT |
217                 _NEW_TRANSFORM | _NEW_BUFFERS),
218       .brw   = (BRW_NEW_REDUCED_PRIMITIVE),
219       .cache = CACHE_NEW_VS_PROG
220    },
221    .emit = brw_upload_sf_prog
222 };
223 
224