Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / vulkan / anv_nir_builder.h
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_nir.h"
25 #include "glsl/nir/nir_builder.h"
26 #include "util/ralloc.h"
27
28 /* This file includes NIR helpers used by meta shaders in the Vulkan
29 * driver. Eventually, these will all be merged into nir_builder.
30 * However, for now, keeping them in their own file helps to prevent merge
31 * conflicts.
32 */
33
34 static inline void
35 nir_builder_init_simple_shader(nir_builder *b, gl_shader_stage stage)
36 {
37 b->shader = nir_shader_create(NULL, stage, NULL);
38
39 nir_function *func = nir_function_create(b->shader,
40 ralloc_strdup(b->shader, "main"));
41 nir_function_overload *overload = nir_function_overload_create(func);
42 overload->num_params = 0;
43
44 b->impl = nir_function_impl_create(overload);
45 b->cursor = nir_after_cf_list(&b->impl->body);
46 }
47
48 static inline void
49 nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
50 {
51 nir_intrinsic_instr *copy =
52 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
53 copy->variables[0] = nir_deref_var_create(copy, dest);
54 copy->variables[1] = nir_deref_var_create(copy, src);
55 nir_builder_instr_insert(build, &copy->instr);
56 }
57
58 static inline nir_variable *
59 nir_variable_create(nir_shader *shader, const char *name,
60 const struct glsl_type *type, nir_variable_mode mode)
61 {
62 nir_variable *var = rzalloc(shader, nir_variable);
63 var->name = ralloc_strdup(var, name);
64 var->type = type;
65 var->data.mode = mode;
66
67 if ((mode == nir_var_shader_in && shader->stage != MESA_SHADER_VERTEX) ||
68 (mode == nir_var_shader_out && shader->stage != MESA_SHADER_FRAGMENT))
69 var->data.interpolation = INTERP_QUALIFIER_SMOOTH;
70
71 switch (var->data.mode) {
72 case nir_var_local:
73 assert(!"nir_variable_create cannot be used for local variables");
74 break;
75
76 case nir_var_global:
77 exec_list_push_tail(&shader->globals, &var->node);
78 break;
79
80 case nir_var_shader_in:
81 exec_list_push_tail(&shader->inputs, &var->node);
82 break;
83
84 case nir_var_shader_out:
85 exec_list_push_tail(&shader->outputs, &var->node);
86 break;
87
88 case nir_var_uniform:
89 case nir_var_shader_storage:
90 exec_list_push_tail(&shader->uniforms, &var->node);
91 break;
92
93 case nir_var_system_value:
94 exec_list_push_tail(&shader->system_values, &var->node);
95 break;
96
97 default:
98 unreachable("not reached");
99 }
100
101 return var;
102 }