mesa/i965/i915/r200: eliminate gl_vertex_program
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_wm_state.c
1 /*
2 * Copyright © 2009 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "brw_wm.h"
33 #include "program/program.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_statevars.h"
36 #include "main/shaderapi.h"
37 #include "main/framebuffer.h"
38 #include "intel_batchbuffer.h"
39
40 static void
41 gen6_upload_wm_push_constants(struct brw_context *brw)
42 {
43 struct brw_stage_state *stage_state = &brw->wm.base;
44 /* BRW_NEW_FRAGMENT_PROGRAM */
45 const struct brw_fragment_program *fp =
46 brw_fragment_program_const(brw->fragment_program);
47 /* BRW_NEW_FS_PROG_DATA */
48 const struct brw_stage_prog_data *prog_data = brw->wm.base.prog_data;
49
50 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_FRAGMENT);
51
52 gen6_upload_push_constants(brw, &fp->program.Base, prog_data,
53 stage_state, AUB_TRACE_WM_CONSTANTS);
54
55 if (brw->gen >= 7) {
56 gen7_upload_constant_state(brw, &brw->wm.base, true,
57 _3DSTATE_CONSTANT_PS);
58 }
59 }
60
61 const struct brw_tracked_state gen6_wm_push_constants = {
62 .dirty = {
63 .mesa = _NEW_PROGRAM_CONSTANTS,
64 .brw = BRW_NEW_BATCH |
65 BRW_NEW_BLORP |
66 BRW_NEW_FRAGMENT_PROGRAM |
67 BRW_NEW_FS_PROG_DATA |
68 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
69 },
70 .emit = gen6_upload_wm_push_constants,
71 };
72
73 void
74 gen6_upload_wm_state(struct brw_context *brw,
75 const struct brw_wm_prog_data *prog_data,
76 const struct brw_stage_state *stage_state,
77 bool multisampled_fbo,
78 bool dual_source_blend_enable, bool kill_enable,
79 bool color_buffer_write_enable, bool msaa_enabled,
80 bool line_stipple_enable, bool polygon_stipple_enable,
81 bool statistic_enable)
82 {
83 const struct gen_device_info *devinfo = &brw->screen->devinfo;
84 uint32_t dw2, dw4, dw5, dw6, ksp0, ksp2;
85
86 /* We can't fold this into gen6_upload_wm_push_constants(), because
87 * according to the SNB PRM, vol 2 part 1 section 7.2.2
88 * (3DSTATE_CONSTANT_PS [DevSNB]):
89 *
90 * "[DevSNB]: This packet must be followed by WM_STATE."
91 */
92 if (prog_data->base.nr_params == 0) {
93 /* Disable the push constant buffers. */
94 BEGIN_BATCH(5);
95 OUT_BATCH(_3DSTATE_CONSTANT_PS << 16 | (5 - 2));
96 OUT_BATCH(0);
97 OUT_BATCH(0);
98 OUT_BATCH(0);
99 OUT_BATCH(0);
100 ADVANCE_BATCH();
101 } else {
102 BEGIN_BATCH(5);
103 OUT_BATCH(_3DSTATE_CONSTANT_PS << 16 |
104 GEN6_CONSTANT_BUFFER_0_ENABLE |
105 (5 - 2));
106 /* Pointer to the WM constant buffer. Covered by the set of
107 * state flags from gen6_upload_wm_push_constants.
108 */
109 OUT_BATCH(stage_state->push_const_offset +
110 stage_state->push_const_size - 1);
111 OUT_BATCH(0);
112 OUT_BATCH(0);
113 OUT_BATCH(0);
114 ADVANCE_BATCH();
115 }
116
117 dw2 = dw4 = dw5 = dw6 = ksp2 = 0;
118
119 if (statistic_enable)
120 dw4 |= GEN6_WM_STATISTICS_ENABLE;
121
122 dw5 |= GEN6_WM_LINE_AA_WIDTH_1_0;
123 dw5 |= GEN6_WM_LINE_END_CAP_AA_WIDTH_0_5;
124
125 if (prog_data->base.use_alt_mode)
126 dw2 |= GEN6_WM_FLOATING_POINT_MODE_ALT;
127
128 dw2 |= (ALIGN(stage_state->sampler_count, 4) / 4) <<
129 GEN6_WM_SAMPLER_COUNT_SHIFT;
130
131 dw2 |= ((prog_data->base.binding_table.size_bytes / 4) <<
132 GEN6_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
133
134 dw5 |= (devinfo->max_wm_threads - 1) << GEN6_WM_MAX_THREADS_SHIFT;
135
136 if (prog_data->dispatch_8)
137 dw5 |= GEN6_WM_8_DISPATCH_ENABLE;
138
139 if (prog_data->dispatch_16)
140 dw5 |= GEN6_WM_16_DISPATCH_ENABLE;
141
142 dw4 |= prog_data->base.dispatch_grf_start_reg <<
143 GEN6_WM_DISPATCH_START_GRF_SHIFT_0;
144 dw4 |= prog_data->dispatch_grf_start_reg_2 <<
145 GEN6_WM_DISPATCH_START_GRF_SHIFT_2;
146
147 ksp0 = stage_state->prog_offset;
148 ksp2 = stage_state->prog_offset + prog_data->prog_offset_2;
149
150 if (dual_source_blend_enable)
151 dw5 |= GEN6_WM_DUAL_SOURCE_BLEND_ENABLE;
152
153 if (line_stipple_enable)
154 dw5 |= GEN6_WM_LINE_STIPPLE_ENABLE;
155
156 if (polygon_stipple_enable)
157 dw5 |= GEN6_WM_POLYGON_STIPPLE_ENABLE;
158
159 if (prog_data->uses_src_depth)
160 dw5 |= GEN6_WM_USES_SOURCE_DEPTH;
161 if (prog_data->uses_src_w)
162 dw5 |= GEN6_WM_USES_SOURCE_W;
163 if (prog_data->computed_depth_mode != BRW_PSCDEPTH_OFF)
164 dw5 |= GEN6_WM_COMPUTED_DEPTH;
165 dw6 |= prog_data->barycentric_interp_modes <<
166 GEN6_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT;
167
168 if (kill_enable)
169 dw5 |= GEN6_WM_KILL_ENABLE;
170
171 if (color_buffer_write_enable ||
172 dw5 & (GEN6_WM_KILL_ENABLE | GEN6_WM_COMPUTED_DEPTH))
173 dw5 |= GEN6_WM_DISPATCH_ENABLE;
174
175 /* From the SNB PRM, volume 2 part 1, page 278:
176 * "This bit is inserted in the PS payload header and made available to
177 * the DataPort (either via the message header or via header bypass) to
178 * indicate that oMask data (one or two phases) is included in Render
179 * Target Write messages. If present, the oMask data is used to mask off
180 * samples."
181 */
182 if (prog_data->uses_omask)
183 dw5 |= GEN6_WM_OMASK_TO_RENDER_TARGET;
184
185 dw6 |= prog_data->num_varying_inputs <<
186 GEN6_WM_NUM_SF_OUTPUTS_SHIFT;
187 if (multisampled_fbo) {
188 if (msaa_enabled)
189 dw6 |= GEN6_WM_MSRAST_ON_PATTERN;
190 else
191 dw6 |= GEN6_WM_MSRAST_OFF_PIXEL;
192
193 if (prog_data->persample_dispatch)
194 dw6 |= GEN6_WM_MSDISPMODE_PERSAMPLE;
195 else {
196 dw6 |= GEN6_WM_MSDISPMODE_PERPIXEL;
197 }
198 } else {
199 dw6 |= GEN6_WM_MSRAST_OFF_PIXEL;
200 dw6 |= GEN6_WM_MSDISPMODE_PERSAMPLE;
201 }
202
203 /* From the SNB PRM, volume 2 part 1, page 281:
204 * "If the PS kernel does not need the Position XY Offsets
205 * to compute a Position XY value, then this field should be
206 * programmed to POSOFFSET_NONE."
207 *
208 * "SW Recommendation: If the PS kernel needs the Position Offsets
209 * to compute a Position XY value, this field should match Position
210 * ZW Interpolation Mode to ensure a consistent position.xyzw
211 * computation."
212 * We only require XY sample offsets. So, this recommendation doesn't
213 * look useful at the moment. We might need this in future.
214 */
215 if (prog_data->uses_pos_offset)
216 dw6 |= GEN6_WM_POSOFFSET_SAMPLE;
217 else
218 dw6 |= GEN6_WM_POSOFFSET_NONE;
219
220 BEGIN_BATCH(9);
221 OUT_BATCH(_3DSTATE_WM << 16 | (9 - 2));
222 OUT_BATCH(ksp0);
223 OUT_BATCH(dw2);
224 if (prog_data->base.total_scratch) {
225 OUT_RELOC(stage_state->scratch_bo,
226 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
227 ffs(stage_state->per_thread_scratch) - 11);
228 } else {
229 OUT_BATCH(0);
230 }
231 OUT_BATCH(dw4);
232 OUT_BATCH(dw5);
233 OUT_BATCH(dw6);
234 OUT_BATCH(0); /* kernel 1 pointer */
235 OUT_BATCH(ksp2);
236 ADVANCE_BATCH();
237 }
238
239 static void
240 upload_wm_state(struct brw_context *brw)
241 {
242 struct gl_context *ctx = &brw->ctx;
243 /* BRW_NEW_FS_PROG_DATA */
244 const struct brw_wm_prog_data *prog_data =
245 brw_wm_prog_data(brw->wm.base.prog_data);
246
247 /* _NEW_BUFFERS */
248 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
249
250 /* BRW_NEW_FS_PROG_DATA | _NEW_COLOR */
251 const bool dual_src_blend_enable = prog_data->dual_src_blend &&
252 (ctx->Color.BlendEnabled & 1) &&
253 ctx->Color.Blend[0]._UsesDualSrc;
254
255 /* _NEW_COLOR, _NEW_MULTISAMPLE */
256 const bool kill_enable = prog_data->uses_kill || ctx->Color.AlphaEnabled ||
257 ctx->Multisample.SampleAlphaToCoverage ||
258 prog_data->uses_omask;
259
260 /* Rendering against the gl-context is always taken into account. */
261 const bool statistic_enable = true;
262
263 /* _NEW_LINE | _NEW_POLYGON | _NEW_BUFFERS | _NEW_COLOR |
264 * _NEW_MULTISAMPLE
265 */
266 gen6_upload_wm_state(brw, prog_data, &brw->wm.base,
267 multisampled_fbo,
268 dual_src_blend_enable, kill_enable,
269 brw_color_buffer_write_enabled(brw),
270 ctx->Multisample.Enabled,
271 ctx->Line.StippleFlag, ctx->Polygon.StippleFlag,
272 statistic_enable);
273 }
274
275 const struct brw_tracked_state gen6_wm_state = {
276 .dirty = {
277 .mesa = _NEW_BUFFERS |
278 _NEW_COLOR |
279 _NEW_LINE |
280 _NEW_MULTISAMPLE |
281 _NEW_POLYGON |
282 _NEW_PROGRAM_CONSTANTS,
283 .brw = BRW_NEW_BATCH |
284 BRW_NEW_BLORP |
285 BRW_NEW_FS_PROG_DATA |
286 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
287 },
288 .emit = upload_wm_state,
289 };