2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keithw@vmware.com>
32 #include "compiler/nir/nir.h"
33 #include "main/macros.h"
34 #include "main/mtypes.h"
35 #include "main/enums.h"
36 #include "main/fbobject.h"
38 #include "intel_batchbuffer.h"
40 #include "brw_defines.h"
41 #include "brw_context.h"
45 #include "brw_state.h"
47 #include "util/ralloc.h"
49 static void compile_sf_prog( struct brw_context
*brw
,
50 struct brw_sf_prog_key
*key
)
52 struct brw_sf_compile c
;
53 const GLuint
*program
;
57 memset(&c
, 0, sizeof(c
));
59 mem_ctx
= ralloc_context(NULL
);
60 /* Begin the compilation:
62 brw_init_codegen(&brw
->screen
->devinfo
, &c
.func
, mem_ctx
);
65 c
.vue_map
= brw
->vue_map_geom_out
;
66 if (c
.key
.do_point_coord
) {
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.
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
;
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
;
80 c
.prog_data
.urb_read_length
= c
.nr_attr_regs
;
81 c
.prog_data
.urb_entry_size
= c
.nr_setup_regs
* 2;
83 /* Which primitive? Or all three?
85 switch (key
->primitive
) {
88 brw_emit_tri_setup( &c
, true );
92 brw_emit_line_setup( &c
, true );
96 if (key
->do_point_sprite
)
97 brw_emit_point_sprite_setup( &c
, true );
99 brw_emit_point_setup( &c
, true );
101 case SF_UNFILLED_TRIS
:
103 brw_emit_anyprim_setup( &c
);
106 unreachable("not reached");
109 /* FINISHME: SF programs use calculated jumps (i.e., JMPI with a register
110 * source). Compacting would be difficult.
112 /* brw_compact_instructions(&c.func, 0, 0, NULL); */
116 program
= brw_get_program(&c
.func
, &program_size
);
118 if (unlikely(INTEL_DEBUG
& DEBUG_SF
)) {
119 fprintf(stderr
, "sf:\n");
120 brw_disassemble(&brw
->screen
->devinfo
,
121 c
.func
.store
, 0, program_size
, stderr
);
122 fprintf(stderr
, "\n");
125 brw_upload_cache(&brw
->cache
, BRW_CACHE_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
);
133 /* Calculate interpolants for triangle and line rasterization.
136 brw_upload_sf_prog(struct brw_context
*brw
)
138 struct gl_context
*ctx
= &brw
->ctx
;
139 struct brw_sf_prog_key key
;
141 if (!brw_state_dirty(brw
,
150 BRW_NEW_FRAGMENT_PROGRAM
|
151 BRW_NEW_REDUCED_PRIMITIVE
|
152 BRW_NEW_VUE_MAP_GEOM_OUT
))
156 bool render_to_fbo
= _mesa_is_user_fbo(ctx
->DrawBuffer
);
158 memset(&key
, 0, sizeof(key
));
160 /* Populate the key, noting state dependencies:
162 /* BRW_NEW_VUE_MAP_GEOM_OUT */
163 key
.attrs
= brw
->vue_map_geom_out
.slots_valid
;
165 /* BRW_NEW_REDUCED_PRIMITIVE */
166 switch (brw
->reduced_primitive
) {
168 /* NOTE: We just use the edgeflag attribute as an indicator that
169 * unfilled triangles are active. We don't actually do the
170 * edgeflag testing here, it is already done in the clip
173 if (key
.attrs
& BITFIELD64_BIT(VARYING_SLOT_EDGE
))
174 key
.primitive
= SF_UNFILLED_TRIS
;
176 key
.primitive
= SF_TRIANGLES
;
179 key
.primitive
= SF_LINES
;
182 key
.primitive
= SF_POINTS
;
187 key
.userclip_active
= (ctx
->Transform
.ClipPlanesEnabled
!= 0);
190 key
.do_point_sprite
= ctx
->Point
.PointSprite
;
191 if (key
.do_point_sprite
) {
192 key
.point_sprite_coord_replace
= ctx
->Point
.CoordReplace
& 0xff;
194 if (brw
->fragment_program
->info
.inputs_read
&
195 BITFIELD64_BIT(VARYING_SLOT_PNTC
)) {
196 key
.do_point_coord
= 1;
200 * Window coordinates in a FBO are inverted, which means point
201 * sprite origin must be inverted, too.
203 if ((ctx
->Point
.SpriteOrigin
== GL_LOWER_LEFT
) != render_to_fbo
)
204 key
.sprite_origin_lower_left
= true;
206 const struct gl_program
*fprog
= brw
->fragment_program
;
208 assert(brw
->gen
< 6);
209 struct gen4_fragment_program
*p
= (struct gen4_fragment_program
*) fprog
;
211 /* BRW_NEW_FRAGMENT_PROGRAM */
212 key
.contains_flat_varying
= p
->contains_flat_varying
;
213 key
.interp_mode
= p
->interp_mode
;
216 /* _NEW_LIGHT | _NEW_PROGRAM */
217 key
.do_twoside_color
= ((ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
) ||
218 ctx
->VertexProgram
._TwoSideEnabled
);
221 if (key
.do_twoside_color
) {
222 /* If we're rendering to a FBO, we have to invert the polygon
223 * face orientation, just as we invert the viewport in
224 * sf_unit_create_from_key().
226 key
.frontface_ccw
= ctx
->Polygon
._FrontBit
== render_to_fbo
;
229 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_SF_PROG
,
231 &brw
->sf
.prog_offset
, &brw
->sf
.prog_data
)) {
232 compile_sf_prog( brw
, &key
);