1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2017 Ilia Mirkin
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
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/**
25b8e80941Smrg * \file lower_cs_derived.cpp
26b8e80941Smrg *
27b8e80941Smrg * For hardware that does not support the gl_GlobalInvocationID and
28b8e80941Smrg * gl_LocalInvocationIndex system values, replace them with fresh
29b8e80941Smrg * globals. Note that we can't rely on gl_WorkGroupSize or
30b8e80941Smrg * gl_LocalGroupSizeARB being available, since they may only have been defined
31b8e80941Smrg * in a non-main shader.
32b8e80941Smrg *
33b8e80941Smrg * [ This can happen if only a secondary shader has the layout(local_size_*)
34b8e80941Smrg *   declaration. ]
35b8e80941Smrg *
36b8e80941Smrg * This is meant to be run post-linking.
37b8e80941Smrg */
38b8e80941Smrg
39b8e80941Smrg#include "glsl_symbol_table.h"
40b8e80941Smrg#include "ir_hierarchical_visitor.h"
41b8e80941Smrg#include "ir.h"
42b8e80941Smrg#include "ir_builder.h"
43b8e80941Smrg#include "linker.h"
44b8e80941Smrg#include "program/prog_statevars.h"
45b8e80941Smrg#include "builtin_functions.h"
46b8e80941Smrg#include "main/mtypes.h"
47b8e80941Smrg
48b8e80941Smrgusing namespace ir_builder;
49b8e80941Smrg
50b8e80941Smrgnamespace {
51b8e80941Smrg
52b8e80941Smrgclass lower_cs_derived_visitor : public ir_hierarchical_visitor {
53b8e80941Smrgpublic:
54b8e80941Smrg   explicit lower_cs_derived_visitor(gl_linked_shader *shader)
55b8e80941Smrg      : progress(false),
56b8e80941Smrg        shader(shader),
57b8e80941Smrg        local_size_variable(shader->Program->info.cs.local_size_variable),
58b8e80941Smrg        gl_WorkGroupSize(NULL),
59b8e80941Smrg        gl_WorkGroupID(NULL),
60b8e80941Smrg        gl_LocalInvocationID(NULL),
61b8e80941Smrg        gl_GlobalInvocationID(NULL),
62b8e80941Smrg        gl_LocalInvocationIndex(NULL)
63b8e80941Smrg   {
64b8e80941Smrg      main_sig = _mesa_get_main_function_signature(shader->symbols);
65b8e80941Smrg      assert(main_sig);
66b8e80941Smrg   }
67b8e80941Smrg
68b8e80941Smrg   virtual ir_visitor_status visit(ir_dereference_variable *);
69b8e80941Smrg
70b8e80941Smrg   ir_variable *add_system_value(
71b8e80941Smrg         int slot, const glsl_type *type, const char *name);
72b8e80941Smrg   void find_sysvals();
73b8e80941Smrg   void make_gl_GlobalInvocationID();
74b8e80941Smrg   void make_gl_LocalInvocationIndex();
75b8e80941Smrg
76b8e80941Smrg   bool progress;
77b8e80941Smrg
78b8e80941Smrgprivate:
79b8e80941Smrg   gl_linked_shader *shader;
80b8e80941Smrg   bool local_size_variable;
81b8e80941Smrg   ir_function_signature *main_sig;
82b8e80941Smrg
83b8e80941Smrg   ir_rvalue *gl_WorkGroupSize;
84b8e80941Smrg   ir_variable *gl_WorkGroupID;
85b8e80941Smrg   ir_variable *gl_LocalInvocationID;
86b8e80941Smrg
87b8e80941Smrg   ir_variable *gl_GlobalInvocationID;
88b8e80941Smrg   ir_variable *gl_LocalInvocationIndex;
89b8e80941Smrg};
90b8e80941Smrg
91b8e80941Smrg} /* anonymous namespace */
92b8e80941Smrg
93b8e80941Smrgir_variable *
94b8e80941Smrglower_cs_derived_visitor::add_system_value(
95b8e80941Smrg      int slot, const glsl_type *type, const char *name)
96b8e80941Smrg{
97b8e80941Smrg   ir_variable *var = new(shader) ir_variable(type, name, ir_var_system_value);
98b8e80941Smrg   var->data.how_declared = ir_var_declared_implicitly;
99b8e80941Smrg   var->data.read_only = true;
100b8e80941Smrg   var->data.location = slot;
101b8e80941Smrg   var->data.explicit_location = true;
102b8e80941Smrg   var->data.explicit_index = 0;
103b8e80941Smrg   shader->ir->push_head(var);
104b8e80941Smrg
105b8e80941Smrg   return var;
106b8e80941Smrg}
107b8e80941Smrg
108b8e80941Smrgvoid
109b8e80941Smrglower_cs_derived_visitor::find_sysvals()
110b8e80941Smrg{
111b8e80941Smrg   if (gl_WorkGroupSize != NULL)
112b8e80941Smrg      return;
113b8e80941Smrg
114b8e80941Smrg   ir_variable *WorkGroupSize;
115b8e80941Smrg   if (local_size_variable)
116b8e80941Smrg      WorkGroupSize = shader->symbols->get_variable("gl_LocalGroupSizeARB");
117b8e80941Smrg   else
118b8e80941Smrg      WorkGroupSize = shader->symbols->get_variable("gl_WorkGroupSize");
119b8e80941Smrg   if (WorkGroupSize)
120b8e80941Smrg      gl_WorkGroupSize = new(shader) ir_dereference_variable(WorkGroupSize);
121b8e80941Smrg   gl_WorkGroupID = shader->symbols->get_variable("gl_WorkGroupID");
122b8e80941Smrg   gl_LocalInvocationID = shader->symbols->get_variable("gl_LocalInvocationID");
123b8e80941Smrg
124b8e80941Smrg   /*
125b8e80941Smrg    * These may be missing due to either dead code elimination, or, in the
126b8e80941Smrg    * case of the group size, due to the layout being declared in a non-main
127b8e80941Smrg    * shader. Re-create them.
128b8e80941Smrg    */
129b8e80941Smrg
130b8e80941Smrg   if (!gl_WorkGroupID)
131b8e80941Smrg      gl_WorkGroupID = add_system_value(
132b8e80941Smrg            SYSTEM_VALUE_WORK_GROUP_ID, glsl_type::uvec3_type, "gl_WorkGroupID");
133b8e80941Smrg   if (!gl_LocalInvocationID)
134b8e80941Smrg      gl_LocalInvocationID = add_system_value(
135b8e80941Smrg            SYSTEM_VALUE_LOCAL_INVOCATION_ID, glsl_type::uvec3_type,
136b8e80941Smrg            "gl_LocalInvocationID");
137b8e80941Smrg   if (!WorkGroupSize) {
138b8e80941Smrg      if (local_size_variable) {
139b8e80941Smrg         gl_WorkGroupSize = new(shader) ir_dereference_variable(
140b8e80941Smrg               add_system_value(
141b8e80941Smrg                     SYSTEM_VALUE_LOCAL_GROUP_SIZE, glsl_type::uvec3_type,
142b8e80941Smrg                     "gl_LocalGroupSizeARB"));
143b8e80941Smrg      } else {
144b8e80941Smrg         ir_constant_data data;
145b8e80941Smrg         memset(&data, 0, sizeof(data));
146b8e80941Smrg         for (int i = 0; i < 3; i++)
147b8e80941Smrg            data.u[i] = shader->Program->info.cs.local_size[i];
148b8e80941Smrg         gl_WorkGroupSize = new(shader) ir_constant(glsl_type::uvec3_type, &data);
149b8e80941Smrg      }
150b8e80941Smrg   }
151b8e80941Smrg}
152b8e80941Smrg
153b8e80941Smrgvoid
154b8e80941Smrglower_cs_derived_visitor::make_gl_GlobalInvocationID()
155b8e80941Smrg{
156b8e80941Smrg   if (gl_GlobalInvocationID != NULL)
157b8e80941Smrg      return;
158b8e80941Smrg
159b8e80941Smrg   find_sysvals();
160b8e80941Smrg
161b8e80941Smrg   /* gl_GlobalInvocationID =
162b8e80941Smrg    *    gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
163b8e80941Smrg    */
164b8e80941Smrg   gl_GlobalInvocationID = new(shader) ir_variable(
165b8e80941Smrg         glsl_type::uvec3_type, "__GlobalInvocationID", ir_var_temporary);
166b8e80941Smrg   shader->ir->push_head(gl_GlobalInvocationID);
167b8e80941Smrg
168b8e80941Smrg   ir_instruction *inst =
169b8e80941Smrg      assign(gl_GlobalInvocationID,
170b8e80941Smrg             add(mul(gl_WorkGroupID, gl_WorkGroupSize->clone(shader, NULL)),
171b8e80941Smrg                 gl_LocalInvocationID));
172b8e80941Smrg   main_sig->body.push_head(inst);
173b8e80941Smrg}
174b8e80941Smrg
175b8e80941Smrgvoid
176b8e80941Smrglower_cs_derived_visitor::make_gl_LocalInvocationIndex()
177b8e80941Smrg{
178b8e80941Smrg   if (gl_LocalInvocationIndex != NULL)
179b8e80941Smrg      return;
180b8e80941Smrg
181b8e80941Smrg   find_sysvals();
182b8e80941Smrg
183b8e80941Smrg   /* gl_LocalInvocationIndex =
184b8e80941Smrg    *    gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
185b8e80941Smrg    *    gl_LocalInvocationID.y * gl_WorkGroupSize.x +
186b8e80941Smrg    *    gl_LocalInvocationID.x;
187b8e80941Smrg    */
188b8e80941Smrg   gl_LocalInvocationIndex = new(shader)
189b8e80941Smrg      ir_variable(glsl_type::uint_type, "__LocalInvocationIndex", ir_var_temporary);
190b8e80941Smrg   shader->ir->push_head(gl_LocalInvocationIndex);
191b8e80941Smrg
192b8e80941Smrg   ir_expression *index_z =
193b8e80941Smrg      mul(mul(swizzle_z(gl_LocalInvocationID), swizzle_x(gl_WorkGroupSize->clone(shader, NULL))),
194b8e80941Smrg          swizzle_y(gl_WorkGroupSize->clone(shader, NULL)));
195b8e80941Smrg   ir_expression *index_y =
196b8e80941Smrg      mul(swizzle_y(gl_LocalInvocationID), swizzle_x(gl_WorkGroupSize->clone(shader, NULL)));
197b8e80941Smrg   ir_expression *index_y_plus_z = add(index_y, index_z);
198b8e80941Smrg   operand index_x(swizzle_x(gl_LocalInvocationID));
199b8e80941Smrg   ir_expression *index_x_plus_y_plus_z = add(index_y_plus_z, index_x);
200b8e80941Smrg   ir_instruction *inst =
201b8e80941Smrg      assign(gl_LocalInvocationIndex, index_x_plus_y_plus_z);
202b8e80941Smrg   main_sig->body.push_head(inst);
203b8e80941Smrg}
204b8e80941Smrg
205b8e80941Smrgir_visitor_status
206b8e80941Smrglower_cs_derived_visitor::visit(ir_dereference_variable *ir)
207b8e80941Smrg{
208b8e80941Smrg   if (ir->var->data.mode == ir_var_system_value &&
209b8e80941Smrg       ir->var->data.location == SYSTEM_VALUE_GLOBAL_INVOCATION_ID) {
210b8e80941Smrg      make_gl_GlobalInvocationID();
211b8e80941Smrg      ir->var = gl_GlobalInvocationID;
212b8e80941Smrg      progress = true;
213b8e80941Smrg   }
214b8e80941Smrg
215b8e80941Smrg   if (ir->var->data.mode == ir_var_system_value &&
216b8e80941Smrg       ir->var->data.location == SYSTEM_VALUE_LOCAL_INVOCATION_INDEX) {
217b8e80941Smrg      make_gl_LocalInvocationIndex();
218b8e80941Smrg      ir->var = gl_LocalInvocationIndex;
219b8e80941Smrg      progress = true;
220b8e80941Smrg   }
221b8e80941Smrg
222b8e80941Smrg   return visit_continue;
223b8e80941Smrg}
224b8e80941Smrg
225b8e80941Smrgbool
226b8e80941Smrglower_cs_derived(gl_linked_shader *shader)
227b8e80941Smrg{
228b8e80941Smrg   if (shader->Stage != MESA_SHADER_COMPUTE)
229b8e80941Smrg      return false;
230b8e80941Smrg
231b8e80941Smrg   lower_cs_derived_visitor v(shader);
232b8e80941Smrg   v.run(shader->ir);
233b8e80941Smrg
234b8e80941Smrg   return v.progress;
235b8e80941Smrg}
236