Path: blob/21.2-virgl/src/panfrost/util/pan_sysval.c
4560 views
/*1* Copyright (C) 2020 Collabora Ltd.2*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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors (Collabora):23* Alyssa Rosenzweig <[email protected]>24*/2526#include "pan_ir.h"27#include "compiler/nir/nir_builder.h"2829/* TODO: ssbo_size */30static int31panfrost_sysval_for_ssbo(nir_intrinsic_instr *instr)32{33nir_src index = instr->src[0];34assert(nir_src_is_const(index));35uint32_t uindex = nir_src_as_uint(index);3637return PAN_SYSVAL(SSBO, uindex);38}3940static int41panfrost_sysval_for_sampler(nir_intrinsic_instr *instr)42{43/* TODO: indirect samplers !!! */44nir_src index = instr->src[0];45assert(nir_src_is_const(index));46uint32_t uindex = nir_src_as_uint(index);4748return PAN_SYSVAL(SAMPLER, uindex);49}5051static int52panfrost_sysval_for_image_size(nir_intrinsic_instr *instr)53{54nir_src index = instr->src[0];55assert(nir_src_is_const(index));5657bool is_array = nir_intrinsic_image_array(instr);58uint32_t uindex = nir_src_as_uint(index);59unsigned dim = nir_intrinsic_dest_components(instr) - is_array;6061return PAN_SYSVAL(IMAGE_SIZE, PAN_TXS_SYSVAL_ID(uindex, dim, is_array));62}6364static unsigned65panfrost_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)66{67switch (instr->intrinsic) {68case nir_intrinsic_load_viewport_scale:69return PAN_SYSVAL_VIEWPORT_SCALE;70case nir_intrinsic_load_viewport_offset:71return PAN_SYSVAL_VIEWPORT_OFFSET;72case nir_intrinsic_load_num_workgroups:73return PAN_SYSVAL_NUM_WORK_GROUPS;74case nir_intrinsic_load_workgroup_size:75return PAN_SYSVAL_LOCAL_GROUP_SIZE;76case nir_intrinsic_load_work_dim:77return PAN_SYSVAL_WORK_DIM;78case nir_intrinsic_load_sample_positions_pan:79return PAN_SYSVAL_SAMPLE_POSITIONS;80case nir_intrinsic_load_first_vertex:81case nir_intrinsic_load_base_vertex:82case nir_intrinsic_load_base_instance:83return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;84case nir_intrinsic_load_draw_id:85return PAN_SYSVAL_DRAWID;86case nir_intrinsic_load_ssbo_address:87case nir_intrinsic_get_ssbo_size:88return panfrost_sysval_for_ssbo(instr);89case nir_intrinsic_load_sampler_lod_parameters_pan:90return panfrost_sysval_for_sampler(instr);91case nir_intrinsic_image_size:92return panfrost_sysval_for_image_size(instr);93default:94return ~0;95}96}9798int99panfrost_sysval_for_instr(nir_instr *instr, nir_dest *dest)100{101nir_intrinsic_instr *intr;102nir_dest *dst = NULL;103nir_tex_instr *tex;104unsigned sysval = ~0;105106switch (instr->type) {107case nir_instr_type_intrinsic:108intr = nir_instr_as_intrinsic(instr);109sysval = panfrost_nir_sysval_for_intrinsic(intr);110dst = &intr->dest;111break;112case nir_instr_type_tex:113tex = nir_instr_as_tex(instr);114if (tex->op != nir_texop_txs)115break;116117sysval = PAN_SYSVAL(TEXTURE_SIZE,118PAN_TXS_SYSVAL_ID(tex->texture_index,119nir_tex_instr_dest_size(tex) -120(tex->is_array ? 1 : 0),121tex->is_array));122dst = &tex->dest;123break;124default:125break;126}127128if (dest && dst)129*dest = *dst;130131return sysval;132}133134unsigned135pan_lookup_sysval(struct hash_table_u64 *sysval_to_id,136struct panfrost_sysvals *sysvals,137int sysval)138{139/* Try to lookup */140141void *cached = _mesa_hash_table_u64_search(sysval_to_id, sysval);142143if (cached)144return ((uintptr_t) cached) - 1;145146/* Else assign */147148unsigned id = sysvals->sysval_count++;149assert(id < MAX_SYSVAL_COUNT);150_mesa_hash_table_u64_insert(sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));151sysvals->sysvals[id] = sysval;152153return id;154}155156struct hash_table_u64 *157panfrost_init_sysvals(struct panfrost_sysvals *sysvals, void *memctx)158{159sysvals->sysval_count = 0;160return _mesa_hash_table_u64_create(memctx);161}162163164