2 * Copyright © 2014 Broadcom
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
25 * @file vc4_reorder_uniforms.c
27 * After optimization has occurred, rewrites the shader to have uniform reads
28 * reading from the c->uniform_contents[] in order, exactly once each.
30 * This allows optimization and instruction scheduling to move things around
31 * without worrying about how the hardware has the "each uniform read bumps
32 * the uniform read address" property.
35 #include "util/ralloc.h"
36 #include "util/u_math.h"
40 qir_reorder_uniforms(struct vc4_compile
*c
)
42 uint32_t *uniform_index
= NULL
;
43 uint32_t uniform_index_size
= 0;
44 uint32_t next_uniform
= 0;
46 qir_for_each_inst_inorder(inst
, c
) {
49 for (int i
= 0; i
< qir_get_nsrc(inst
); i
++) {
50 if (inst
->src
[i
].file
!= QFILE_UNIF
)
55 if (uniform_index_size
<= new) {
57 MAX2(uniform_index_size
* 2, 16);
59 realloc(uniform_index
,
64 /* If we've got two uniform references in this
65 * instruction, they need to be the same
68 assert(inst
->src
[i
].index
== uniform_index
[new]);
71 uniform_index
[new] = inst
->src
[i
].index
;
72 inst
->src
[i
].index
= new;
76 uint32_t *uniform_data
= ralloc_array(c
, uint32_t, next_uniform
);
77 enum quniform_contents
*uniform_contents
=
78 ralloc_array(c
, enum quniform_contents
, next_uniform
);
80 for (uint32_t i
= 0; i
< next_uniform
; i
++) {
81 uniform_data
[i
] = c
->uniform_data
[uniform_index
[i
]];
82 uniform_contents
[i
] = c
->uniform_contents
[uniform_index
[i
]];
85 ralloc_free(c
->uniform_data
);
86 c
->uniform_data
= uniform_data
;
87 ralloc_free(c
->uniform_contents
);
88 c
->uniform_contents
= uniform_contents
;
89 c
->num_uniforms
= next_uniform
;