c2227777cfb645b4517ac07e8fb06d4d00fea0cd
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sf.c
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
48 static 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, idx;
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
71 c.prog_data.urb_read_length = c.nr_attr_regs;
72 c.prog_data.urb_entry_size = c.nr_setup_regs * 2;
73
74 /* Construct map from attribute number to position in the vertex.
75 */
76 for (i = idx = 0; i < VERT_RESULT_MAX; i++) {
77 if (c.key.attrs & BITFIELD64_BIT(i)) {
78 c.attr_to_idx[i] = idx;
79 c.idx_to_attr[idx] = i;
80 idx++;
81 }
82 }
83
84 /* Which primitive? Or all three?
85 */
86 switch (key->primitive) {
87 case SF_TRIANGLES:
88 c.nr_verts = 3;
89 brw_emit_tri_setup( &c, GL_TRUE );
90 break;
91 case SF_LINES:
92 c.nr_verts = 2;
93 brw_emit_line_setup( &c, GL_TRUE );
94 break;
95 case SF_POINTS:
96 c.nr_verts = 1;
97 if (key->do_point_sprite)
98 brw_emit_point_sprite_setup( &c, GL_TRUE );
99 else
100 brw_emit_point_setup( &c, GL_TRUE );
101 break;
102 case SF_UNFILLED_TRIS:
103 c.nr_verts = 3;
104 brw_emit_anyprim_setup( &c );
105 break;
106 default:
107 assert(0);
108 return;
109 }
110
111 /* get the program
112 */
113 program = brw_get_program(&c.func, &program_size);
114
115 if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
116 printf("sf:\n");
117 for (i = 0; i < program_size / sizeof(struct brw_instruction); i++)
118 brw_disasm(stdout, &((struct brw_instruction *)program)[i],
119 intel->gen);
120 printf("\n");
121 }
122
123 /* Upload
124 */
125 drm_intel_bo_unreference(brw->sf.prog_bo);
126 brw->sf.prog_bo = brw_upload_cache(&brw->cache, BRW_SF_PROG,
127 &c.key, sizeof(c.key),
128 program, program_size,
129 &c.prog_data, sizeof(c.prog_data),
130 &brw->sf.prog_data);
131 ralloc_free(mem_ctx);
132 }
133
134 /* Calculate interpolants for triangle and line rasterization.
135 */
136 static void upload_sf_prog(struct brw_context *brw)
137 {
138 struct gl_context *ctx = &brw->intel.ctx;
139 struct brw_sf_prog_key key;
140
141 memset(&key, 0, sizeof(key));
142
143 /* Populate the key, noting state dependencies:
144 */
145 /* CACHE_NEW_VS_PROG */
146 key.attrs = brw->vs.prog_data->outputs_written;
147
148 /* BRW_NEW_REDUCED_PRIMITIVE */
149 switch (brw->intel.reduced_primitive) {
150 case GL_TRIANGLES:
151 /* NOTE: We just use the edgeflag attribute as an indicator that
152 * unfilled triangles are active. We don't actually do the
153 * edgeflag testing here, it is already done in the clip
154 * program.
155 */
156 if (key.attrs & BITFIELD64_BIT(VERT_RESULT_EDGE))
157 key.primitive = SF_UNFILLED_TRIS;
158 else
159 key.primitive = SF_TRIANGLES;
160 break;
161 case GL_LINES:
162 key.primitive = SF_LINES;
163 break;
164 case GL_POINTS:
165 key.primitive = SF_POINTS;
166 break;
167 }
168
169 /* _NEW_POINT */
170 key.do_point_sprite = ctx->Point.PointSprite;
171 if (key.do_point_sprite) {
172 int i;
173
174 for (i = 0; i < 8; i++) {
175 if (ctx->Point.CoordReplace[i])
176 key.point_sprite_coord_replace |= (1 << i);
177 }
178 }
179 key.sprite_origin_lower_left = (ctx->Point.SpriteOrigin == GL_LOWER_LEFT);
180 /* _NEW_LIGHT */
181 key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT);
182 key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
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 &brw->sf.prog_data);
198 if (brw->sf.prog_bo == NULL)
199 compile_sf_prog( brw, &key );
200 }
201
202
203 const struct brw_tracked_state brw_sf_prog = {
204 .dirty = {
205 .mesa = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT),
206 .brw = (BRW_NEW_REDUCED_PRIMITIVE),
207 .cache = CACHE_NEW_VS_PROG
208 },
209 .prepare = upload_sf_prog
210 };
211