1 /* 2 * Copyright 2014 Broadcom 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 /** 25 * @file vc4_reorder_uniforms.c 26 * 27 * After optimization has occurred, rewrites the shader to have uniform reads 28 * reading from the c->uniform_contents[] in order, exactly once each. 29 * 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. 33 */ 34 35 #include "util/ralloc.h" 36 #include "util/u_math.h" 37 #include "vc4_qir.h" 38 39 void 40 qir_reorder_uniforms(struct vc4_compile *c) 41 { 42 uint32_t *uniform_index = NULL; 43 uint32_t uniform_index_size = 0; 44 uint32_t next_uniform = 0; 45 46 qir_for_each_inst_inorder(inst, c) { 47 uint32_t new = ~0; 48 49 for (int i = 0; i < qir_get_nsrc(inst); i++) { 50 if (inst->src[i].file != QFILE_UNIF) 51 continue; 52 53 if (new == ~0) { 54 new = next_uniform++; 55 if (uniform_index_size <= new) { 56 uniform_index_size = 57 MAX2(uniform_index_size * 2, 16); 58 uniform_index = 59 realloc(uniform_index, 60 uniform_index_size * 61 sizeof(uint32_t)); 62 } 63 } else { 64 /* If we've got two uniform references in this 65 * instruction, they need to be the same 66 * uniform value. 67 */ 68 assert(inst->src[i].index == uniform_index[new]); 69 } 70 71 uniform_index[new] = inst->src[i].index; 72 inst->src[i].index = new; 73 } 74 } 75 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); 79 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]]; 83 } 84 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; 90 91 free(uniform_index); 92 } 93