1/* 2 * Copyright © 2016 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 "nir_builder.h" 25#include "program/prog_instruction.h" 26 27static nir_variable * 28make_uniform(nir_shader *nir, const gl_state_index16 *tokens) 29{ 30 /* Note: name must be prefixed with "gl_" to trigger slot based 31 * special handling in uniform setup. 32 */ 33 nir_variable *var = 34 nir_variable_create(nir, nir_var_uniform, glsl_int_type(), 35 "gl_PatchVerticesIn"); 36 var->num_state_slots = 1; 37 var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots); 38 memcpy(var->state_slots[0].tokens, tokens, sizeof(*tokens) * STATE_LENGTH); 39 var->state_slots[0].swizzle = SWIZZLE_XXXX; 40 41 return var; 42} 43 44/** 45 * This pass lowers the load_patch_vertices_in intrinsic. 46 * 47 * - If we statically know the value, we lower it to a constant. 48 * (If a TES is linked against a TCS, the TCS tells us the TES input count.) 49 * 50 * - If not, and we're given Mesa state slots, we lower it to a uniform. 51 * 52 * - Otherwise, we leave it as a system value. 53 * 54 * This pass must be run after nir_lower_system_values(). 55 */ 56bool 57nir_lower_patch_vertices(nir_shader *nir, 58 unsigned static_count, 59 const gl_state_index16 *uniform_state_tokens) 60{ 61 bool progress = false; 62 nir_variable *var = NULL; 63 64 /* If there's no static count and we don't want uniforms, there's no 65 * lowering to do...just bail early. 66 */ 67 if (static_count == 0 && !uniform_state_tokens) 68 return false; 69 70 nir_foreach_function(function, nir) { 71 if (function->impl) { 72 nir_foreach_block(block, function->impl) { 73 nir_builder b; 74 nir_builder_init(&b, function->impl); 75 nir_foreach_instr_safe(instr, block) { 76 if (instr->type == nir_instr_type_intrinsic) { 77 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 78 if (intr->intrinsic != nir_intrinsic_load_patch_vertices_in) 79 continue; 80 81 b.cursor = nir_before_instr(&intr->instr); 82 83 nir_ssa_def *val = NULL; 84 if (static_count) { 85 val = nir_imm_int(&b, static_count); 86 } else { 87 if (!var) 88 var = make_uniform(nir, uniform_state_tokens); 89 90 val = nir_load_var(&b, var); 91 } 92 93 progress = true; 94 nir_ssa_def_rewrite_uses(&intr->dest.ssa, 95 val); 96 nir_instr_remove(instr); 97 } 98 } 99 } 100 101 if (progress) { 102 nir_metadata_preserve(function->impl, nir_metadata_block_index | 103 nir_metadata_dominance); 104 } 105 } 106 } 107 108 return progress; 109} 110