Path: blob/21.2-virgl/src/broadcom/compiler/v3d33_tex.c
4565 views
/*1* Copyright © 2016-2018 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#include "v3d_compiler.h"2425/* We don't do any address packing. */26#define __gen_user_data void27#define __gen_address_type uint32_t28#define __gen_address_offset(reloc) (*reloc)29#define __gen_emit_reloc(cl, reloc)30#include "cle/v3d_packet_v33_pack.h"3132void33v3d33_vir_emit_tex(struct v3d_compile *c, nir_tex_instr *instr)34{35/* FIXME: We don't bother implementing pipelining for texture reads36* for any pre 4.x hardware. It should be straight forward to do but37* we are not really testing or even targetting this hardware at38* present.39*/40ntq_flush_tmu(c);4142unsigned unit = instr->texture_index;4344struct V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1 p0_unpacked = {45V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1_header,4647.fetch_sample_mode = instr->op == nir_texop_txf,48};4950struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 p1_unpacked = {51};5253switch (instr->sampler_dim) {54case GLSL_SAMPLER_DIM_1D:55if (instr->is_array)56p0_unpacked.lookup_type = TEXTURE_1D_ARRAY;57else58p0_unpacked.lookup_type = TEXTURE_1D;59break;60case GLSL_SAMPLER_DIM_2D:61case GLSL_SAMPLER_DIM_RECT:62if (instr->is_array)63p0_unpacked.lookup_type = TEXTURE_2D_ARRAY;64else65p0_unpacked.lookup_type = TEXTURE_2D;66break;67case GLSL_SAMPLER_DIM_3D:68p0_unpacked.lookup_type = TEXTURE_3D;69break;70case GLSL_SAMPLER_DIM_CUBE:71p0_unpacked.lookup_type = TEXTURE_CUBE_MAP;72break;73default:74unreachable("Bad sampler type");75}7677struct qreg coords[5];78int next_coord = 0;79for (unsigned i = 0; i < instr->num_srcs; i++) {80switch (instr->src[i].src_type) {81case nir_tex_src_coord:82for (int j = 0; j < instr->coord_components; j++) {83coords[next_coord++] =84ntq_get_src(c, instr->src[i].src, j);85}86if (instr->coord_components < 2)87coords[next_coord++] = vir_uniform_f(c, 0.5);88break;89case nir_tex_src_bias:90coords[next_coord++] =91ntq_get_src(c, instr->src[i].src, 0);9293p0_unpacked.bias_supplied = true;94break;95case nir_tex_src_lod:96coords[next_coord++] =97vir_FADD(c,98ntq_get_src(c, instr->src[i].src, 0),99vir_uniform(c, QUNIFORM_TEXTURE_FIRST_LEVEL,100unit));101102if (instr->op != nir_texop_txf &&103instr->op != nir_texop_tg4) {104p0_unpacked.disable_autolod_use_bias_only = true;105}106break;107case nir_tex_src_comparator:108coords[next_coord++] =109ntq_get_src(c, instr->src[i].src, 0);110111p0_unpacked.shadow = true;112break;113114case nir_tex_src_offset: {115p0_unpacked.texel_offset_for_s_coordinate =116nir_src_comp_as_int(instr->src[i].src, 0);117118if (instr->coord_components >= 2)119p0_unpacked.texel_offset_for_t_coordinate =120nir_src_comp_as_int(instr->src[i].src, 1);121122if (instr->coord_components >= 3)123p0_unpacked.texel_offset_for_r_coordinate =124nir_src_comp_as_int(instr->src[i].src, 2);125break;126}127128default:129unreachable("unknown texture source");130}131}132133/* Limit the number of channels returned to both how many the NIR134* instruction writes and how many the instruction could produce.135*/136p1_unpacked.return_words_of_texture_data =137instr->dest.is_ssa ?138nir_ssa_def_components_read(&instr->dest.ssa) :139(1 << instr->dest.reg.reg->num_components) - 1;140141uint32_t p0_packed;142V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1_pack(NULL,143(uint8_t *)&p0_packed,144&p0_unpacked);145146uint32_t p1_packed;147V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(NULL,148(uint8_t *)&p1_packed,149&p1_unpacked);150/* Load unit number into the address field, which will be be used by151* the driver to decide which texture to put in the actual address152* field.153*/154p1_packed |= unit << 5;155156/* There is no native support for GL texture rectangle coordinates, so157* we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,158* 1]).159*/160if (instr->sampler_dim == GLSL_SAMPLER_DIM_RECT) {161coords[0] = vir_FMUL(c, coords[0],162vir_uniform(c, QUNIFORM_TEXRECT_SCALE_X,163unit));164coords[1] = vir_FMUL(c, coords[1],165vir_uniform(c, QUNIFORM_TEXRECT_SCALE_Y,166unit));167}168169int texture_u[] = {170vir_get_uniform_index(c, QUNIFORM_TEXTURE_CONFIG_P0_0 + unit, p0_packed),171vir_get_uniform_index(c, QUNIFORM_TEXTURE_CONFIG_P1, p1_packed),172};173174for (int i = 0; i < next_coord; i++) {175struct qreg dst;176177if (i == next_coord - 1)178dst = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUL);179else180dst = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMU);181182struct qinst *tmu = vir_MOV_dest(c, dst, coords[i]);183184if (i < 2)185tmu->uniform = texture_u[i];186}187188vir_emit_thrsw(c);189190for (int i = 0; i < 4; i++) {191if (p1_unpacked.return_words_of_texture_data & (1 << i))192ntq_store_dest(c, &instr->dest, i, vir_LDTMU(c));193}194}195196197