Path: blob/21.2-virgl/src/gallium/drivers/freedreno/ir3/ir3_const.h
4574 views
/*1* Copyright (C) 2014 Rob Clark <[email protected]>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:23* Rob Clark <[email protected]>24*/2526#include "ir3/ir3_nir.h"2728/* This has to reach into the fd_context a bit more than the rest of29* ir3, but it needs to be aligned with the compiler, so both agree30* on which const regs hold what. And the logic is identical between31* ir3 generations, the only difference is small details in the actual32* CP_LOAD_STATE packets (which is handled inside the generation33* specific ctx->emit_const(_bo)() fxns)34*35* This file should be included in only a single .c file per gen, which36* defines the following functions:37*/3839static bool is_stateobj(struct fd_ringbuffer *ring);4041static void emit_const_user(struct fd_ringbuffer *ring,42const struct ir3_shader_variant *v, uint32_t regid,43uint32_t size, const uint32_t *user_buffer);4445static void emit_const_bo(struct fd_ringbuffer *ring,46const struct ir3_shader_variant *v, uint32_t regid,47uint32_t offset, uint32_t size, struct fd_bo *bo);4849static void50emit_const_prsc(struct fd_ringbuffer *ring, const struct ir3_shader_variant *v,51uint32_t regid, uint32_t offset, uint32_t size,52struct pipe_resource *buffer)53{54struct fd_resource *rsc = fd_resource(buffer);55emit_const_bo(ring, v, regid, offset, size, rsc->bo);56}5758static void emit_const_ptrs(struct fd_ringbuffer *ring,59const struct ir3_shader_variant *v,60uint32_t dst_offset, uint32_t num,61struct fd_bo **bos, uint32_t *offsets);6263static void64emit_const_asserts(struct fd_ringbuffer *ring,65const struct ir3_shader_variant *v, uint32_t regid,66uint32_t sizedwords)67{68assert((regid % 4) == 0);69assert((sizedwords % 4) == 0);70assert(regid + sizedwords <= v->constlen * 4);71}7273static void74ring_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring) assert_dt75{76/* when we emit const state via ring (IB2) we need a WFI, but when77* it is emit'd via stateobj, we don't78*/79if (is_stateobj(ring))80return;8182fd_wfi(batch, ring);83}8485/**86* Indirectly calculates size of cmdstream needed for ir3_emit_user_consts().87* Returns number of packets, and total size of all the payload.88*89* The value can be a worst-case, ie. some shader variants may not read all90* consts, etc.91*92* Returns size in dwords.93*/94static inline void95ir3_user_consts_size(struct ir3_ubo_analysis_state *state, unsigned *packets,96unsigned *size)97{98*packets = *size = 0;99100for (uint32_t i = 0; i < ARRAY_SIZE(state->range); i++) {101if (state->range[i].start < state->range[i].end) {102*size += state->range[i].end - state->range[i].start;103(*packets)++;104}105}106}107108/**109* Uploads the referenced subranges of the nir constant_data to the hardware's110* constant buffer.111*/112static inline void113ir3_emit_constant_data(struct fd_screen *screen,114const struct ir3_shader_variant *v,115struct fd_ringbuffer *ring)116{117const struct ir3_const_state *const_state = ir3_const_state(v);118const struct ir3_ubo_analysis_state *state = &const_state->ubo_state;119120for (unsigned i = 0; i < state->num_enabled; i++) {121unsigned ubo = state->range[i].ubo.block;122if (ubo != const_state->constant_data_ubo)123continue;124125uint32_t size = state->range[i].end - state->range[i].start;126127/* Pre-a6xx, we might have ranges enabled in the shader that aren't128* used in the binning variant.129*/130if (16 * v->constlen <= state->range[i].offset)131continue;132133/* and even if the start of the const buffer is before134* first_immediate, the end may not be:135*/136size = MIN2(size, (16 * v->constlen) - state->range[i].offset);137138if (size == 0)139continue;140141emit_const_bo(ring, v, state->range[i].offset / 4,142v->info.constant_data_offset + state->range[i].start,143size / 4, v->bo);144}145}146147/**148* Uploads sub-ranges of UBOs to the hardware's constant buffer (UBO access149* outside of these ranges will be done using full UBO accesses in the150* shader).151*/152static inline void153ir3_emit_user_consts(struct fd_screen *screen,154const struct ir3_shader_variant *v,155struct fd_ringbuffer *ring,156struct fd_constbuf_stateobj *constbuf)157{158const struct ir3_const_state *const_state = ir3_const_state(v);159const struct ir3_ubo_analysis_state *state = &const_state->ubo_state;160161for (unsigned i = 0; i < state->num_enabled; i++) {162assert(!state->range[i].ubo.bindless);163unsigned ubo = state->range[i].ubo.block;164if (!(constbuf->enabled_mask & (1 << ubo)) ||165ubo == const_state->constant_data_ubo) {166continue;167}168struct pipe_constant_buffer *cb = &constbuf->cb[ubo];169170uint32_t size = state->range[i].end - state->range[i].start;171uint32_t offset = cb->buffer_offset + state->range[i].start;172173/* Pre-a6xx, we might have ranges enabled in the shader that aren't174* used in the binning variant.175*/176if (16 * v->constlen <= state->range[i].offset)177continue;178179/* and even if the start of the const buffer is before180* first_immediate, the end may not be:181*/182size = MIN2(size, (16 * v->constlen) - state->range[i].offset);183184if (size == 0)185continue;186187/* things should be aligned to vec4: */188debug_assert((state->range[i].offset % 16) == 0);189debug_assert((size % 16) == 0);190debug_assert((offset % 16) == 0);191192if (cb->user_buffer) {193emit_const_user(ring, v, state->range[i].offset / 4, size / 4,194cb->user_buffer + state->range[i].start);195} else {196emit_const_prsc(ring, v, state->range[i].offset / 4, offset, size / 4,197cb->buffer);198}199}200}201202static inline void203ir3_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,204struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)205{206const struct ir3_const_state *const_state = ir3_const_state(v);207uint32_t offset = const_state->offsets.ubo;208209/* a6xx+ uses UBO state and ldc instead of pointers emitted in210* const state and ldg:211*/212if (ctx->screen->gpu_id >= 600)213return;214215if (v->constlen > offset) {216uint32_t params = const_state->num_ubos;217uint32_t offsets[params];218struct fd_bo *bos[params];219220for (uint32_t i = 0; i < params; i++) {221if (i == const_state->constant_data_ubo) {222bos[i] = v->bo;223offsets[i] = v->info.constant_data_offset;224continue;225}226227struct pipe_constant_buffer *cb = &constbuf->cb[i];228229/* If we have user pointers (constbuf 0, aka GL uniforms), upload230* them to a buffer now, and save it in the constbuf so that we231* don't have to reupload until they get changed.232*/233if (cb->user_buffer) {234struct pipe_context *pctx = &ctx->base;235u_upload_data(pctx->stream_uploader, 0, cb->buffer_size, 64,236cb->user_buffer, &cb->buffer_offset, &cb->buffer);237cb->user_buffer = NULL;238}239240if ((constbuf->enabled_mask & (1 << i)) && cb->buffer) {241offsets[i] = cb->buffer_offset;242bos[i] = fd_resource(cb->buffer)->bo;243} else {244offsets[i] = 0;245bos[i] = NULL;246}247}248249assert(offset * 4 + params <= v->constlen * 4);250251emit_const_ptrs(ring, v, offset * 4, params, bos, offsets);252}253}254255static inline void256ir3_emit_ssbo_sizes(struct fd_screen *screen,257const struct ir3_shader_variant *v,258struct fd_ringbuffer *ring,259struct fd_shaderbuf_stateobj *sb)260{261const struct ir3_const_state *const_state = ir3_const_state(v);262uint32_t offset = const_state->offsets.ssbo_sizes;263if (v->constlen > offset) {264uint32_t sizes[align(const_state->ssbo_size.count, 4)];265unsigned mask = const_state->ssbo_size.mask;266267while (mask) {268unsigned index = u_bit_scan(&mask);269unsigned off = const_state->ssbo_size.off[index];270sizes[off] = sb->sb[index].buffer_size;271}272273emit_const_user(ring, v, offset * 4, ARRAY_SIZE(sizes), sizes);274}275}276277static inline void278ir3_emit_image_dims(struct fd_screen *screen,279const struct ir3_shader_variant *v,280struct fd_ringbuffer *ring,281struct fd_shaderimg_stateobj *si)282{283const struct ir3_const_state *const_state = ir3_const_state(v);284uint32_t offset = const_state->offsets.image_dims;285if (v->constlen > offset) {286uint32_t dims[align(const_state->image_dims.count, 4)];287unsigned mask = const_state->image_dims.mask;288289while (mask) {290struct pipe_image_view *img;291struct fd_resource *rsc;292unsigned index = u_bit_scan(&mask);293unsigned off = const_state->image_dims.off[index];294295img = &si->si[index];296rsc = fd_resource(img->resource);297298dims[off + 0] = util_format_get_blocksize(img->format);299if (img->resource->target != PIPE_BUFFER) {300struct fdl_slice *slice = fd_resource_slice(rsc, img->u.tex.level);301/* note for 2d/cube/etc images, even if re-interpreted302* as a different color format, the pixel size should303* be the same, so use original dimensions for y and z304* stride:305*/306dims[off + 1] = fd_resource_pitch(rsc, img->u.tex.level);307/* see corresponding logic in fd_resource_offset(): */308if (rsc->layout.layer_first) {309dims[off + 2] = rsc->layout.layer_size;310} else {311dims[off + 2] = slice->size0;312}313} else {314/* For buffer-backed images, the log2 of the format's315* bytes-per-pixel is placed on the 2nd slot. This is useful316* when emitting image_size instructions, for which we need317* to divide by bpp for image buffers. Since the bpp318* can only be power-of-two, the division is implemented319* as a SHR, and for that it is handy to have the log2 of320* bpp as a constant. (log2 = first-set-bit - 1)321*/322dims[off + 1] = ffs(dims[off + 0]) - 1;323}324}325uint32_t size = MIN2(ARRAY_SIZE(dims), v->constlen * 4 - offset * 4);326327emit_const_user(ring, v, offset * 4, size, dims);328}329}330331static inline void332ir3_emit_immediates(struct fd_screen *screen,333const struct ir3_shader_variant *v,334struct fd_ringbuffer *ring)335{336const struct ir3_const_state *const_state = ir3_const_state(v);337uint32_t base = const_state->offsets.immediate;338int size = DIV_ROUND_UP(const_state->immediates_count, 4);339340/* truncate size to avoid writing constants that shader341* does not use:342*/343size = MIN2(size + base, v->constlen) - base;344345/* convert out of vec4: */346base *= 4;347size *= 4;348349if (size > 0)350emit_const_user(ring, v, base, size, const_state->immediates);351352/* NIR constant data has the same lifetime as immediates, so upload it353* now, too.354*/355ir3_emit_constant_data(screen, v, ring);356}357358static inline void359ir3_emit_link_map(struct fd_screen *screen,360const struct ir3_shader_variant *producer,361const struct ir3_shader_variant *v,362struct fd_ringbuffer *ring)363{364const struct ir3_const_state *const_state = ir3_const_state(v);365uint32_t base = const_state->offsets.primitive_map;366int size = DIV_ROUND_UP(v->input_size, 4);367368/* truncate size to avoid writing constants that shader369* does not use:370*/371size = MIN2(size + base, v->constlen) - base;372373/* convert out of vec4: */374base *= 4;375size *= 4;376377if (size > 0)378emit_const_user(ring, v, base, size, producer->output_loc);379}380381/* emit stream-out buffers: */382static inline void383emit_tfbos(struct fd_context *ctx, const struct ir3_shader_variant *v,384struct fd_ringbuffer *ring)385{386/* streamout addresses after driver-params: */387const struct ir3_const_state *const_state = ir3_const_state(v);388uint32_t offset = const_state->offsets.tfbo;389if (v->constlen > offset) {390struct fd_streamout_stateobj *so = &ctx->streamout;391struct ir3_stream_output_info *info = &v->shader->stream_output;392uint32_t params = 4;393uint32_t offsets[params];394struct fd_bo *bos[params];395396for (uint32_t i = 0; i < params; i++) {397struct pipe_stream_output_target *target = so->targets[i];398399if (target) {400offsets[i] =401(so->offsets[i] * info->stride[i] * 4) + target->buffer_offset;402bos[i] = fd_resource(target->buffer)->bo;403} else {404offsets[i] = 0;405bos[i] = NULL;406}407}408409assert(offset * 4 + params <= v->constlen * 4);410411emit_const_ptrs(ring, v, offset * 4, params, bos, offsets);412}413}414415static inline void416emit_common_consts(const struct ir3_shader_variant *v,417struct fd_ringbuffer *ring, struct fd_context *ctx,418enum pipe_shader_type t) assert_dt419{420enum fd_dirty_shader_state dirty = ctx->dirty_shader[t];421422/* When we use CP_SET_DRAW_STATE objects to emit constant state,423* if we emit any of it we need to emit all. This is because424* we are using the same state-group-id each time for uniform425* state, and if previous update is never evaluated (due to no426* visible primitives in the current tile) then the new stateobj427* completely replaces the old one.428*429* Possibly if we split up different parts of the const state to430* different state-objects we could avoid this.431*/432if (dirty && is_stateobj(ring))433dirty = ~0;434435if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)) {436struct fd_constbuf_stateobj *constbuf;437bool shader_dirty;438439constbuf = &ctx->constbuf[t];440shader_dirty = !!(dirty & FD_DIRTY_SHADER_PROG);441442ring_wfi(ctx->batch, ring);443444ir3_emit_user_consts(ctx->screen, v, ring, constbuf);445ir3_emit_ubos(ctx, v, ring, constbuf);446if (shader_dirty)447ir3_emit_immediates(ctx->screen, v, ring);448}449450if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_SSBO)) {451struct fd_shaderbuf_stateobj *sb = &ctx->shaderbuf[t];452ring_wfi(ctx->batch, ring);453ir3_emit_ssbo_sizes(ctx->screen, v, ring, sb);454}455456if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_IMAGE)) {457struct fd_shaderimg_stateobj *si = &ctx->shaderimg[t];458ring_wfi(ctx->batch, ring);459ir3_emit_image_dims(ctx->screen, v, ring, si);460}461}462463static inline void464ir3_emit_vs_driver_params(const struct ir3_shader_variant *v,465struct fd_ringbuffer *ring, struct fd_context *ctx,466const struct pipe_draw_info *info,467const struct pipe_draw_indirect_info *indirect,468const struct pipe_draw_start_count_bias *draw) assert_dt469{470assert(v->need_driver_params);471472const struct ir3_const_state *const_state = ir3_const_state(v);473uint32_t offset = const_state->offsets.driver_param;474uint32_t vertex_params[IR3_DP_VS_COUNT] = {475[IR3_DP_DRAWID] = 0, /* filled by hw (CP_DRAW_INDIRECT_MULTI) */476[IR3_DP_VTXID_BASE] = info->index_size ? draw->index_bias : draw->start,477[IR3_DP_INSTID_BASE] = info->start_instance,478[IR3_DP_VTXCNT_MAX] = ctx->streamout.max_tf_vtx,479};480if (v->key.ucp_enables) {481struct pipe_clip_state *ucp = &ctx->ucp;482unsigned pos = IR3_DP_UCP0_X;483for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {484for (unsigned j = 0; j < 4; j++) {485vertex_params[pos] = fui(ucp->ucp[i][j]);486pos++;487}488}489}490491/* Only emit as many params as needed, i.e. up to the highest enabled UCP492* plane. However a binning pass may drop even some of these, so limit to493* program max.494*/495const uint32_t vertex_params_size =496MIN2(const_state->num_driver_params, (v->constlen - offset) * 4);497assert(vertex_params_size <= IR3_DP_VS_COUNT);498499bool needs_vtxid_base =500ir3_find_sysval_regid(v, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) !=501regid(63, 0);502503/* for indirect draw, we need to copy VTXID_BASE from504* indirect-draw parameters buffer.. which is annoying505* and means we can't easily emit these consts in cmd506* stream so need to copy them to bo.507*/508if (indirect && needs_vtxid_base) {509struct pipe_resource *vertex_params_rsc =510pipe_buffer_create(&ctx->screen->base, PIPE_BIND_CONSTANT_BUFFER,511PIPE_USAGE_STREAM, vertex_params_size * 4);512unsigned src_off = indirect->offset;513;514void *ptr;515516ptr = fd_bo_map(fd_resource(vertex_params_rsc)->bo);517memcpy(ptr, vertex_params, vertex_params_size * 4);518519if (info->index_size) {520/* indexed draw, index_bias is 4th field: */521src_off += 3 * 4;522} else {523/* non-indexed draw, start is 3rd field: */524src_off += 2 * 4;525}526527/* copy index_bias or start from draw params: */528ctx->screen->mem_to_mem(ring, vertex_params_rsc, 0, indirect->buffer,529src_off, 1);530531emit_const_prsc(ring, v, offset * 4, 0, vertex_params_size,532vertex_params_rsc);533534pipe_resource_reference(&vertex_params_rsc, NULL);535} else {536emit_const_user(ring, v, offset * 4, vertex_params_size, vertex_params);537}538539/* if needed, emit stream-out buffer addresses: */540if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {541emit_tfbos(ctx, v, ring);542}543}544545static inline void546ir3_emit_vs_consts(const struct ir3_shader_variant *v,547struct fd_ringbuffer *ring, struct fd_context *ctx,548const struct pipe_draw_info *info,549const struct pipe_draw_indirect_info *indirect,550const struct pipe_draw_start_count_bias *draw) assert_dt551{552debug_assert(v->type == MESA_SHADER_VERTEX);553554emit_common_consts(v, ring, ctx, PIPE_SHADER_VERTEX);555556/* emit driver params every time: */557if (info && v->need_driver_params) {558ring_wfi(ctx->batch, ring);559ir3_emit_vs_driver_params(v, ring, ctx, info, indirect, draw);560}561}562563static inline void564ir3_emit_fs_consts(const struct ir3_shader_variant *v,565struct fd_ringbuffer *ring, struct fd_context *ctx) assert_dt566{567debug_assert(v->type == MESA_SHADER_FRAGMENT);568569emit_common_consts(v, ring, ctx, PIPE_SHADER_FRAGMENT);570}571572/* emit compute-shader consts: */573static inline void574ir3_emit_cs_consts(const struct ir3_shader_variant *v,575struct fd_ringbuffer *ring, struct fd_context *ctx,576const struct pipe_grid_info *info) assert_dt577{578debug_assert(gl_shader_stage_is_compute(v->type));579580emit_common_consts(v, ring, ctx, PIPE_SHADER_COMPUTE);581582/* emit compute-shader driver-params: */583const struct ir3_const_state *const_state = ir3_const_state(v);584uint32_t offset = const_state->offsets.driver_param;585if (v->constlen > offset) {586ring_wfi(ctx->batch, ring);587588if (info->indirect) {589struct pipe_resource *indirect = NULL;590unsigned indirect_offset;591592/* This is a bit awkward, but CP_LOAD_STATE.EXT_SRC_ADDR needs593* to be aligned more strongly than 4 bytes. So in this case594* we need a temporary buffer to copy NumWorkGroups.xyz to.595*596* TODO if previous compute job is writing to info->indirect,597* we might need a WFI.. but since we currently flush for each598* compute job, we are probably ok for now.599*/600if (info->indirect_offset & 0xf) {601indirect = pipe_buffer_create(&ctx->screen->base,602PIPE_BIND_COMMAND_ARGS_BUFFER,603PIPE_USAGE_STREAM, 0x1000);604indirect_offset = 0;605606ctx->screen->mem_to_mem(ring, indirect, 0, info->indirect,607info->indirect_offset, 3);608} else {609pipe_resource_reference(&indirect, info->indirect);610indirect_offset = info->indirect_offset;611}612613emit_const_prsc(ring, v, offset * 4, indirect_offset, 16, indirect);614615pipe_resource_reference(&indirect, NULL);616} else {617uint32_t compute_params[IR3_DP_CS_COUNT] = {618[IR3_DP_NUM_WORK_GROUPS_X] = info->grid[0],619[IR3_DP_NUM_WORK_GROUPS_Y] = info->grid[1],620[IR3_DP_NUM_WORK_GROUPS_Z] = info->grid[2],621[IR3_DP_LOCAL_GROUP_SIZE_X] = info->block[0],622[IR3_DP_LOCAL_GROUP_SIZE_Y] = info->block[1],623[IR3_DP_LOCAL_GROUP_SIZE_Z] = info->block[2],624};625uint32_t size =626MIN2(const_state->num_driver_params, v->constlen * 4 - offset * 4);627628emit_const_user(ring, v, offset * 4, size, compute_params);629}630}631}632633634