Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_reorder_uniforms.c
4570 views
/*1* Copyright © 2014 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/**24* @file vc4_reorder_uniforms.c25*26* After optimization has occurred, rewrites the shader to have uniform reads27* reading from the c->uniform_contents[] in order, exactly once each.28*29* This allows optimization and instruction scheduling to move things around30* without worrying about how the hardware has the "each uniform read bumps31* the uniform read address" property.32*/3334#include "util/ralloc.h"35#include "util/u_math.h"36#include "vc4_qir.h"3738void39qir_reorder_uniforms(struct vc4_compile *c)40{41uint32_t *uniform_index = NULL;42uint32_t uniform_index_size = 0;43uint32_t next_uniform = 0;4445qir_for_each_inst_inorder(inst, c) {46uint32_t new = ~0;4748for (int i = 0; i < qir_get_nsrc(inst); i++) {49if (inst->src[i].file != QFILE_UNIF)50continue;5152if (new == ~0) {53new = next_uniform++;54if (uniform_index_size <= new) {55uniform_index_size =56MAX2(uniform_index_size * 2, 16);57uniform_index =58realloc(uniform_index,59uniform_index_size *60sizeof(uint32_t));61}62} else {63/* If we've got two uniform references in this64* instruction, they need to be the same65* uniform value.66*/67assert(inst->src[i].index == uniform_index[new]);68}6970uniform_index[new] = inst->src[i].index;71inst->src[i].index = new;72}73}7475uint32_t *uniform_data = ralloc_array(c, uint32_t, next_uniform);76enum quniform_contents *uniform_contents =77ralloc_array(c, enum quniform_contents, next_uniform);7879for (uint32_t i = 0; i < next_uniform; i++) {80uniform_data[i] = c->uniform_data[uniform_index[i]];81uniform_contents[i] = c->uniform_contents[uniform_index[i]];82}8384ralloc_free(c->uniform_data);85c->uniform_data = uniform_data;86ralloc_free(c->uniform_contents);87c->uniform_contents = uniform_contents;88c->num_uniforms = next_uniform;8990free(uniform_index);91}929394