Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a6xx/fd6_const.c
4574 views
/*1* Copyright (C) 2016 Rob Clark <[email protected]>2* Copyright © 2018 Google, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "fd6_const.h"25#include "fd6_pack.h"2627#define emit_const_user fd6_emit_const_user28#define emit_const_bo fd6_emit_const_bo29#include "ir3_const.h"3031/* regid: base const register32* prsc or dwords: buffer containing constant values33* sizedwords: size of const value buffer34*/35void36fd6_emit_const_user(struct fd_ringbuffer *ring,37const struct ir3_shader_variant *v, uint32_t regid,38uint32_t sizedwords, const uint32_t *dwords)39{40emit_const_asserts(ring, v, regid, sizedwords);4142/* NOTE we cheat a bit here, since we know mesa is aligning43* the size of the user buffer to 16 bytes. And we want to44* cut cycles in a hot path.45*/46uint32_t align_sz = align(sizedwords, 4);4748if (fd6_geom_stage(v->type)) {49OUT_PKTBUF(50ring, CP_LOAD_STATE6_GEOM, dwords, align_sz,51CP_LOAD_STATE6_0(.dst_off = regid / 4, .state_type = ST6_CONSTANTS,52.state_src = SS6_DIRECT,53.state_block = fd6_stage2shadersb(v->type),54.num_unit = DIV_ROUND_UP(sizedwords, 4)),55CP_LOAD_STATE6_1(), CP_LOAD_STATE6_2());56} else {57OUT_PKTBUF(58ring, CP_LOAD_STATE6_FRAG, dwords, align_sz,59CP_LOAD_STATE6_0(.dst_off = regid / 4, .state_type = ST6_CONSTANTS,60.state_src = SS6_DIRECT,61.state_block = fd6_stage2shadersb(v->type),62.num_unit = DIV_ROUND_UP(sizedwords, 4)),63CP_LOAD_STATE6_1(), CP_LOAD_STATE6_2());64}65}66void67fd6_emit_const_bo(struct fd_ringbuffer *ring,68const struct ir3_shader_variant *v, uint32_t regid,69uint32_t offset, uint32_t sizedwords, struct fd_bo *bo)70{71uint32_t dst_off = regid / 4;72assert(dst_off % 4 == 0);73uint32_t num_unit = DIV_ROUND_UP(sizedwords, 4);74assert(num_unit % 4 == 0);7576emit_const_asserts(ring, v, regid, sizedwords);7778if (fd6_geom_stage(v->type)) {79OUT_PKT(ring, CP_LOAD_STATE6_GEOM,80CP_LOAD_STATE6_0(.dst_off = dst_off, .state_type = ST6_CONSTANTS,81.state_src = SS6_INDIRECT,82.state_block = fd6_stage2shadersb(v->type),83.num_unit = num_unit, ),84CP_LOAD_STATE6_EXT_SRC_ADDR(.bo = bo, .bo_offset = offset));85} else {86OUT_PKT(ring, CP_LOAD_STATE6_FRAG,87CP_LOAD_STATE6_0(.dst_off = dst_off, .state_type = ST6_CONSTANTS,88.state_src = SS6_INDIRECT,89.state_block = fd6_stage2shadersb(v->type),90.num_unit = num_unit, ),91CP_LOAD_STATE6_EXT_SRC_ADDR(.bo = bo, .bo_offset = offset));92}93}9495static bool96is_stateobj(struct fd_ringbuffer *ring)97{98return true;99}100101static void102emit_const_ptrs(struct fd_ringbuffer *ring, const struct ir3_shader_variant *v,103uint32_t dst_offset, uint32_t num, struct fd_bo **bos,104uint32_t *offsets)105{106unreachable("shouldn't be called on a6xx");107}108109static void110emit_tess_bos(struct fd_ringbuffer *ring, struct fd6_emit *emit,111struct ir3_shader_variant *s) assert_dt112{113struct fd_context *ctx = emit->ctx;114const struct ir3_const_state *const_state = ir3_const_state(s);115const unsigned regid = const_state->offsets.primitive_param * 4 + 4;116uint32_t dwords = 16;117118OUT_PKT7(ring, fd6_stage2opcode(s->type), 3);119OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(regid / 4) |120CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |121CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |122CP_LOAD_STATE6_0_STATE_BLOCK(fd6_stage2shadersb(s->type)) |123CP_LOAD_STATE6_0_NUM_UNIT(dwords / 4));124OUT_RB(ring, ctx->batch->tess_addrs_constobj);125}126127static void128emit_stage_tess_consts(struct fd_ringbuffer *ring, struct ir3_shader_variant *v,129uint32_t *params, int num_params)130{131const struct ir3_const_state *const_state = ir3_const_state(v);132const unsigned regid = const_state->offsets.primitive_param;133int size = MIN2(1 + regid, v->constlen) - regid;134if (size > 0)135fd6_emit_const_user(ring, v, regid * 4, num_params, params);136}137138struct fd_ringbuffer *139fd6_build_tess_consts(struct fd6_emit *emit)140{141struct fd_context *ctx = emit->ctx;142143struct fd_ringbuffer *constobj = fd_submit_new_ringbuffer(144ctx->batch->submit, 0x1000, FD_RINGBUFFER_STREAMING);145146/* VS sizes are in bytes since that's what STLW/LDLW use, while the HS147* size is dwords, since that's what LDG/STG use.148*/149unsigned num_vertices = emit->hs150? emit->info->vertices_per_patch151: emit->gs->shader->nir->info.gs.vertices_in;152153uint32_t vs_params[4] = {154emit->vs->output_size * num_vertices * 4, /* vs primitive stride */155emit->vs->output_size * 4, /* vs vertex stride */1560, 0};157158emit_stage_tess_consts(constobj, emit->vs, vs_params, ARRAY_SIZE(vs_params));159160if (emit->hs) {161uint32_t hs_params[4] = {162emit->vs->output_size * num_vertices * 4, /* vs primitive stride */163emit->vs->output_size * 4, /* vs vertex stride */164emit->hs->output_size, emit->info->vertices_per_patch};165166emit_stage_tess_consts(constobj, emit->hs, hs_params,167ARRAY_SIZE(hs_params));168emit_tess_bos(constobj, emit, emit->hs);169170if (emit->gs)171num_vertices = emit->gs->shader->nir->info.gs.vertices_in;172173uint32_t ds_params[4] = {174emit->ds->output_size * num_vertices * 4, /* ds primitive stride */175emit->ds->output_size * 4, /* ds vertex stride */176emit->hs->output_size, /* hs vertex stride (dwords) */177emit->hs->shader->nir->info.tess.tcs_vertices_out};178179emit_stage_tess_consts(constobj, emit->ds, ds_params,180ARRAY_SIZE(ds_params));181emit_tess_bos(constobj, emit, emit->ds);182}183184if (emit->gs) {185struct ir3_shader_variant *prev;186if (emit->ds)187prev = emit->ds;188else189prev = emit->vs;190191uint32_t gs_params[4] = {192prev->output_size * num_vertices * 4, /* ds primitive stride */193prev->output_size * 4, /* ds vertex stride */1940,1950,196};197198num_vertices = emit->gs->shader->nir->info.gs.vertices_in;199emit_stage_tess_consts(constobj, emit->gs, gs_params,200ARRAY_SIZE(gs_params));201}202203return constobj;204}205206static void207fd6_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,208struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)209{210const struct ir3_const_state *const_state = ir3_const_state(v);211int num_ubos = const_state->num_ubos;212213if (!num_ubos)214return;215216OUT_PKT7(ring, fd6_stage2opcode(v->type), 3 + (2 * num_ubos));217OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |218CP_LOAD_STATE6_0_STATE_TYPE(ST6_UBO) |219CP_LOAD_STATE6_0_STATE_SRC(SS6_DIRECT) |220CP_LOAD_STATE6_0_STATE_BLOCK(fd6_stage2shadersb(v->type)) |221CP_LOAD_STATE6_0_NUM_UNIT(num_ubos));222OUT_RING(ring, CP_LOAD_STATE6_1_EXT_SRC_ADDR(0));223OUT_RING(ring, CP_LOAD_STATE6_2_EXT_SRC_ADDR_HI(0));224225for (int i = 0; i < num_ubos; i++) {226/* NIR constant data is packed into the end of the shader. */227if (i == const_state->constant_data_ubo) {228int size_vec4s = DIV_ROUND_UP(v->constant_data_size, 16);229OUT_RELOC(ring, v->bo, v->info.constant_data_offset,230(uint64_t)A6XX_UBO_1_SIZE(size_vec4s) << 32, 0);231continue;232}233234struct pipe_constant_buffer *cb = &constbuf->cb[i];235236/* If we have user pointers (constbuf 0, aka GL uniforms), upload them237* to a buffer now, and save it in the constbuf so that we don't have238* to reupload until they get changed.239*/240if (cb->user_buffer) {241struct pipe_context *pctx = &ctx->base;242u_upload_data(pctx->stream_uploader, 0, cb->buffer_size, 64,243cb->user_buffer, &cb->buffer_offset, &cb->buffer);244cb->user_buffer = NULL;245}246247if (cb->buffer) {248int size_vec4s = DIV_ROUND_UP(cb->buffer_size, 16);249OUT_RELOC(ring, fd_resource(cb->buffer)->bo, cb->buffer_offset,250(uint64_t)A6XX_UBO_1_SIZE(size_vec4s) << 32, 0);251} else {252OUT_RING(ring, 0xbad00000 | (i << 16));253OUT_RING(ring, A6XX_UBO_1_SIZE(0));254}255}256}257258static unsigned259user_consts_cmdstream_size(struct ir3_shader_variant *v)260{261struct ir3_const_state *const_state = ir3_const_state(v);262struct ir3_ubo_analysis_state *ubo_state = &const_state->ubo_state;263264if (unlikely(!ubo_state->cmdstream_size)) {265unsigned packets, size;266267/* pre-calculate size required for userconst stateobj: */268ir3_user_consts_size(ubo_state, &packets, &size);269270/* also account for UBO addresses: */271packets += 1;272size += 2 * const_state->num_ubos;273274unsigned sizedwords = (4 * packets) + size;275ubo_state->cmdstream_size = sizedwords * 4;276}277278return ubo_state->cmdstream_size;279}280281struct fd_ringbuffer *282fd6_build_user_consts(struct fd6_emit *emit)283{284static const enum pipe_shader_type types[] = {285PIPE_SHADER_VERTEX, PIPE_SHADER_TESS_CTRL, PIPE_SHADER_TESS_EVAL,286PIPE_SHADER_GEOMETRY, PIPE_SHADER_FRAGMENT,287};288struct ir3_shader_variant *variants[] = {289emit->vs, emit->hs, emit->ds, emit->gs, emit->fs,290};291struct fd_context *ctx = emit->ctx;292unsigned sz = 0;293294for (unsigned i = 0; i < ARRAY_SIZE(types); i++) {295if (!variants[i])296continue;297sz += user_consts_cmdstream_size(variants[i]);298}299300struct fd_ringbuffer *constobj =301fd_submit_new_ringbuffer(ctx->batch->submit, sz, FD_RINGBUFFER_STREAMING);302303for (unsigned i = 0; i < ARRAY_SIZE(types); i++) {304if (!variants[i])305continue;306ir3_emit_user_consts(ctx->screen, variants[i], constobj,307&ctx->constbuf[types[i]]);308fd6_emit_ubos(ctx, variants[i], constobj, &ctx->constbuf[types[i]]);309}310311return constobj;312}313314struct fd_ringbuffer *315fd6_build_vs_driver_params(struct fd6_emit *emit)316{317struct fd_context *ctx = emit->ctx;318struct fd6_context *fd6_ctx = fd6_context(ctx);319const struct ir3_shader_variant *vs = emit->vs;320321if (vs->need_driver_params) {322struct fd_ringbuffer *dpconstobj = fd_submit_new_ringbuffer(323ctx->batch->submit, IR3_DP_VS_COUNT * 4, FD_RINGBUFFER_STREAMING);324ir3_emit_vs_driver_params(vs, dpconstobj, ctx, emit->info, emit->indirect,325emit->draw);326fd6_ctx->has_dp_state = true;327return dpconstobj;328}329330fd6_ctx->has_dp_state = false;331return NULL;332}333334void335fd6_emit_ibo_consts(struct fd6_emit *emit, const struct ir3_shader_variant *v,336enum pipe_shader_type stage, struct fd_ringbuffer *ring)337{338struct fd_context *ctx = emit->ctx;339340ir3_emit_ssbo_sizes(ctx->screen, v, ring, &ctx->shaderbuf[stage]);341ir3_emit_image_dims(ctx->screen, v, ring, &ctx->shaderimg[stage]);342}343344void345fd6_emit_cs_consts(const struct ir3_shader_variant *v,346struct fd_ringbuffer *ring, struct fd_context *ctx,347const struct pipe_grid_info *info)348{349ir3_emit_cs_consts(v, ring, ctx, info);350fd6_emit_ubos(ctx, v, ring, &ctx->constbuf[PIPE_SHADER_COMPUTE]);351}352353void354fd6_emit_immediates(struct fd_screen *screen,355const struct ir3_shader_variant *v,356struct fd_ringbuffer *ring)357{358ir3_emit_immediates(screen, v, ring);359}360361void362fd6_emit_link_map(struct fd_screen *screen,363const struct ir3_shader_variant *producer,364const struct ir3_shader_variant *v,365struct fd_ringbuffer *ring)366{367ir3_emit_link_map(screen, producer, v, ring);368}369370371