2 * Copyright © 2015 Intel Corporation
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:
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
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
25 #include "glsl/nir/nir_builder.h"
26 #include "util/ralloc.h"
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
35 nir_builder_init_simple_shader(nir_builder
*b
, gl_shader_stage stage
)
37 b
->shader
= nir_shader_create(NULL
, stage
, NULL
);
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;
44 b
->impl
= nir_function_impl_create(overload
);
45 b
->cursor
= nir_after_cf_list(&b
->impl
->body
);
49 nir_copy_var(nir_builder
*build
, nir_variable
*dest
, nir_variable
*src
)
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
, ©
->instr
);
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
)
62 nir_variable
*var
= rzalloc(shader
, nir_variable
);
63 var
->name
= ralloc_strdup(var
, name
);
65 var
->data
.mode
= mode
;
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
;
71 switch (var
->data
.mode
) {
73 assert(!"nir_variable_create cannot be used for local variables");
77 exec_list_push_tail(&shader
->globals
, &var
->node
);
80 case nir_var_shader_in
:
81 exec_list_push_tail(&shader
->inputs
, &var
->node
);
84 case nir_var_shader_out
:
85 exec_list_push_tail(&shader
->outputs
, &var
->node
);
89 case nir_var_shader_storage
:
90 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
93 case nir_var_system_value
:
94 exec_list_push_tail(&shader
->system_values
, &var
->node
);
98 unreachable("not reached");