Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a6xx/fd6_draw.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*23* Authors:24* Rob Clark <[email protected]>25*/2627#include "pipe/p_state.h"28#include "util/u_memory.h"29#include "util/u_prim.h"30#include "util/u_string.h"3132#include "freedreno_resource.h"33#include "freedreno_state.h"3435#include "fd6_context.h"36#include "fd6_draw.h"37#include "fd6_emit.h"38#include "fd6_format.h"39#include "fd6_program.h"40#include "fd6_vsc.h"41#include "fd6_zsa.h"4243#include "fd6_pack.h"4445static void46draw_emit_xfb(struct fd_ringbuffer *ring, struct CP_DRAW_INDX_OFFSET_0 *draw0,47const struct pipe_draw_info *info,48const struct pipe_draw_indirect_info *indirect)49{50struct fd_stream_output_target *target =51fd_stream_output_target(indirect->count_from_stream_output);52struct fd_resource *offset = fd_resource(target->offset_buf);5354/* All known firmware versions do not wait for WFI's with CP_DRAW_AUTO.55* Plus, for the common case where the counter buffer is written by56* vkCmdEndTransformFeedback, we need to wait for the CP_WAIT_MEM_WRITES to57* complete which means we need a WAIT_FOR_ME anyway.58*/59OUT_PKT7(ring, CP_WAIT_FOR_ME, 0);6061OUT_PKT7(ring, CP_DRAW_AUTO, 6);62OUT_RING(ring, pack_CP_DRAW_INDX_OFFSET_0(*draw0).value);63OUT_RING(ring, info->instance_count);64OUT_RELOC(ring, offset->bo, 0, 0, 0);65OUT_RING(66ring,670); /* byte counter offset subtraced from the value read from above */68OUT_RING(ring, target->stride);69}7071static void72draw_emit_indirect(struct fd_ringbuffer *ring,73struct CP_DRAW_INDX_OFFSET_0 *draw0,74const struct pipe_draw_info *info,75const struct pipe_draw_indirect_info *indirect,76unsigned index_offset)77{78struct fd_resource *ind = fd_resource(indirect->buffer);7980if (info->index_size) {81struct pipe_resource *idx = info->index.resource;82unsigned max_indices = (idx->width0 - index_offset) / info->index_size;8384OUT_PKT(ring, CP_DRAW_INDX_INDIRECT, pack_CP_DRAW_INDX_OFFSET_0(*draw0),85A5XX_CP_DRAW_INDX_INDIRECT_INDX_BASE(fd_resource(idx)->bo,86index_offset),87A5XX_CP_DRAW_INDX_INDIRECT_3(.max_indices = max_indices),88A5XX_CP_DRAW_INDX_INDIRECT_INDIRECT(ind->bo, indirect->offset));89} else {90OUT_PKT(ring, CP_DRAW_INDIRECT, pack_CP_DRAW_INDX_OFFSET_0(*draw0),91A5XX_CP_DRAW_INDIRECT_INDIRECT(ind->bo, indirect->offset));92}93}9495static void96draw_emit(struct fd_ringbuffer *ring, struct CP_DRAW_INDX_OFFSET_0 *draw0,97const struct pipe_draw_info *info,98const struct pipe_draw_start_count_bias *draw, unsigned index_offset)99{100if (info->index_size) {101assert(!info->has_user_indices);102103struct pipe_resource *idx_buffer = info->index.resource;104unsigned max_indices =105(idx_buffer->width0 - index_offset) / info->index_size;106107OUT_PKT(ring, CP_DRAW_INDX_OFFSET, pack_CP_DRAW_INDX_OFFSET_0(*draw0),108CP_DRAW_INDX_OFFSET_1(.num_instances = info->instance_count),109CP_DRAW_INDX_OFFSET_2(.num_indices = draw->count),110CP_DRAW_INDX_OFFSET_3(.first_indx = draw->start),111A5XX_CP_DRAW_INDX_OFFSET_INDX_BASE(fd_resource(idx_buffer)->bo,112index_offset),113A5XX_CP_DRAW_INDX_OFFSET_6(.max_indices = max_indices));114} else {115OUT_PKT(ring, CP_DRAW_INDX_OFFSET, pack_CP_DRAW_INDX_OFFSET_0(*draw0),116CP_DRAW_INDX_OFFSET_1(.num_instances = info->instance_count),117CP_DRAW_INDX_OFFSET_2(.num_indices = draw->count));118}119}120121static void122fixup_draw_state(struct fd_context *ctx, struct fd6_emit *emit) assert_dt123{124if (ctx->last.dirty ||125(ctx->last.primitive_restart != emit->primitive_restart)) {126/* rasterizer state is effected by primitive-restart: */127fd_context_dirty(ctx, FD_DIRTY_RASTERIZER);128ctx->last.primitive_restart = emit->primitive_restart;129}130}131132static bool133fd6_draw_vbo(struct fd_context *ctx, const struct pipe_draw_info *info,134unsigned drawid_offset,135const struct pipe_draw_indirect_info *indirect,136const struct pipe_draw_start_count_bias *draw,137unsigned index_offset) assert_dt138{139struct fd6_context *fd6_ctx = fd6_context(ctx);140struct shader_info *gs_info = ir3_get_shader_info(ctx->prog.gs);141struct fd6_emit emit = {142.ctx = ctx,143.vtx = &ctx->vtx,144.info = info,145.drawid_offset = drawid_offset,146.indirect = indirect,147.draw = draw,148.key = {149.vs = ctx->prog.vs,150.gs = ctx->prog.gs,151.fs = ctx->prog.fs,152.key = {153.rasterflat = ctx->rasterizer->flatshade,154.layer_zero = !gs_info || !(gs_info->outputs_written & VARYING_BIT_LAYER),155.sample_shading = (ctx->min_samples > 1),156.msaa = (ctx->framebuffer.samples > 1),157},158},159.rasterflat = ctx->rasterizer->flatshade,160.sprite_coord_enable = ctx->rasterizer->sprite_coord_enable,161.sprite_coord_mode = ctx->rasterizer->sprite_coord_mode,162.primitive_restart = info->primitive_restart && info->index_size,163};164165if (!(ctx->prog.vs && ctx->prog.fs))166return false;167168if (info->mode == PIPE_PRIM_PATCHES) {169emit.key.hs = ctx->prog.hs;170emit.key.ds = ctx->prog.ds;171172if (!(ctx->prog.hs && ctx->prog.ds))173return false;174175struct shader_info *ds_info = ir3_get_shader_info(emit.key.ds);176emit.key.key.tessellation = ir3_tess_mode(ds_info->tess.primitive_mode);177ctx->gen_dirty |= BIT(FD6_GROUP_PRIMITIVE_PARAMS);178}179180if (emit.key.gs) {181emit.key.key.has_gs = true;182ctx->gen_dirty |= BIT(FD6_GROUP_PRIMITIVE_PARAMS);183}184185if (!(emit.key.hs || emit.key.ds || emit.key.gs || indirect))186fd6_vsc_update_sizes(ctx->batch, info, draw);187188ir3_fixup_shader_state(&ctx->base, &emit.key.key);189190if (!(ctx->dirty & FD_DIRTY_PROG)) {191emit.prog = fd6_ctx->prog;192} else {193fd6_ctx->prog = fd6_emit_get_prog(&emit);194}195196/* bail if compile failed: */197if (!fd6_ctx->prog)198return false;199200fixup_draw_state(ctx, &emit);201202/* *after* fixup_shader_state(): */203emit.dirty = ctx->dirty;204emit.dirty_groups = ctx->gen_dirty;205206emit.bs = fd6_emit_get_prog(&emit)->bs;207emit.vs = fd6_emit_get_prog(&emit)->vs;208emit.hs = fd6_emit_get_prog(&emit)->hs;209emit.ds = fd6_emit_get_prog(&emit)->ds;210emit.gs = fd6_emit_get_prog(&emit)->gs;211emit.fs = fd6_emit_get_prog(&emit)->fs;212213if (emit.vs->need_driver_params || fd6_ctx->has_dp_state)214emit.dirty_groups |= BIT(FD6_GROUP_VS_DRIVER_PARAMS);215216/* If we are doing xfb, we need to emit the xfb state on every draw: */217if (emit.prog->stream_output)218emit.dirty_groups |= BIT(FD6_GROUP_SO);219220if (unlikely(ctx->stats_users > 0)) {221ctx->stats.vs_regs += ir3_shader_halfregs(emit.vs);222ctx->stats.hs_regs += COND(emit.hs, ir3_shader_halfregs(emit.hs));223ctx->stats.ds_regs += COND(emit.ds, ir3_shader_halfregs(emit.ds));224ctx->stats.gs_regs += COND(emit.gs, ir3_shader_halfregs(emit.gs));225ctx->stats.fs_regs += ir3_shader_halfregs(emit.fs);226}227228struct fd_ringbuffer *ring = ctx->batch->draw;229230struct CP_DRAW_INDX_OFFSET_0 draw0 = {231.prim_type = ctx->primtypes[info->mode],232.vis_cull = USE_VISIBILITY,233.gs_enable = !!emit.key.gs,234};235236if (indirect && indirect->count_from_stream_output) {237draw0.source_select = DI_SRC_SEL_AUTO_XFB;238} else if (info->index_size) {239draw0.source_select = DI_SRC_SEL_DMA;240draw0.index_size = fd4_size2indextype(info->index_size);241} else {242draw0.source_select = DI_SRC_SEL_AUTO_INDEX;243}244245if (info->mode == PIPE_PRIM_PATCHES) {246shader_info *ds_info = &emit.ds->shader->nir->info;247uint32_t factor_stride;248249switch (ds_info->tess.primitive_mode) {250case GL_ISOLINES:251draw0.patch_type = TESS_ISOLINES;252factor_stride = 12;253break;254case GL_TRIANGLES:255draw0.patch_type = TESS_TRIANGLES;256factor_stride = 20;257break;258case GL_QUADS:259draw0.patch_type = TESS_QUADS;260factor_stride = 28;261break;262default:263unreachable("bad tessmode");264}265266draw0.prim_type = DI_PT_PATCHES0 + info->vertices_per_patch;267draw0.tess_enable = true;268269const unsigned max_count = 2048;270unsigned count;271272/**273* We can cap tessparam/tessfactor buffer sizes at the sub-draw274* limit. But in the indirect-draw case we must assume the worst.275*/276if (indirect && indirect->buffer) {277count = ALIGN_NPOT(max_count, info->vertices_per_patch);278} else {279count = MIN2(max_count, draw->count);280count = ALIGN_NPOT(count, info->vertices_per_patch);281}282283OUT_PKT7(ring, CP_SET_SUBDRAW_SIZE, 1);284OUT_RING(ring, count);285286ctx->batch->tessellation = true;287ctx->batch->tessparam_size =288MAX2(ctx->batch->tessparam_size, emit.hs->output_size * 4 * count);289ctx->batch->tessfactor_size =290MAX2(ctx->batch->tessfactor_size, factor_stride * count);291292if (!ctx->batch->tess_addrs_constobj) {293/* Reserve space for the bo address - we'll write them later in294* setup_tess_buffers(). We need 2 bo address, but indirect295* constant upload needs at least 4 vec4s.296*/297unsigned size = 4 * 16;298299ctx->batch->tess_addrs_constobj = fd_submit_new_ringbuffer(300ctx->batch->submit, size, FD_RINGBUFFER_STREAMING);301302ctx->batch->tess_addrs_constobj->cur += size;303}304}305306uint32_t index_start = info->index_size ? draw->index_bias : draw->start;307if (ctx->last.dirty || (ctx->last.index_start != index_start)) {308OUT_PKT4(ring, REG_A6XX_VFD_INDEX_OFFSET, 1);309OUT_RING(ring, index_start); /* VFD_INDEX_OFFSET */310ctx->last.index_start = index_start;311}312313if (ctx->last.dirty || (ctx->last.instance_start != info->start_instance)) {314OUT_PKT4(ring, REG_A6XX_VFD_INSTANCE_START_OFFSET, 1);315OUT_RING(ring, info->start_instance); /* VFD_INSTANCE_START_OFFSET */316ctx->last.instance_start = info->start_instance;317}318319uint32_t restart_index =320info->primitive_restart ? info->restart_index : 0xffffffff;321if (ctx->last.dirty || (ctx->last.restart_index != restart_index)) {322OUT_PKT4(ring, REG_A6XX_PC_RESTART_INDEX, 1);323OUT_RING(ring, restart_index); /* PC_RESTART_INDEX */324ctx->last.restart_index = restart_index;325}326327// TODO move fd6_emit_streamout.. I think..328if (emit.dirty_groups)329fd6_emit_state(ring, &emit);330331/* for debug after a lock up, write a unique counter value332* to scratch7 for each draw, to make it easier to match up333* register dumps to cmdstream. The combination of IB334* (scratch6) and DRAW is enough to "triangulate" the335* particular draw that caused lockup.336*/337emit_marker6(ring, 7);338339if (indirect) {340if (indirect->count_from_stream_output) {341draw_emit_xfb(ring, &draw0, info, indirect);342} else {343draw_emit_indirect(ring, &draw0, info, indirect, index_offset);344}345} else {346draw_emit(ring, &draw0, info, draw, index_offset);347}348349emit_marker6(ring, 7);350fd_reset_wfi(ctx->batch);351352if (emit.streamout_mask) {353struct fd_ringbuffer *ring = ctx->batch->draw;354355for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {356if (emit.streamout_mask & (1 << i)) {357fd6_event_write(ctx->batch, ring, FLUSH_SO_0 + i, false);358}359}360}361362fd_context_all_clean(ctx);363364return true;365}366367static void368fd6_clear_lrz(struct fd_batch *batch, struct fd_resource *zsbuf, double depth)369{370struct fd_ringbuffer *ring;371struct fd_screen *screen = batch->ctx->screen;372373ring = fd_batch_get_prologue(batch);374375emit_marker6(ring, 7);376OUT_PKT7(ring, CP_SET_MARKER, 1);377OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BYPASS));378emit_marker6(ring, 7);379380OUT_WFI5(ring);381382OUT_REG(ring, A6XX_RB_CCU_CNTL(.color_offset = screen->ccu_offset_bypass));383384OUT_REG(ring,385A6XX_HLSQ_INVALIDATE_CMD(.vs_state = true, .hs_state = true,386.ds_state = true, .gs_state = true,387.fs_state = true, .cs_state = true,388.gfx_ibo = true, .cs_ibo = true,389.gfx_shared_const = true,390.gfx_bindless = 0x1f, .cs_bindless = 0x1f));391392emit_marker6(ring, 7);393OUT_PKT7(ring, CP_SET_MARKER, 1);394OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BLIT2DSCALE));395emit_marker6(ring, 7);396397OUT_PKT4(ring, REG_A6XX_RB_2D_UNKNOWN_8C01, 1);398OUT_RING(ring, 0x0);399400OUT_PKT4(ring, REG_A6XX_SP_PS_2D_SRC_INFO, 13);401OUT_RING(ring, 0x00000000);402OUT_RING(ring, 0x00000000);403OUT_RING(ring, 0x00000000);404OUT_RING(ring, 0x00000000);405OUT_RING(ring, 0x00000000);406OUT_RING(ring, 0x00000000);407OUT_RING(ring, 0x00000000);408OUT_RING(ring, 0x00000000);409OUT_RING(ring, 0x00000000);410OUT_RING(ring, 0x00000000);411OUT_RING(ring, 0x00000000);412OUT_RING(ring, 0x00000000);413OUT_RING(ring, 0x00000000);414415OUT_PKT4(ring, REG_A6XX_SP_2D_DST_FORMAT, 1);416OUT_RING(ring, 0x0000f410);417418OUT_PKT4(ring, REG_A6XX_GRAS_2D_BLIT_CNTL, 1);419OUT_RING(ring,420A6XX_GRAS_2D_BLIT_CNTL_COLOR_FORMAT(FMT6_16_UNORM) | 0x4f00080);421422OUT_PKT4(ring, REG_A6XX_RB_2D_BLIT_CNTL, 1);423OUT_RING(ring, A6XX_RB_2D_BLIT_CNTL_COLOR_FORMAT(FMT6_16_UNORM) | 0x4f00080);424425fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);426fd6_event_write(batch, ring, PC_CCU_INVALIDATE_COLOR, false);427428OUT_PKT4(ring, REG_A6XX_RB_2D_SRC_SOLID_C0, 4);429OUT_RING(ring, fui(depth));430OUT_RING(ring, 0x00000000);431OUT_RING(ring, 0x00000000);432OUT_RING(ring, 0x00000000);433434OUT_PKT4(ring, REG_A6XX_RB_2D_DST_INFO, 9);435OUT_RING(ring, A6XX_RB_2D_DST_INFO_COLOR_FORMAT(FMT6_16_UNORM) |436A6XX_RB_2D_DST_INFO_TILE_MODE(TILE6_LINEAR) |437A6XX_RB_2D_DST_INFO_COLOR_SWAP(WZYX));438OUT_RELOC(ring, zsbuf->lrz, 0, 0, 0);439OUT_RING(ring, A6XX_RB_2D_DST_PITCH(zsbuf->lrz_pitch * 2).value);440OUT_RING(ring, 0x00000000);441OUT_RING(ring, 0x00000000);442OUT_RING(ring, 0x00000000);443OUT_RING(ring, 0x00000000);444OUT_RING(ring, 0x00000000);445446OUT_REG(ring, A6XX_GRAS_2D_SRC_TL_X(0), A6XX_GRAS_2D_SRC_BR_X(0),447A6XX_GRAS_2D_SRC_TL_Y(0), A6XX_GRAS_2D_SRC_BR_Y(0));448449OUT_PKT4(ring, REG_A6XX_GRAS_2D_DST_TL, 2);450OUT_RING(ring, A6XX_GRAS_2D_DST_TL_X(0) | A6XX_GRAS_2D_DST_TL_Y(0));451OUT_RING(ring, A6XX_GRAS_2D_DST_BR_X(zsbuf->lrz_width - 1) |452A6XX_GRAS_2D_DST_BR_Y(zsbuf->lrz_height - 1));453454fd6_event_write(batch, ring, 0x3f, false);455456OUT_WFI5(ring);457458OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);459OUT_RING(ring, screen->info->a6xx.magic.RB_UNKNOWN_8E04_blit);460461OUT_PKT7(ring, CP_BLIT, 1);462OUT_RING(ring, CP_BLIT_0_OP(BLIT_OP_SCALE));463464OUT_WFI5(ring);465466OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);467OUT_RING(ring, 0x0); /* RB_UNKNOWN_8E04 */468469fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);470fd6_event_write(batch, ring, PC_CCU_FLUSH_DEPTH_TS, true);471fd6_event_write(batch, ring, CACHE_FLUSH_TS, true);472473fd6_cache_inv(batch, ring);474}475476static bool477is_z32(enum pipe_format format)478{479switch (format) {480case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:481case PIPE_FORMAT_Z32_UNORM:482case PIPE_FORMAT_Z32_FLOAT:483return true;484default:485return false;486}487}488489static bool490fd6_clear(struct fd_context *ctx, unsigned buffers,491const union pipe_color_union *color, double depth,492unsigned stencil) assert_dt493{494struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;495const bool has_depth = pfb->zsbuf;496unsigned color_buffers = buffers >> 2;497498/* we need to do multisample clear on 3d pipe, so fallback to u_blitter: */499if (pfb->samples > 1)500return false;501502/* If we're clearing after draws, fallback to 3D pipe clears. We could503* use blitter clears in the draw batch but then we'd have to patch up the504* gmem offsets. This doesn't seem like a useful thing to optimize for505* however.*/506if (ctx->batch->num_draws > 0)507return false;508509u_foreach_bit (i, color_buffers)510ctx->batch->clear_color[i] = *color;511if (buffers & PIPE_CLEAR_DEPTH)512ctx->batch->clear_depth = depth;513if (buffers & PIPE_CLEAR_STENCIL)514ctx->batch->clear_stencil = stencil;515516ctx->batch->fast_cleared |= buffers;517518if (has_depth && (buffers & PIPE_CLEAR_DEPTH)) {519struct fd_resource *zsbuf = fd_resource(pfb->zsbuf->texture);520if (zsbuf->lrz && !is_z32(pfb->zsbuf->format)) {521zsbuf->lrz_valid = true;522zsbuf->lrz_direction = FD_LRZ_UNKNOWN;523fd6_clear_lrz(ctx->batch, zsbuf, depth);524}525}526527return true;528}529530void531fd6_draw_init(struct pipe_context *pctx) disable_thread_safety_analysis532{533struct fd_context *ctx = fd_context(pctx);534ctx->draw_vbo = fd6_draw_vbo;535ctx->clear = fd6_clear;536}537538539