i965: Create prog_data temporary variables in PS state upload code.
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_ps_state.c
1 /*
2 * Copyright © 2012 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 "program/program.h"
26 #include "brw_state.h"
27 #include "brw_defines.h"
28 #include "intel_batchbuffer.h"
29
30 static void
31 upload_ps_extra(struct brw_context *brw)
32 {
33 struct gl_context *ctx = &brw->ctx;
34 /* BRW_NEW_FRAGMENT_PROGRAM */
35 const struct brw_fragment_program *fp =
36 brw_fragment_program_const(brw->fragment_program);
37 /* CACHE_NEW_WM_PROG */
38 const struct brw_wm_prog_data *prog_data = brw->wm.prog_data;
39 uint32_t dw1 = 0;
40
41 dw1 |= GEN8_PSX_PIXEL_SHADER_VALID;
42
43 if (fp->program.UsesKill)
44 dw1 |= GEN8_PSX_KILL_ENABLE;
45
46 if (prog_data->num_varying_inputs != 0)
47 dw1 |= GEN8_PSX_ATTRIBUTE_ENABLE;
48
49 if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
50 switch (fp->program.FragDepthLayout) {
51 case FRAG_DEPTH_LAYOUT_NONE:
52 case FRAG_DEPTH_LAYOUT_ANY:
53 dw1 |= GEN8_PSX_PSCDEPTH_ON;
54 break;
55 case FRAG_DEPTH_LAYOUT_GREATER:
56 dw1 |= GEN8_PSX_PSCDEPTH_ON_GE;
57 break;
58 case FRAG_DEPTH_LAYOUT_LESS:
59 dw1 |= GEN8_PSX_PSCDEPTH_ON_LE;
60 break;
61 case FRAG_DEPTH_LAYOUT_UNCHANGED:
62 break;
63 }
64 }
65
66 if (fp->program.Base.InputsRead & VARYING_BIT_POS)
67 dw1 |= GEN8_PSX_USES_SOURCE_DEPTH | GEN8_PSX_USES_SOURCE_W;
68
69 /* BRW_NEW_NUM_SAMPLES | _NEW_MULTISAMPLE */
70 bool multisampled_fbo = brw->num_samples > 1;
71 if (multisampled_fbo &&
72 _mesa_get_min_invocations_per_fragment(ctx, &fp->program, false) > 1)
73 dw1 |= GEN8_PSX_SHADER_IS_PER_SAMPLE;
74
75 if (fp->program.Base.SystemValuesRead & SYSTEM_BIT_SAMPLE_MASK_IN)
76 dw1 |= GEN8_PSX_SHADER_USES_INPUT_COVERAGE_MASK;
77
78 if (prog_data->uses_omask)
79 dw1 |= GEN8_PSX_OMASK_TO_RENDER_TARGET;
80
81 BEGIN_BATCH(2);
82 OUT_BATCH(_3DSTATE_PS_EXTRA << 16 | (2 - 2));
83 OUT_BATCH(dw1);
84 ADVANCE_BATCH();
85 }
86
87 const struct brw_tracked_state gen8_ps_extra = {
88 .dirty = {
89 .mesa = _NEW_MULTISAMPLE,
90 .brw = BRW_NEW_CONTEXT | BRW_NEW_FRAGMENT_PROGRAM | BRW_NEW_NUM_SAMPLES,
91 .cache = CACHE_NEW_WM_PROG,
92 },
93 .emit = upload_ps_extra,
94 };
95
96 static void
97 upload_wm_state(struct brw_context *brw)
98 {
99 struct gl_context *ctx = &brw->ctx;
100 uint32_t dw1 = 0;
101
102 dw1 |= GEN7_WM_STATISTICS_ENABLE;
103 dw1 |= GEN7_WM_LINE_AA_WIDTH_1_0;
104 dw1 |= GEN7_WM_LINE_END_CAP_AA_WIDTH_0_5;
105 dw1 |= GEN7_WM_POINT_RASTRULE_UPPER_RIGHT;
106
107 /* _NEW_LINE */
108 if (ctx->Line.StippleFlag)
109 dw1 |= GEN7_WM_LINE_STIPPLE_ENABLE;
110
111 /* _NEW_POLYGON */
112 if (ctx->Polygon.StippleFlag)
113 dw1 |= GEN7_WM_POLYGON_STIPPLE_ENABLE;
114
115 /* CACHE_NEW_WM_PROG */
116 dw1 |= brw->wm.prog_data->barycentric_interp_modes <<
117 GEN7_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT;
118
119 BEGIN_BATCH(2);
120 OUT_BATCH(_3DSTATE_WM << 16 | (2 - 2));
121 OUT_BATCH(dw1);
122 ADVANCE_BATCH();
123 }
124
125 const struct brw_tracked_state gen8_wm_state = {
126 .dirty = {
127 .mesa = _NEW_LINE | _NEW_POLYGON,
128 .brw = BRW_NEW_CONTEXT,
129 .cache = CACHE_NEW_WM_PROG,
130 },
131 .emit = upload_wm_state,
132 };
133
134 static void
135 upload_ps_state(struct brw_context *brw)
136 {
137 struct gl_context *ctx = &brw->ctx;
138 uint32_t dw3 = 0, dw6 = 0, dw7 = 0, ksp0, ksp2 = 0;
139
140 /* CACHE_NEW_WM_PROG */
141 const struct brw_wm_prog_data *prog_data = brw->wm.prog_data;
142
143 /* Initialize the execution mask with VMask. Otherwise, derivatives are
144 * incorrect for subspans where some of the pixels are unlit. We believe
145 * the bit just didn't take effect in previous generations.
146 */
147 dw3 |= GEN7_PS_VECTOR_MASK_ENABLE;
148
149 dw3 |=
150 (ALIGN(brw->wm.base.sampler_count, 4) / 4) << GEN7_PS_SAMPLER_COUNT_SHIFT;
151
152 /* CACHE_NEW_WM_PROG */
153 dw3 |=
154 ((prog_data->base.binding_table.size_bytes / 4) <<
155 GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT);
156
157 /* Use ALT floating point mode for ARB fragment programs, because they
158 * require 0^0 == 1. Even though _CurrentFragmentProgram is used for
159 * rendering, CurrentFragmentProgram is used for this check to
160 * differentiate between the GLSL and non-GLSL cases.
161 */
162 if (ctx->Shader.CurrentProgram[MESA_SHADER_FRAGMENT] == NULL)
163 dw3 |= GEN7_PS_FLOATING_POINT_MODE_ALT;
164
165 /* 3DSTATE_PS expects the number of threads per PSD, which is always 64;
166 * it implicitly scales for different GT levels (which have some # of PSDs).
167 */
168 dw6 |= (64 - 2) << HSW_PS_MAX_THREADS_SHIFT;
169
170 if (prog_data->base.nr_params > 0)
171 dw6 |= GEN7_PS_PUSH_CONSTANT_ENABLE;
172
173 /* From the documentation for this packet:
174 * "If the PS kernel does not need the Position XY Offsets to
175 * compute a Position Value, then this field should be programmed
176 * to POSOFFSET_NONE."
177 *
178 * "SW Recommendation: If the PS kernel needs the Position Offsets
179 * to compute a Position XY value, this field should match Position
180 * ZW Interpolation Mode to ensure a consistent position.xyzw
181 * computation."
182 *
183 * We only require XY sample offsets. So, this recommendation doesn't
184 * look useful at the moment. We might need this in future.
185 */
186 if (brw->wm.prog_data->uses_pos_offset)
187 dw6 |= GEN7_PS_POSOFFSET_SAMPLE;
188 else
189 dw6 |= GEN7_PS_POSOFFSET_NONE;
190
191 dw6 |= brw->wm.fast_clear_op;
192
193 /* _NEW_MULTISAMPLE
194 * In case of non 1x per sample shading, only one of SIMD8 and SIMD16
195 * should be enabled. We do 'SIMD16 only' dispatch if a SIMD16 shader
196 * is successfully compiled. In majority of the cases that bring us
197 * better performance than 'SIMD8 only' dispatch.
198 */
199 int min_invocations_per_fragment =
200 _mesa_get_min_invocations_per_fragment(ctx, brw->fragment_program, false);
201 assert(min_invocations_per_fragment >= 1);
202
203 if (prog_data->prog_offset_16 || prog_data->no_8) {
204 dw6 |= GEN7_PS_16_DISPATCH_ENABLE;
205 if (!prog_data->no_8 && min_invocations_per_fragment == 1) {
206 dw6 |= GEN7_PS_8_DISPATCH_ENABLE;
207 dw7 |= (prog_data->base.dispatch_grf_start_reg <<
208 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
209 dw7 |= (prog_data->dispatch_grf_start_reg_16 <<
210 GEN7_PS_DISPATCH_START_GRF_SHIFT_2);
211 ksp0 = brw->wm.base.prog_offset;
212 ksp2 = brw->wm.base.prog_offset + prog_data->prog_offset_16;
213 } else {
214 dw7 |= (prog_data->dispatch_grf_start_reg_16 <<
215 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
216
217 ksp0 = brw->wm.base.prog_offset + prog_data->prog_offset_16;
218 }
219 } else {
220 dw6 |= GEN7_PS_8_DISPATCH_ENABLE;
221 dw7 |= (prog_data->base.dispatch_grf_start_reg <<
222 GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
223 ksp0 = brw->wm.base.prog_offset;
224 }
225
226 BEGIN_BATCH(12);
227 OUT_BATCH(_3DSTATE_PS << 16 | (12 - 2));
228 OUT_BATCH(ksp0);
229 OUT_BATCH(0);
230 OUT_BATCH(dw3);
231 if (prog_data->base.total_scratch) {
232 OUT_RELOC64(brw->wm.base.scratch_bo,
233 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
234 ffs(prog_data->base.total_scratch) - 11);
235 } else {
236 OUT_BATCH(0);
237 OUT_BATCH(0);
238 }
239 OUT_BATCH(dw6);
240 OUT_BATCH(dw7);
241 OUT_BATCH(0); /* kernel 1 pointer */
242 OUT_BATCH(0);
243 OUT_BATCH(ksp2);
244 OUT_BATCH(0);
245 ADVANCE_BATCH();
246 }
247
248 const struct brw_tracked_state gen8_ps_state = {
249 .dirty = {
250 .mesa = _NEW_MULTISAMPLE,
251 .brw = BRW_NEW_FRAGMENT_PROGRAM |
252 BRW_NEW_BATCH,
253 .cache = CACHE_NEW_WM_PROG
254 },
255 .emit = upload_ps_state,
256 };