1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir_builder.h"
25bf215546Sopenharmony_ci#include "program/prog_instruction.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cistatic nir_variable *
28bf215546Sopenharmony_cimake_uniform(nir_shader *nir, const gl_state_index16 *tokens)
29bf215546Sopenharmony_ci{
30bf215546Sopenharmony_ci   /* Note: name must be prefixed with "gl_" to trigger slot based
31bf215546Sopenharmony_ci    * special handling in uniform setup.
32bf215546Sopenharmony_ci    */
33bf215546Sopenharmony_ci   nir_variable *var =
34bf215546Sopenharmony_ci      nir_variable_create(nir, nir_var_uniform, glsl_int_type(),
35bf215546Sopenharmony_ci                          "gl_PatchVerticesIn");
36bf215546Sopenharmony_ci   var->num_state_slots = 1;
37bf215546Sopenharmony_ci   var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);
38bf215546Sopenharmony_ci   memcpy(var->state_slots[0].tokens, tokens, sizeof(*tokens) * STATE_LENGTH);
39bf215546Sopenharmony_ci   var->state_slots[0].swizzle = SWIZZLE_XXXX;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   return var;
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci/**
45bf215546Sopenharmony_ci * This pass lowers the load_patch_vertices_in intrinsic.
46bf215546Sopenharmony_ci *
47bf215546Sopenharmony_ci * - If we statically know the value, we lower it to a constant.
48bf215546Sopenharmony_ci *   (If a TES is linked against a TCS, the TCS tells us the TES input count.)
49bf215546Sopenharmony_ci *
50bf215546Sopenharmony_ci * - If not, and we're given Mesa state slots, we lower it to a uniform.
51bf215546Sopenharmony_ci *
52bf215546Sopenharmony_ci * - Otherwise, we leave it as a system value.
53bf215546Sopenharmony_ci *
54bf215546Sopenharmony_ci * This pass must be run after nir_lower_system_values().
55bf215546Sopenharmony_ci */
56bf215546Sopenharmony_cibool
57bf215546Sopenharmony_cinir_lower_patch_vertices(nir_shader *nir,
58bf215546Sopenharmony_ci                         unsigned static_count,
59bf215546Sopenharmony_ci                         const gl_state_index16 *uniform_state_tokens)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   bool progress = false;
62bf215546Sopenharmony_ci   nir_variable *var = NULL;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   /* If there's no static count and we don't want uniforms, there's no
65bf215546Sopenharmony_ci    * lowering to do...just bail early.
66bf215546Sopenharmony_ci    */
67bf215546Sopenharmony_ci   if (static_count == 0 && !uniform_state_tokens)
68bf215546Sopenharmony_ci      return false;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   nir_foreach_function(function, nir) {
71bf215546Sopenharmony_ci      if (function->impl) {
72bf215546Sopenharmony_ci         nir_foreach_block(block, function->impl) {
73bf215546Sopenharmony_ci            nir_builder b;
74bf215546Sopenharmony_ci            nir_builder_init(&b, function->impl);
75bf215546Sopenharmony_ci            nir_foreach_instr_safe(instr, block) {
76bf215546Sopenharmony_ci               if (instr->type == nir_instr_type_intrinsic) {
77bf215546Sopenharmony_ci                  nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
78bf215546Sopenharmony_ci                  if (intr->intrinsic != nir_intrinsic_load_patch_vertices_in)
79bf215546Sopenharmony_ci                     continue;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci                  b.cursor = nir_before_instr(&intr->instr);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci                  nir_ssa_def *val = NULL;
84bf215546Sopenharmony_ci                  if (static_count) {
85bf215546Sopenharmony_ci                     val = nir_imm_int(&b, static_count);
86bf215546Sopenharmony_ci                  } else {
87bf215546Sopenharmony_ci                     if (!var)
88bf215546Sopenharmony_ci                        var = make_uniform(nir, uniform_state_tokens);
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci                     val = nir_load_var(&b, var);
91bf215546Sopenharmony_ci                  }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci                  progress = true;
94bf215546Sopenharmony_ci                  nir_ssa_def_rewrite_uses(&intr->dest.ssa,
95bf215546Sopenharmony_ci                                           val);
96bf215546Sopenharmony_ci                  nir_instr_remove(instr);
97bf215546Sopenharmony_ci               }
98bf215546Sopenharmony_ci            }
99bf215546Sopenharmony_ci         }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci         if (progress) {
102bf215546Sopenharmony_ci            nir_metadata_preserve(function->impl, nir_metadata_block_index |
103bf215546Sopenharmony_ci                                                  nir_metadata_dominance);
104bf215546Sopenharmony_ci         }
105bf215546Sopenharmony_ci      }
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   return progress;
109bf215546Sopenharmony_ci}
110