1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2015 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#ifndef BRW_NIR_H
25b8e80941Smrg#define BRW_NIR_H
26b8e80941Smrg
27b8e80941Smrg#include "brw_reg.h"
28b8e80941Smrg#include "compiler/nir/nir.h"
29b8e80941Smrg#include "brw_compiler.h"
30b8e80941Smrg
31b8e80941Smrg#ifdef __cplusplus
32b8e80941Smrgextern "C" {
33b8e80941Smrg#endif
34b8e80941Smrg
35b8e80941Smrgint type_size_scalar(const struct glsl_type *type, bool bindless);
36b8e80941Smrgint type_size_vec4(const struct glsl_type *type, bool bindless);
37b8e80941Smrgint type_size_dvec4(const struct glsl_type *type, bool bindless);
38b8e80941Smrg
39b8e80941Smrgstatic inline int
40b8e80941Smrgtype_size_scalar_bytes(const struct glsl_type *type, bool bindless)
41b8e80941Smrg{
42b8e80941Smrg   return type_size_scalar(type, bindless) * 4;
43b8e80941Smrg}
44b8e80941Smrg
45b8e80941Smrgstatic inline int
46b8e80941Smrgtype_size_vec4_bytes(const struct glsl_type *type, bool bindless)
47b8e80941Smrg{
48b8e80941Smrg   return type_size_vec4(type, bindless) * 16;
49b8e80941Smrg}
50b8e80941Smrg
51b8e80941Smrg/* Flags set in the instr->pass_flags field by i965 analysis passes */
52b8e80941Smrgenum {
53b8e80941Smrg   BRW_NIR_NON_BOOLEAN           = 0x0,
54b8e80941Smrg
55b8e80941Smrg   /* Indicates that the given instruction's destination is a boolean
56b8e80941Smrg    * value but that it needs to be resolved before it can be used.
57b8e80941Smrg    * On Gen <= 5, CMP instructions return a 32-bit value where the bottom
58b8e80941Smrg    * bit represents the actual true/false value of the compare and the top
59b8e80941Smrg    * 31 bits are undefined.  In order to use this value, we have to do a
60b8e80941Smrg    * "resolve" operation by replacing the value of the CMP with -(x & 1)
61b8e80941Smrg    * to sign-extend the bottom bit to 0/~0.
62b8e80941Smrg    */
63b8e80941Smrg   BRW_NIR_BOOLEAN_NEEDS_RESOLVE = 0x1,
64b8e80941Smrg
65b8e80941Smrg   /* Indicates that the given instruction's destination is a boolean
66b8e80941Smrg    * value that has intentionally been left unresolved.  Not all boolean
67b8e80941Smrg    * values need to be resolved immediately.  For instance, if we have
68b8e80941Smrg    *
69b8e80941Smrg    *    CMP r1 r2 r3
70b8e80941Smrg    *    CMP r4 r5 r6
71b8e80941Smrg    *    AND r7 r1 r4
72b8e80941Smrg    *
73b8e80941Smrg    * We don't have to resolve the result of the two CMP instructions
74b8e80941Smrg    * immediately because the AND still does an AND of the bottom bits.
75b8e80941Smrg    * Instead, we can save ourselves instructions by delaying the resolve
76b8e80941Smrg    * until after the AND.  The result of the two CMP instructions is left
77b8e80941Smrg    * as BRW_NIR_BOOLEAN_UNRESOLVED.
78b8e80941Smrg    */
79b8e80941Smrg   BRW_NIR_BOOLEAN_UNRESOLVED    = 0x2,
80b8e80941Smrg
81b8e80941Smrg   /* Indicates a that the given instruction's destination is a boolean
82b8e80941Smrg    * value that does not need a resolve.  For instance, if you AND two
83b8e80941Smrg    * values that are BRW_NIR_BOOLEAN_NEEDS_RESOLVE then we know that both
84b8e80941Smrg    * values will be 0/~0 before we get them and the result of the AND is
85b8e80941Smrg    * also guaranteed to be 0/~0 and does not need a resolve.
86b8e80941Smrg    */
87b8e80941Smrg   BRW_NIR_BOOLEAN_NO_RESOLVE    = 0x3,
88b8e80941Smrg
89b8e80941Smrg   /* A mask to mask the boolean status values off of instr->pass_flags */
90b8e80941Smrg   BRW_NIR_BOOLEAN_MASK          = 0x3,
91b8e80941Smrg};
92b8e80941Smrg
93b8e80941Smrgvoid brw_nir_analyze_boolean_resolves(nir_shader *nir);
94b8e80941Smrg
95b8e80941Smrgnir_shader *brw_preprocess_nir(const struct brw_compiler *compiler,
96b8e80941Smrg                               nir_shader *nir,
97b8e80941Smrg                               const nir_shader *softfp64);
98b8e80941Smrg
99b8e80941Smrgvoid
100b8e80941Smrgbrw_nir_link_shaders(const struct brw_compiler *compiler,
101b8e80941Smrg                     nir_shader **producer, nir_shader **consumer);
102b8e80941Smrg
103b8e80941Smrgbool brw_nir_lower_cs_intrinsics(nir_shader *nir,
104b8e80941Smrg                                 unsigned dispatch_width);
105b8e80941Smrgvoid brw_nir_lower_vs_inputs(nir_shader *nir,
106b8e80941Smrg                             const uint8_t *vs_attrib_wa_flags);
107b8e80941Smrgvoid brw_nir_lower_vue_inputs(nir_shader *nir,
108b8e80941Smrg                              const struct brw_vue_map *vue_map);
109b8e80941Smrgvoid brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue);
110b8e80941Smrgvoid brw_nir_lower_fs_inputs(nir_shader *nir,
111b8e80941Smrg                             const struct gen_device_info *devinfo,
112b8e80941Smrg                             const struct brw_wm_prog_key *key);
113b8e80941Smrgvoid brw_nir_lower_vue_outputs(nir_shader *nir);
114b8e80941Smrgvoid brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue,
115b8e80941Smrg                               GLenum tes_primitive_mode);
116b8e80941Smrgvoid brw_nir_lower_fs_outputs(nir_shader *nir);
117b8e80941Smrg
118b8e80941Smrgbool brw_nir_lower_conversions(nir_shader *nir);
119b8e80941Smrg
120b8e80941Smrgbool brw_nir_lower_image_load_store(nir_shader *nir,
121b8e80941Smrg                                    const struct gen_device_info *devinfo);
122b8e80941Smrgvoid brw_nir_rewrite_image_intrinsic(nir_intrinsic_instr *intrin,
123b8e80941Smrg                                     nir_ssa_def *index);
124b8e80941Smrgvoid brw_nir_rewrite_bindless_image_intrinsic(nir_intrinsic_instr *intrin,
125b8e80941Smrg                                              nir_ssa_def *handle);
126b8e80941Smrg
127b8e80941Smrgbool brw_nir_lower_mem_access_bit_sizes(nir_shader *shader);
128b8e80941Smrg
129b8e80941Smrgnir_shader *brw_postprocess_nir(nir_shader *nir,
130b8e80941Smrg                                const struct brw_compiler *compiler,
131b8e80941Smrg                                bool is_scalar);
132b8e80941Smrg
133b8e80941Smrgbool brw_nir_apply_attribute_workarounds(nir_shader *nir,
134b8e80941Smrg                                         const uint8_t *attrib_wa_flags);
135b8e80941Smrg
136b8e80941Smrgbool brw_nir_apply_trig_workarounds(nir_shader *nir);
137b8e80941Smrg
138b8e80941Smrgvoid brw_nir_apply_tcs_quads_workaround(nir_shader *nir);
139b8e80941Smrg
140b8e80941Smrgnir_shader *brw_nir_apply_sampler_key(nir_shader *nir,
141b8e80941Smrg                                      const struct brw_compiler *compiler,
142b8e80941Smrg                                      const struct brw_sampler_prog_key_data *key,
143b8e80941Smrg                                      bool is_scalar);
144b8e80941Smrg
145b8e80941Smrgenum brw_reg_type brw_type_for_nir_type(const struct gen_device_info *devinfo,
146b8e80941Smrg                                        nir_alu_type type);
147b8e80941Smrg
148b8e80941Smrgenum glsl_base_type brw_glsl_base_type_for_nir_type(nir_alu_type type);
149b8e80941Smrg
150b8e80941Smrgvoid brw_nir_setup_glsl_uniforms(void *mem_ctx, nir_shader *shader,
151b8e80941Smrg                                 const struct gl_program *prog,
152b8e80941Smrg                                 struct brw_stage_prog_data *stage_prog_data,
153b8e80941Smrg                                 bool is_scalar);
154b8e80941Smrg
155b8e80941Smrgvoid brw_nir_setup_arb_uniforms(void *mem_ctx, nir_shader *shader,
156b8e80941Smrg                                struct gl_program *prog,
157b8e80941Smrg                                struct brw_stage_prog_data *stage_prog_data);
158b8e80941Smrg
159b8e80941Smrgvoid brw_nir_lower_gl_images(nir_shader *shader,
160b8e80941Smrg                             const struct gl_program *prog);
161b8e80941Smrg
162b8e80941Smrgvoid brw_nir_analyze_ubo_ranges(const struct brw_compiler *compiler,
163b8e80941Smrg                                nir_shader *nir,
164b8e80941Smrg                                const struct brw_vs_prog_key *vs_key,
165b8e80941Smrg                                struct brw_ubo_range out_ranges[4]);
166b8e80941Smrg
167b8e80941Smrgbool brw_nir_opt_peephole_ffma(nir_shader *shader);
168b8e80941Smrg
169b8e80941Smrgnir_shader *brw_nir_optimize(nir_shader *nir,
170b8e80941Smrg                             const struct brw_compiler *compiler,
171b8e80941Smrg                             bool is_scalar,
172b8e80941Smrg                             bool allow_copies);
173b8e80941Smrg
174b8e80941Smrgnir_shader *brw_nir_create_passthrough_tcs(void *mem_ctx,
175b8e80941Smrg                                           const struct brw_compiler *compiler,
176b8e80941Smrg                                           const nir_shader_compiler_options *options,
177b8e80941Smrg                                           const struct brw_tcs_prog_key *key);
178b8e80941Smrg
179b8e80941Smrg#define BRW_NIR_FRAG_OUTPUT_INDEX_SHIFT 0
180b8e80941Smrg#define BRW_NIR_FRAG_OUTPUT_INDEX_MASK INTEL_MASK(0, 0)
181b8e80941Smrg#define BRW_NIR_FRAG_OUTPUT_LOCATION_SHIFT 1
182b8e80941Smrg#define BRW_NIR_FRAG_OUTPUT_LOCATION_MASK INTEL_MASK(31, 1)
183b8e80941Smrg
184b8e80941Smrg#ifdef __cplusplus
185b8e80941Smrg}
186b8e80941Smrg#endif
187b8e80941Smrg
188b8e80941Smrg#endif /* BRW_NIR_H */
189