1 /* -*- c++ -*- */
2 /*
3  * Copyright © 2010 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef GLSL_LINKER_H
26 #define GLSL_LINKER_H
27 
28 #include "linker_util.h"
29 
30 struct gl_shader_program;
31 struct gl_shader;
32 struct gl_linked_shader;
33 
34 extern bool
35 link_function_calls(gl_shader_program *prog, gl_linked_shader *main,
36                     gl_shader **shader_list, unsigned num_shaders);
37 
38 extern void
39 link_invalidate_variable_locations(exec_list *ir);
40 
41 extern void
42 link_assign_uniform_locations(struct gl_shader_program *prog,
43                               struct gl_context *ctx);
44 
45 extern void
46 link_set_uniform_initializers(struct gl_shader_program *prog,
47                               unsigned int boolean_true);
48 
49 extern int
50 link_cross_validate_uniform_block(void *mem_ctx,
51                                   struct gl_uniform_block **linked_blocks,
52                                   unsigned int *num_linked_blocks,
53                                   struct gl_uniform_block *new_block);
54 
55 extern void
56 link_uniform_blocks(void *mem_ctx,
57                     struct gl_context *ctx,
58                     struct gl_shader_program *prog,
59                     struct gl_linked_shader *shader,
60                     struct gl_uniform_block **ubo_blocks,
61                     unsigned *num_ubo_blocks,
62                     struct gl_uniform_block **ssbo_blocks,
63                     unsigned *num_ssbo_blocks);
64 
65 bool
66 validate_intrastage_arrays(struct gl_shader_program *prog,
67                            ir_variable *const var,
68                            ir_variable *const existing,
69                            bool match_precision = true);
70 
71 void
72 validate_intrastage_interface_blocks(struct gl_shader_program *prog,
73                                      const gl_shader **shader_list,
74                                      unsigned num_shaders);
75 
76 void
77 validate_interstage_inout_blocks(struct gl_shader_program *prog,
78                                  const gl_linked_shader *producer,
79                                  const gl_linked_shader *consumer);
80 
81 void
82 validate_interstage_uniform_blocks(struct gl_shader_program *prog,
83                                    gl_linked_shader **stages);
84 
85 extern void
86 link_assign_atomic_counter_resources(struct gl_context *ctx,
87                                      struct gl_shader_program *prog);
88 
89 extern void
90 link_check_atomic_counter_resources(struct gl_context *ctx,
91                                     struct gl_shader_program *prog);
92 
93 
94 extern struct gl_linked_shader *
95 link_intrastage_shaders(void *mem_ctx,
96                         struct gl_context *ctx,
97                         struct gl_shader_program *prog,
98                         struct gl_shader **shader_list,
99                         unsigned num_shaders,
100                         bool allow_missing_main);
101 
102 extern unsigned
103 link_calculate_matrix_stride(const glsl_type *matrix, bool row_major,
104                              enum glsl_interface_packing packing);
105 
106 /**
107  * Class for processing all of the leaf fields of a variable that corresponds
108  * to a program resource.
109  *
110  * The leaf fields are all the parts of the variable that the application
111  * could query using \c glGetProgramResourceIndex (or that could be returned
112  * by \c glGetProgramResourceName).
113  *
114  * Classes my derive from this class to implement specific functionality.
115  * This class only provides the mechanism to iterate over the leaves.  Derived
116  * classes must implement \c ::visit_field and may override \c ::process.
117  */
118 class program_resource_visitor {
119 public:
120    /**
121     * Begin processing a variable
122     *
123     * Classes that overload this function should call \c ::process from the
124     * base class to start the recursive processing of the variable.
125     *
126     * \param var  The variable that is to be processed
127     *
128     * Calls \c ::visit_field for each leaf of the variable.
129     *
130     * \warning
131     * When processing a uniform block, this entry should only be used in cases
132     * where the row / column ordering of matrices in the block does not
133     * matter.  For example, enumerating the names of members of the block, but
134     * not for determining the offsets of members.
135     */
136    void process(ir_variable *var, bool use_std430_as_default);
137 
138    /**
139     * Begin processing a variable
140     *
141     * Classes that overload this function should call \c ::process from the
142     * base class to start the recursive processing of the variable.
143     *
144     * \param var  The variable that is to be processed
145     * \param var_type The glsl_type reference of the variable
146     *
147     * Calls \c ::visit_field for each leaf of the variable.
148     *
149     * \warning
150     * When processing a uniform block, this entry should only be used in cases
151     * where the row / column ordering of matrices in the block does not
152     * matter.  For example, enumerating the names of members of the block, but
153     * not for determining the offsets of members.
154     */
155    void process(ir_variable *var, const glsl_type *var_type,
156                 bool use_std430_as_default);
157 
158    /**
159     * Begin processing a variable of a structured type.
160     *
161     * This flavor of \c process should be used to handle structured types
162     * (i.e., structures, interfaces, or arrays there of) that need special
163     * name handling.  A common usage is to handle cases where the block name
164     * (instead of the instance name) is used for an interface block.
165     *
166     * \param type  Type that is to be processed, associated with \c name
167     * \param name  Base name of the structured variable being processed
168     *
169     * \note
170     * \c type must be \c GLSL_TYPE_RECORD, \c GLSL_TYPE_INTERFACE, or an array
171     * there of.
172     */
173    void process(const glsl_type *type, const char *name,
174                 bool use_std430_as_default);
175 
176 protected:
177    /**
178     * Method invoked for each leaf of the variable
179     *
180     * \param type  Type of the field.
181     * \param name  Fully qualified name of the field.
182     * \param row_major  For a matrix type, is it stored row-major.
183     * \param record_type  Type of the record containing the field.
184     * \param last_field   Set if \c name is the last field of the structure
185     *                     containing it.  This will always be false for items
186     *                     not contained in a structure or interface block.
187     */
188    virtual void visit_field(const glsl_type *type, const char *name,
189                             bool row_major, const glsl_type *record_type,
190                             const enum glsl_interface_packing packing,
191                             bool last_field) = 0;
192 
193    virtual void enter_record(const glsl_type *type, const char *name,
194                              bool row_major, const enum glsl_interface_packing packing);
195 
196    virtual void leave_record(const glsl_type *type, const char *name,
197                              bool row_major, const enum glsl_interface_packing packing);
198 
199    virtual void set_buffer_offset(unsigned offset);
200 
201    virtual void set_record_array_count(unsigned record_array_count);
202 
203 private:
204    /**
205     * \param name_length  Length of the current name \b not including the
206     *                     terminating \c NUL character.
207     * \param last_field   Set if \c name is the last field of the structure
208     *                     containing it.  This will always be false for items
209     *                     not contained in a structure or interface block.
210     */
211    void recursion(const glsl_type *t, char **name, size_t name_length,
212                   bool row_major, const glsl_type *record_type,
213                   const enum glsl_interface_packing packing,
214                   bool last_field, unsigned record_array_count,
215                   const glsl_struct_field *named_ifc_member);
216 };
217 
218 #endif /* GLSL_LINKER_H */
219