2 * Copyright © 2016 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
24 #include "nir_builder.h"
25 #include "program/prog_instruction.h"
28 make_uniform(nir_shader
*nir
, const gl_state_index16
*tokens
)
30 /* Note: name must be prefixed with "gl_" to trigger slot based
31 * special handling in uniform setup.
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
;
45 * This pass lowers the load_patch_vertices_in intrinsic.
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.)
50 * - If not, and we're given Mesa state slots, we lower it to a uniform.
52 * - Otherwise, we leave it as a system value.
54 * This pass must be run after nir_lower_system_values().
57 nir_lower_patch_vertices(nir_shader
*nir
,
58 unsigned static_count
,
59 const gl_state_index16
*uniform_state_tokens
)
61 bool progress
= false;
62 nir_variable
*var
= NULL
;
64 /* If there's no static count and we don't want uniforms, there's no
65 * lowering to do...just bail early.
67 if (static_count
== 0 && !uniform_state_tokens
)
70 nir_foreach_function(function
, nir
) {
72 nir_foreach_block(block
, function
->impl
) {
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
)
81 b
.cursor
= nir_before_instr(&intr
->instr
);
83 nir_ssa_def
*val
= NULL
;
85 val
= nir_imm_int(&b
, static_count
);
88 var
= make_uniform(nir
, uniform_state_tokens
);
90 val
= nir_load_var(&b
, var
);
94 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
,
95 nir_src_for_ssa(val
));
96 nir_instr_remove(instr
);
102 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
103 nir_metadata_dominance
);