Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3d_uniforms.c
4570 views
/*1* Copyright © 2014-2017 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 "util/u_pack_color.h"24#include "util/u_upload_mgr.h"25#include "util/format_srgb.h"2627#include "v3d_context.h"28#include "compiler/v3d_compiler.h"2930/* We don't expect that the packets we use in this file change across across31* hw versions, so we just include directly the v33 header32*/33#include "broadcom/cle/v3d_packet_v33_pack.h"3435static uint32_t36get_texrect_scale(struct v3d_texture_stateobj *texstate,37enum quniform_contents contents,38uint32_t data)39{40struct pipe_sampler_view *texture = texstate->textures[data];41uint32_t dim;4243if (contents == QUNIFORM_TEXRECT_SCALE_X)44dim = texture->texture->width0;45else46dim = texture->texture->height0;4748return fui(1.0f / dim);49}5051static uint32_t52get_texture_size(struct v3d_texture_stateobj *texstate,53enum quniform_contents contents,54uint32_t data)55{56struct pipe_sampler_view *texture = texstate->textures[data];5758switch (contents) {59case QUNIFORM_TEXTURE_WIDTH:60return u_minify(texture->texture->width0,61texture->u.tex.first_level);62case QUNIFORM_TEXTURE_HEIGHT:63return u_minify(texture->texture->height0,64texture->u.tex.first_level);65case QUNIFORM_TEXTURE_DEPTH:66return u_minify(texture->texture->depth0,67texture->u.tex.first_level);68case QUNIFORM_TEXTURE_ARRAY_SIZE:69return texture->texture->array_size;70case QUNIFORM_TEXTURE_LEVELS:71return (texture->u.tex.last_level -72texture->u.tex.first_level) + 1;73default:74unreachable("Bad texture size field");75}76}7778static uint32_t79get_image_size(struct v3d_shaderimg_stateobj *shaderimg,80enum quniform_contents contents,81uint32_t data)82{83struct v3d_image_view *image = &shaderimg->si[data];8485switch (contents) {86case QUNIFORM_IMAGE_WIDTH:87return u_minify(image->base.resource->width0,88image->base.u.tex.level);89case QUNIFORM_IMAGE_HEIGHT:90return u_minify(image->base.resource->height0,91image->base.u.tex.level);92case QUNIFORM_IMAGE_DEPTH:93return u_minify(image->base.resource->depth0,94image->base.u.tex.level);95case QUNIFORM_IMAGE_ARRAY_SIZE:96return image->base.resource->array_size;97default:98unreachable("Bad texture size field");99}100}101102/**103* Writes the V3D 3.x P0 (CFG_MODE=1) texture parameter.104*105* Some bits of this field are dependent on the type of sample being done by106* the shader, while other bits are dependent on the sampler state. We OR the107* two together here.108*/109static void110write_texture_p0(struct v3d_job *job,111struct v3d_cl_out **uniforms,112struct v3d_texture_stateobj *texstate,113uint32_t unit,114uint32_t shader_data)115{116struct pipe_sampler_state *psampler = texstate->samplers[unit];117struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);118119cl_aligned_u32(uniforms, shader_data | sampler->p0);120}121122/** Writes the V3D 3.x P1 (CFG_MODE=1) texture parameter. */123static void124write_texture_p1(struct v3d_job *job,125struct v3d_cl_out **uniforms,126struct v3d_texture_stateobj *texstate,127uint32_t data)128{129/* Extract the texture unit from the top bits, and the compiler's130* packed p1 from the bottom.131*/132uint32_t unit = data >> 5;133uint32_t p1 = data & 0x1f;134135struct pipe_sampler_view *psview = texstate->textures[unit];136struct v3d_sampler_view *sview = v3d_sampler_view(psview);137138struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 unpacked = {139.texture_state_record_base_address = texstate->texture_state[unit],140};141142uint32_t packed;143V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(&job->indirect,144(uint8_t *)&packed,145&unpacked);146147cl_aligned_u32(uniforms, p1 | packed | sview->p1);148}149150/** Writes the V3D 4.x TMU configuration parameter 0. */151static void152write_tmu_p0(struct v3d_job *job,153struct v3d_cl_out **uniforms,154struct v3d_texture_stateobj *texstate,155uint32_t data)156{157int unit = v3d_unit_data_get_unit(data);158struct pipe_sampler_view *psview = texstate->textures[unit];159struct v3d_sampler_view *sview = v3d_sampler_view(psview);160struct v3d_resource *rsc = v3d_resource(sview->texture);161162cl_aligned_reloc(&job->indirect, uniforms, sview->bo,163v3d_unit_data_get_offset(data));164v3d_job_add_bo(job, rsc->bo);165}166167static void168write_image_tmu_p0(struct v3d_job *job,169struct v3d_cl_out **uniforms,170struct v3d_shaderimg_stateobj *img,171uint32_t data)172{173/* Extract the image unit from the top bits, and the compiler's174* packed p0 from the bottom.175*/176uint32_t unit = data >> 24;177uint32_t p0 = data & 0x00ffffff;178179struct v3d_image_view *iview = &img->si[unit];180struct v3d_resource *rsc = v3d_resource(iview->base.resource);181182cl_aligned_reloc(&job->indirect, uniforms,183v3d_resource(iview->tex_state)->bo,184iview->tex_state_offset | p0);185v3d_job_add_bo(job, rsc->bo);186}187188/** Writes the V3D 4.x TMU configuration parameter 1. */189static void190write_tmu_p1(struct v3d_job *job,191struct v3d_cl_out **uniforms,192struct v3d_texture_stateobj *texstate,193uint32_t data)194{195uint32_t unit = v3d_unit_data_get_unit(data);196struct pipe_sampler_state *psampler = texstate->samplers[unit];197struct v3d_sampler_state *sampler = v3d_sampler_state(psampler);198struct pipe_sampler_view *psview = texstate->textures[unit];199struct v3d_sampler_view *sview = v3d_sampler_view(psview);200int variant = 0;201202if (sampler->border_color_variants)203variant = sview->sampler_variant;204205cl_aligned_reloc(&job->indirect, uniforms,206v3d_resource(sampler->sampler_state)->bo,207sampler->sampler_state_offset[variant] |208v3d_unit_data_get_offset(data));209}210211struct v3d_cl_reloc212v3d_write_uniforms(struct v3d_context *v3d, struct v3d_job *job,213struct v3d_compiled_shader *shader,214enum pipe_shader_type stage)215{216struct v3d_constbuf_stateobj *cb = &v3d->constbuf[stage];217struct v3d_texture_stateobj *texstate = &v3d->tex[stage];218struct v3d_uniform_list *uinfo = &shader->prog_data.base->uniforms;219const uint32_t *gallium_uniforms = cb->cb[0].user_buffer;220221/* The hardware always pre-fetches the next uniform (also when there222* aren't any), so we always allocate space for an extra slot. This223* fixes MMU exceptions reported since Linux kernel 5.4 when the224* uniforms fill up the tail bytes of a page in the indirect225* BO. In that scenario, when the hardware pre-fetches after reading226* the last uniform it will read beyond the end of the page and trigger227* the MMU exception.228*/229v3d_cl_ensure_space(&job->indirect, (uinfo->count + 1) * 4, 4);230231struct v3d_cl_reloc uniform_stream = cl_get_address(&job->indirect);232v3d_bo_reference(uniform_stream.bo);233234struct v3d_cl_out *uniforms =235cl_start(&job->indirect);236237for (int i = 0; i < uinfo->count; i++) {238uint32_t data = uinfo->data[i];239240switch (uinfo->contents[i]) {241case QUNIFORM_CONSTANT:242cl_aligned_u32(&uniforms, data);243break;244case QUNIFORM_UNIFORM:245cl_aligned_u32(&uniforms, gallium_uniforms[data]);246break;247case QUNIFORM_VIEWPORT_X_SCALE:248cl_aligned_f(&uniforms, v3d->viewport.scale[0] * 256.0f);249break;250case QUNIFORM_VIEWPORT_Y_SCALE:251cl_aligned_f(&uniforms, v3d->viewport.scale[1] * 256.0f);252break;253254case QUNIFORM_VIEWPORT_Z_OFFSET:255cl_aligned_f(&uniforms, v3d->viewport.translate[2]);256break;257case QUNIFORM_VIEWPORT_Z_SCALE:258cl_aligned_f(&uniforms, v3d->viewport.scale[2]);259break;260261case QUNIFORM_USER_CLIP_PLANE:262cl_aligned_f(&uniforms,263v3d->clip.ucp[data / 4][data % 4]);264break;265266case QUNIFORM_TMU_CONFIG_P0:267write_tmu_p0(job, &uniforms, texstate, data);268break;269270case QUNIFORM_TMU_CONFIG_P1:271write_tmu_p1(job, &uniforms, texstate, data);272break;273274case QUNIFORM_IMAGE_TMU_CONFIG_P0:275write_image_tmu_p0(job, &uniforms,276&v3d->shaderimg[stage], data);277break;278279case QUNIFORM_TEXTURE_CONFIG_P1:280write_texture_p1(job, &uniforms, texstate,281data);282break;283284case QUNIFORM_TEXRECT_SCALE_X:285case QUNIFORM_TEXRECT_SCALE_Y:286cl_aligned_u32(&uniforms,287get_texrect_scale(texstate,288uinfo->contents[i],289data));290break;291292case QUNIFORM_TEXTURE_WIDTH:293case QUNIFORM_TEXTURE_HEIGHT:294case QUNIFORM_TEXTURE_DEPTH:295case QUNIFORM_TEXTURE_ARRAY_SIZE:296case QUNIFORM_TEXTURE_LEVELS:297cl_aligned_u32(&uniforms,298get_texture_size(texstate,299uinfo->contents[i],300data));301break;302303case QUNIFORM_IMAGE_WIDTH:304case QUNIFORM_IMAGE_HEIGHT:305case QUNIFORM_IMAGE_DEPTH:306case QUNIFORM_IMAGE_ARRAY_SIZE:307cl_aligned_u32(&uniforms,308get_image_size(&v3d->shaderimg[stage],309uinfo->contents[i],310data));311break;312313case QUNIFORM_LINE_WIDTH:314cl_aligned_f(&uniforms,315v3d->rasterizer->base.line_width);316break;317318case QUNIFORM_AA_LINE_WIDTH:319cl_aligned_f(&uniforms, v3d_get_real_line_width(v3d));320break;321322case QUNIFORM_UBO_ADDR: {323uint32_t unit = v3d_unit_data_get_unit(data);324/* Constant buffer 0 may be a system memory pointer,325* in which case we want to upload a shadow copy to326* the GPU.327*/328if (!cb->cb[unit].buffer) {329u_upload_data(v3d->uploader, 0,330cb->cb[unit].buffer_size, 16,331cb->cb[unit].user_buffer,332&cb->cb[unit].buffer_offset,333&cb->cb[unit].buffer);334}335336cl_aligned_reloc(&job->indirect, &uniforms,337v3d_resource(cb->cb[unit].buffer)->bo,338cb->cb[unit].buffer_offset +339v3d_unit_data_get_offset(data));340break;341}342343case QUNIFORM_SSBO_OFFSET: {344struct pipe_shader_buffer *sb =345&v3d->ssbo[stage].sb[data];346347cl_aligned_reloc(&job->indirect, &uniforms,348v3d_resource(sb->buffer)->bo,349sb->buffer_offset);350break;351}352353case QUNIFORM_GET_SSBO_SIZE:354cl_aligned_u32(&uniforms,355v3d->ssbo[stage].sb[data].buffer_size);356break;357358case QUNIFORM_TEXTURE_FIRST_LEVEL:359cl_aligned_f(&uniforms,360texstate->textures[data]->u.tex.first_level);361break;362363case QUNIFORM_SPILL_OFFSET:364cl_aligned_reloc(&job->indirect, &uniforms,365v3d->prog.spill_bo, 0);366break;367368case QUNIFORM_SPILL_SIZE_PER_THREAD:369cl_aligned_u32(&uniforms,370v3d->prog.spill_size_per_thread);371break;372373case QUNIFORM_NUM_WORK_GROUPS:374cl_aligned_u32(&uniforms,375v3d->compute_num_workgroups[data]);376break;377378case QUNIFORM_SHARED_OFFSET:379cl_aligned_reloc(&job->indirect, &uniforms,380v3d->compute_shared_memory, 0);381break;382383case QUNIFORM_FB_LAYERS:384cl_aligned_u32(&uniforms, job->num_layers);385break;386387default:388assert(quniform_contents_is_texture_p0(uinfo->contents[i]));389390write_texture_p0(job, &uniforms, texstate,391uinfo->contents[i] -392QUNIFORM_TEXTURE_CONFIG_P0_0,393data);394break;395396}397#if 0398uint32_t written_val = *((uint32_t *)uniforms - 1);399fprintf(stderr, "shader %p[%d]: 0x%08x / 0x%08x (%f) ",400shader, i, __gen_address_offset(&uniform_stream) + i * 4,401written_val, uif(written_val));402vir_dump_uniform(uinfo->contents[i], data);403fprintf(stderr, "\n");404#endif405}406407cl_end(&job->indirect, uniforms);408409return uniform_stream;410}411412void413v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader)414{415uint32_t dirty = 0;416417for (int i = 0; i < shader->prog_data.base->uniforms.count; i++) {418switch (shader->prog_data.base->uniforms.contents[i]) {419case QUNIFORM_CONSTANT:420break;421case QUNIFORM_UNIFORM:422case QUNIFORM_UBO_ADDR:423dirty |= V3D_DIRTY_CONSTBUF;424break;425426case QUNIFORM_VIEWPORT_X_SCALE:427case QUNIFORM_VIEWPORT_Y_SCALE:428case QUNIFORM_VIEWPORT_Z_OFFSET:429case QUNIFORM_VIEWPORT_Z_SCALE:430dirty |= V3D_DIRTY_VIEWPORT;431break;432433case QUNIFORM_USER_CLIP_PLANE:434dirty |= V3D_DIRTY_CLIP;435break;436437case QUNIFORM_TMU_CONFIG_P0:438case QUNIFORM_TMU_CONFIG_P1:439case QUNIFORM_TEXTURE_CONFIG_P1:440case QUNIFORM_TEXTURE_FIRST_LEVEL:441case QUNIFORM_TEXRECT_SCALE_X:442case QUNIFORM_TEXRECT_SCALE_Y:443case QUNIFORM_TEXTURE_WIDTH:444case QUNIFORM_TEXTURE_HEIGHT:445case QUNIFORM_TEXTURE_DEPTH:446case QUNIFORM_TEXTURE_ARRAY_SIZE:447case QUNIFORM_TEXTURE_LEVELS:448case QUNIFORM_SPILL_OFFSET:449case QUNIFORM_SPILL_SIZE_PER_THREAD:450/* We could flag this on just the stage we're451* compiling for, but it's not passed in.452*/453dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |454V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;455break;456457case QUNIFORM_SSBO_OFFSET:458case QUNIFORM_GET_SSBO_SIZE:459dirty |= V3D_DIRTY_SSBO;460break;461462case QUNIFORM_IMAGE_TMU_CONFIG_P0:463case QUNIFORM_IMAGE_WIDTH:464case QUNIFORM_IMAGE_HEIGHT:465case QUNIFORM_IMAGE_DEPTH:466case QUNIFORM_IMAGE_ARRAY_SIZE:467dirty |= V3D_DIRTY_SHADER_IMAGE;468break;469470case QUNIFORM_LINE_WIDTH:471case QUNIFORM_AA_LINE_WIDTH:472dirty |= V3D_DIRTY_RASTERIZER;473break;474475case QUNIFORM_NUM_WORK_GROUPS:476case QUNIFORM_SHARED_OFFSET:477/* Compute always recalculates uniforms. */478break;479480case QUNIFORM_FB_LAYERS:481dirty |= V3D_DIRTY_FRAMEBUFFER;482break;483484default:485assert(quniform_contents_is_texture_p0(shader->prog_data.base->uniforms.contents[i]));486dirty |= V3D_DIRTY_FRAGTEX | V3D_DIRTY_VERTTEX |487V3D_DIRTY_GEOMTEX | V3D_DIRTY_COMPTEX;488break;489}490}491492shader->uniform_dirty_bits = dirty;493}494495496