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