1 /* -*- mesa-c++  -*-
2  *
3  * Copyright (c) 2019 Collabora LTD
4  *
5  * Author: Gert Wollny <gert.wollny@collabora.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * on the rights to use, copy, modify, merge, publish, distribute, sub
11  * license, and/or sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #ifndef SFN_NIR_H
28 #define SFN_NIR_H
29 
30 #include "nir.h"
31 #include "nir_builder.h"
32 
33 #ifdef __cplusplus
34 #include "sfn_shader_base.h"
35 #include <vector>
36 
37 namespace r600 {
38 
39 class NirLowerInstruction {
40 public:
41 	NirLowerInstruction();
42 
43 	bool run(nir_shader *shader);
44 
45 private:
46 	static bool filter_instr(const nir_instr *instr, const void *data);
47         static nir_ssa_def *lower_instr(nir_builder *b, nir_instr *instr,  void *data);
48 
set_builder(nir_builder * _b)49         void set_builder(nir_builder *_b) { b = _b;}
50 
51 	virtual bool filter(const nir_instr *instr) const = 0;
52 	virtual nir_ssa_def *lower(nir_instr *instr) = 0;
53 protected:
54 	nir_builder *b;
55 };
56 
57 bool r600_lower_scratch_addresses(nir_shader *shader);
58 
59 bool r600_lower_ubo_to_align16(nir_shader *shader);
60 
61 bool r600_nir_split_64bit_io(nir_shader *sh);
62 
63 bool r600_nir_64_to_vec2(nir_shader *sh);
64 
65 bool r600_merge_vec2_stores(nir_shader *shader);
66 
67 class Shader {
68 public:
69    std::vector<InstructionBlock>& m_ir;
70    ValueMap m_temp;
71 };
72 
73 class ShaderFromNir {
74 public:
75    ShaderFromNir();
76    ~ShaderFromNir();
77 
78    unsigned ninputs() const;
79 
80    bool lower(const nir_shader *shader, r600_pipe_shader *sh,
81               r600_pipe_shader_selector *sel, r600_shader_key &key,
82               r600_shader *gs_shader, enum chip_class chip_class);
83 
84    bool process_declaration();
85 
86    pipe_shader_type processor_type() const;
87 
88    bool emit_instruction(nir_instr *instr);
89 
90    const std::vector<InstructionBlock> &shader_ir() const;
91 
92    Shader shader() const;
93 private:
94 
95    bool process_block();
96    bool process_cf_node(nir_cf_node *node);
97    bool process_if(nir_if *node);
98    bool process_loop(nir_loop *node);
99    bool process_block(nir_block *node);
100 
101    std::unique_ptr<ShaderFromNirProcessor> impl;
102    const nir_shader *sh;
103 
104    enum chip_class chip_class;
105    int m_current_if_id;
106    int m_current_loop_id;
107    std::stack<int> m_if_stack;
108    int scratch_size;
109 };
110 
111 class AssemblyFromShader {
112 public:
113    virtual ~AssemblyFromShader();
114    bool lower(const std::vector<InstructionBlock> &ir);
115 private:
116    virtual bool do_lower(const std::vector<InstructionBlock>& ir)  = 0 ;
117 };
118 
119 }
120 
121 static inline nir_ssa_def *
r600_imm_ivec3(nir_builder * build,int x,int y,int z)122 r600_imm_ivec3(nir_builder *build, int x, int y, int z)
123 {
124    nir_const_value v[3] = {
125       nir_const_value_for_int(x, 32),
126       nir_const_value_for_int(y, 32),
127       nir_const_value_for_int(z, 32),
128    };
129 
130    return nir_build_imm(build, 3, 32, v);
131 }
132 
133 bool r600_lower_tess_io(nir_shader *shader, enum pipe_prim_type prim_type);
134 bool r600_append_tcs_TF_emission(nir_shader *shader, enum pipe_prim_type prim_type);
135 bool r600_lower_tess_coord(nir_shader *sh, enum pipe_prim_type prim_type);
136 
137 bool
138 r600_legalize_image_load_store(nir_shader *shader);
139 
140 
141 #else
142 #include "gallium/drivers/r600/r600_shader.h"
143 #endif
144 
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148 
149 bool r600_vectorize_vs_inputs(nir_shader *shader);
150 
151 bool r600_lower_to_scalar_instr_filter(const nir_instr *instr, const void *);
152 
153 int r600_shader_from_nir(struct r600_context *rctx,
154                          struct r600_pipe_shader *pipeshader,
155                          union r600_shader_key *key);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 
162 #endif // SFN_NIR_H
163