i965/gen7: Enable fragment shader dispatch if the program has image uniforms.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_wm_state.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdbool.h>
25 #include "brw_context.h"
26 #include "brw_state.h"
27 #include "brw_defines.h"
28 #include "brw_util.h"
29 #include "brw_wm.h"
30 #include "program/program.h"
31 #include "program/prog_parameter.h"
32 #include "program/prog_statevars.h"
33 #include "main/framebuffer.h"
34 #include "intel_batchbuffer.h"
35
36 static void
37 upload_wm_state(struct brw_context *brw)
38 {
39 struct gl_context *ctx = &brw->ctx;
40 /* BRW_NEW_FRAGMENT_PROGRAM */
41 const struct brw_fragment_program *fp =
42 brw_fragment_program_const(brw->fragment_program);
43 /* BRW_NEW_FS_PROG_DATA */
44 const struct brw_wm_prog_data *prog_data = brw->wm.prog_data;
45 bool writes_depth = prog_data->computed_depth_mode != BRW_PSCDEPTH_OFF;
46 uint32_t dw1, dw2;
47
48 /* _NEW_BUFFERS */
49 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
50
51 dw1 = dw2 = 0;
52 dw1 |= GEN7_WM_STATISTICS_ENABLE;
53 dw1 |= GEN7_WM_LINE_AA_WIDTH_1_0;
54 dw1 |= GEN7_WM_LINE_END_CAP_AA_WIDTH_0_5;
55
56 /* _NEW_LINE */
57 if (ctx->Line.StippleFlag)
58 dw1 |= GEN7_WM_LINE_STIPPLE_ENABLE;
59
60 /* _NEW_POLYGON */
61 if (ctx->Polygon.StippleFlag)
62 dw1 |= GEN7_WM_POLYGON_STIPPLE_ENABLE;
63
64 if (fp->program.Base.InputsRead & VARYING_BIT_POS)
65 dw1 |= GEN7_WM_USES_SOURCE_DEPTH | GEN7_WM_USES_SOURCE_W;
66
67 dw1 |= prog_data->computed_depth_mode << GEN7_WM_COMPUTED_DEPTH_MODE_SHIFT;
68 dw1 |= prog_data->barycentric_interp_modes <<
69 GEN7_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT;
70
71 /* _NEW_COLOR, _NEW_MULTISAMPLE */
72 /* Enable if the pixel shader kernel generates and outputs oMask.
73 */
74 if (prog_data->uses_kill || ctx->Color.AlphaEnabled ||
75 ctx->Multisample.SampleAlphaToCoverage ||
76 prog_data->uses_omask) {
77 dw1 |= GEN7_WM_KILL_ENABLE;
78 }
79
80 if (_mesa_active_fragment_shader_has_atomic_ops(&brw->ctx)) {
81 dw1 |= GEN7_WM_DISPATCH_ENABLE;
82 }
83
84 /* _NEW_BUFFERS | _NEW_COLOR */
85 if (brw_color_buffer_write_enabled(brw) || writes_depth ||
86 prog_data->base.nr_image_params ||
87 dw1 & GEN7_WM_KILL_ENABLE) {
88 dw1 |= GEN7_WM_DISPATCH_ENABLE;
89 }
90 if (multisampled_fbo) {
91 /* _NEW_MULTISAMPLE */
92 if (ctx->Multisample.Enabled)
93 dw1 |= GEN7_WM_MSRAST_ON_PATTERN;
94 else
95 dw1 |= GEN7_WM_MSRAST_OFF_PIXEL;
96
97 if (_mesa_get_min_invocations_per_fragment(ctx, brw->fragment_program, false) > 1)
98 dw2 |= GEN7_WM_MSDISPMODE_PERSAMPLE;
99 else
100 dw2 |= GEN7_WM_MSDISPMODE_PERPIXEL;
101 } else {
102 dw1 |= GEN7_WM_MSRAST_OFF_PIXEL;
103 dw2 |= GEN7_WM_MSDISPMODE_PERSAMPLE;
104 }
105
106 if (fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_MASK_IN) {
107 dw1 |= GEN7_WM_USES_INPUT_COVERAGE_MASK;
108 }
109
110 BEGIN_BATCH(3);
111 OUT_BATCH(_3DSTATE_WM << 16 | (3 - 2));
112 OUT_BATCH(dw1);
113 OUT_BATCH(dw2);
114 ADVANCE_BATCH();
115 }
116
117 const struct brw_tracked_state gen7_wm_state = {
118 .dirty = {
119 .mesa = _NEW_BUFFERS |
120 _NEW_COLOR |
121 _NEW_LINE |
122 _NEW_MULTISAMPLE |
123 _NEW_POLYGON,
124 .brw = BRW_NEW_BATCH |
125 BRW_NEW_FRAGMENT_PROGRAM |
126 BRW_NEW_FS_PROG_DATA,
127 },
128 .emit = upload_wm_state,
129 };
130
131 static void
132 gen7_upload_ps_state(struct brw_context *brw,
133 const struct gl_fragment_program *fp,
134 const struct brw_stage_state *stage_state,
135 const struct brw_wm_prog_data *prog_data,
136 bool enable_dual_src_blend, unsigned sample_mask,
137 unsigned fast_clear_op)
138 {
139 struct gl_context *ctx = &brw->ctx;
140 uint32_t dw2, dw4, dw5, ksp0, ksp2;
141 const int max_threads_shift = brw->is_haswell ?
142 HSW_PS_MAX_THREADS_SHIFT : IVB_PS_MAX_THREADS_SHIFT;
143
144 dw2 = dw4 = dw5 = ksp2 = 0;
145
146 const unsigned sampler_count =
147 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4);
148 dw2 |= SET_FIELD(sampler_count, GEN7_PS_SAMPLER_COUNT);
149
150 dw2 |= ((prog_data->base.binding_table.size_bytes / 4) <<
151 GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT);
152
153 if (prog_data->base.use_alt_mode)
154 dw2 |= GEN7_PS_FLOATING_POINT_MODE_ALT;
155
156 /* Haswell requires the sample mask to be set in this packet as well as
157 * in 3DSTATE_SAMPLE_MASK; the values should match. */
158 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
159 if (brw->is_haswell)
160 dw4 |= SET_FIELD(sample_mask, HSW_PS_SAMPLE_MASK);
161
162 dw4 |= (brw->max_wm_threads - 1) << max_threads_shift;
163
164 if (prog_data->base.nr_params > 0)
165 dw4 |= GEN7_PS_PUSH_CONSTANT_ENABLE;
166
167 /* From the IVB PRM, volume 2 part 1, page 287:
168 * "This bit is inserted in the PS payload header and made available to
169 * the DataPort (either via the message header or via header bypass) to
170 * indicate that oMask data (one or two phases) is included in Render
171 * Target Write messages. If present, the oMask data is used to mask off
172 * samples."
173 */
174 if (prog_data->uses_omask)
175 dw4 |= GEN7_PS_OMASK_TO_RENDER_TARGET;
176
177 /* From the IVB PRM, volume 2 part 1, page 287:
178 * "If the PS kernel does not need the Position XY Offsets to
179 * compute a Position Value, then this field should be programmed
180 * to POSOFFSET_NONE."
181 * "SW Recommendation: If the PS kernel needs the Position Offsets
182 * to compute a Position XY value, this field should match Position
183 * ZW Interpolation Mode to ensure a consistent position.xyzw
184 * computation."
185 * We only require XY sample offsets. So, this recommendation doesn't
186 * look useful at the moment. We might need this in future.
187 */
188 if (prog_data->uses_pos_offset)
189 dw4 |= GEN7_PS_POSOFFSET_SAMPLE;
190 else
191 dw4 |= GEN7_PS_POSOFFSET_NONE;
192
193 /* The hardware wedges if you have this bit set but don't turn on any dual
194 * source blend factors.
195 */
196 if (enable_dual_src_blend)
197 dw4 |= GEN7_PS_DUAL_SOURCE_BLEND_ENABLE;
198
199 /* BRW_NEW_FS_PROG_DATA */
200 if (prog_data->num_varying_inputs != 0)
201 dw4 |= GEN7_PS_ATTRIBUTE_ENABLE;
202
203 /* In case of non 1x per sample shading, only one of SIMD8 and SIMD16
204 * should be enabled. We do 'SIMD16 only' dispatch if a SIMD16 shader
205 * is successfully compiled. In majority of the cases that bring us
206 * better performance than 'SIMD8 only' dispatch.
207 */
208 int min_inv_per_frag =
209 _mesa_get_min_invocations_per_fragment(ctx, fp, false);
210 assert(min_inv_per_frag >= 1);
211
212 if (prog_data->prog_offset_16 || prog_data->no_8) {
213 dw4 |= GEN7_PS_16_DISPATCH_ENABLE;
214 if (!prog_data->no_8 && min_inv_per_frag == 1) {
215 dw4 |= GEN7_PS_8_DISPATCH_ENABLE;
216 dw5 |= (prog_data->base.dispatch_grf_start_reg <<
217 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
218 dw5 |= (prog_data->dispatch_grf_start_reg_16 <<
219 GEN7_PS_DISPATCH_START_GRF_SHIFT_2);
220 ksp0 = stage_state->prog_offset;
221 ksp2 = stage_state->prog_offset + prog_data->prog_offset_16;
222 } else {
223 dw5 |= (prog_data->dispatch_grf_start_reg_16 <<
224 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
225 ksp0 = stage_state->prog_offset + prog_data->prog_offset_16;
226 }
227 }
228 else {
229 dw4 |= GEN7_PS_8_DISPATCH_ENABLE;
230 dw5 |= (prog_data->base.dispatch_grf_start_reg <<
231 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
232 ksp0 = stage_state->prog_offset;
233 }
234
235 dw4 |= fast_clear_op;
236
237 BEGIN_BATCH(8);
238 OUT_BATCH(_3DSTATE_PS << 16 | (8 - 2));
239 OUT_BATCH(ksp0);
240 OUT_BATCH(dw2);
241 if (prog_data->base.total_scratch) {
242 OUT_RELOC(brw->wm.base.scratch_bo,
243 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
244 ffs(prog_data->base.total_scratch) - 11);
245 } else {
246 OUT_BATCH(0);
247 }
248 OUT_BATCH(dw4);
249 OUT_BATCH(dw5);
250 OUT_BATCH(0); /* kernel 1 pointer */
251 OUT_BATCH(ksp2);
252 ADVANCE_BATCH();
253 }
254
255 static void
256 upload_ps_state(struct brw_context *brw)
257 {
258 /* BRW_NEW_FS_PROG_DATA */
259 const struct brw_wm_prog_data *prog_data = brw->wm.prog_data;
260 const struct gl_context *ctx = &brw->ctx;
261 /* BRW_NEW_FS_PROG_DATA | _NEW_COLOR */
262 const bool enable_dual_src_blend = prog_data->dual_src_blend &&
263 (ctx->Color.BlendEnabled & 1) &&
264 ctx->Color.Blend[0]._UsesDualSrc;
265 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
266 const unsigned sample_mask =
267 brw->is_haswell ? gen6_determine_sample_mask(brw) : 0;
268
269 gen7_upload_ps_state(brw, brw->fragment_program, &brw->wm.base, prog_data,
270 enable_dual_src_blend, sample_mask,
271 brw->wm.fast_clear_op);
272 }
273
274 const struct brw_tracked_state gen7_ps_state = {
275 .dirty = {
276 .mesa = _NEW_BUFFERS |
277 _NEW_COLOR |
278 _NEW_MULTISAMPLE,
279 .brw = BRW_NEW_BATCH |
280 BRW_NEW_FRAGMENT_PROGRAM |
281 BRW_NEW_FS_PROG_DATA,
282 },
283 .emit = upload_ps_state,
284 };