Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3d_program.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 <inttypes.h>24#include "util/format/u_format.h"25#include "util/u_math.h"26#include "util/u_memory.h"27#include "util/ralloc.h"28#include "util/hash_table.h"29#include "util/u_upload_mgr.h"30#include "tgsi/tgsi_dump.h"31#include "tgsi/tgsi_parse.h"32#include "compiler/nir/nir.h"33#include "compiler/nir/nir_builder.h"34#include "nir/tgsi_to_nir.h"35#include "compiler/v3d_compiler.h"36#include "v3d_context.h"37#include "broadcom/cle/v3d_packet_v33_pack.h"3839static struct v3d_compiled_shader *40v3d_get_compiled_shader(struct v3d_context *v3d,41struct v3d_key *key, size_t key_size);42static void43v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader *uncompiled,44struct v3d_key *key);4546static gl_varying_slot47v3d_get_slot_for_driver_location(nir_shader *s, uint32_t driver_location)48{49nir_foreach_shader_out_variable(var, s) {50if (var->data.driver_location == driver_location) {51return var->data.location;52}53}5455return -1;56}5758/**59* Precomputes the TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC array for the shader.60*61* A shader can have 16 of these specs, and each one of them can write up to62* 16 dwords. Since we allow a total of 64 transform feedback output63* components (not 16 vectors), we have to group the writes of multiple64* varyings together in a single data spec.65*/66static void67v3d_set_transform_feedback_outputs(struct v3d_uncompiled_shader *so,68const struct pipe_stream_output_info *stream_output)69{70if (!stream_output->num_outputs)71return;7273struct v3d_varying_slot slots[PIPE_MAX_SO_OUTPUTS * 4];74int slot_count = 0;7576for (int buffer = 0; buffer < PIPE_MAX_SO_BUFFERS; buffer++) {77uint32_t buffer_offset = 0;78uint32_t vpm_start = slot_count;7980for (int i = 0; i < stream_output->num_outputs; i++) {81const struct pipe_stream_output *output =82&stream_output->output[i];8384if (output->output_buffer != buffer)85continue;8687/* We assume that the SO outputs appear in increasing88* order in the buffer.89*/90assert(output->dst_offset >= buffer_offset);9192/* Pad any undefined slots in the output */93for (int j = buffer_offset; j < output->dst_offset; j++) {94slots[slot_count] =95v3d_slot_from_slot_and_component(VARYING_SLOT_POS, 0);96slot_count++;97buffer_offset++;98}99100/* Set the coordinate shader up to output the101* components of this varying.102*/103for (int j = 0; j < output->num_components; j++) {104gl_varying_slot slot =105v3d_get_slot_for_driver_location(so->base.ir.nir, output->register_index);106107slots[slot_count] =108v3d_slot_from_slot_and_component(slot,109output->start_component + j);110slot_count++;111buffer_offset++;112}113}114115uint32_t vpm_size = slot_count - vpm_start;116if (!vpm_size)117continue;118119uint32_t vpm_start_offset = vpm_start + 6;120121while (vpm_size) {122uint32_t write_size = MIN2(vpm_size, 1 << 4);123124struct V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC unpacked = {125/* We need the offset from the coordinate shader's VPM126* output block, which has the [X, Y, Z, W, Xs, Ys]127* values at the start.128*/129.first_shaded_vertex_value_to_output = vpm_start_offset,130.number_of_consecutive_vertex_values_to_output_as_32_bit_values = write_size,131.output_buffer_to_write_to = buffer,132};133134/* GFXH-1559 */135assert(unpacked.first_shaded_vertex_value_to_output != 8 ||136so->num_tf_specs != 0);137138assert(so->num_tf_specs != ARRAY_SIZE(so->tf_specs));139V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC_pack(NULL,140(void *)&so->tf_specs[so->num_tf_specs],141&unpacked);142143/* If point size is being written by the shader, then144* all the VPM start offsets are shifted up by one.145* We won't know that until the variant is compiled,146* though.147*/148unpacked.first_shaded_vertex_value_to_output++;149150/* GFXH-1559 */151assert(unpacked.first_shaded_vertex_value_to_output != 8 ||152so->num_tf_specs != 0);153154V3D33_TRANSFORM_FEEDBACK_OUTPUT_DATA_SPEC_pack(NULL,155(void *)&so->tf_specs_psiz[so->num_tf_specs],156&unpacked);157so->num_tf_specs++;158vpm_start_offset += write_size;159vpm_size -= write_size;160}161so->base.stream_output.stride[buffer] =162stream_output->stride[buffer];163}164165so->num_tf_outputs = slot_count;166so->tf_outputs = ralloc_array(so->base.ir.nir, struct v3d_varying_slot,167slot_count);168memcpy(so->tf_outputs, slots, sizeof(*slots) * slot_count);169}170171static int172type_size(const struct glsl_type *type, bool bindless)173{174return glsl_count_attribute_slots(type, false);175}176177static void178precompile_all_outputs(nir_shader *s,179struct v3d_varying_slot *outputs,180uint8_t *num_outputs)181{182nir_foreach_shader_out_variable(var, s) {183const int array_len = MAX2(glsl_get_length(var->type), 1);184for (int j = 0; j < array_len; j++) {185const int slot = var->data.location + j;186const int num_components =187glsl_get_components(var->type);188for (int i = 0; i < num_components; i++) {189const int swiz = var->data.location_frac + i;190outputs[(*num_outputs)++] =191v3d_slot_from_slot_and_component(slot,192swiz);193}194}195}196}197198/**199* Precompiles a shader variant at shader state creation time if200* V3D_DEBUG=precompile is set. Used for shader-db201* (https://gitlab.freedesktop.org/mesa/shader-db)202*/203static void204v3d_shader_precompile(struct v3d_context *v3d,205struct v3d_uncompiled_shader *so)206{207nir_shader *s = so->base.ir.nir;208209if (s->info.stage == MESA_SHADER_FRAGMENT) {210struct v3d_fs_key key = {211.base.shader_state = so,212};213214nir_foreach_shader_out_variable(var, s) {215if (var->data.location == FRAG_RESULT_COLOR) {216key.cbufs |= 1 << 0;217} else if (var->data.location >= FRAG_RESULT_DATA0) {218key.cbufs |= 1 << (var->data.location -219FRAG_RESULT_DATA0);220}221}222223key.logicop_func = PIPE_LOGICOP_COPY;224225v3d_setup_shared_precompile_key(so, &key.base);226v3d_get_compiled_shader(v3d, &key.base, sizeof(key));227} else if (s->info.stage == MESA_SHADER_GEOMETRY) {228struct v3d_gs_key key = {229.base.shader_state = so,230.base.is_last_geometry_stage = true,231};232233v3d_setup_shared_precompile_key(so, &key.base);234235precompile_all_outputs(s,236key.used_outputs,237&key.num_used_outputs);238239v3d_get_compiled_shader(v3d, &key.base, sizeof(key));240241/* Compile GS bin shader: only position (XXX: include TF) */242key.is_coord = true;243key.num_used_outputs = 0;244for (int i = 0; i < 4; i++) {245key.used_outputs[key.num_used_outputs++] =246v3d_slot_from_slot_and_component(VARYING_SLOT_POS,247i);248}249v3d_get_compiled_shader(v3d, &key.base, sizeof(key));250} else {251assert(s->info.stage == MESA_SHADER_VERTEX);252struct v3d_vs_key key = {253.base.shader_state = so,254/* Emit fixed function outputs */255.base.is_last_geometry_stage = true,256};257258v3d_setup_shared_precompile_key(so, &key.base);259260precompile_all_outputs(s,261key.used_outputs,262&key.num_used_outputs);263264v3d_get_compiled_shader(v3d, &key.base, sizeof(key));265266/* Compile VS bin shader: only position (XXX: include TF) */267key.is_coord = true;268key.num_used_outputs = 0;269for (int i = 0; i < 4; i++) {270key.used_outputs[key.num_used_outputs++] =271v3d_slot_from_slot_and_component(VARYING_SLOT_POS,272i);273}274v3d_get_compiled_shader(v3d, &key.base, sizeof(key));275}276}277278static void *279v3d_uncompiled_shader_create(struct pipe_context *pctx,280enum pipe_shader_ir type, void *ir)281{282struct v3d_context *v3d = v3d_context(pctx);283struct v3d_uncompiled_shader *so = CALLOC_STRUCT(v3d_uncompiled_shader);284if (!so)285return NULL;286287so->program_id = v3d->next_uncompiled_program_id++;288289nir_shader *s;290291if (type == PIPE_SHADER_IR_NIR) {292/* The backend takes ownership of the NIR shader on state293* creation.294*/295s = ir;296} else {297assert(type == PIPE_SHADER_IR_TGSI);298299if (V3D_DEBUG & V3D_DEBUG_TGSI) {300fprintf(stderr, "prog %d TGSI:\n",301so->program_id);302tgsi_dump(ir, 0);303fprintf(stderr, "\n");304}305s = tgsi_to_nir(ir, pctx->screen, false);306}307308if (s->info.stage != MESA_SHADER_VERTEX &&309s->info.stage != MESA_SHADER_GEOMETRY) {310NIR_PASS_V(s, nir_lower_io,311nir_var_shader_in | nir_var_shader_out,312type_size, (nir_lower_io_options)0);313}314315NIR_PASS_V(s, nir_lower_regs_to_ssa);316NIR_PASS_V(s, nir_normalize_cubemap_coords);317318NIR_PASS_V(s, nir_lower_load_const_to_scalar);319320v3d_optimize_nir(NULL, s);321322NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL);323324/* Garbage collect dead instructions */325nir_sweep(s);326327so->base.type = PIPE_SHADER_IR_NIR;328so->base.ir.nir = s;329330if (V3D_DEBUG & (V3D_DEBUG_NIR |331v3d_debug_flag_for_shader_stage(s->info.stage))) {332fprintf(stderr, "%s prog %d NIR:\n",333gl_shader_stage_name(s->info.stage),334so->program_id);335nir_print_shader(s, stderr);336fprintf(stderr, "\n");337}338339if (V3D_DEBUG & V3D_DEBUG_PRECOMPILE)340v3d_shader_precompile(v3d, so);341342return so;343}344345static void346v3d_shader_debug_output(const char *message, void *data)347{348struct v3d_context *v3d = data;349350pipe_debug_message(&v3d->debug, SHADER_INFO, "%s", message);351}352353static void *354v3d_shader_state_create(struct pipe_context *pctx,355const struct pipe_shader_state *cso)356{357struct v3d_uncompiled_shader *so =358v3d_uncompiled_shader_create(pctx,359cso->type,360(cso->type == PIPE_SHADER_IR_TGSI ?361(void *)cso->tokens :362cso->ir.nir));363364v3d_set_transform_feedback_outputs(so, &cso->stream_output);365366return so;367}368369struct v3d_compiled_shader *370v3d_get_compiled_shader(struct v3d_context *v3d,371struct v3d_key *key,372size_t key_size)373{374struct v3d_uncompiled_shader *shader_state = key->shader_state;375nir_shader *s = shader_state->base.ir.nir;376377struct hash_table *ht = v3d->prog.cache[s->info.stage];378struct hash_entry *entry = _mesa_hash_table_search(ht, key);379if (entry)380return entry->data;381382struct v3d_compiled_shader *shader =383rzalloc(NULL, struct v3d_compiled_shader);384385int program_id = shader_state->program_id;386int variant_id =387p_atomic_inc_return(&shader_state->compiled_variant_count);388uint64_t *qpu_insts;389uint32_t shader_size;390391qpu_insts = v3d_compile(v3d->screen->compiler, key,392&shader->prog_data.base, s,393v3d_shader_debug_output,394v3d,395program_id, variant_id, &shader_size);396ralloc_steal(shader, shader->prog_data.base);397398v3d_set_shader_uniform_dirty_flags(shader);399400if (shader_size) {401u_upload_data(v3d->state_uploader, 0, shader_size, 8,402qpu_insts, &shader->offset, &shader->resource);403}404405free(qpu_insts);406407if (ht) {408struct v3d_key *dup_key;409dup_key = ralloc_size(shader, key_size);410memcpy(dup_key, key, key_size);411_mesa_hash_table_insert(ht, dup_key, shader);412}413414if (shader->prog_data.base->spill_size >415v3d->prog.spill_size_per_thread) {416/* The TIDX register we use for choosing the area to access417* for scratch space is: (core << 6) | (qpu << 2) | thread.418* Even at minimum threadcount in a particular shader, that419* means we still multiply by qpus by 4.420*/421int total_spill_size = (v3d->screen->devinfo.qpu_count * 4 *422shader->prog_data.base->spill_size);423424v3d_bo_unreference(&v3d->prog.spill_bo);425v3d->prog.spill_bo = v3d_bo_alloc(v3d->screen,426total_spill_size, "spill");427v3d->prog.spill_size_per_thread =428shader->prog_data.base->spill_size;429}430431return shader;432}433434static void435v3d_free_compiled_shader(struct v3d_compiled_shader *shader)436{437pipe_resource_reference(&shader->resource, NULL);438ralloc_free(shader);439}440441static void442v3d_setup_shared_key(struct v3d_context *v3d, struct v3d_key *key,443struct v3d_texture_stateobj *texstate)444{445const struct v3d_device_info *devinfo = &v3d->screen->devinfo;446447key->num_tex_used = texstate->num_textures;448key->num_samplers_used = texstate->num_textures;449assert(key->num_tex_used == key->num_samplers_used);450for (int i = 0; i < texstate->num_textures; i++) {451struct pipe_sampler_view *sampler = texstate->textures[i];452struct v3d_sampler_view *v3d_sampler = v3d_sampler_view(sampler);453struct pipe_sampler_state *sampler_state =454texstate->samplers[i];455456if (!sampler)457continue;458459key->sampler[i].return_size =460v3d_get_tex_return_size(devinfo,461sampler->format,462sampler_state->compare_mode);463464/* For 16-bit, we set up the sampler to always return 2465* channels (meaning no recompiles for most statechanges),466* while for 32 we actually scale the returns with channels.467*/468if (key->sampler[i].return_size == 16) {469key->sampler[i].return_channels = 2;470} else if (devinfo->ver > 40) {471key->sampler[i].return_channels = 4;472} else {473key->sampler[i].return_channels =474v3d_get_tex_return_channels(devinfo,475sampler->format);476}477478if (key->sampler[i].return_size == 32 && devinfo->ver < 40) {479memcpy(key->tex[i].swizzle,480v3d_sampler->swizzle,481sizeof(v3d_sampler->swizzle));482} else {483/* For 16-bit returns, we let the sampler state handle484* the swizzle.485*/486key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;487key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;488key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;489key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;490}491}492}493494static void495v3d_setup_shared_precompile_key(struct v3d_uncompiled_shader *uncompiled,496struct v3d_key *key)497{498nir_shader *s = uncompiled->base.ir.nir;499500/* Note that below we access they key's texture and sampler fields501* using the same index. On OpenGL they are the same (they are502* combined)503*/504key->num_tex_used = s->info.num_textures;505key->num_samplers_used = s->info.num_textures;506for (int i = 0; i < s->info.num_textures; i++) {507key->sampler[i].return_size = 16;508key->sampler[i].return_channels = 2;509510key->tex[i].swizzle[0] = PIPE_SWIZZLE_X;511key->tex[i].swizzle[1] = PIPE_SWIZZLE_Y;512key->tex[i].swizzle[2] = PIPE_SWIZZLE_Z;513key->tex[i].swizzle[3] = PIPE_SWIZZLE_W;514}515}516517static void518v3d_update_compiled_fs(struct v3d_context *v3d, uint8_t prim_mode)519{520struct v3d_job *job = v3d->job;521struct v3d_fs_key local_key;522struct v3d_fs_key *key = &local_key;523nir_shader *s = v3d->prog.bind_fs->base.ir.nir;524525if (!(v3d->dirty & (V3D_DIRTY_PRIM_MODE |526V3D_DIRTY_BLEND |527V3D_DIRTY_FRAMEBUFFER |528V3D_DIRTY_ZSA |529V3D_DIRTY_RASTERIZER |530V3D_DIRTY_SAMPLE_STATE |531V3D_DIRTY_FRAGTEX |532V3D_DIRTY_UNCOMPILED_FS))) {533return;534}535536memset(key, 0, sizeof(*key));537v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_FRAGMENT]);538key->base.shader_state = v3d->prog.bind_fs;539key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;540key->is_points = (prim_mode == PIPE_PRIM_POINTS);541key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&542prim_mode <= PIPE_PRIM_LINE_STRIP);543key->line_smoothing = (key->is_lines &&544v3d_line_smoothing_enabled(v3d));545key->has_gs = v3d->prog.bind_gs != NULL;546if (v3d->blend->base.logicop_enable) {547key->logicop_func = v3d->blend->base.logicop_func;548} else {549key->logicop_func = PIPE_LOGICOP_COPY;550}551if (job->msaa) {552key->msaa = v3d->rasterizer->base.multisample;553key->sample_coverage = (v3d->rasterizer->base.multisample &&554v3d->sample_mask != (1 << V3D_MAX_SAMPLES) - 1);555key->sample_alpha_to_coverage = v3d->blend->base.alpha_to_coverage;556key->sample_alpha_to_one = v3d->blend->base.alpha_to_one;557}558559key->swap_color_rb = v3d->swap_color_rb;560561for (int i = 0; i < v3d->framebuffer.nr_cbufs; i++) {562struct pipe_surface *cbuf = v3d->framebuffer.cbufs[i];563if (!cbuf)564continue;565566/* gl_FragColor's propagation to however many bound color567* buffers there are means that the shader compile needs to568* know what buffers are present.569*/570key->cbufs |= 1 << i;571572/* If logic operations are enabled then we might emit color573* reads and we need to know the color buffer format and574* swizzle for that.575*/576if (key->logicop_func != PIPE_LOGICOP_COPY) {577key->color_fmt[i].format = cbuf->format;578key->color_fmt[i].swizzle =579v3d_get_format_swizzle(&v3d->screen->devinfo,580cbuf->format);581}582583const struct util_format_description *desc =584util_format_description(cbuf->format);585586if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT &&587desc->channel[0].size == 32) {588key->f32_color_rb |= 1 << i;589}590591if (s->info.fs.untyped_color_outputs) {592if (util_format_is_pure_uint(cbuf->format))593key->uint_color_rb |= 1 << i;594else if (util_format_is_pure_sint(cbuf->format))595key->int_color_rb |= 1 << i;596}597}598599if (key->is_points) {600key->point_sprite_mask =601v3d->rasterizer->base.sprite_coord_enable;602key->point_coord_upper_left =603(v3d->rasterizer->base.sprite_coord_mode ==604PIPE_SPRITE_COORD_UPPER_LEFT);605}606607struct v3d_compiled_shader *old_fs = v3d->prog.fs;608v3d->prog.fs = v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));609if (v3d->prog.fs == old_fs)610return;611612v3d->dirty |= V3D_DIRTY_COMPILED_FS;613614if (old_fs) {615if (v3d->prog.fs->prog_data.fs->flat_shade_flags !=616old_fs->prog_data.fs->flat_shade_flags) {617v3d->dirty |= V3D_DIRTY_FLAT_SHADE_FLAGS;618}619620if (v3d->prog.fs->prog_data.fs->noperspective_flags !=621old_fs->prog_data.fs->noperspective_flags) {622v3d->dirty |= V3D_DIRTY_NOPERSPECTIVE_FLAGS;623}624625if (v3d->prog.fs->prog_data.fs->centroid_flags !=626old_fs->prog_data.fs->centroid_flags) {627v3d->dirty |= V3D_DIRTY_CENTROID_FLAGS;628}629}630631if (old_fs && memcmp(v3d->prog.fs->prog_data.fs->input_slots,632old_fs->prog_data.fs->input_slots,633sizeof(v3d->prog.fs->prog_data.fs->input_slots))) {634v3d->dirty |= V3D_DIRTY_FS_INPUTS;635}636}637638static void639v3d_update_compiled_gs(struct v3d_context *v3d, uint8_t prim_mode)640{641struct v3d_gs_key local_key;642struct v3d_gs_key *key = &local_key;643644if (!(v3d->dirty & (V3D_DIRTY_GEOMTEX |645V3D_DIRTY_RASTERIZER |646V3D_DIRTY_UNCOMPILED_GS |647V3D_DIRTY_PRIM_MODE |648V3D_DIRTY_FS_INPUTS))) {649return;650}651652if (!v3d->prog.bind_gs) {653v3d->prog.gs = NULL;654v3d->prog.gs_bin = NULL;655return;656}657658memset(key, 0, sizeof(*key));659v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_GEOMETRY]);660key->base.shader_state = v3d->prog.bind_gs;661key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;662key->base.is_last_geometry_stage = true;663key->num_used_outputs = v3d->prog.fs->prog_data.fs->num_inputs;664STATIC_ASSERT(sizeof(key->used_outputs) ==665sizeof(v3d->prog.fs->prog_data.fs->input_slots));666memcpy(key->used_outputs, v3d->prog.fs->prog_data.fs->input_slots,667sizeof(key->used_outputs));668669key->per_vertex_point_size =670(prim_mode == PIPE_PRIM_POINTS &&671v3d->rasterizer->base.point_size_per_vertex);672673struct v3d_compiled_shader *gs =674v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));675if (gs != v3d->prog.gs) {676v3d->prog.gs = gs;677v3d->dirty |= V3D_DIRTY_COMPILED_GS;678}679680key->is_coord = true;681682/* The last bin-mode shader in the geometry pipeline only outputs683* varyings used by transform feedback.684*/685struct v3d_uncompiled_shader *shader_state = key->base.shader_state;686memcpy(key->used_outputs, shader_state->tf_outputs,687sizeof(*key->used_outputs) * shader_state->num_tf_outputs);688if (shader_state->num_tf_outputs < key->num_used_outputs) {689uint32_t size = sizeof(*key->used_outputs) *690(key->num_used_outputs -691shader_state->num_tf_outputs);692memset(&key->used_outputs[shader_state->num_tf_outputs],6930, size);694}695key->num_used_outputs = shader_state->num_tf_outputs;696697struct v3d_compiled_shader *old_gs = v3d->prog.gs;698struct v3d_compiled_shader *gs_bin =699v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));700if (gs_bin != old_gs) {701v3d->prog.gs_bin = gs_bin;702v3d->dirty |= V3D_DIRTY_COMPILED_GS_BIN;703}704705if (old_gs && memcmp(v3d->prog.gs->prog_data.gs->input_slots,706old_gs->prog_data.gs->input_slots,707sizeof(v3d->prog.gs->prog_data.gs->input_slots))) {708v3d->dirty |= V3D_DIRTY_GS_INPUTS;709}710}711712static void713v3d_update_compiled_vs(struct v3d_context *v3d, uint8_t prim_mode)714{715struct v3d_vs_key local_key;716struct v3d_vs_key *key = &local_key;717718if (!(v3d->dirty & (V3D_DIRTY_VERTTEX |719V3D_DIRTY_VTXSTATE |720V3D_DIRTY_UNCOMPILED_VS |721(v3d->prog.bind_gs ? 0 : V3D_DIRTY_RASTERIZER) |722(v3d->prog.bind_gs ? 0 : V3D_DIRTY_PRIM_MODE) |723(v3d->prog.bind_gs ? V3D_DIRTY_GS_INPUTS :724V3D_DIRTY_FS_INPUTS)))) {725return;726}727728memset(key, 0, sizeof(*key));729v3d_setup_shared_key(v3d, &key->base, &v3d->tex[PIPE_SHADER_VERTEX]);730key->base.shader_state = v3d->prog.bind_vs;731key->base.ucp_enables = v3d->rasterizer->base.clip_plane_enable;732key->base.is_last_geometry_stage = !v3d->prog.bind_gs;733734if (!v3d->prog.bind_gs) {735key->num_used_outputs = v3d->prog.fs->prog_data.fs->num_inputs;736STATIC_ASSERT(sizeof(key->used_outputs) ==737sizeof(v3d->prog.fs->prog_data.fs->input_slots));738memcpy(key->used_outputs, v3d->prog.fs->prog_data.fs->input_slots,739sizeof(key->used_outputs));740} else {741key->num_used_outputs = v3d->prog.gs->prog_data.gs->num_inputs;742STATIC_ASSERT(sizeof(key->used_outputs) ==743sizeof(v3d->prog.gs->prog_data.gs->input_slots));744memcpy(key->used_outputs, v3d->prog.gs->prog_data.gs->input_slots,745sizeof(key->used_outputs));746}747748key->per_vertex_point_size =749(prim_mode == PIPE_PRIM_POINTS &&750v3d->rasterizer->base.point_size_per_vertex);751752nir_shader *s = v3d->prog.bind_vs->base.ir.nir;753uint64_t inputs_read = s->info.inputs_read;754assert(util_bitcount(inputs_read) <= v3d->vtx->num_elements);755756while (inputs_read) {757int location = u_bit_scan64(&inputs_read);758nir_variable *var =759nir_find_variable_with_location(s, nir_var_shader_in, location);760assert (var != NULL);761int driver_location = var->data.driver_location;762switch (v3d->vtx->pipe[driver_location].src_format) {763case PIPE_FORMAT_B8G8R8A8_UNORM:764case PIPE_FORMAT_B10G10R10A2_UNORM:765case PIPE_FORMAT_B10G10R10A2_SNORM:766case PIPE_FORMAT_B10G10R10A2_USCALED:767case PIPE_FORMAT_B10G10R10A2_SSCALED:768key->va_swap_rb_mask |= 1 << location;769break;770default:771break;772}773}774775struct v3d_compiled_shader *vs =776v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));777if (vs != v3d->prog.vs) {778v3d->prog.vs = vs;779v3d->dirty |= V3D_DIRTY_COMPILED_VS;780}781782key->is_coord = true;783784/* Coord shaders only output varyings used by transform feedback,785* unless they are linked to other shaders in the geometry side786* of the pipeline, since in that case any of the output varyings787* could be required in later geometry stages to compute788* gl_Position or TF outputs.789*/790if (!v3d->prog.bind_gs) {791struct v3d_uncompiled_shader *shader_state =792key->base.shader_state;793memcpy(key->used_outputs, shader_state->tf_outputs,794sizeof(*key->used_outputs) *795shader_state->num_tf_outputs);796if (shader_state->num_tf_outputs < key->num_used_outputs) {797uint32_t tail_bytes =798sizeof(*key->used_outputs) *799(key->num_used_outputs -800shader_state->num_tf_outputs);801memset(&key->used_outputs[shader_state->num_tf_outputs],8020, tail_bytes);803}804key->num_used_outputs = shader_state->num_tf_outputs;805} else {806key->num_used_outputs = v3d->prog.gs_bin->prog_data.gs->num_inputs;807STATIC_ASSERT(sizeof(key->used_outputs) ==808sizeof(v3d->prog.gs_bin->prog_data.gs->input_slots));809memcpy(key->used_outputs, v3d->prog.gs_bin->prog_data.gs->input_slots,810sizeof(key->used_outputs));811}812813struct v3d_compiled_shader *cs =814v3d_get_compiled_shader(v3d, &key->base, sizeof(*key));815if (cs != v3d->prog.cs) {816v3d->prog.cs = cs;817v3d->dirty |= V3D_DIRTY_COMPILED_CS;818}819}820821void822v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode)823{824v3d_update_compiled_fs(v3d, prim_mode);825v3d_update_compiled_gs(v3d, prim_mode);826v3d_update_compiled_vs(v3d, prim_mode);827}828829void830v3d_update_compiled_cs(struct v3d_context *v3d)831{832struct v3d_key local_key;833struct v3d_key *key = &local_key;834835if (!(v3d->dirty & (V3D_DIRTY_UNCOMPILED_CS |836V3D_DIRTY_COMPTEX))) {837return;838}839840memset(key, 0, sizeof(*key));841v3d_setup_shared_key(v3d, key, &v3d->tex[PIPE_SHADER_COMPUTE]);842key->shader_state = v3d->prog.bind_compute;843844struct v3d_compiled_shader *cs =845v3d_get_compiled_shader(v3d, key, sizeof(*key));846if (cs != v3d->prog.compute) {847v3d->prog.compute = cs;848v3d->dirty |= V3D_DIRTY_COMPILED_CS; /* XXX */849}850}851852static uint32_t853fs_cache_hash(const void *key)854{855return _mesa_hash_data(key, sizeof(struct v3d_fs_key));856}857858static uint32_t859gs_cache_hash(const void *key)860{861return _mesa_hash_data(key, sizeof(struct v3d_gs_key));862}863864static uint32_t865vs_cache_hash(const void *key)866{867return _mesa_hash_data(key, sizeof(struct v3d_vs_key));868}869870static uint32_t871cs_cache_hash(const void *key)872{873return _mesa_hash_data(key, sizeof(struct v3d_key));874}875876static bool877fs_cache_compare(const void *key1, const void *key2)878{879return memcmp(key1, key2, sizeof(struct v3d_fs_key)) == 0;880}881882static bool883gs_cache_compare(const void *key1, const void *key2)884{885return memcmp(key1, key2, sizeof(struct v3d_gs_key)) == 0;886}887888static bool889vs_cache_compare(const void *key1, const void *key2)890{891return memcmp(key1, key2, sizeof(struct v3d_vs_key)) == 0;892}893894static bool895cs_cache_compare(const void *key1, const void *key2)896{897return memcmp(key1, key2, sizeof(struct v3d_key)) == 0;898}899900static void901v3d_shader_state_delete(struct pipe_context *pctx, void *hwcso)902{903struct v3d_context *v3d = v3d_context(pctx);904struct v3d_uncompiled_shader *so = hwcso;905nir_shader *s = so->base.ir.nir;906907hash_table_foreach(v3d->prog.cache[s->info.stage], entry) {908const struct v3d_key *key = entry->key;909struct v3d_compiled_shader *shader = entry->data;910911if (key->shader_state != so)912continue;913914if (v3d->prog.fs == shader)915v3d->prog.fs = NULL;916if (v3d->prog.vs == shader)917v3d->prog.vs = NULL;918if (v3d->prog.cs == shader)919v3d->prog.cs = NULL;920if (v3d->prog.compute == shader)921v3d->prog.compute = NULL;922923_mesa_hash_table_remove(v3d->prog.cache[s->info.stage], entry);924v3d_free_compiled_shader(shader);925}926927ralloc_free(so->base.ir.nir);928free(so);929}930931static void932v3d_fp_state_bind(struct pipe_context *pctx, void *hwcso)933{934struct v3d_context *v3d = v3d_context(pctx);935v3d->prog.bind_fs = hwcso;936v3d->dirty |= V3D_DIRTY_UNCOMPILED_FS;937}938939static void940v3d_gp_state_bind(struct pipe_context *pctx, void *hwcso)941{942struct v3d_context *v3d = v3d_context(pctx);943v3d->prog.bind_gs = hwcso;944v3d->dirty |= V3D_DIRTY_UNCOMPILED_GS;945}946947static void948v3d_vp_state_bind(struct pipe_context *pctx, void *hwcso)949{950struct v3d_context *v3d = v3d_context(pctx);951v3d->prog.bind_vs = hwcso;952v3d->dirty |= V3D_DIRTY_UNCOMPILED_VS;953}954955static void956v3d_compute_state_bind(struct pipe_context *pctx, void *state)957{958struct v3d_context *v3d = v3d_context(pctx);959960v3d->prog.bind_compute = state;961v3d->dirty |= V3D_DIRTY_UNCOMPILED_CS;962}963964static void *965v3d_create_compute_state(struct pipe_context *pctx,966const struct pipe_compute_state *cso)967{968return v3d_uncompiled_shader_create(pctx, cso->ir_type,969(void *)cso->prog);970}971972void973v3d_program_init(struct pipe_context *pctx)974{975struct v3d_context *v3d = v3d_context(pctx);976977pctx->create_vs_state = v3d_shader_state_create;978pctx->delete_vs_state = v3d_shader_state_delete;979980pctx->create_gs_state = v3d_shader_state_create;981pctx->delete_gs_state = v3d_shader_state_delete;982983pctx->create_fs_state = v3d_shader_state_create;984pctx->delete_fs_state = v3d_shader_state_delete;985986pctx->bind_fs_state = v3d_fp_state_bind;987pctx->bind_gs_state = v3d_gp_state_bind;988pctx->bind_vs_state = v3d_vp_state_bind;989990if (v3d->screen->has_csd) {991pctx->create_compute_state = v3d_create_compute_state;992pctx->delete_compute_state = v3d_shader_state_delete;993pctx->bind_compute_state = v3d_compute_state_bind;994}995996v3d->prog.cache[MESA_SHADER_VERTEX] =997_mesa_hash_table_create(pctx, vs_cache_hash, vs_cache_compare);998v3d->prog.cache[MESA_SHADER_GEOMETRY] =999_mesa_hash_table_create(pctx, gs_cache_hash, gs_cache_compare);1000v3d->prog.cache[MESA_SHADER_FRAGMENT] =1001_mesa_hash_table_create(pctx, fs_cache_hash, fs_cache_compare);1002v3d->prog.cache[MESA_SHADER_COMPUTE] =1003_mesa_hash_table_create(pctx, cs_cache_hash, cs_cache_compare);1004}10051006void1007v3d_program_fini(struct pipe_context *pctx)1008{1009struct v3d_context *v3d = v3d_context(pctx);10101011for (int i = 0; i < MESA_SHADER_STAGES; i++) {1012struct hash_table *cache = v3d->prog.cache[i];1013if (!cache)1014continue;10151016hash_table_foreach(cache, entry) {1017struct v3d_compiled_shader *shader = entry->data;1018v3d_free_compiled_shader(shader);1019_mesa_hash_table_remove(cache, entry);1020}1021}10221023v3d_bo_unreference(&v3d->prog.spill_bo);1024}102510261027