2 * Copyright © 2011 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
25 #include "brw_context.h"
26 #include "brw_state.h"
27 #include "brw_defines.h"
28 #include "compiler/brw_eu_defines.h"
31 #include "program/program.h"
32 #include "program/prog_parameter.h"
33 #include "program/prog_statevars.h"
34 #include "main/framebuffer.h"
35 #include "intel_batchbuffer.h"
38 upload_wm_state(struct brw_context
*brw
)
40 struct gl_context
*ctx
= &brw
->ctx
;
41 /* BRW_NEW_FS_PROG_DATA */
42 const struct brw_wm_prog_data
*prog_data
=
43 brw_wm_prog_data(brw
->wm
.base
.prog_data
);
44 bool writes_depth
= prog_data
->computed_depth_mode
!= BRW_PSCDEPTH_OFF
;
48 const bool multisampled_fbo
= _mesa_geometric_samples(ctx
->DrawBuffer
) > 1;
51 dw1
|= GEN7_WM_STATISTICS_ENABLE
;
52 dw1
|= GEN7_WM_LINE_AA_WIDTH_1_0
;
53 dw1
|= GEN7_WM_LINE_END_CAP_AA_WIDTH_0_5
;
56 if (ctx
->Line
.StippleFlag
)
57 dw1
|= GEN7_WM_LINE_STIPPLE_ENABLE
;
60 if (ctx
->Polygon
.StippleFlag
)
61 dw1
|= GEN7_WM_POLYGON_STIPPLE_ENABLE
;
63 if (prog_data
->uses_src_depth
)
64 dw1
|= GEN7_WM_USES_SOURCE_DEPTH
;
66 if (prog_data
->uses_src_w
)
67 dw1
|= GEN7_WM_USES_SOURCE_W
;
69 dw1
|= prog_data
->computed_depth_mode
<< GEN7_WM_COMPUTED_DEPTH_MODE_SHIFT
;
70 dw1
|= prog_data
->barycentric_interp_modes
<<
71 GEN7_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT
;
73 /* _NEW_COLOR, _NEW_MULTISAMPLE _NEW_BUFFERS */
74 /* Enable if the pixel shader kernel generates and outputs oMask.
76 if (prog_data
->uses_kill
||
77 _mesa_is_alpha_test_enabled(ctx
) ||
78 _mesa_is_alpha_to_coverage_enabled(ctx
) ||
79 prog_data
->uses_omask
) {
80 dw1
|= GEN7_WM_KILL_ENABLE
;
83 /* _NEW_BUFFERS | _NEW_COLOR */
84 if (brw_color_buffer_write_enabled(brw
) || writes_depth
||
85 prog_data
->has_side_effects
|| dw1
& GEN7_WM_KILL_ENABLE
) {
86 dw1
|= GEN7_WM_DISPATCH_ENABLE
;
88 if (multisampled_fbo
) {
89 /* _NEW_MULTISAMPLE */
90 if (ctx
->Multisample
.Enabled
)
91 dw1
|= GEN7_WM_MSRAST_ON_PATTERN
;
93 dw1
|= GEN7_WM_MSRAST_OFF_PIXEL
;
95 if (prog_data
->persample_dispatch
)
96 dw2
|= GEN7_WM_MSDISPMODE_PERSAMPLE
;
98 dw2
|= GEN7_WM_MSDISPMODE_PERPIXEL
;
100 dw1
|= GEN7_WM_MSRAST_OFF_PIXEL
;
101 dw2
|= GEN7_WM_MSDISPMODE_PERSAMPLE
;
104 if (prog_data
->uses_sample_mask
) {
105 dw1
|= GEN7_WM_USES_INPUT_COVERAGE_MASK
;
108 /* BRW_NEW_FS_PROG_DATA */
109 if (prog_data
->early_fragment_tests
)
110 dw1
|= GEN7_WM_EARLY_DS_CONTROL_PREPS
;
111 else if (prog_data
->has_side_effects
)
112 dw1
|= GEN7_WM_EARLY_DS_CONTROL_PSEXEC
;
114 /* The "UAV access enable" bits are unnecessary on HSW because they only
115 * seem to have an effect on the HW-assisted coherency mechanism which we
116 * don't need, and the rasterization-related UAV_ONLY flag and the
117 * DISPATCH_ENABLE bit can be set independently from it.
118 * C.f. gen8_upload_ps_extra().
120 * BRW_NEW_FRAGMENT_PROGRAM | BRW_NEW_FS_PROG_DATA | _NEW_BUFFERS | _NEW_COLOR
122 if (brw
->is_haswell
&&
123 !(brw_color_buffer_write_enabled(brw
) || writes_depth
) &&
124 prog_data
->has_side_effects
)
125 dw2
|= HSW_WM_UAV_ONLY
;
128 OUT_BATCH(_3DSTATE_WM
<< 16 | (3 - 2));
134 const struct brw_tracked_state gen7_wm_state
= {
136 .mesa
= _NEW_BUFFERS
|
141 .brw
= BRW_NEW_BATCH
|
143 BRW_NEW_FS_PROG_DATA
,
145 .emit
= upload_wm_state
,
149 gen7_upload_ps_state(struct brw_context
*brw
,
150 const struct brw_stage_state
*stage_state
,
151 const struct brw_wm_prog_data
*prog_data
,
152 bool enable_dual_src_blend
, unsigned sample_mask
,
153 unsigned fast_clear_op
)
155 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
156 uint32_t dw2
, dw4
, dw5
, ksp0
, ksp2
;
157 const int max_threads_shift
= brw
->is_haswell
?
158 HSW_PS_MAX_THREADS_SHIFT
: IVB_PS_MAX_THREADS_SHIFT
;
160 dw2
= dw4
= dw5
= ksp2
= 0;
162 const unsigned sampler_count
=
163 DIV_ROUND_UP(CLAMP(stage_state
->sampler_count
, 0, 16), 4);
164 dw2
|= SET_FIELD(sampler_count
, GEN7_PS_SAMPLER_COUNT
);
166 dw2
|= ((prog_data
->base
.binding_table
.size_bytes
/ 4) <<
167 GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT
);
169 if (prog_data
->base
.use_alt_mode
)
170 dw2
|= GEN7_PS_FLOATING_POINT_MODE_ALT
;
172 /* Haswell requires the sample mask to be set in this packet as well as
173 * in 3DSTATE_SAMPLE_MASK; the values should match. */
174 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
176 dw4
|= SET_FIELD(sample_mask
, HSW_PS_SAMPLE_MASK
);
178 dw4
|= (devinfo
->max_wm_threads
- 1) << max_threads_shift
;
180 if (prog_data
->base
.nr_params
> 0)
181 dw4
|= GEN7_PS_PUSH_CONSTANT_ENABLE
;
183 /* From the IVB PRM, volume 2 part 1, page 287:
184 * "This bit is inserted in the PS payload header and made available to
185 * the DataPort (either via the message header or via header bypass) to
186 * indicate that oMask data (one or two phases) is included in Render
187 * Target Write messages. If present, the oMask data is used to mask off
190 if (prog_data
->uses_omask
)
191 dw4
|= GEN7_PS_OMASK_TO_RENDER_TARGET
;
193 /* From the IVB PRM, volume 2 part 1, page 287:
194 * "If the PS kernel does not need the Position XY Offsets to
195 * compute a Position Value, then this field should be programmed
196 * to POSOFFSET_NONE."
197 * "SW Recommendation: If the PS kernel needs the Position Offsets
198 * to compute a Position XY value, this field should match Position
199 * ZW Interpolation Mode to ensure a consistent position.xyzw
201 * We only require XY sample offsets. So, this recommendation doesn't
202 * look useful at the moment. We might need this in future.
204 if (prog_data
->uses_pos_offset
)
205 dw4
|= GEN7_PS_POSOFFSET_SAMPLE
;
207 dw4
|= GEN7_PS_POSOFFSET_NONE
;
209 /* The hardware wedges if you have this bit set but don't turn on any dual
210 * source blend factors.
212 if (enable_dual_src_blend
)
213 dw4
|= GEN7_PS_DUAL_SOURCE_BLEND_ENABLE
;
215 /* BRW_NEW_FS_PROG_DATA */
216 if (prog_data
->num_varying_inputs
!= 0)
217 dw4
|= GEN7_PS_ATTRIBUTE_ENABLE
;
219 dw4
|= fast_clear_op
;
221 if (prog_data
->dispatch_16
)
222 dw4
|= GEN7_PS_16_DISPATCH_ENABLE
;
224 if (prog_data
->dispatch_8
)
225 dw4
|= GEN7_PS_8_DISPATCH_ENABLE
;
227 dw5
|= prog_data
->base
.dispatch_grf_start_reg
<<
228 GEN7_PS_DISPATCH_START_GRF_SHIFT_0
;
229 dw5
|= prog_data
->dispatch_grf_start_reg_2
<<
230 GEN7_PS_DISPATCH_START_GRF_SHIFT_2
;
232 ksp0
= stage_state
->prog_offset
;
233 ksp2
= stage_state
->prog_offset
+ prog_data
->prog_offset_2
;
236 OUT_BATCH(_3DSTATE_PS
<< 16 | (8 - 2));
239 if (prog_data
->base
.total_scratch
) {
240 OUT_RELOC(brw
->wm
.base
.scratch_bo
,
241 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
242 ffs(stage_state
->per_thread_scratch
) - 11);
248 OUT_BATCH(0); /* kernel 1 pointer */
254 upload_ps_state(struct brw_context
*brw
)
256 /* BRW_NEW_FS_PROG_DATA */
257 const struct brw_wm_prog_data
*prog_data
=
258 brw_wm_prog_data(brw
->wm
.base
.prog_data
);
259 const struct gl_context
*ctx
= &brw
->ctx
;
260 /* BRW_NEW_FS_PROG_DATA | _NEW_COLOR */
261 const bool enable_dual_src_blend
= prog_data
->dual_src_blend
&&
262 (ctx
->Color
.BlendEnabled
& 1) &&
263 ctx
->Color
.Blend
[0]._UsesDualSrc
;
264 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
265 const unsigned sample_mask
=
266 brw
->is_haswell
? gen6_determine_sample_mask(brw
) : 0;
268 gen7_upload_ps_state(brw
, &brw
->wm
.base
, prog_data
,
269 enable_dual_src_blend
, sample_mask
,
270 brw
->wm
.fast_clear_op
);
273 const struct brw_tracked_state gen7_ps_state
= {
275 .mesa
= _NEW_BUFFERS
|
278 .brw
= BRW_NEW_BATCH
|
280 BRW_NEW_FS_PROG_DATA
,
282 .emit
= upload_ps_state
,