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;
45 struct simple_node
*node
;
46 foreach(node
, &c
->instructions
) {
47 struct qinst
*inst
= (struct qinst
*)node
;
49 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
50 if (inst
->src
[i
].file
!= QFILE_UNIF
)
53 uint32_t new = next_uniform
++;
54 if (uniform_index_size
<= new) {
56 MAX2(uniform_index_size
* 2, 16);
58 realloc(uniform_index
,
63 uniform_index
[new] = inst
->src
[i
].index
;
64 inst
->src
[i
].index
= new;
68 uint32_t *uniform_data
= ralloc_array(c
, uint32_t, next_uniform
);
69 enum quniform_contents
*uniform_contents
=
70 ralloc_array(c
, enum quniform_contents
, next_uniform
);
72 for (uint32_t i
= 0; i
< next_uniform
; i
++) {
73 uniform_data
[i
] = c
->uniform_data
[uniform_index
[i
]];
74 uniform_contents
[i
] = c
->uniform_contents
[uniform_index
[i
]];
77 ralloc_free(c
->uniform_data
);
78 c
->uniform_data
= uniform_data
;
79 ralloc_free(c
->uniform_contents
);
80 c
->uniform_contents
= uniform_contents
;
81 c
->num_uniforms
= next_uniform
;