• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2010 - 2015 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 #pragma once
25 
26 #include <stdio.h>
27 #include "c11/threads.h"
28 #include "dev/intel_device_info.h"
29 #include "isl/isl.h"
30 #include "util/macros.h"
31 #include "util/mesa-sha1.h"
32 #include "util/enum_operators.h"
33 #include "util/ralloc.h"
34 #include "util/u_math.h"
35 #include "util/u_printf.h"
36 #include "brw_isa_info.h"
37 #include "intel_shader_enums.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 struct ra_regs;
44 struct nir_shader;
45 struct shader_info;
46 
47 struct nir_shader_compiler_options;
48 typedef struct nir_shader nir_shader;
49 
50 #define REG_CLASS_COUNT 20
51 
52 struct brw_compiler {
53    const struct intel_device_info *devinfo;
54 
55    /* This lock must be taken if the compiler is to be modified in any way,
56     * including adding something to the ralloc child list.
57     */
58    mtx_t mutex;
59 
60    struct brw_isa_info isa;
61 
62    struct {
63       struct ra_regs *regs;
64 
65       /**
66        * Array of the ra classes for the unaligned contiguous register
67        * block sizes used, indexed by register size.
68        */
69       struct ra_class *classes[REG_CLASS_COUNT];
70    } fs_reg_set;
71 
72    void (*shader_debug_log)(void *, unsigned *id, const char *str, ...) PRINTFLIKE(3, 4);
73    void (*shader_perf_log)(void *, unsigned *id, const char *str, ...) PRINTFLIKE(3, 4);
74 
75    bool use_tcs_multi_patch;
76    struct nir_shader_compiler_options *nir_options[MESA_ALL_SHADER_STAGES];
77 
78    /**
79     * Apply workarounds for SIN and COS output range problems.
80     * This can negatively impact performance.
81     */
82    bool precise_trig;
83 
84    /**
85     * Whether indirect UBO loads should use the sampler or go through the
86     * data/constant cache.  For the sampler, UBO surface states have to be set
87     * up with VK_FORMAT_R32G32B32A32_FLOAT whereas if it's going through the
88     * constant or data cache, UBOs must use VK_FORMAT_RAW.
89     */
90    bool indirect_ubos_use_sampler;
91 
92    /**
93     * Gfx12.5+ has a bit in the SEND instruction extending the bindless
94     * surface offset range from 20 to 26 bits, effectively giving us 4Gb of
95     * bindless surface descriptors instead of 64Mb previously.
96     */
97    bool extended_bindless_surface_offset;
98 
99    /**
100     * Gfx11+ has a bit in the dword 3 of the sampler message header that
101     * indicates whether the sampler handle is relative to the dynamic state
102     * base address (0) or the bindless sampler base address (1). The driver
103     * can select this.
104     */
105    bool use_bindless_sampler_offset;
106 
107    /**
108     * Should DPAS instructions be lowered?
109     *
110     * This will be set for all platforms before Gfx12.5. It may also be set
111     * platforms that support DPAS for testing purposes.
112     */
113    bool lower_dpas;
114 
115    /**
116     * Calling the ra_allocate function after each register spill can take
117     * several minutes. This option speeds up shader compilation by spilling
118     * more registers after the ra_allocate failure. Required for
119     * Cyberpunk 2077, which uses a watchdog thread to terminate the process
120     * in case the render thread hasn't responded within 2 minutes.
121     */
122    int spilling_rate;
123 
124    struct nir_shader *clc_shader;
125 
126    struct {
127       unsigned mue_header_packing;
128       bool mue_compaction;
129    } mesh;
130 };
131 
132 #define brw_shader_debug_log(compiler, data, fmt, ... ) do {    \
133    static unsigned id = 0;                                      \
134    compiler->shader_debug_log(data, &id, fmt, ##__VA_ARGS__);   \
135 } while (0)
136 
137 #define brw_shader_perf_log(compiler, data, fmt, ... ) do {     \
138    static unsigned id = 0;                                      \
139    compiler->shader_perf_log(data, &id, fmt, ##__VA_ARGS__);    \
140 } while (0)
141 
142 /**
143  * We use a constant subgroup size of 32.  It really only needs to be a
144  * maximum and, since we do SIMD32 for compute shaders in some cases, it
145  * needs to be at least 32.  SIMD8 and SIMD16 shaders will still claim a
146  * subgroup size of 32 but will act as if 16 or 24 of those channels are
147  * disabled.
148  */
149 #define BRW_SUBGROUP_SIZE 32
150 
151 static inline bool
brw_shader_stage_is_bindless(gl_shader_stage stage)152 brw_shader_stage_is_bindless(gl_shader_stage stage)
153 {
154    return stage >= MESA_SHADER_RAYGEN &&
155           stage <= MESA_SHADER_CALLABLE;
156 }
157 
158 static inline bool
brw_shader_stage_requires_bindless_resources(gl_shader_stage stage)159 brw_shader_stage_requires_bindless_resources(gl_shader_stage stage)
160 {
161    return brw_shader_stage_is_bindless(stage) || gl_shader_stage_is_mesh(stage);
162 }
163 
164 static inline bool
brw_shader_stage_has_inline_data(const struct intel_device_info * devinfo,gl_shader_stage stage)165 brw_shader_stage_has_inline_data(const struct intel_device_info *devinfo,
166                                  gl_shader_stage stage)
167 {
168    return stage == MESA_SHADER_MESH || stage == MESA_SHADER_TASK ||
169           (stage == MESA_SHADER_COMPUTE && devinfo->verx10 >= 125);
170 }
171 
172 /**
173  * Program key structures.
174  *
175  * When drawing, we look for the currently bound shaders in the program
176  * cache.  This is essentially a hash table lookup, and these are the keys.
177  *
178  * Sometimes OpenGL features specified as state need to be simulated via
179  * shader code, due to a mismatch between the API and the hardware.  This
180  * is often referred to as "non-orthagonal state" or "NOS".  We store NOS
181  * in the program key so it's considered when searching for a program.  If
182  * we haven't seen a particular combination before, we have to recompile a
183  * new specialized version.
184  *
185  * Shader compilation should not look up state in gl_context directly, but
186  * instead use the copy in the program key.  This guarantees recompiles will
187  * happen correctly.
188  *
189  *  @{
190  */
191 
192 #define BRW_MAX_SAMPLERS 32
193 
194 /* Provide explicit padding for each member, to ensure that the compiler
195  * initializes every bit in the shader cache keys.  The keys will be compared
196  * with memcmp.
197  */
198 PRAGMA_DIAGNOSTIC_PUSH
199 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
200 
201 enum brw_robustness_flags {
202    BRW_ROBUSTNESS_UBO  = BITFIELD_BIT(0),
203    BRW_ROBUSTNESS_SSBO = BITFIELD_BIT(1),
204 };
205 
206 struct brw_base_prog_key {
207    unsigned program_string_id;
208 
209    enum brw_robustness_flags robust_flags:2;
210 
211    unsigned padding:22;
212 
213    /**
214     * Apply workarounds for SIN and COS input range problems.
215     * This limits input range for SIN and COS to [-2p : 2p] to
216     * avoid precision issues.
217     */
218    bool limit_trig_input_range;
219 };
220 
221 /**
222  * OpenGL attribute slots fall in [0, VERT_ATTRIB_MAX - 1] with the range
223  * [VERT_ATTRIB_GENERIC0, VERT_ATTRIB_MAX - 1] reserved for up to 16 user
224  * input vertex attributes. In Vulkan, we expose up to 28 user vertex input
225  * attributes that are mapped to slots also starting at VERT_ATTRIB_GENERIC0.
226  */
227 #define MAX_GL_VERT_ATTRIB     VERT_ATTRIB_MAX
228 #define MAX_VK_VERT_ATTRIB     (VERT_ATTRIB_GENERIC0 + 28)
229 
230 /** The program key for Vertex Shaders. */
231 struct brw_vs_prog_key {
232    struct brw_base_prog_key base;
233 };
234 
235 /** The program key for Tessellation Control Shaders. */
236 struct brw_tcs_prog_key
237 {
238    struct brw_base_prog_key base;
239 
240    /** A bitfield of per-vertex outputs written. */
241    uint64_t outputs_written;
242 
243    enum tess_primitive_mode _tes_primitive_mode;
244 
245    /** Number of input vertices, 0 means dynamic */
246    unsigned input_vertices;
247 
248    /** A bitfield of per-patch outputs written. */
249    uint32_t patch_outputs_written;
250 
251    uint32_t padding;
252 };
253 
254 #define BRW_MAX_TCS_INPUT_VERTICES (32)
255 
256 static inline uint32_t
brw_tcs_prog_key_input_vertices(const struct brw_tcs_prog_key * key)257 brw_tcs_prog_key_input_vertices(const struct brw_tcs_prog_key *key)
258 {
259    return key->input_vertices != 0 ?
260           key->input_vertices : BRW_MAX_TCS_INPUT_VERTICES;
261 }
262 
263 /** The program key for Tessellation Evaluation Shaders. */
264 struct brw_tes_prog_key
265 {
266    struct brw_base_prog_key base;
267 
268    /** A bitfield of per-vertex inputs read. */
269    uint64_t inputs_read;
270 
271    /** A bitfield of per-patch inputs read. */
272    uint32_t patch_inputs_read;
273 
274    uint32_t padding;
275 };
276 
277 /** The program key for Geometry Shaders. */
278 struct brw_gs_prog_key
279 {
280    struct brw_base_prog_key base;
281 };
282 
283 struct brw_task_prog_key
284 {
285    struct brw_base_prog_key base;
286 };
287 
288 struct brw_mesh_prog_key
289 {
290    struct brw_base_prog_key base;
291 
292    bool compact_mue:1;
293    unsigned padding:31;
294 };
295 
296 /** The program key for Fragment/Pixel Shaders. */
297 struct brw_wm_prog_key {
298    struct brw_base_prog_key base;
299 
300    uint64_t input_slots_valid;
301    uint8_t color_outputs_valid;
302 
303    /* Some collection of BRW_WM_IZ_* */
304    bool flat_shade:1;
305    unsigned nr_color_regions:5;
306    bool alpha_test_replicate_alpha:1;
307    enum intel_sometimes alpha_to_coverage:2;
308    bool clamp_fragment_color:1;
309 
310    bool force_dual_color_blend:1;
311 
312    /** Whether or inputs are interpolated at sample rate by default
313     *
314     * This corresponds to the sample shading API bit in Vulkan or OpenGL which
315     * controls how inputs with no interpolation qualifier are interpolated.
316     * This is distinct from the way that using gl_SampleID or similar requires
317     * us to run per-sample.  Even when running per-sample due to gl_SampleID,
318     * we may still interpolate unqualified inputs at the pixel center.
319     */
320    enum intel_sometimes persample_interp:2;
321 
322    /* Whether or not we are running on a multisampled framebuffer */
323    enum intel_sometimes multisample_fbo:2;
324 
325    /* Whether the preceding shader stage is mesh */
326    enum intel_sometimes mesh_input:2;
327 
328    bool coherent_fb_fetch:1;
329    bool ignore_sample_mask_out:1;
330    bool coarse_pixel:1;
331    bool null_push_constant_tbimr_workaround:1;
332 
333    uint64_t padding:35;
334 };
335 
336 struct brw_cs_prog_key {
337    struct brw_base_prog_key base;
338 };
339 
340 struct brw_bs_prog_key {
341    struct brw_base_prog_key base;
342 
343    /* Represents enum enum brw_rt_ray_flags values given at pipeline creation
344     * to be combined with ray_flags handed to the traceRayEXT() calls by the
345     * shader.
346     */
347    uint32_t pipeline_ray_flags;
348 };
349 
350 /* brw_any_prog_key is any of the keys that map to an API stage */
351 union brw_any_prog_key {
352    struct brw_base_prog_key base;
353    struct brw_vs_prog_key vs;
354    struct brw_tcs_prog_key tcs;
355    struct brw_tes_prog_key tes;
356    struct brw_gs_prog_key gs;
357    struct brw_wm_prog_key wm;
358    struct brw_cs_prog_key cs;
359    struct brw_bs_prog_key bs;
360    struct brw_task_prog_key task;
361    struct brw_mesh_prog_key mesh;
362 };
363 
364 PRAGMA_DIAGNOSTIC_POP
365 
366 /** Max number of render targets in a shader */
367 #define BRW_MAX_DRAW_BUFFERS 8
368 
369 struct brw_ubo_range
370 {
371    uint16_t block;
372 
373    /* In units of 32-byte registers */
374    uint8_t start;
375    uint8_t length;
376 };
377 
378 /* We reserve the first 2^16 values for builtins */
379 #define BRW_PARAM_IS_BUILTIN(param) (((param) & 0xffff0000) == 0)
380 
381 enum brw_param_builtin {
382    BRW_PARAM_BUILTIN_ZERO,
383 
384    BRW_PARAM_BUILTIN_CLIP_PLANE_0_X,
385    BRW_PARAM_BUILTIN_CLIP_PLANE_0_Y,
386    BRW_PARAM_BUILTIN_CLIP_PLANE_0_Z,
387    BRW_PARAM_BUILTIN_CLIP_PLANE_0_W,
388    BRW_PARAM_BUILTIN_CLIP_PLANE_1_X,
389    BRW_PARAM_BUILTIN_CLIP_PLANE_1_Y,
390    BRW_PARAM_BUILTIN_CLIP_PLANE_1_Z,
391    BRW_PARAM_BUILTIN_CLIP_PLANE_1_W,
392    BRW_PARAM_BUILTIN_CLIP_PLANE_2_X,
393    BRW_PARAM_BUILTIN_CLIP_PLANE_2_Y,
394    BRW_PARAM_BUILTIN_CLIP_PLANE_2_Z,
395    BRW_PARAM_BUILTIN_CLIP_PLANE_2_W,
396    BRW_PARAM_BUILTIN_CLIP_PLANE_3_X,
397    BRW_PARAM_BUILTIN_CLIP_PLANE_3_Y,
398    BRW_PARAM_BUILTIN_CLIP_PLANE_3_Z,
399    BRW_PARAM_BUILTIN_CLIP_PLANE_3_W,
400    BRW_PARAM_BUILTIN_CLIP_PLANE_4_X,
401    BRW_PARAM_BUILTIN_CLIP_PLANE_4_Y,
402    BRW_PARAM_BUILTIN_CLIP_PLANE_4_Z,
403    BRW_PARAM_BUILTIN_CLIP_PLANE_4_W,
404    BRW_PARAM_BUILTIN_CLIP_PLANE_5_X,
405    BRW_PARAM_BUILTIN_CLIP_PLANE_5_Y,
406    BRW_PARAM_BUILTIN_CLIP_PLANE_5_Z,
407    BRW_PARAM_BUILTIN_CLIP_PLANE_5_W,
408    BRW_PARAM_BUILTIN_CLIP_PLANE_6_X,
409    BRW_PARAM_BUILTIN_CLIP_PLANE_6_Y,
410    BRW_PARAM_BUILTIN_CLIP_PLANE_6_Z,
411    BRW_PARAM_BUILTIN_CLIP_PLANE_6_W,
412    BRW_PARAM_BUILTIN_CLIP_PLANE_7_X,
413    BRW_PARAM_BUILTIN_CLIP_PLANE_7_Y,
414    BRW_PARAM_BUILTIN_CLIP_PLANE_7_Z,
415    BRW_PARAM_BUILTIN_CLIP_PLANE_7_W,
416 
417    BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X,
418    BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y,
419    BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Z,
420    BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_W,
421    BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X,
422    BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y,
423 
424    BRW_PARAM_BUILTIN_PATCH_VERTICES_IN,
425 
426    BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_X,
427    BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Y,
428    BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Z,
429    BRW_PARAM_BUILTIN_SUBGROUP_ID,
430    BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_X,
431    BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_Y,
432    BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_Z,
433    BRW_PARAM_BUILTIN_WORK_DIM,
434 };
435 
436 #define BRW_PARAM_BUILTIN_CLIP_PLANE(idx, comp) \
437    (BRW_PARAM_BUILTIN_CLIP_PLANE_0_X + ((idx) << 2) + (comp))
438 
439 #define BRW_PARAM_BUILTIN_IS_CLIP_PLANE(param)  \
440    ((param) >= BRW_PARAM_BUILTIN_CLIP_PLANE_0_X && \
441     (param) <= BRW_PARAM_BUILTIN_CLIP_PLANE_7_W)
442 
443 #define BRW_PARAM_BUILTIN_CLIP_PLANE_IDX(param) \
444    (((param) - BRW_PARAM_BUILTIN_CLIP_PLANE_0_X) >> 2)
445 
446 #define BRW_PARAM_BUILTIN_CLIP_PLANE_COMP(param) \
447    (((param) - BRW_PARAM_BUILTIN_CLIP_PLANE_0_X) & 0x3)
448 
449 #define BRW_MAX_EMBEDDED_SAMPLERS (4096)
450 
451 enum brw_shader_reloc_id {
452    BRW_SHADER_RELOC_CONST_DATA_ADDR_LOW,
453    BRW_SHADER_RELOC_CONST_DATA_ADDR_HIGH,
454    BRW_SHADER_RELOC_SHADER_START_OFFSET,
455    BRW_SHADER_RELOC_RESUME_SBT_ADDR_LOW,
456    BRW_SHADER_RELOC_RESUME_SBT_ADDR_HIGH,
457    BRW_SHADER_RELOC_DESCRIPTORS_ADDR_HIGH,
458    BRW_SHADER_RELOC_DESCRIPTORS_BUFFER_ADDR_HIGH,
459    BRW_SHADER_RELOC_EMBEDDED_SAMPLER_HANDLE,
460    BRW_SHADER_RELOC_LAST_EMBEDDED_SAMPLER_HANDLE =
461    BRW_SHADER_RELOC_EMBEDDED_SAMPLER_HANDLE + BRW_MAX_EMBEDDED_SAMPLERS - 1,
462    BRW_SHADER_RELOC_PRINTF_BUFFER_ADDR_LOW,
463    BRW_SHADER_RELOC_PRINTF_BUFFER_ADDR_HIGH,
464    BRW_SHADER_RELOC_PRINTF_BASE_IDENTIFIER,
465    BRW_SHADER_RELOC_PRINTF_BUFFER_SIZE,
466 };
467 
468 enum brw_shader_reloc_type {
469    /** An arbitrary 32-bit value */
470    BRW_SHADER_RELOC_TYPE_U32,
471    /** A MOV instruction with an immediate source */
472    BRW_SHADER_RELOC_TYPE_MOV_IMM,
473 };
474 
475 /** Represents a code relocation
476  *
477  * Relocatable constants are immediates in the code which we want to be able
478  * to replace post-compile with the actual value.
479  */
480 struct brw_shader_reloc {
481    /** The 32-bit ID of the relocatable constant */
482    uint32_t id;
483 
484    /** Type of this relocation */
485    enum brw_shader_reloc_type type;
486 
487    /** The offset in the shader to the relocated value
488     *
489     * For MOV_IMM relocs, this is an offset to the MOV instruction.  This
490     * allows us to do some sanity checking while we update the value.
491     */
492    uint32_t offset;
493 
494    /** Value to be added to the relocated value before it is written */
495    uint32_t delta;
496 };
497 
498 /** A value to write to a relocation */
499 struct brw_shader_reloc_value {
500    /** The 32-bit ID of the relocatable constant */
501    uint32_t id;
502 
503    /** The value with which to replace the relocated immediate */
504    uint32_t value;
505 };
506 
507 struct brw_stage_prog_data {
508    struct brw_ubo_range ubo_ranges[4];
509 
510    unsigned nr_params;       /**< number of float params/constants */
511 
512    gl_shader_stage stage;
513 
514    /* zero_push_reg is a bitfield which indicates what push registers (if any)
515     * should be zeroed by SW at the start of the shader.  The corresponding
516     * push_reg_mask_param specifies the param index (in 32-bit units) where
517     * the actual runtime 64-bit mask will be pushed.  The shader will zero
518     * push reg i if
519     *
520     *    reg_used & zero_push_reg & ~*push_reg_mask_param & (1ull << i)
521     *
522     * If this field is set, brw_compiler::compact_params must be false.
523     */
524    uint64_t zero_push_reg;
525    unsigned push_reg_mask_param;
526 
527    unsigned curb_read_length;
528    unsigned total_scratch;
529    unsigned total_shared;
530 
531    unsigned program_size;
532 
533    unsigned const_data_size;
534    unsigned const_data_offset;
535 
536    unsigned num_relocs;
537    const struct brw_shader_reloc *relocs;
538 
539    /** Does this program pull from any UBO or other constant buffers? */
540    bool has_ubo_pull;
541 
542    /** How many ray queries objects in this shader. */
543    unsigned ray_queries;
544 
545    /**
546     * Register where the thread expects to find input data from the URB
547     * (typically uniforms, followed by vertex or fragment attributes).
548     */
549    unsigned dispatch_grf_start_reg;
550 
551    bool use_alt_mode; /**< Use ALT floating point mode?  Otherwise, IEEE. */
552 
553    /* 32-bit identifiers for all push/pull parameters.  These can be anything
554     * the driver wishes them to be; the core of the back-end compiler simply
555     * re-arranges them.  The one restriction is that the bottom 2^16 values
556     * are reserved for builtins defined in the brw_param_builtin enum defined
557     * above.
558     */
559    uint32_t *param;
560 
561    /* Whether shader uses atomic operations. */
562    bool uses_atomic_load_store;
563 
564    /* Printf descriptions contained by the shader */
565    uint32_t printf_info_count;
566    u_printf_info *printf_info;
567 };
568 
569 static inline uint32_t *
brw_stage_prog_data_add_params(struct brw_stage_prog_data * prog_data,unsigned nr_new_params)570 brw_stage_prog_data_add_params(struct brw_stage_prog_data *prog_data,
571                                unsigned nr_new_params)
572 {
573    unsigned old_nr_params = prog_data->nr_params;
574    prog_data->nr_params += nr_new_params;
575    prog_data->param = reralloc(ralloc_parent(prog_data->param),
576                                prog_data->param, uint32_t,
577                                prog_data->nr_params);
578    return prog_data->param + old_nr_params;
579 }
580 
581 void
582 brw_stage_prog_data_add_printf(struct brw_stage_prog_data *prog_data,
583                                void *mem_ctx,
584                                const u_printf_info *print);
585 
586 enum brw_pixel_shader_computed_depth_mode {
587    BRW_PSCDEPTH_OFF   = 0, /* PS does not compute depth */
588    BRW_PSCDEPTH_ON    = 1, /* PS computes depth; no guarantee about value */
589    BRW_PSCDEPTH_ON_GE = 2, /* PS guarantees output depth >= source depth */
590    BRW_PSCDEPTH_ON_LE = 3, /* PS guarantees output depth <= source depth */
591 };
592 
593 /* Data about a particular attempt to compile a program.  Note that
594  * there can be many of these, each in a different GL state
595  * corresponding to a different brw_wm_prog_key struct, with different
596  * compiled programs.
597  */
598 struct brw_wm_prog_data {
599    struct brw_stage_prog_data base;
600 
601    unsigned num_per_primitive_inputs;
602    unsigned num_varying_inputs;
603 
604    uint8_t dispatch_grf_start_reg_16;
605    uint8_t dispatch_grf_start_reg_32;
606    uint32_t prog_offset_16;
607    uint32_t prog_offset_32;
608 
609    uint8_t computed_depth_mode;
610 
611    /**
612     * Number of polygons handled in parallel by the multi-polygon PS
613     * kernel.
614     */
615    uint8_t max_polygons;
616 
617    /**
618     * Dispatch width of the multi-polygon PS kernel, or 0 if no
619     * multi-polygon kernel was built.
620     */
621    uint8_t dispatch_multi;
622 
623    bool computed_stencil;
624    bool early_fragment_tests;
625    bool post_depth_coverage;
626    bool inner_coverage;
627    bool dispatch_8;
628    bool dispatch_16;
629    bool dispatch_32;
630    bool dual_src_blend;
631    bool uses_pos_offset;
632    bool uses_omask;
633    bool uses_kill;
634    bool uses_src_depth;
635    bool uses_src_w;
636    bool uses_depth_w_coefficients;
637    bool uses_pc_bary_coefficients;
638    bool uses_npc_bary_coefficients;
639    bool uses_sample_offsets;
640    bool uses_sample_mask;
641    bool uses_vmask;
642    bool has_side_effects;
643    bool pulls_bary;
644 
645    bool contains_flat_varying;
646    bool contains_noperspective_varying;
647 
648    /** True if the shader wants sample shading
649     *
650     * This corresponds to whether or not a gl_SampleId, gl_SamplePosition, or
651     * a sample-qualified input are used in the shader.  It is independent of
652     * GL_MIN_SAMPLE_SHADING_VALUE in GL or minSampleShading in Vulkan.
653     */
654    bool sample_shading;
655 
656    /** Min sample shading value
657     *
658     * Not used by the compiler, but useful for restore from the cache. The
659     * driver is expected to write the value it wants.
660     */
661    float min_sample_shading;
662 
663    /** Should this shader be dispatched per-sample */
664    enum intel_sometimes persample_dispatch;
665 
666    /**
667     * Shader is ran at the coarse pixel shading dispatch rate (3DSTATE_CPS).
668     */
669    enum intel_sometimes coarse_pixel_dispatch;
670 
671    /**
672     * Shader writes the SampleMask and this is AND-ed with the API's
673     * SampleMask to generate a new coverage mask.
674     */
675    enum intel_sometimes alpha_to_coverage;
676 
677    unsigned msaa_flags_param;
678 
679    /**
680     * Mask of which interpolation modes are required by the fragment shader.
681     * Those interpolations are delivered as part of the thread payload. Used
682     * in hardware setup on gfx6+.
683     */
684    uint32_t barycentric_interp_modes;
685 
686    /**
687     * Whether nonperspective interpolation modes are used by the
688     * barycentric_interp_modes or fragment shader through interpolator messages.
689     */
690    bool uses_nonperspective_interp_modes;
691 
692    /**
693     * Mask of which FS inputs are marked flat by the shader source.  This is
694     * needed for setting up 3DSTATE_SF/SBE.
695     */
696    uint32_t flat_inputs;
697 
698    /**
699     * The FS inputs
700     */
701    uint64_t inputs;
702 
703    /**
704     * Map from gl_varying_slot to the position within the FS setup data
705     * payload where the varying's attribute vertex deltas should be delivered.
706     * For varying slots that are not used by the FS, the value is -1.
707     */
708    int urb_setup[VARYING_SLOT_MAX];
709    int urb_setup_channel[VARYING_SLOT_MAX];
710 
711    /**
712     * Cache structure into the urb_setup array above that contains the
713     * attribute numbers of active varyings out of urb_setup.
714     * The actual count is stored in urb_setup_attribs_count.
715     */
716    uint8_t urb_setup_attribs[VARYING_SLOT_MAX];
717    uint8_t urb_setup_attribs_count;
718 };
719 
720 #ifdef GFX_VERx10
721 
722 #if GFX_VERx10 >= 200
723 
724 /** Returns the SIMD width corresponding to a given KSP index
725  *
726  * The "Variable Pixel Dispatch" table in the PRM (which can be found, for
727  * example in Vol. 7 of the SKL PRM) has a mapping from dispatch widths to
728  * kernel start pointer (KSP) indices that is based on what dispatch widths
729  * are enabled.  This function provides, effectively, the reverse mapping.
730  *
731  * If the given KSP is enabled, a SIMD width of 8, 16, or 32 is
732  * returned.  Note that for a multipolygon dispatch kernel 8 is always
733  * returned, since multipolygon kernels use the "_8" fields from
734  * brw_wm_prog_data regardless of their SIMD width.  If the KSP is
735  * invalid, 0 is returned.
736  */
737 static inline unsigned
brw_fs_simd_width_for_ksp(unsigned ksp_idx,bool enabled,unsigned width_sel)738 brw_fs_simd_width_for_ksp(unsigned ksp_idx, bool enabled, unsigned width_sel)
739 {
740    assert(ksp_idx < 2);
741    return !enabled ? 0 :
742           width_sel ? 32 :
743           16;
744 }
745 
746 #define brw_wm_state_simd_width_for_ksp(wm_state, ksp_idx)              \
747         (ksp_idx == 0 && (wm_state).Kernel0MaximumPolysperThread ? 8 :  \
748          ksp_idx == 0 ? brw_fs_simd_width_for_ksp(ksp_idx, (wm_state).Kernel0Enable, \
749                                                   (wm_state).Kernel0SIMDWidth): \
750          brw_fs_simd_width_for_ksp(ksp_idx, (wm_state).Kernel1Enable,   \
751                                    (wm_state).Kernel1SIMDWidth))
752 
753 #else
754 
755 /** Returns the SIMD width corresponding to a given KSP index
756  *
757  * The "Variable Pixel Dispatch" table in the PRM (which can be found, for
758  * example in Vol. 7 of the SKL PRM) has a mapping from dispatch widths to
759  * kernel start pointer (KSP) indices that is based on what dispatch widths
760  * are enabled.  This function provides, effectively, the reverse mapping.
761  *
762  * If the given KSP is valid with respect to the SIMD8/16/32 enables, a SIMD
763  * width of 8, 16, or 32 is returned.  If the KSP is invalid, 0 is returned.
764  */
765 static inline unsigned
brw_fs_simd_width_for_ksp(unsigned ksp_idx,bool simd8_enabled,bool simd16_enabled,bool simd32_enabled)766 brw_fs_simd_width_for_ksp(unsigned ksp_idx, bool simd8_enabled,
767                           bool simd16_enabled, bool simd32_enabled)
768 {
769    /* This function strictly ignores contiguous dispatch */
770    switch (ksp_idx) {
771    case 0:
772       return simd8_enabled ? 8 :
773              (simd16_enabled && !simd32_enabled) ? 16 :
774              (simd32_enabled && !simd16_enabled) ? 32 : 0;
775    case 1:
776       return (simd32_enabled && (simd16_enabled || simd8_enabled)) ? 32 : 0;
777    case 2:
778       return (simd16_enabled && (simd32_enabled || simd8_enabled)) ? 16 : 0;
779    default:
780       unreachable("Invalid KSP index");
781    }
782 }
783 
784 #define brw_wm_state_simd_width_for_ksp(wm_state, ksp_idx)              \
785    brw_fs_simd_width_for_ksp((ksp_idx), (wm_state)._8PixelDispatchEnable, \
786                              (wm_state)._16PixelDispatchEnable, \
787                              (wm_state)._32PixelDispatchEnable)
788 
789 #endif
790 
791 #endif
792 
793 #define brw_wm_state_has_ksp(wm_state, ksp_idx) \
794    (brw_wm_state_simd_width_for_ksp((wm_state), (ksp_idx)) != 0)
795 
796 static inline uint32_t
_brw_wm_prog_data_prog_offset(const struct brw_wm_prog_data * prog_data,unsigned simd_width)797 _brw_wm_prog_data_prog_offset(const struct brw_wm_prog_data *prog_data,
798                               unsigned simd_width)
799 {
800    switch (simd_width) {
801    case 8: return 0;
802    case 16: return prog_data->prog_offset_16;
803    case 32: return prog_data->prog_offset_32;
804    default: return 0;
805    }
806 }
807 
808 #define brw_wm_prog_data_prog_offset(prog_data, wm_state, ksp_idx) \
809    _brw_wm_prog_data_prog_offset(prog_data, \
810       brw_wm_state_simd_width_for_ksp(wm_state, ksp_idx))
811 
812 static inline uint8_t
_brw_wm_prog_data_dispatch_grf_start_reg(const struct brw_wm_prog_data * prog_data,unsigned simd_width)813 _brw_wm_prog_data_dispatch_grf_start_reg(const struct brw_wm_prog_data *prog_data,
814                                          unsigned simd_width)
815 {
816    switch (simd_width) {
817    case 8: return prog_data->base.dispatch_grf_start_reg;
818    case 16: return prog_data->dispatch_grf_start_reg_16;
819    case 32: return prog_data->dispatch_grf_start_reg_32;
820    default: return 0;
821    }
822 }
823 
824 #define brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm_state, ksp_idx) \
825    _brw_wm_prog_data_dispatch_grf_start_reg(prog_data, \
826       brw_wm_state_simd_width_for_ksp(wm_state, ksp_idx))
827 
828 static inline bool
brw_wm_prog_data_is_persample(const struct brw_wm_prog_data * prog_data,enum intel_msaa_flags pushed_msaa_flags)829 brw_wm_prog_data_is_persample(const struct brw_wm_prog_data *prog_data,
830                               enum intel_msaa_flags pushed_msaa_flags)
831 {
832    return intel_fs_is_persample(prog_data->persample_dispatch,
833                                 prog_data->sample_shading,
834                                 pushed_msaa_flags);
835 }
836 
837 static inline uint32_t
wm_prog_data_barycentric_modes(const struct brw_wm_prog_data * prog_data,enum intel_msaa_flags pushed_msaa_flags)838 wm_prog_data_barycentric_modes(const struct brw_wm_prog_data *prog_data,
839                                enum intel_msaa_flags pushed_msaa_flags)
840 {
841    return intel_fs_barycentric_modes(prog_data->persample_dispatch,
842                                      prog_data->barycentric_interp_modes,
843                                      pushed_msaa_flags);
844 }
845 
846 static inline bool
brw_wm_prog_data_is_coarse(const struct brw_wm_prog_data * prog_data,enum intel_msaa_flags pushed_msaa_flags)847 brw_wm_prog_data_is_coarse(const struct brw_wm_prog_data *prog_data,
848                            enum intel_msaa_flags pushed_msaa_flags)
849 {
850    return intel_fs_is_coarse(prog_data->coarse_pixel_dispatch,
851                              pushed_msaa_flags);
852 }
853 
854 struct brw_push_const_block {
855    unsigned dwords;     /* Dword count, not reg aligned */
856    unsigned regs;
857    unsigned size;       /* Bytes, register aligned */
858 };
859 
860 struct brw_cs_prog_data {
861    struct brw_stage_prog_data base;
862 
863    unsigned local_size[3];
864 
865    /* Program offsets for the 8/16/32 SIMD variants.  Multiple variants are
866     * kept when using variable group size, and the right one can only be
867     * decided at dispatch time.
868     */
869    unsigned prog_offset[3];
870 
871    /* Bitmask indicating which program offsets are valid. */
872    unsigned prog_mask;
873 
874    /* Bitmask indicating which programs have spilled. */
875    unsigned prog_spilled;
876 
877    bool uses_barrier;
878    bool uses_num_work_groups;
879    bool uses_inline_data;
880    bool uses_btd_stack_ids;
881    bool uses_systolic;
882    uint8_t generate_local_id;
883    enum intel_compute_walk_order walk_order;
884 
885    /* True if shader has any sample operation */
886    bool uses_sampler;
887 
888    struct {
889       struct brw_push_const_block cross_thread;
890       struct brw_push_const_block per_thread;
891    } push;
892 };
893 
894 static inline uint32_t
brw_cs_prog_data_prog_offset(const struct brw_cs_prog_data * prog_data,unsigned dispatch_width)895 brw_cs_prog_data_prog_offset(const struct brw_cs_prog_data *prog_data,
896                              unsigned dispatch_width)
897 {
898    assert(dispatch_width == 8 ||
899           dispatch_width == 16 ||
900           dispatch_width == 32);
901    const unsigned index = dispatch_width / 16;
902    assert(prog_data->prog_mask & (1 << index));
903    return prog_data->prog_offset[index];
904 }
905 
906 struct brw_bs_prog_data {
907    struct brw_stage_prog_data base;
908 
909    /** SIMD size of the root shader */
910    uint8_t simd_size;
911 
912    /** Maximum stack size of all shaders */
913    uint32_t max_stack_size;
914 
915    /** Offset into the shader where the resume SBT is located */
916    uint32_t resume_sbt_offset;
917 
918    /** Number of resume shaders */
919    uint32_t num_resume_shaders;
920 };
921 
922 /**
923  * Enum representing the i965-specific vertex results that don't correspond
924  * exactly to any element of gl_varying_slot.  The values of this enum are
925  * assigned such that they don't conflict with gl_varying_slot.
926  */
927 typedef enum
928 {
929    BRW_VARYING_SLOT_PAD = VARYING_SLOT_MAX,
930    BRW_VARYING_SLOT_COUNT
931 } brw_varying_slot;
932 
933 /**
934  * Bitmask indicating which fragment shader inputs represent varyings (and
935  * hence have to be delivered to the fragment shader by the SF/SBE stage).
936  */
937 #define BRW_FS_VARYING_INPUT_MASK \
938    (BITFIELD64_RANGE(0, VARYING_SLOT_MAX) & \
939     ~VARYING_BIT_POS & ~VARYING_BIT_FACE)
940 
941 void brw_print_vue_map(FILE *fp, const struct intel_vue_map *vue_map,
942                        gl_shader_stage stage);
943 
944 /**
945  * Convert a VUE slot number into a byte offset within the VUE.
946  */
brw_vue_slot_to_offset(unsigned slot)947 static inline unsigned brw_vue_slot_to_offset(unsigned slot)
948 {
949    return 16*slot;
950 }
951 
952 /**
953  * Convert a vertex output (brw_varying_slot) into a byte offset within the
954  * VUE.
955  */
956 static inline unsigned
brw_varying_to_offset(const struct intel_vue_map * vue_map,unsigned varying)957 brw_varying_to_offset(const struct intel_vue_map *vue_map, unsigned varying)
958 {
959    return brw_vue_slot_to_offset(vue_map->varying_to_slot[varying]);
960 }
961 
962 void brw_compute_vue_map(const struct intel_device_info *devinfo,
963                          struct intel_vue_map *vue_map,
964                          uint64_t slots_valid,
965                          bool separate_shader,
966                          uint32_t pos_slots);
967 
968 void brw_compute_tess_vue_map(struct intel_vue_map *const vue_map,
969                               uint64_t slots_valid,
970                               uint32_t is_patch);
971 
972 struct brw_vue_prog_data {
973    struct brw_stage_prog_data base;
974    struct intel_vue_map vue_map;
975 
976    /** Should the hardware deliver input VUE handles for URB pull loads? */
977    bool include_vue_handles;
978 
979    unsigned urb_read_length;
980    unsigned total_grf;
981 
982    uint32_t clip_distance_mask;
983    uint32_t cull_distance_mask;
984 
985    /* Used for calculating urb partitions.  In the VS, this is the size of the
986     * URB entry used for both input and output to the thread.  In the GS, this
987     * is the size of the URB entry used for output.
988     */
989    unsigned urb_entry_size;
990 
991    enum intel_shader_dispatch_mode dispatch_mode;
992 };
993 
994 struct brw_vs_prog_data {
995    struct brw_vue_prog_data base;
996 
997    uint64_t inputs_read;
998    uint64_t double_inputs_read;
999 
1000    unsigned nr_attribute_slots;
1001 
1002    bool uses_vertexid;
1003    bool uses_instanceid;
1004    bool uses_is_indexed_draw;
1005    bool uses_firstvertex;
1006    bool uses_baseinstance;
1007    bool uses_drawid;
1008 };
1009 
1010 struct brw_tcs_prog_data
1011 {
1012    struct brw_vue_prog_data base;
1013 
1014    /** Should the non-SINGLE_PATCH payload provide primitive ID? */
1015    bool include_primitive_id;
1016 
1017    /** Number vertices in output patch */
1018    int instances;
1019 
1020    /** Track patch count threshold */
1021    int patch_count_threshold;
1022 };
1023 
1024 
1025 struct brw_tes_prog_data
1026 {
1027    struct brw_vue_prog_data base;
1028 
1029    enum intel_tess_partitioning partitioning;
1030    enum intel_tess_output_topology output_topology;
1031    enum intel_tess_domain domain;
1032    bool include_primitive_id;
1033 };
1034 
1035 struct brw_gs_prog_data
1036 {
1037    struct brw_vue_prog_data base;
1038 
1039    unsigned vertices_in;
1040 
1041    /**
1042     * Size of an output vertex, measured in HWORDS (32 bytes).
1043     */
1044    unsigned output_vertex_size_hwords;
1045 
1046    unsigned output_topology;
1047 
1048    /**
1049     * Size of the control data (cut bits or StreamID bits), in hwords (32
1050     * bytes).  0 if there is no control data.
1051     */
1052    unsigned control_data_header_size_hwords;
1053 
1054    /**
1055     * Format of the control data (either GFX7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
1056     * if the control data is StreamID bits, or
1057     * GFX7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT if the control data is cut bits).
1058     * Ignored if control_data_header_size is 0.
1059     */
1060    unsigned control_data_format;
1061 
1062    bool include_primitive_id;
1063 
1064    /**
1065     * The number of vertices emitted, if constant - otherwise -1.
1066     */
1067    int static_vertex_count;
1068 
1069    int invocations;
1070 };
1071 
1072 struct brw_tue_map {
1073    uint32_t size_dw;
1074 
1075    uint32_t per_task_data_start_dw;
1076 };
1077 
1078 struct brw_mue_map {
1079    int32_t start_dw[VARYING_SLOT_MAX];
1080    uint32_t len_dw[VARYING_SLOT_MAX];
1081    uint32_t per_primitive_indices_dw;
1082 
1083    uint32_t size_dw;
1084 
1085    uint32_t max_primitives;
1086    uint32_t per_primitive_start_dw;
1087    uint32_t per_primitive_header_size_dw;
1088    uint32_t per_primitive_data_size_dw;
1089    uint32_t per_primitive_pitch_dw;
1090    bool user_data_in_primitive_header;
1091 
1092    uint32_t max_vertices;
1093    uint32_t per_vertex_start_dw;
1094    uint32_t per_vertex_header_size_dw;
1095    uint32_t per_vertex_data_size_dw;
1096    uint32_t per_vertex_pitch_dw;
1097    bool user_data_in_vertex_header;
1098 };
1099 
1100 struct brw_task_prog_data {
1101    struct brw_cs_prog_data base;
1102    struct brw_tue_map map;
1103    bool uses_drawid;
1104 };
1105 
1106 enum brw_mesh_index_format {
1107    BRW_INDEX_FORMAT_U32,
1108    BRW_INDEX_FORMAT_U888X,
1109 };
1110 
1111 struct brw_mesh_prog_data {
1112    struct brw_cs_prog_data base;
1113    struct brw_mue_map map;
1114 
1115    uint32_t clip_distance_mask;
1116    uint32_t cull_distance_mask;
1117    uint16_t primitive_type;
1118 
1119    enum brw_mesh_index_format index_format;
1120 
1121    bool uses_drawid;
1122    bool autostrip_enable;
1123 };
1124 
1125 /* brw_any_prog_data is prog_data for any stage that maps to an API stage */
1126 union brw_any_prog_data {
1127    struct brw_stage_prog_data base;
1128    struct brw_vue_prog_data vue;
1129    struct brw_vs_prog_data vs;
1130    struct brw_tcs_prog_data tcs;
1131    struct brw_tes_prog_data tes;
1132    struct brw_gs_prog_data gs;
1133    struct brw_wm_prog_data wm;
1134    struct brw_cs_prog_data cs;
1135    struct brw_bs_prog_data bs;
1136    struct brw_task_prog_data task;
1137    struct brw_mesh_prog_data mesh;
1138 };
1139 
1140 #define DEFINE_PROG_DATA_DOWNCAST(STAGE, CHECK)                            \
1141 static inline struct brw_##STAGE##_prog_data *                             \
1142 brw_##STAGE##_prog_data(struct brw_stage_prog_data *prog_data)             \
1143 {                                                                          \
1144    if (prog_data)                                                          \
1145       assert(CHECK);                                                       \
1146    return (struct brw_##STAGE##_prog_data *) prog_data;                    \
1147 }                                                                          \
1148 static inline const struct brw_##STAGE##_prog_data *                       \
1149 brw_##STAGE##_prog_data_const(const struct brw_stage_prog_data *prog_data) \
1150 {                                                                          \
1151    if (prog_data)                                                          \
1152       assert(CHECK);                                                       \
1153    return (const struct brw_##STAGE##_prog_data *) prog_data;              \
1154 }
1155 
1156 DEFINE_PROG_DATA_DOWNCAST(vs,  prog_data->stage == MESA_SHADER_VERTEX)
1157 DEFINE_PROG_DATA_DOWNCAST(tcs, prog_data->stage == MESA_SHADER_TESS_CTRL)
1158 DEFINE_PROG_DATA_DOWNCAST(tes, prog_data->stage == MESA_SHADER_TESS_EVAL)
1159 DEFINE_PROG_DATA_DOWNCAST(gs,  prog_data->stage == MESA_SHADER_GEOMETRY)
1160 DEFINE_PROG_DATA_DOWNCAST(wm,  prog_data->stage == MESA_SHADER_FRAGMENT)
1161 DEFINE_PROG_DATA_DOWNCAST(cs,  gl_shader_stage_uses_workgroup(prog_data->stage))
1162 DEFINE_PROG_DATA_DOWNCAST(bs,  brw_shader_stage_is_bindless(prog_data->stage))
1163 
1164 DEFINE_PROG_DATA_DOWNCAST(vue, prog_data->stage == MESA_SHADER_VERTEX ||
1165                                prog_data->stage == MESA_SHADER_TESS_CTRL ||
1166                                prog_data->stage == MESA_SHADER_TESS_EVAL ||
1167                                prog_data->stage == MESA_SHADER_GEOMETRY)
1168 
1169 DEFINE_PROG_DATA_DOWNCAST(task, prog_data->stage == MESA_SHADER_TASK)
1170 DEFINE_PROG_DATA_DOWNCAST(mesh, prog_data->stage == MESA_SHADER_MESH)
1171 
1172 #undef DEFINE_PROG_DATA_DOWNCAST
1173 
1174 struct brw_compile_stats {
1175    uint32_t dispatch_width; /**< 0 for vec4 */
1176    uint32_t max_polygons;
1177    uint32_t max_dispatch_width;
1178    uint32_t instructions;
1179    uint32_t sends;
1180    uint32_t loops;
1181    uint32_t cycles;
1182    uint32_t spills;
1183    uint32_t fills;
1184    uint32_t max_live_registers;
1185    uint32_t non_ssa_registers_after_nir;
1186 };
1187 
1188 /** @} */
1189 
1190 struct brw_compiler *
1191 brw_compiler_create(void *mem_ctx, const struct intel_device_info *devinfo);
1192 
1193 /**
1194  * Returns a compiler configuration for use with disk shader cache
1195  *
1196  * This value only needs to change for settings that can cause different
1197  * program generation between two runs on the same hardware.
1198  *
1199  * For example, it doesn't need to be different for gen 8 and gen 9 hardware,
1200  * but it does need to be different if INTEL_DEBUG=nocompact is or isn't used.
1201  */
1202 uint64_t
1203 brw_get_compiler_config_value(const struct brw_compiler *compiler);
1204 
1205 /* Provides a string sha1 hash of all device information fields that could
1206  * affect shader compilation.
1207  */
1208 void
1209 brw_device_sha1(char *hex, const struct intel_device_info *devinfo);
1210 
1211 /* For callers computing their own UUID or hash.  Hashes all device
1212  * information fields that could affect shader compilation into the provided
1213  * sha1_ctx.
1214  */
1215 void
1216 brw_device_sha1_update(struct mesa_sha1 *sha1_ctx,
1217                        const struct intel_device_info *devinfo);
1218 
1219 unsigned
1220 brw_prog_data_size(gl_shader_stage stage);
1221 
1222 unsigned
1223 brw_prog_key_size(gl_shader_stage stage);
1224 
1225 struct brw_compile_params {
1226    void *mem_ctx;
1227 
1228    nir_shader *nir;
1229 
1230    struct brw_compile_stats *stats;
1231 
1232    void *log_data;
1233 
1234    char *error_str;
1235 
1236    uint64_t debug_flag;
1237 
1238    uint32_t source_hash;
1239 };
1240 
1241 /**
1242  * Parameters for compiling a vertex shader.
1243  *
1244  * Some of these will be modified during the shader compilation.
1245  */
1246 struct brw_compile_vs_params {
1247    struct brw_compile_params base;
1248 
1249    const struct brw_vs_prog_key *key;
1250    struct brw_vs_prog_data *prog_data;
1251 };
1252 
1253 /**
1254  * Compile a vertex shader.
1255  *
1256  * Returns the final assembly and updates the parameters structure.
1257  */
1258 const unsigned *
1259 brw_compile_vs(const struct brw_compiler *compiler,
1260                struct brw_compile_vs_params *params);
1261 
1262 /**
1263  * Parameters for compiling a tessellation control shader.
1264  *
1265  * Some of these will be modified during the shader compilation.
1266  */
1267 struct brw_compile_tcs_params {
1268    struct brw_compile_params base;
1269 
1270    const struct brw_tcs_prog_key *key;
1271    struct brw_tcs_prog_data *prog_data;
1272 };
1273 
1274 /**
1275  * Compile a tessellation control shader.
1276  *
1277  * Returns the final assembly and updates the parameters structure.
1278  */
1279 const unsigned *
1280 brw_compile_tcs(const struct brw_compiler *compiler,
1281                 struct brw_compile_tcs_params *params);
1282 
1283 /**
1284  * Parameters for compiling a tessellation evaluation shader.
1285  *
1286  * Some of these will be modified during the shader compilation.
1287  */
1288 struct brw_compile_tes_params {
1289    struct brw_compile_params base;
1290 
1291    const struct brw_tes_prog_key *key;
1292    struct brw_tes_prog_data *prog_data;
1293    const struct intel_vue_map *input_vue_map;
1294 };
1295 
1296 /**
1297  * Compile a tessellation evaluation shader.
1298  *
1299  * Returns the final assembly and updates the parameters structure.
1300  */
1301 const unsigned *
1302 brw_compile_tes(const struct brw_compiler *compiler,
1303                 struct brw_compile_tes_params *params);
1304 
1305 /**
1306  * Parameters for compiling a geometry shader.
1307  *
1308  * Some of these will be modified during the shader compilation.
1309  */
1310 struct brw_compile_gs_params {
1311    struct brw_compile_params base;
1312 
1313    const struct brw_gs_prog_key *key;
1314    struct brw_gs_prog_data *prog_data;
1315 };
1316 
1317 /**
1318  * Compile a geometry shader.
1319  *
1320  * Returns the final assembly and updates the parameters structure.
1321  */
1322 const unsigned *
1323 brw_compile_gs(const struct brw_compiler *compiler,
1324                struct brw_compile_gs_params *params);
1325 
1326 struct brw_compile_task_params {
1327    struct brw_compile_params base;
1328 
1329    const struct brw_task_prog_key *key;
1330    struct brw_task_prog_data *prog_data;
1331 };
1332 
1333 const unsigned *
1334 brw_compile_task(const struct brw_compiler *compiler,
1335                  struct brw_compile_task_params *params);
1336 
1337 struct brw_compile_mesh_params {
1338    struct brw_compile_params base;
1339 
1340    const struct brw_mesh_prog_key *key;
1341    struct brw_mesh_prog_data *prog_data;
1342    const struct brw_tue_map *tue_map;
1343 };
1344 
1345 const unsigned *
1346 brw_compile_mesh(const struct brw_compiler *compiler,
1347                  struct brw_compile_mesh_params *params);
1348 
1349 /**
1350  * Parameters for compiling a fragment shader.
1351  *
1352  * Some of these will be modified during the shader compilation.
1353  */
1354 struct brw_compile_fs_params {
1355    struct brw_compile_params base;
1356 
1357    const struct brw_wm_prog_key *key;
1358    struct brw_wm_prog_data *prog_data;
1359 
1360    const struct intel_vue_map *vue_map;
1361    const struct brw_mue_map *mue_map;
1362 
1363    bool allow_spilling;
1364    bool use_rep_send;
1365    uint8_t max_polygons;
1366 };
1367 
1368 /**
1369  * Compile a fragment shader.
1370  *
1371  * Returns the final assembly and updates the parameters structure.
1372  */
1373 const unsigned *
1374 brw_compile_fs(const struct brw_compiler *compiler,
1375                struct brw_compile_fs_params *params);
1376 
1377 /**
1378  * Parameters for compiling a compute shader.
1379  *
1380  * Some of these will be modified during the shader compilation.
1381  */
1382 struct brw_compile_cs_params {
1383    struct brw_compile_params base;
1384 
1385    const struct brw_cs_prog_key *key;
1386    struct brw_cs_prog_data *prog_data;
1387 };
1388 
1389 /**
1390  * Compile a compute shader.
1391  *
1392  * Returns the final assembly and updates the parameters structure.
1393  */
1394 const unsigned *
1395 brw_compile_cs(const struct brw_compiler *compiler,
1396                struct brw_compile_cs_params *params);
1397 
1398 /**
1399  * Parameters for compiling a Bindless shader.
1400  *
1401  * Some of these will be modified during the shader compilation.
1402  */
1403 struct brw_compile_bs_params {
1404    struct brw_compile_params base;
1405 
1406    const struct brw_bs_prog_key *key;
1407    struct brw_bs_prog_data *prog_data;
1408 
1409    unsigned num_resume_shaders;
1410    struct nir_shader **resume_shaders;
1411 };
1412 
1413 /**
1414  * Compile a Bindless shader.
1415  *
1416  * Returns the final assembly and updates the parameters structure.
1417  */
1418 const unsigned *
1419 brw_compile_bs(const struct brw_compiler *compiler,
1420                struct brw_compile_bs_params *params);
1421 
1422 void brw_debug_key_recompile(const struct brw_compiler *c, void *log,
1423                              gl_shader_stage stage,
1424                              const struct brw_base_prog_key *old_key,
1425                              const struct brw_base_prog_key *key);
1426 
1427 unsigned
1428 brw_cs_push_const_total_size(const struct brw_cs_prog_data *cs_prog_data,
1429                              unsigned threads);
1430 
1431 void
1432 brw_write_shader_relocs(const struct brw_isa_info *isa,
1433                         void *program,
1434                         const struct brw_stage_prog_data *prog_data,
1435                         struct brw_shader_reloc_value *values,
1436                         unsigned num_values);
1437 
1438 /**
1439  * Get the dispatch information for a shader to be used with GPGPU_WALKER and
1440  * similar instructions.
1441  *
1442  * If override_local_size is not NULL, it must to point to a 3-element that
1443  * will override the value from prog_data->local_size.  This is used by
1444  * ARB_compute_variable_group_size, where the size is set only at dispatch
1445  * time (so prog_data is outdated).
1446  */
1447 struct intel_cs_dispatch_info
1448 brw_cs_get_dispatch_info(const struct intel_device_info *devinfo,
1449                          const struct brw_cs_prog_data *prog_data,
1450                          const unsigned *override_local_size);
1451 
1452 /**
1453  * Return true if the given shader stage is dispatched contiguously by the
1454  * relevant fixed function starting from channel 0 of the SIMD thread, which
1455  * implies that the dispatch mask of a thread can be assumed to have the form
1456  * '2^n - 1' for some n.
1457  */
1458 static inline bool
brw_stage_has_packed_dispatch(ASSERTED const struct intel_device_info * devinfo,gl_shader_stage stage,unsigned max_polygons,const struct brw_stage_prog_data * prog_data)1459 brw_stage_has_packed_dispatch(ASSERTED const struct intel_device_info *devinfo,
1460                               gl_shader_stage stage, unsigned max_polygons,
1461                               const struct brw_stage_prog_data *prog_data)
1462 {
1463    /* The code below makes assumptions about the hardware's thread dispatch
1464     * behavior that could be proven wrong in future generations -- Make sure
1465     * to do a full test run with brw_fs_test_dispatch_packing() hooked up to
1466     * the NIR front-end before changing this assertion. It can be temporarily
1467     * enabled by setting the macro below to true.
1468     */
1469    #define ENABLE_FS_TEST_DISPATCH_PACKING false
1470    assert(devinfo->ver <= 30);
1471 
1472    switch (stage) {
1473    case MESA_SHADER_FRAGMENT: {
1474       /* The PSD discards subspans coming in with no lit samples, which in the
1475        * per-pixel shading case implies that each subspan will either be fully
1476        * lit (due to the VMask being used to allow derivative computations),
1477        * or not dispatched at all.  In per-sample dispatch mode individual
1478        * samples from the same subspan have a fixed relative location within
1479        * the SIMD thread, so dispatch of unlit samples cannot be avoided in
1480        * general and we should return false.
1481        */
1482       const struct brw_wm_prog_data *wm_prog_data =
1483          (const struct brw_wm_prog_data *)prog_data;
1484       return devinfo->verx10 < 125 &&
1485              !wm_prog_data->persample_dispatch &&
1486              wm_prog_data->uses_vmask &&
1487              max_polygons < 2;
1488    }
1489    case MESA_SHADER_COMPUTE:
1490       /* Compute shaders will be spawned with either a fully enabled dispatch
1491        * mask or with whatever bottom/right execution mask was given to the
1492        * GPGPU walker command to be used along the workgroup edges -- In both
1493        * cases the dispatch mask is required to be tightly packed for our
1494        * invocation index calculations to work.
1495        */
1496       return true;
1497    default:
1498       /* Most remaining fixed functions are limited to use a packed dispatch
1499        * mask due to the hardware representation of the dispatch mask as a
1500        * single counter representing the number of enabled channels.
1501        */
1502       return true;
1503    }
1504 }
1505 
1506 /**
1507  * Computes the first varying slot in the URB produced by the previous stage
1508  * that is used in the next stage. We do this by testing the varying slots in
1509  * the previous stage's vue map against the inputs read in the next stage.
1510  *
1511  * Note that:
1512  *
1513  * - Each URB offset contains two varying slots and we can only skip a
1514  *   full offset if both slots are unused, so the value we return here is always
1515  *   rounded down to the closest multiple of two.
1516  *
1517  * - gl_Layer and gl_ViewportIndex don't have their own varying slots, they are
1518  *   part of the vue header, so if these are read we can't skip anything.
1519  */
1520 static inline int
brw_compute_first_urb_slot_required(uint64_t inputs_read,const struct intel_vue_map * prev_stage_vue_map)1521 brw_compute_first_urb_slot_required(uint64_t inputs_read,
1522                                     const struct intel_vue_map *prev_stage_vue_map)
1523 {
1524    if ((inputs_read & (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PRIMITIVE_SHADING_RATE)) == 0) {
1525       for (int i = 0; i < prev_stage_vue_map->num_slots; i++) {
1526          int varying = prev_stage_vue_map->slot_to_varying[i];
1527          if (varying != BRW_VARYING_SLOT_PAD && varying > 0 &&
1528              (inputs_read & BITFIELD64_BIT(varying)) != 0) {
1529             return ROUND_DOWN_TO(i, 2);
1530          }
1531       }
1532    }
1533 
1534    return 0;
1535 }
1536 
1537 /* From InlineData in 3DSTATE_TASK_SHADER_DATA and 3DSTATE_MESH_SHADER_DATA. */
1538 #define BRW_TASK_MESH_INLINE_DATA_SIZE_DW 8
1539 
1540 /* InlineData[0-1] is used for Vulkan descriptor. */
1541 #define BRW_TASK_MESH_PUSH_CONSTANTS_START_DW 2
1542 
1543 #define BRW_TASK_MESH_PUSH_CONSTANTS_SIZE_DW \
1544    (BRW_TASK_MESH_INLINE_DATA_SIZE_DW - BRW_TASK_MESH_PUSH_CONSTANTS_START_DW)
1545 
1546 /**
1547  * This enum is used as the base indice of the nir_load_topology_id_intel
1548  * intrinsic. This is used to return different values based on some aspect of
1549  * the topology of the device.
1550  */
1551 enum brw_topology_id
1552 {
1553    /* A value based of the DSS identifier the shader is currently running on.
1554     * Be mindful that the DSS ID can be higher than the total number of DSS on
1555     * the device. This is because of the fusing that can occur on different
1556     * parts.
1557     */
1558    BRW_TOPOLOGY_ID_DSS,
1559 
1560    /* A value composed of EU ID, thread ID & SIMD lane ID. */
1561    BRW_TOPOLOGY_ID_EU_THREAD_SIMD,
1562 };
1563 
1564 #ifdef __cplusplus
1565 } /* extern "C" */
1566 #endif
1567