2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
32 #include "main/macros.h"
33 #include "intel_batchbuffer.h"
36 get_attr_override(struct brw_context
*brw
, int fs_attr
, int two_side_color
)
38 int attr_index
= 0, i
, vs_attr
;
41 if (fs_attr
<= FRAG_ATTRIB_TEX7
)
43 else if (fs_attr
== FRAG_ATTRIB_FACE
)
44 vs_attr
= 0; /* XXX */
45 else if (fs_attr
== FRAG_ATTRIB_PNTC
)
46 vs_attr
= 0; /* XXX */
48 assert(fs_attr
>= FRAG_ATTRIB_VAR0
);
49 vs_attr
= fs_attr
- FRAG_ATTRIB_VAR0
+ VERT_RESULT_VAR0
;
52 /* Find the source index (0 = first attribute after the 4D position)
53 * for this output attribute. attr is currently a VERT_RESULT_* but should
56 for (i
= 1; i
< vs_attr
; i
++) {
57 if (brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(i
))
61 assert(attr_index
< 32);
64 if ((brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_COL1
)) &&
65 (brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_BFC1
))) {
66 assert(brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_COL0
));
67 assert(brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_BFC0
));
69 } else if ((brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_COL0
)) &&
70 (brw
->vs
.prog_data
->outputs_written
& BITFIELD64_BIT(VERT_RESULT_BFC0
)))
74 if (bfc
&& (fs_attr
<= FRAG_ATTRIB_TEX7
&& fs_attr
> FRAG_ATTRIB_WPOS
)) {
75 if (fs_attr
== FRAG_ATTRIB_COL0
)
76 attr_index
|= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING
<< ATTRIBUTE_SWIZZLE_SHIFT
);
77 else if (fs_attr
== FRAG_ATTRIB_COL1
&& bfc
== 2) {
79 attr_index
|= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING
<< ATTRIBUTE_SWIZZLE_SHIFT
);
89 upload_sf_state(struct brw_context
*brw
)
91 struct intel_context
*intel
= &brw
->intel
;
92 struct gl_context
*ctx
= &intel
->ctx
;
93 /* CACHE_NEW_VS_PROG */
94 uint32_t num_inputs
= brw_count_bits(brw
->vs
.prog_data
->outputs_written
);
95 /* BRW_NEW_FRAGMENT_PROGRAM */
96 uint32_t num_outputs
= brw_count_bits(brw
->fragment_program
->Base
.InputsRead
);
97 uint32_t dw1
, dw2
, dw3
, dw4
, dw16
, dw17
;
100 GLboolean render_to_fbo
= brw
->intel
.ctx
.DrawBuffer
->Name
!= 0;
103 int two_side_color
= (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
);
106 if (ctx
->Transform
.ClipPlanesEnabled
)
112 GEN6_SF_SWIZZLE_ENABLE
|
113 num_outputs
<< GEN6_SF_NUM_OUTPUTS_SHIFT
|
114 (num_inputs
+ 1) / 2 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT
|
115 urb_start
<< GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT
;
116 dw2
= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE
|
117 GEN6_SF_STATISTICS_ENABLE
;
124 if ((ctx
->Polygon
.FrontFace
== GL_CCW
) ^ render_to_fbo
)
125 dw2
|= GEN6_SF_WINDING_CCW
;
127 if (ctx
->Polygon
.OffsetFill
)
128 dw2
|= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID
;
130 if (ctx
->Polygon
.OffsetLine
)
131 dw2
|= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME
;
133 if (ctx
->Polygon
.OffsetPoint
)
134 dw2
|= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT
;
136 switch (ctx
->Polygon
.FrontMode
) {
138 dw2
|= GEN6_SF_FRONT_SOLID
;
142 dw2
|= GEN6_SF_FRONT_WIREFRAME
;
146 dw2
|= GEN6_SF_FRONT_POINT
;
154 switch (ctx
->Polygon
.BackMode
) {
156 dw2
|= GEN6_SF_BACK_SOLID
;
160 dw2
|= GEN6_SF_BACK_WIREFRAME
;
164 dw2
|= GEN6_SF_BACK_POINT
;
173 if (ctx
->Scissor
.Enabled
)
174 dw3
|= GEN6_SF_SCISSOR_ENABLE
;
177 if (ctx
->Polygon
.CullFlag
) {
178 switch (ctx
->Polygon
.CullFaceMode
) {
180 dw3
|= GEN6_SF_CULL_FRONT
;
183 dw3
|= GEN6_SF_CULL_BACK
;
185 case GL_FRONT_AND_BACK
:
186 dw3
|= GEN6_SF_CULL_BOTH
;
193 dw3
|= GEN6_SF_CULL_NONE
;
197 dw3
|= U_FIXED(CLAMP(ctx
->Line
.Width
, 0.0, 7.99), 7) <<
198 GEN6_SF_LINE_WIDTH_SHIFT
;
199 if (ctx
->Line
.SmoothFlag
) {
200 dw3
|= GEN6_SF_LINE_AA_ENABLE
;
201 dw3
|= GEN6_SF_LINE_AA_MODE_TRUE
;
202 dw3
|= GEN6_SF_LINE_END_CAP_WIDTH_1_0
;
206 if (!(ctx
->VertexProgram
.PointSizeEnabled
||
207 ctx
->Point
._Attenuated
))
208 dw4
|= GEN6_SF_USE_STATE_POINT_WIDTH
;
210 dw4
|= U_FIXED(CLAMP(ctx
->Point
.Size
, 0.125, 225.875), 3) <<
211 GEN6_SF_POINT_WIDTH_SHIFT
;
212 if (ctx
->Point
.SpriteOrigin
== GL_LOWER_LEFT
)
213 dw1
|= GEN6_SF_POINT_SPRITE_LOWERLEFT
;
216 if (ctx
->Light
.ProvokingVertex
!= GL_FIRST_VERTEX_CONVENTION
) {
218 (2 << GEN6_SF_TRI_PROVOKE_SHIFT
) |
219 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT
) |
220 (1 << GEN6_SF_LINE_PROVOKE_SHIFT
);
223 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT
);
226 if (ctx
->Point
.PointSprite
) {
227 for (i
= 0; i
< 8; i
++) {
228 if (ctx
->Point
.CoordReplace
[i
])
234 if (ctx
->Light
.ShadeModel
== GL_FLAT
) {
235 dw17
|= ((brw
->fragment_program
->Base
.InputsRead
& (FRAG_BIT_COL0
| FRAG_BIT_COL1
)) >>
236 ((brw
->fragment_program
->Base
.InputsRead
& FRAG_BIT_WPOS
) ? 0 : 1));
240 OUT_BATCH(CMD_3D_SF_STATE
<< 16 | (20 - 2));
245 OUT_BATCH_F(ctx
->Polygon
.OffsetUnits
* 2); /* constant. copied from gen4 */
246 OUT_BATCH_F(ctx
->Polygon
.OffsetFactor
); /* scale */
247 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
248 for (i
= 0; i
< 8; i
++) {
249 uint32_t attr_overrides
= 0;
251 for (; attr
< 64; attr
++) {
252 if (brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(attr
)) {
253 attr_overrides
|= get_attr_override(brw
, attr
, two_side_color
);
259 for (; attr
< 64; attr
++) {
260 if (brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(attr
)) {
261 attr_overrides
|= get_attr_override(brw
, attr
, two_side_color
) << 16;
266 OUT_BATCH(attr_overrides
);
268 OUT_BATCH(dw16
); /* point sprite texcoord bitmask */
269 OUT_BATCH(dw17
); /* constant interp bitmask */
270 OUT_BATCH(0); /* wrapshortest enables 0-7 */
271 OUT_BATCH(0); /* wrapshortest enables 8-15 */
275 const struct brw_tracked_state gen6_sf_state
= {
277 .mesa
= (_NEW_LIGHT
|
284 .brw
= (BRW_NEW_CONTEXT
|
285 BRW_NEW_FRAGMENT_PROGRAM
),
286 .cache
= CACHE_NEW_VS_PROG
288 .emit
= upload_sf_state
,