4917bdc59540e52abf765187a4a21e1c4544c6e7
[mesa.git] / src / vulkan / anv_nir_lower_push_constants.c
1 /*
2 * Copyright © 2015 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 "anv_nir.h"
25
26 struct lower_push_constants_state {
27 nir_shader *shader;
28 bool is_scalar;
29 };
30
31 static bool
32 lower_push_constants_block(nir_block *block, void *void_state)
33 {
34 struct lower_push_constants_state *state = void_state;
35
36 nir_foreach_instr(block, instr) {
37 if (instr->type != nir_instr_type_intrinsic)
38 continue;
39
40 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
41
42 /* TODO: Handle indirect push constants */
43 if (intrin->intrinsic != nir_intrinsic_load_push_constant)
44 continue;
45
46 assert(intrin->const_index[0] % 4 == 0);
47 unsigned dword_offset = intrin->const_index[0] / 4;
48
49 /* We just turn them into uniform loads with the appropreate offset */
50 intrin->intrinsic = nir_intrinsic_load_uniform;
51 intrin->const_index[0] = 0;
52 if (state->is_scalar) {
53 intrin->const_index[1] = dword_offset;
54 } else {
55 unsigned shift = dword_offset % 4;
56 /* Can't cross the vec4 boundary */
57 assert(shift + intrin->num_components <= 4);
58
59 /* vec4 shifts are in units of vec4's */
60 intrin->const_index[1] = dword_offset / 4;
61
62 if (shift) {
63 /* If there's a non-zero shift then we need to load a whole vec4
64 * and use a move to swizzle it into place.
65 */
66 assert(intrin->dest.is_ssa);
67 nir_alu_instr *mov = nir_alu_instr_create(state->shader,
68 nir_op_imov);
69 mov->src[0].src = nir_src_for_ssa(&intrin->dest.ssa);
70 for (unsigned i = 0; i < intrin->num_components; i++)
71 mov->src[0].swizzle[i] = i + shift;
72 mov->dest.write_mask = (1 << intrin->num_components) - 1;
73 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
74 intrin->num_components, NULL);
75
76 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
77 nir_src_for_ssa(&mov->dest.dest.ssa));
78 nir_instr_insert_after(&intrin->instr, &mov->instr);
79
80 /* Stomp the number of components to 4 */
81 intrin->num_components = 4;
82 intrin->dest.ssa.num_components = 4;
83 }
84 }
85 }
86
87 return true;
88 }
89
90 void
91 anv_nir_lower_push_constants(nir_shader *shader, bool is_scalar)
92 {
93 struct lower_push_constants_state state = {
94 .shader = shader,
95 .is_scalar = is_scalar,
96 };
97
98 nir_foreach_overload(shader, overload) {
99 if (overload->impl)
100 nir_foreach_block(overload->impl, lower_push_constants_block, &state);
101 }
102
103 assert(shader->num_uniforms % 4 == 0);
104 if (is_scalar)
105 shader->num_uniforms /= 4;
106 else
107 shader->num_uniforms = DIV_ROUND_UP(shader->num_uniforms, 16);
108 }