1 /*
2 * Copyright © 2022 Google, Inc.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 /*
28 * A helper to create a passthrough TCS shader for drivers that cannot handle
29 * having a TES without TCS.
30 *
31 * Uses the load_tess_level_outer_default and load_tess_level_inner_default
32 * intrinsics to load the gl_TessLevelOuter and gl_TessLevelInner values,
33 * so driver will somehow need to implement those to load the values set
34 * by pipe_context::set_tess_state() or similar.
35 */
36
37 nir_shader *
nir_create_passthrough_tcs_impl(const nir_shader_compiler_options * options,unsigned * locations,unsigned num_locations,uint8_t patch_vertices)38 nir_create_passthrough_tcs_impl(const nir_shader_compiler_options *options,
39 unsigned *locations, unsigned num_locations,
40 uint8_t patch_vertices)
41 {
42 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_TESS_CTRL, options,
43 "tcs passthrough");
44
45 nir_variable *in_inner =
46 nir_create_variable_with_location(b.shader, nir_var_system_value,
47 SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT, glsl_vec_type(2));
48
49 nir_variable *out_inner =
50 nir_create_variable_with_location(b.shader, nir_var_shader_out,
51 VARYING_SLOT_TESS_LEVEL_INNER, glsl_vec_type(2));
52
53 nir_def *inner = nir_load_var(&b, in_inner);
54 nir_store_var(&b, out_inner, inner, 0x3);
55
56 nir_variable *in_outer =
57 nir_create_variable_with_location(b.shader, nir_var_system_value,
58 SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT, glsl_vec4_type());
59
60 nir_variable *out_outer =
61 nir_create_variable_with_location(b.shader, nir_var_shader_out,
62 VARYING_SLOT_TESS_LEVEL_OUTER, glsl_vec4_type());
63
64 nir_def *outer = nir_load_var(&b, in_outer);
65 nir_store_var(&b, out_outer, outer, 0xf);
66
67 nir_def *id = nir_load_invocation_id(&b);
68 for (unsigned i = 0; i < num_locations; i++) {
69 const struct glsl_type *type;
70 unsigned semantic = locations[i];
71
72 /* These are illegal in TCS. */
73 if (semantic == VARYING_SLOT_EDGE ||
74 semantic == VARYING_SLOT_PRIMITIVE_ID ||
75 semantic == VARYING_SLOT_LAYER ||
76 semantic == VARYING_SLOT_VIEWPORT ||
77 semantic == VARYING_SLOT_VIEW_INDEX ||
78 semantic == VARYING_SLOT_VIEWPORT_MASK ||
79 semantic == VARYING_SLOT_PRIMITIVE_SHADING_RATE)
80 continue;
81
82 type = glsl_array_type(glsl_vec4_type(), 0, 0);
83
84 nir_variable *in = nir_create_variable_with_location(b.shader, nir_var_shader_in,
85 semantic, type);
86
87 nir_variable *out = nir_create_variable_with_location(b.shader, nir_var_shader_out,
88 semantic, type);
89
90 /* no need to use copy_var to save a lower pass */
91 nir_def *value = nir_load_array_var(&b, in, id);
92 nir_store_array_var(&b, out, id, value, 0xf);
93 }
94
95 b.shader->info.tess.tcs_vertices_out = patch_vertices;
96
97 nir_validate_shader(b.shader, "in nir_create_passthrough_tcs");
98
99 return b.shader;
100 }
101
102 nir_shader *
nir_create_passthrough_tcs(const nir_shader_compiler_options * options,const nir_shader * vs,uint8_t patch_vertices)103 nir_create_passthrough_tcs(const nir_shader_compiler_options *options,
104 const nir_shader *vs, uint8_t patch_vertices)
105 {
106 unsigned locations[MAX_VARYING];
107 unsigned num_outputs = 0;
108
109 nir_foreach_shader_out_variable(var, vs) {
110 assert(num_outputs < ARRAY_SIZE(locations));
111 locations[num_outputs++] = var->data.location;
112 }
113
114 return nir_create_passthrough_tcs_impl(options, locations, num_outputs, patch_vertices);
115 }
116