1 /* -*- mesa-c++  -*-
2  *
3  * Copyright (c) 2018 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_SHADERIO_H
28 #define SFN_SHADERIO_H
29 
30 #include "compiler/nir/nir.h"
31 #include "pipe/p_defines.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "gallium/drivers/r600/r600_shader.h"
34 
35 #include <vector>
36 #include <memory>
37 
38 namespace r600 {
39 
40 class ShaderInput {
41 public:
42    ShaderInput();
43    virtual  ~ShaderInput();
44 
45    ShaderInput(tgsi_semantic name);
name()46    tgsi_semantic name() const {return m_name;}
47 
set_gpr(int gpr)48    void set_gpr(int gpr) {m_gpr = gpr;}
gpr()49    int gpr() const {return m_gpr;}
50    void set_ioinfo(r600_shader_io& io, int translated_ij_index) const;
51 
52    virtual void set_lds_pos(int lds_pos);
53    virtual int ij_index() const;
54    virtual bool interpolate() const;
55    virtual int lds_pos() const;
56    void set_uses_interpolate_at_centroid();
57 
58    virtual bool is_varying() const;
59 
60 private:
61    virtual void set_specific_ioinfo(r600_shader_io& io) const;
62 
63    tgsi_semantic m_name;
64    int m_gpr;
65    bool m_uses_interpolate_at_centroid;
66 };
67 
68 using PShaderInput = std::shared_ptr<ShaderInput>;
69 
70 class ShaderInputSystemValue: public ShaderInput {
71 public:
72    ShaderInputSystemValue(tgsi_semantic name, int gpr);
73    void set_specific_ioinfo(r600_shader_io& io) const;
74    int m_gpr;
75 };
76 
77 class ShaderInputVarying : public ShaderInput {
78 public:
79    ShaderInputVarying(tgsi_semantic name, int sid, nir_variable *input);
80    ShaderInputVarying(tgsi_semantic name, const ShaderInputVarying& orig,
81                       size_t location);
82 
83    void set_lds_pos(int lds_pos) override;
84 
85    int ij_index() const override;
86 
87    bool interpolate() const override;
88 
89    int lds_pos() const override;
90 
sid()91    int sid() const {return m_sid;}
92 
93    void update_mask(int additional_comps);
94 
location()95    size_t location() const {return m_driver_location;}
location_frac()96    int location_frac() const {return m_location_frac;}
97 
98    bool is_varying() const override;
99 
100 private:
101    void evaluate_spi_sid();
102 
103    virtual void set_color_ioinfo(r600_shader_io& io) const;
104    void set_specific_ioinfo(r600_shader_io& io) const override;
105    size_t m_driver_location;
106    int m_location_frac;
107    int m_sid;
108    int m_spi_sid;
109    tgsi_interpolate_mode m_interpolate;
110    tgsi_interpolate_loc m_interpolate_loc;
111    int m_ij_index;
112    int m_lds_pos;
113    int m_mask;
114 };
115 
116 class ShaderInputColor: public ShaderInputVarying {
117 public:
118    ShaderInputColor(tgsi_semantic name, int sid, nir_variable *input);
119    void set_back_color(unsigned back_color_input_idx);
back_color_input_index()120    unsigned back_color_input_index() const {
121       return m_back_color_input_idx;
122    }
123 private:
124    void set_color_ioinfo(UNUSED r600_shader_io& io) const override;
125    unsigned m_back_color_input_idx;
126 
127 };
128 
129 class ShaderIO
130 {
131 public:
132    ShaderIO();
133 
134    size_t add_input(ShaderInput *input);
135 
136    std::vector<PShaderInput>& inputs();
137    ShaderInput& input(size_t k);
138 
139    ShaderInput& input(size_t driver_loc, int frac);
140 
141    void set_two_sided();
two_sided()142    bool two_sided() {return m_two_sided;}
143 
nlds()144    int nlds() const  {
145       return m_lds_pos;
146    }
147 
148    void sort_varying_inputs();
149 
size()150    size_t size() const {return m_inputs.size();}
151 
152    PShaderInput find_varying(tgsi_semantic name, int sid, int frac);
153 
154    void update_lds_pos();
155 
156 private:
157    std::vector<PShaderInput> m_inputs;
158    std::vector<int> m_ldspos;
159    bool m_two_sided;
160    int m_lds_pos;
161 
162 };
163 
164 std::pair<unsigned, unsigned>
165 r600_get_varying_semantic(unsigned varying_location);
166 
167 
168 }
169 
170 #endif // SFN_SHADERIO_H
171