1 /*
2 * Copyright (c) 2020 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian Gmeiner <[email protected]>
25 */
26
27 #include "etnaviv_nir.h"
28
29 /*
30 * Pass to lower the load_ubo intrinsics for block 0 back to load_uniform intrinsics.
31 */
32
33 static bool
is_const_ubo(const nir_instr * instr,const void * _data)34 is_const_ubo(const nir_instr *instr, const void *_data)
35 {
36 if (instr->type != nir_instr_type_intrinsic)
37 return false;
38
39 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
40 if (intr->intrinsic != nir_intrinsic_load_ubo)
41 return false;
42
43 if (!nir_src_is_const(intr->src[0]) || !nir_src_is_const(intr->src[1]))
44 return false;
45
46 const uint32_t block = nir_src_as_uint(intr->src[0]);
47 if (block > 0)
48 return false;
49
50 return true;
51 }
52
53 static nir_def *
lower_ubo_to_uniform(nir_builder * b,nir_instr * instr,void * _data)54 lower_ubo_to_uniform(nir_builder *b, nir_instr *instr, void *_data)
55 {
56 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
57 b->cursor = nir_before_instr(instr);
58
59 /* Undo the operations done in nir_lower_uniforms_to_ubo. */
60 nir_def *ubo_offset = intr->src[1].ssa;
61 nir_def *range_base = nir_imm_int(b, nir_intrinsic_range_base(intr));
62
63 nir_def *uniform_offset =
64 nir_ushr_imm(b, nir_isub(b, ubo_offset, range_base), 4);
65
66 nir_def *uniform =
67 nir_load_uniform(b, intr->num_components, intr->def.bit_size, uniform_offset,
68 .base = nir_intrinsic_range_base(intr) / 16,
69 .range = nir_intrinsic_range(intr) / 16,
70 .dest_type = nir_type_float32);
71
72 nir_def_rewrite_uses(&intr->def, uniform);
73
74 return uniform;
75 }
76
77 bool
etna_nir_lower_ubo_to_uniform(nir_shader * shader)78 etna_nir_lower_ubo_to_uniform(nir_shader *shader)
79 {
80 return nir_shader_lower_instructions(shader,
81 is_const_ubo,
82 lower_ubo_to_uniform,
83 NULL);
84
85 }
86