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