1 /*
2 * Copyright © 2006-2022 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 "brw_fs.h"
25 #include "brw_fs_builder.h"
26
27 using namespace brw;
28
vs_thread_payload(const fs_visitor & v)29 vs_thread_payload::vs_thread_payload(const fs_visitor &v)
30 {
31 unsigned r = 0;
32
33 /* R0: Thread header. */
34 r += reg_unit(v.devinfo);
35
36 /* R1: URB handles. */
37 urb_handles = brw_ud8_grf(r, 0);
38 r += reg_unit(v.devinfo);
39
40 num_regs = r;
41 }
42
tcs_thread_payload(const fs_visitor & v)43 tcs_thread_payload::tcs_thread_payload(const fs_visitor &v)
44 {
45 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(v.prog_data);
46 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(v.prog_data);
47 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) v.key;
48
49 if (vue_prog_data->dispatch_mode == INTEL_DISPATCH_MODE_TCS_SINGLE_PATCH) {
50 patch_urb_output = brw_ud1_grf(0, 0);
51 primitive_id = brw_vec1_grf(0, 1);
52
53 /* r1-r4 contain the ICP handles. */
54 icp_handle_start = brw_ud8_grf(1, 0);
55
56 num_regs = 5;
57 } else {
58 assert(vue_prog_data->dispatch_mode == INTEL_DISPATCH_MODE_TCS_MULTI_PATCH);
59 assert(tcs_key->input_vertices <= BRW_MAX_TCS_INPUT_VERTICES);
60
61 unsigned r = 0;
62
63 r += reg_unit(v.devinfo);
64
65 patch_urb_output = brw_ud8_grf(r, 0);
66 r += reg_unit(v.devinfo);
67
68 if (tcs_prog_data->include_primitive_id) {
69 primitive_id = brw_vec8_grf(r, 0);
70 r += reg_unit(v.devinfo);
71 }
72
73 /* ICP handles occupy the next 1-32 registers. */
74 icp_handle_start = brw_ud8_grf(r, 0);
75 r += brw_tcs_prog_key_input_vertices(tcs_key) * reg_unit(v.devinfo);
76
77 num_regs = r;
78 }
79 }
80
tes_thread_payload(const fs_visitor & v)81 tes_thread_payload::tes_thread_payload(const fs_visitor &v)
82 {
83 unsigned r = 0;
84
85 /* R0: Thread Header. */
86 patch_urb_input = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
87 primitive_id = brw_vec1_grf(0, 1);
88 r += reg_unit(v.devinfo);
89
90 /* R1-3: gl_TessCoord.xyz. */
91 for (unsigned i = 0; i < 3; i++) {
92 coords[i] = brw_vec8_grf(r, 0);
93 r += reg_unit(v.devinfo);
94 }
95
96 /* R4: URB output handles. */
97 urb_output = brw_ud8_grf(r, 0);
98 r += reg_unit(v.devinfo);
99
100 num_regs = r;
101 }
102
gs_thread_payload(fs_visitor & v)103 gs_thread_payload::gs_thread_payload(fs_visitor &v)
104 {
105 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(v.prog_data);
106 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(v.prog_data);
107 const fs_builder bld = fs_builder(&v).at_end();
108
109 /* R0: thread header. */
110 unsigned r = reg_unit(v.devinfo);
111
112 /* R1: output URB handles. */
113 urb_handles = bld.vgrf(BRW_REGISTER_TYPE_UD);
114 bld.AND(urb_handles, brw_ud8_grf(r, 0),
115 v.devinfo->ver >= 20 ? brw_imm_ud(0xFFFFFF) : brw_imm_ud(0xFFFF));
116
117 /* R1: Instance ID stored in bits 31:27 */
118 instance_id = bld.vgrf(BRW_REGISTER_TYPE_UD);
119 bld.SHR(instance_id, brw_ud8_grf(r, 0), brw_imm_ud(27u));
120
121 r += reg_unit(v.devinfo);
122
123 if (gs_prog_data->include_primitive_id) {
124 primitive_id = brw_ud8_grf(r, 0);
125 r += reg_unit(v.devinfo);
126 }
127
128 /* Always enable VUE handles so we can safely use pull model if needed.
129 *
130 * The push model for a GS uses a ton of register space even for trivial
131 * scenarios with just a few inputs, so just make things easier and a bit
132 * safer by always having pull model available.
133 */
134 gs_prog_data->base.include_vue_handles = true;
135
136 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
137 icp_handle_start = brw_ud8_grf(r, 0);
138 r += v.nir->info.gs.vertices_in * reg_unit(v.devinfo);
139
140 num_regs = r;
141
142 /* Use a maximum of 24 registers for push-model inputs. */
143 const unsigned max_push_components = 24;
144
145 /* If pushing our inputs would take too many registers, reduce the URB read
146 * length (which is in HWords, or 8 registers), and resort to pulling.
147 *
148 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
149 * have to multiply by VerticesIn to obtain the total storage requirement.
150 */
151 if (8 * vue_prog_data->urb_read_length * v.nir->info.gs.vertices_in >
152 max_push_components) {
153 vue_prog_data->urb_read_length =
154 ROUND_DOWN_TO(max_push_components / v.nir->info.gs.vertices_in, 8) / 8;
155 }
156 }
157
158 static inline void
setup_fs_payload_gfx20(fs_thread_payload & payload,const fs_visitor & v,bool & source_depth_to_render_target)159 setup_fs_payload_gfx20(fs_thread_payload &payload,
160 const fs_visitor &v,
161 bool &source_depth_to_render_target)
162 {
163 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(v.prog_data);
164 const unsigned payload_width = 16;
165 assert(v.dispatch_width % payload_width == 0);
166 assert(v.devinfo->ver >= 20);
167
168 for (unsigned j = 0; j < v.dispatch_width / payload_width; j++) {
169 /* R0-1: PS thread payload header, masks and pixel X/Y coordinates. */
170 payload.num_regs++;
171 payload.subspan_coord_reg[j] = payload.num_regs++;
172 }
173
174 for (unsigned j = 0; j < v.dispatch_width / payload_width; j++) {
175 /* R2-13: Barycentric interpolation coordinates. These appear
176 * in the same order that they appear in the brw_barycentric_mode
177 * enum. Each set of coordinates occupies 2 64B registers per
178 * SIMD16 half. Coordinates only appear if they were enabled
179 * using the "Barycentric Interpolation Mode" bits in WM_STATE.
180 */
181 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
182 if (prog_data->barycentric_interp_modes & (1 << i)) {
183 payload.barycentric_coord_reg[i][j] = payload.num_regs;
184 payload.num_regs += payload_width / 4;
185 }
186 }
187
188 /* R14: Interpolated depth if "Pixel Shader Uses Source Depth" is set. */
189 if (prog_data->uses_src_depth) {
190 payload.source_depth_reg[j] = payload.num_regs;
191 payload.num_regs += payload_width / 8;
192 }
193
194 /* R15: Interpolated W if "Pixel Shader Uses Source W" is set. */
195 if (prog_data->uses_src_w) {
196 payload.source_w_reg[j] = payload.num_regs;
197 payload.num_regs += payload_width / 8;
198 }
199
200 /* R16: MSAA input coverage mask if "Pixel Shader Uses Input
201 * Coverage Mask" is set.
202 */
203 if (prog_data->uses_sample_mask) {
204 payload.sample_mask_in_reg[j] = payload.num_regs;
205 payload.num_regs += payload_width / 8;
206 }
207
208 /* R19: MSAA position XY offsets if "Position XY Offset Select"
209 * is either POSOFFSET_CENTROID or POSOFFSET_SAMPLE. Note that
210 * this is delivered as a single SIMD32 vector, inconsistently
211 * with most other PS payload fields.
212 */
213 if (prog_data->uses_pos_offset && j == 0) {
214 for (unsigned k = 0; k < 2; k++) {
215 payload.sample_pos_reg[k] = payload.num_regs;
216 payload.num_regs++;
217 }
218 }
219 }
220
221 if (prog_data->uses_depth_w_coefficients) {
222 assert(v.max_polygons == 1);
223 payload.depth_w_coef_reg = payload.num_regs;
224 payload.num_regs += 2;
225 }
226
227 if (v.nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
228 source_depth_to_render_target = true;
229 }
230 }
231
232 static inline void
setup_fs_payload_gfx9(fs_thread_payload & payload,const fs_visitor & v,bool & source_depth_to_render_target)233 setup_fs_payload_gfx9(fs_thread_payload &payload,
234 const fs_visitor &v,
235 bool &source_depth_to_render_target)
236 {
237 struct brw_wm_prog_data *prog_data = brw_wm_prog_data(v.prog_data);
238
239 const unsigned payload_width = MIN2(16, v.dispatch_width);
240 assert(v.dispatch_width % payload_width == 0);
241 assert(v.devinfo->ver < 20);
242
243 payload.num_regs = 0;
244
245 /* R0: PS thread payload header. */
246 payload.num_regs++;
247
248 for (unsigned j = 0; j < v.dispatch_width / payload_width; j++) {
249 /* R1: masks, pixel X/Y coordinates. */
250 payload.subspan_coord_reg[j] = payload.num_regs++;
251 }
252
253 for (unsigned j = 0; j < v.dispatch_width / payload_width; j++) {
254 /* R3-26: barycentric interpolation coordinates. These appear in the
255 * same order that they appear in the brw_barycentric_mode enum. Each
256 * set of coordinates occupies 2 registers if dispatch width == 8 and 4
257 * registers if dispatch width == 16. Coordinates only appear if they
258 * were enabled using the "Barycentric Interpolation Mode" bits in
259 * WM_STATE.
260 */
261 for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {
262 if (prog_data->barycentric_interp_modes & (1 << i)) {
263 payload.barycentric_coord_reg[i][j] = payload.num_regs;
264 payload.num_regs += payload_width / 4;
265 }
266 }
267
268 /* R27-28: interpolated depth if uses source depth */
269 if (prog_data->uses_src_depth) {
270 payload.source_depth_reg[j] = payload.num_regs;
271 payload.num_regs += payload_width / 8;
272 }
273
274 /* R29-30: interpolated W set if GFX6_WM_USES_SOURCE_W. */
275 if (prog_data->uses_src_w) {
276 payload.source_w_reg[j] = payload.num_regs;
277 payload.num_regs += payload_width / 8;
278 }
279
280 /* R31: MSAA position offsets. */
281 if (prog_data->uses_pos_offset) {
282 payload.sample_pos_reg[j] = payload.num_regs;
283 payload.num_regs++;
284 }
285
286 /* R32-33: MSAA input coverage mask */
287 if (prog_data->uses_sample_mask) {
288 payload.sample_mask_in_reg[j] = payload.num_regs;
289 payload.num_regs += payload_width / 8;
290 }
291 }
292
293 /* R66: Source Depth and/or W Attribute Vertex Deltas */
294 if (prog_data->uses_depth_w_coefficients) {
295 assert(v.max_polygons == 1);
296 payload.depth_w_coef_reg = payload.num_regs;
297 payload.num_regs++;
298 }
299
300 if (v.nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
301 source_depth_to_render_target = true;
302 }
303 }
304
fs_thread_payload(const fs_visitor & v,bool & source_depth_to_render_target)305 fs_thread_payload::fs_thread_payload(const fs_visitor &v,
306 bool &source_depth_to_render_target)
307 : subspan_coord_reg(),
308 source_depth_reg(),
309 source_w_reg(),
310 aa_dest_stencil_reg(),
311 dest_depth_reg(),
312 sample_pos_reg(),
313 sample_mask_in_reg(),
314 depth_w_coef_reg(),
315 barycentric_coord_reg()
316 {
317 if (v.devinfo->ver >= 20)
318 setup_fs_payload_gfx20(*this, v, source_depth_to_render_target);
319 else
320 setup_fs_payload_gfx9(*this, v, source_depth_to_render_target);
321 }
322
cs_thread_payload(const fs_visitor & v)323 cs_thread_payload::cs_thread_payload(const fs_visitor &v)
324 {
325 struct brw_cs_prog_data *prog_data = brw_cs_prog_data(v.prog_data);
326
327 unsigned r = reg_unit(v.devinfo);
328
329 /* See nir_setup_uniforms for subgroup_id in earlier versions. */
330 if (v.devinfo->verx10 >= 125) {
331 subgroup_id_ = brw_ud1_grf(0, 2);
332
333 for (int i = 0; i < 3; i++) {
334 if (prog_data->generate_local_id & (1 << i)) {
335 local_invocation_id[i] = brw_uw8_grf(r, 0);
336 r += reg_unit(v.devinfo);
337 if (v.devinfo->ver < 20 && v.dispatch_width == 32)
338 r += reg_unit(v.devinfo);
339 } else {
340 local_invocation_id[i] = brw_imm_uw(0);
341 }
342 }
343
344 /* TODO: Fill out uses_btd_stack_ids automatically */
345 if (prog_data->uses_btd_stack_ids)
346 r += reg_unit(v.devinfo);
347 }
348
349 num_regs = r;
350 }
351
352 void
load_subgroup_id(const fs_builder & bld,fs_reg & dest) const353 cs_thread_payload::load_subgroup_id(const fs_builder &bld,
354 fs_reg &dest) const
355 {
356 auto devinfo = bld.shader->devinfo;
357 dest = retype(dest, BRW_REGISTER_TYPE_UD);
358
359 if (subgroup_id_.file != BAD_FILE) {
360 assert(devinfo->verx10 >= 125);
361 bld.AND(dest, subgroup_id_, brw_imm_ud(INTEL_MASK(7, 0)));
362 } else {
363 assert(devinfo->verx10 < 125);
364 assert(gl_shader_stage_is_compute(bld.shader->stage));
365 int index = brw_get_subgroup_id_param_index(devinfo,
366 bld.shader->stage_prog_data);
367 bld.MOV(dest, fs_reg(UNIFORM, index, BRW_REGISTER_TYPE_UD));
368 }
369 }
370
task_mesh_thread_payload(fs_visitor & v)371 task_mesh_thread_payload::task_mesh_thread_payload(fs_visitor &v)
372 : cs_thread_payload(v)
373 {
374 /* Task and Mesh Shader Payloads (SIMD8 and SIMD16)
375 *
376 * R0: Header
377 * R1: Local_ID.X[0-7 or 0-15]
378 * R2: Inline Parameter
379 *
380 * Task and Mesh Shader Payloads (SIMD32)
381 *
382 * R0: Header
383 * R1: Local_ID.X[0-15]
384 * R2: Local_ID.X[16-31]
385 * R3: Inline Parameter
386 *
387 * Local_ID.X values are 16 bits.
388 *
389 * Inline parameter is optional but always present since we use it to pass
390 * the address to descriptors.
391 */
392
393 const fs_builder bld = fs_builder(&v).at_end();
394
395 unsigned r = 0;
396 assert(subgroup_id_.file != BAD_FILE);
397 extended_parameter_0 = retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD);
398
399 if (v.devinfo->ver >= 20) {
400 urb_output = brw_ud1_grf(1, 0);
401 } else {
402 urb_output = bld.vgrf(BRW_REGISTER_TYPE_UD);
403 /* In both mesh and task shader payload, lower 16 bits of g0.6 is
404 * an offset within Slice's Local URB, which says where shader is
405 * supposed to output its data.
406 */
407 bld.AND(urb_output, brw_ud1_grf(0, 6), brw_imm_ud(0xFFFF));
408 }
409
410 if (v.stage == MESA_SHADER_MESH) {
411 /* g0.7 is Task Shader URB Entry Offset, which contains both an offset
412 * within Slice's Local USB (bits 0:15) and a slice selector
413 * (bits 16:24). Slice selector can be non zero when mesh shader
414 * is spawned on slice other than the one where task shader was run.
415 * Bit 24 says that Slice ID is present and bits 16:23 is the Slice ID.
416 */
417 task_urb_input = brw_ud1_grf(0, 7);
418 }
419 r += reg_unit(v.devinfo);
420
421 local_index = brw_uw8_grf(r, 0);
422 r += reg_unit(v.devinfo);
423 if (v.devinfo->ver < 20 && v.dispatch_width == 32)
424 r += reg_unit(v.devinfo);
425
426 inline_parameter = brw_ud1_grf(r, 0);
427 r += reg_unit(v.devinfo);
428
429 num_regs = r;
430 }
431
bs_thread_payload(const fs_visitor & v)432 bs_thread_payload::bs_thread_payload(const fs_visitor &v)
433 {
434 unsigned r = 0;
435
436 /* R0: Thread header. */
437 r += reg_unit(v.devinfo);
438
439 /* R1: Stack IDs. */
440 r += reg_unit(v.devinfo);
441
442 /* R2: Inline Parameter. Used for argument addresses. */
443 global_arg_ptr = brw_ud1_grf(r, 0);
444 local_arg_ptr = brw_ud1_grf(r, 2);
445 r += reg_unit(v.devinfo);
446
447 num_regs = r;
448 }
449
450 void
load_shader_type(const fs_builder & bld,fs_reg & dest) const451 bs_thread_payload::load_shader_type(const fs_builder &bld, fs_reg &dest) const
452 {
453 fs_reg ud_dest = retype(dest, BRW_REGISTER_TYPE_UD);
454 bld.MOV(ud_dest, retype(brw_vec1_grf(0, 3), ud_dest.type));
455 bld.AND(ud_dest, ud_dest, brw_imm_ud(0xf));
456 }
457