Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3dx_draw.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_blitter.h"24#include "util/u_draw.h"25#include "util/u_prim.h"26#include "util/format/u_format.h"27#include "util/u_pack_color.h"28#include "util/u_prim_restart.h"29#include "util/u_upload_mgr.h"30#include "indices/u_primconvert.h"3132#include "v3d_context.h"33#include "v3d_resource.h"34#include "v3d_cl.h"35#include "broadcom/compiler/v3d_compiler.h"36#include "broadcom/common/v3d_macros.h"37#include "broadcom/common/v3d_util.h"38#include "broadcom/cle/v3dx_pack.h"3940static void41v3d_start_binning(struct v3d_context *v3d, struct v3d_job *job)42{43assert(job->needs_flush);4445/* Get space to emit our BCL state, using a branch to jump to a new BO46* if necessary.47*/4849v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);5051job->submit.bcl_start = job->bcl.bo->offset;52v3d_job_add_bo(job, job->bcl.bo);5354/* The PTB will request the tile alloc initial size per tile at start55* of tile binning.56*/57uint32_t tile_alloc_size =58MAX2(job->num_layers, 1) * job->draw_tiles_x * job->draw_tiles_y * 64;5960/* The PTB allocates in aligned 4k chunks after the initial setup. */61tile_alloc_size = align(tile_alloc_size, 4096);6263/* Include the first two chunk allocations that the PTB does so that64* we definitely clear the OOM condition before triggering one (the HW65* won't trigger OOM during the first allocations).66*/67tile_alloc_size += 8192;6869/* For performance, allocate some extra initial memory after the PTB's70* minimal allocations, so that we hopefully don't have to block the71* GPU on the kernel handling an OOM signal.72*/73tile_alloc_size += 512 * 1024;7475job->tile_alloc = v3d_bo_alloc(v3d->screen, tile_alloc_size,76"tile_alloc");77uint32_t tsda_per_tile_size = v3d->screen->devinfo.ver >= 40 ? 256 : 64;78job->tile_state = v3d_bo_alloc(v3d->screen,79MAX2(job->num_layers, 1) *80job->draw_tiles_y *81job->draw_tiles_x *82tsda_per_tile_size,83"TSDA");8485#if V3D_VERSION >= 4186/* This must go before the binning mode configuration. It is87* required for layered framebuffers to work.88*/89if (job->num_layers > 0) {90cl_emit(&job->bcl, NUMBER_OF_LAYERS, config) {91config.number_of_layers = job->num_layers;92}93}94#endif9596#if V3D_VERSION >= 4097cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) {98config.width_in_pixels = job->draw_width;99config.height_in_pixels = job->draw_height;100config.number_of_render_targets =101MAX2(job->nr_cbufs, 1);102103config.multisample_mode_4x = job->msaa;104105config.maximum_bpp_of_all_render_targets = job->internal_bpp;106}107#else /* V3D_VERSION < 40 */108/* "Binning mode lists start with a Tile Binning Mode Configuration109* item (120)"110*111* Part1 signals the end of binning config setup.112*/113cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART2, config) {114config.tile_allocation_memory_address =115cl_address(job->tile_alloc, 0);116config.tile_allocation_memory_size = job->tile_alloc->size;117}118119cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART1, config) {120config.tile_state_data_array_base_address =121cl_address(job->tile_state, 0);122123config.width_in_tiles = job->draw_tiles_x;124config.height_in_tiles = job->draw_tiles_y;125/* Must be >= 1 */126config.number_of_render_targets =127MAX2(job->nr_cbufs, 1);128129config.multisample_mode_4x = job->msaa;130131config.maximum_bpp_of_all_render_targets = job->internal_bpp;132}133#endif /* V3D_VERSION < 40 */134135/* There's definitely nothing in the VCD cache we want. */136cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin);137138/* Disable any leftover OQ state from another job. */139cl_emit(&job->bcl, OCCLUSION_QUERY_COUNTER, counter);140141/* "Binning mode lists must have a Start Tile Binning item (6) after142* any prefix state data before the binning list proper starts."143*/144cl_emit(&job->bcl, START_TILE_BINNING, bin);145}146/**147* Does the initial bining command list setup for drawing to a given FBO.148*/149static void150v3d_start_draw(struct v3d_context *v3d)151{152struct v3d_job *job = v3d->job;153154if (job->needs_flush)155return;156157job->needs_flush = true;158job->draw_width = v3d->framebuffer.width;159job->draw_height = v3d->framebuffer.height;160job->num_layers = util_framebuffer_get_num_layers(&v3d->framebuffer);161162v3d_start_binning(v3d, job);163}164165static void166v3d_predraw_check_stage_inputs(struct pipe_context *pctx,167enum pipe_shader_type s)168{169struct v3d_context *v3d = v3d_context(pctx);170171/* Flush writes to textures we're sampling. */172for (int i = 0; i < v3d->tex[s].num_textures; i++) {173struct pipe_sampler_view *pview = v3d->tex[s].textures[i];174if (!pview)175continue;176struct v3d_sampler_view *view = v3d_sampler_view(pview);177178if (view->texture != view->base.texture &&179view->base.format != PIPE_FORMAT_X32_S8X24_UINT)180v3d_update_shadow_texture(pctx, &view->base);181182v3d_flush_jobs_writing_resource(v3d, view->texture,183V3D_FLUSH_DEFAULT,184s == PIPE_SHADER_COMPUTE);185}186187/* Flush writes to UBOs. */188u_foreach_bit(i, v3d->constbuf[s].enabled_mask) {189struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];190if (cb->buffer) {191v3d_flush_jobs_writing_resource(v3d, cb->buffer,192V3D_FLUSH_DEFAULT,193s == PIPE_SHADER_COMPUTE);194}195}196197/* Flush reads/writes to our SSBOs */198u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {199struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];200if (sb->buffer) {201v3d_flush_jobs_reading_resource(v3d, sb->buffer,202V3D_FLUSH_NOT_CURRENT_JOB,203s == PIPE_SHADER_COMPUTE);204}205}206207/* Flush reads/writes to our image views */208u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) {209struct v3d_image_view *view = &v3d->shaderimg[s].si[i];210211v3d_flush_jobs_reading_resource(v3d, view->base.resource,212V3D_FLUSH_NOT_CURRENT_JOB,213s == PIPE_SHADER_COMPUTE);214}215216/* Flush writes to our vertex buffers (i.e. from transform feedback) */217if (s == PIPE_SHADER_VERTEX) {218u_foreach_bit(i, v3d->vertexbuf.enabled_mask) {219struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];220221v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource,222V3D_FLUSH_DEFAULT,223false);224}225}226}227228static void229v3d_predraw_check_outputs(struct pipe_context *pctx)230{231struct v3d_context *v3d = v3d_context(pctx);232233/* Flush jobs reading from TF buffers that we are about to write. */234if (v3d_transform_feedback_enabled(v3d)) {235struct v3d_streamout_stateobj *so = &v3d->streamout;236237for (int i = 0; i < so->num_targets; i++) {238if (!so->targets[i])239continue;240241const struct pipe_stream_output_target *target =242so->targets[i];243v3d_flush_jobs_reading_resource(v3d, target->buffer,244V3D_FLUSH_DEFAULT,245false);246}247}248}249250/**251* Checks if the state for the current draw reads a particular resource in252* in the given shader stage.253*/254static bool255v3d_state_reads_resource(struct v3d_context *v3d,256struct pipe_resource *prsc,257enum pipe_shader_type s)258{259struct v3d_resource *rsc = v3d_resource(prsc);260261/* Vertex buffers */262if (s == PIPE_SHADER_VERTEX) {263u_foreach_bit(i, v3d->vertexbuf.enabled_mask) {264struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];265if (!vb->buffer.resource)266continue;267268struct v3d_resource *vb_rsc =269v3d_resource(vb->buffer.resource);270if (rsc->bo == vb_rsc->bo)271return true;272}273}274275/* Constant buffers */276u_foreach_bit(i, v3d->constbuf[s].enabled_mask) {277struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];278if (!cb->buffer)279continue;280281struct v3d_resource *cb_rsc = v3d_resource(cb->buffer);282if (rsc->bo == cb_rsc->bo)283return true;284}285286/* Shader storage buffers */287u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {288struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];289if (!sb->buffer)290continue;291292struct v3d_resource *sb_rsc = v3d_resource(sb->buffer);293if (rsc->bo == sb_rsc->bo)294return true;295}296297/* Textures */298for (int i = 0; i < v3d->tex[s].num_textures; i++) {299struct pipe_sampler_view *pview = v3d->tex[s].textures[i];300if (!pview)301continue;302303struct v3d_sampler_view *view = v3d_sampler_view(pview);304struct v3d_resource *v_rsc = v3d_resource(view->texture);305if (rsc->bo == v_rsc->bo)306return true;307}308309return false;310}311312static void313v3d_emit_wait_for_tf(struct v3d_job *job)314{315/* XXX: we might be able to skip this in some cases, for now we316* always emit it.317*/318cl_emit(&job->bcl, FLUSH_TRANSFORM_FEEDBACK_DATA, flush);319320cl_emit(&job->bcl, WAIT_FOR_TRANSFORM_FEEDBACK, wait) {321/* XXX: Wait for all outstanding writes... maybe we can do322* better in some cases.323*/324wait.block_count = 255;325}326327/* We have just flushed all our outstanding TF work in this job so make328* sure we don't emit TF flushes again for any of it again.329*/330_mesa_set_clear(job->tf_write_prscs, NULL);331}332333static void334v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job)335{336if (!job->tf_enabled)337return;338339set_foreach(job->tf_write_prscs, entry) {340struct pipe_resource *prsc = (struct pipe_resource *)entry->key;341for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {342/* Fragment shaders can only start executing after all343* binning (and thus TF) is complete.344*345* XXX: For VS/GS/TES, if the binning shader does not346* read the resource then we could also avoid emitting347* the wait.348*/349if (s == PIPE_SHADER_FRAGMENT)350continue;351352if (v3d_state_reads_resource(v3d, prsc, s)) {353v3d_emit_wait_for_tf(job);354return;355}356}357}358}359360#if V3D_VERSION >= 41361static void362v3d_emit_gs_state_record(struct v3d_job *job,363struct v3d_compiled_shader *gs_bin,364struct v3d_cl_reloc gs_bin_uniforms,365struct v3d_compiled_shader *gs,366struct v3d_cl_reloc gs_render_uniforms)367{368cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) {369shader.geometry_bin_mode_shader_code_address =370cl_address(v3d_resource(gs_bin->resource)->bo,371gs_bin->offset);372shader.geometry_bin_mode_shader_4_way_threadable =373gs_bin->prog_data.gs->base.threads == 4;374shader.geometry_bin_mode_shader_start_in_final_thread_section =375gs_bin->prog_data.gs->base.single_seg;376shader.geometry_bin_mode_shader_propagate_nans = true;377shader.geometry_bin_mode_shader_uniforms_address =378gs_bin_uniforms;379380shader.geometry_render_mode_shader_code_address =381cl_address(v3d_resource(gs->resource)->bo, gs->offset);382shader.geometry_render_mode_shader_4_way_threadable =383gs->prog_data.gs->base.threads == 4;384shader.geometry_render_mode_shader_start_in_final_thread_section =385gs->prog_data.gs->base.single_seg;386shader.geometry_render_mode_shader_propagate_nans = true;387shader.geometry_render_mode_shader_uniforms_address =388gs_render_uniforms;389}390}391392static uint8_t393v3d_gs_output_primitive(uint32_t prim_type)394{395switch (prim_type) {396case GL_POINTS:397return GEOMETRY_SHADER_POINTS;398case GL_LINE_STRIP:399return GEOMETRY_SHADER_LINE_STRIP;400case GL_TRIANGLE_STRIP:401return GEOMETRY_SHADER_TRI_STRIP;402default:403unreachable("Unsupported primitive type");404}405}406407static void408v3d_emit_tes_gs_common_params(struct v3d_job *job,409uint8_t gs_out_prim_type,410uint8_t gs_num_invocations)411{412/* This, and v3d_emit_tes_gs_shader_params below, fill in default413* values for tessellation fields even though we don't support414* tessellation yet because our packing functions (and the simulator)415* complain if we don't.416*/417cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) {418shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE;419shader.tessellation_point_mode = false;420shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN;421shader.tessellation_clockwise = true;422shader.tessellation_invocations = 1;423424shader.geometry_shader_output_format =425v3d_gs_output_primitive(gs_out_prim_type);426shader.geometry_shader_instances = gs_num_invocations & 0x1F;427}428}429430static uint8_t431simd_width_to_gs_pack_mode(uint32_t width)432{433switch (width) {434case 16:435return V3D_PACK_MODE_16_WAY;436case 8:437return V3D_PACK_MODE_8_WAY;438case 4:439return V3D_PACK_MODE_4_WAY;440case 1:441return V3D_PACK_MODE_1_WAY;442default:443unreachable("Invalid SIMD width");444};445}446447static void448v3d_emit_tes_gs_shader_params(struct v3d_job *job,449uint32_t gs_simd,450uint32_t gs_vpm_output_size,451uint32_t gs_max_vpm_input_size_per_batch)452{453cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) {454shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED;455shader.per_patch_data_column_depth = 1;456shader.tcs_output_segment_size_in_sectors = 1;457shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;458shader.tes_output_segment_size_in_sectors = 1;459shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;460shader.gs_output_segment_size_in_sectors = gs_vpm_output_size;461shader.gs_output_segment_pack_mode =462simd_width_to_gs_pack_mode(gs_simd);463shader.tbg_max_patches_per_tcs_batch = 1;464shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0;465shader.tbg_min_tcs_output_segments_required_in_play = 1;466shader.tbg_min_per_patch_data_segments_required_in_play = 1;467shader.tpg_max_patches_per_tes_batch = 1;468shader.tpg_max_vertex_segments_per_tes_batch = 0;469shader.tpg_max_tcs_output_segments_per_tes_batch = 1;470shader.tpg_min_tes_output_segments_required_in_play = 1;471shader.gbg_max_tes_output_vertex_segments_per_gs_batch =472gs_max_vpm_input_size_per_batch;473shader.gbg_min_gs_output_segments_required_in_play = 1;474}475}476#endif477478static void479v3d_emit_gl_shader_state(struct v3d_context *v3d,480const struct pipe_draw_info *info)481{482struct v3d_job *job = v3d->job;483/* V3D_DIRTY_VTXSTATE */484struct v3d_vertex_stateobj *vtx = v3d->vtx;485/* V3D_DIRTY_VTXBUF */486struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf;487488/* Upload the uniforms to the indirect CL first */489struct v3d_cl_reloc fs_uniforms =490v3d_write_uniforms(v3d, job, v3d->prog.fs,491PIPE_SHADER_FRAGMENT);492493struct v3d_cl_reloc gs_uniforms = { NULL, 0 };494struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 };495if (v3d->prog.gs) {496gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs,497PIPE_SHADER_GEOMETRY);498}499if (v3d->prog.gs_bin) {500gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin,501PIPE_SHADER_GEOMETRY);502}503504struct v3d_cl_reloc vs_uniforms =505v3d_write_uniforms(v3d, job, v3d->prog.vs,506PIPE_SHADER_VERTEX);507struct v3d_cl_reloc cs_uniforms =508v3d_write_uniforms(v3d, job, v3d->prog.cs,509PIPE_SHADER_VERTEX);510511/* Update the cache dirty flag based on the shader progs data */512job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl;513job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl;514if (v3d->prog.gs_bin) {515job->tmu_dirty_rcl |=516v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl;517}518if (v3d->prog.gs) {519job->tmu_dirty_rcl |=520v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl;521}522job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl;523524uint32_t num_elements_to_emit = 0;525for (int i = 0; i < vtx->num_elements; i++) {526struct pipe_vertex_element *elem = &vtx->pipe[i];527struct pipe_vertex_buffer *vb =528&vertexbuf->vb[elem->vertex_buffer_index];529if (vb->buffer.resource)530num_elements_to_emit++;531}532533uint32_t shader_state_record_length =534cl_packet_length(GL_SHADER_STATE_RECORD);535#if V3D_VERSION >= 41536if (v3d->prog.gs) {537shader_state_record_length +=538cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) +539cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) +5402 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS);541}542#endif543544/* See GFXH-930 workaround below */545uint32_t shader_rec_offset =546v3d_cl_ensure_space(&job->indirect,547shader_state_record_length +548MAX2(num_elements_to_emit, 1) *549cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),55032);551552/* XXX perf: We should move most of the SHADER_STATE_RECORD setup to553* compile time, so that we mostly just have to OR the VS and FS554* records together at draw time.555*/556557struct vpm_config vpm_cfg_bin, vpm_cfg;558559assert(v3d->screen->devinfo.ver >= 41 || !v3d->prog.gs);560v3d_compute_vpm_config(&v3d->screen->devinfo,561v3d->prog.cs->prog_data.vs,562v3d->prog.vs->prog_data.vs,563v3d->prog.gs ? v3d->prog.gs_bin->prog_data.gs : NULL,564v3d->prog.gs ? v3d->prog.gs->prog_data.gs : NULL,565&vpm_cfg_bin,566&vpm_cfg);567568if (v3d->prog.gs) {569#if V3D_VERSION >= 41570v3d_emit_gs_state_record(v3d->job,571v3d->prog.gs_bin, gs_bin_uniforms,572v3d->prog.gs, gs_uniforms);573574struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs;575v3d_emit_tes_gs_common_params(v3d->job,576gs->out_prim_type,577gs->num_invocations);578579/* Bin Tes/Gs params */580v3d_emit_tes_gs_shader_params(v3d->job,581vpm_cfg_bin.gs_width,582vpm_cfg_bin.Gd,583vpm_cfg_bin.Gv);584585/* Render Tes/Gs params */586v3d_emit_tes_gs_shader_params(v3d->job,587vpm_cfg.gs_width,588vpm_cfg.Gd,589vpm_cfg.Gv);590#else591unreachable("No GS support pre-4.1");592#endif593}594595cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {596shader.enable_clipping = true;597/* V3D_DIRTY_PRIM_MODE | V3D_DIRTY_RASTERIZER */598shader.point_size_in_shaded_vertex_data =599(info->mode == PIPE_PRIM_POINTS &&600v3d->rasterizer->base.point_size_per_vertex);601602/* Must be set if the shader modifies Z, discards, or modifies603* the sample mask. For any of these cases, the fragment604* shader needs to write the Z value (even just discards).605*/606shader.fragment_shader_does_z_writes =607v3d->prog.fs->prog_data.fs->writes_z;608/* Set if the EZ test must be disabled (due to shader side609* effects and the early_z flag not being present in the610* shader).611*/612shader.turn_off_early_z_test =613v3d->prog.fs->prog_data.fs->disable_ez;614615shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =616v3d->prog.fs->prog_data.fs->uses_center_w;617618#if V3D_VERSION >= 41619shader.any_shader_reads_hardware_written_primitive_id =620(v3d->prog.gs && v3d->prog.gs->prog_data.gs->uses_pid) ||621v3d->prog.fs->prog_data.fs->uses_pid;622shader.insert_primitive_id_as_first_varying_to_fragment_shader =623!v3d->prog.gs && v3d->prog.fs->prog_data.fs->uses_pid;624#endif625626#if V3D_VERSION >= 40627shader.do_scoreboard_wait_on_first_thread_switch =628v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;629shader.disable_implicit_point_line_varyings =630!v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings;631#endif632633shader.number_of_varyings_in_fragment_shader =634v3d->prog.fs->prog_data.fs->num_inputs;635636shader.coordinate_shader_propagate_nans = true;637shader.vertex_shader_propagate_nans = true;638shader.fragment_shader_propagate_nans = true;639640shader.coordinate_shader_code_address =641cl_address(v3d_resource(v3d->prog.cs->resource)->bo,642v3d->prog.cs->offset);643shader.vertex_shader_code_address =644cl_address(v3d_resource(v3d->prog.vs->resource)->bo,645v3d->prog.vs->offset);646shader.fragment_shader_code_address =647cl_address(v3d_resource(v3d->prog.fs->resource)->bo,648v3d->prog.fs->offset);649650/* XXX: Use combined input/output size flag in the common651* case.652*/653shader.coordinate_shader_has_separate_input_and_output_vpm_blocks =654v3d->prog.cs->prog_data.vs->separate_segments;655shader.vertex_shader_has_separate_input_and_output_vpm_blocks =656v3d->prog.vs->prog_data.vs->separate_segments;657658shader.coordinate_shader_input_vpm_segment_size =659v3d->prog.cs->prog_data.vs->separate_segments ?660v3d->prog.cs->prog_data.vs->vpm_input_size : 1;661shader.vertex_shader_input_vpm_segment_size =662v3d->prog.vs->prog_data.vs->separate_segments ?663v3d->prog.vs->prog_data.vs->vpm_input_size : 1;664665shader.coordinate_shader_output_vpm_segment_size =666v3d->prog.cs->prog_data.vs->vpm_output_size;667shader.vertex_shader_output_vpm_segment_size =668v3d->prog.vs->prog_data.vs->vpm_output_size;669670shader.coordinate_shader_uniforms_address = cs_uniforms;671shader.vertex_shader_uniforms_address = vs_uniforms;672shader.fragment_shader_uniforms_address = fs_uniforms;673674#if V3D_VERSION >= 41675shader.min_coord_shader_input_segments_required_in_play =676vpm_cfg_bin.As;677shader.min_vertex_shader_input_segments_required_in_play =678vpm_cfg.As;679680shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =681vpm_cfg_bin.Ve;682shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =683vpm_cfg.Ve;684685shader.coordinate_shader_4_way_threadable =686v3d->prog.cs->prog_data.vs->base.threads == 4;687shader.vertex_shader_4_way_threadable =688v3d->prog.vs->prog_data.vs->base.threads == 4;689shader.fragment_shader_4_way_threadable =690v3d->prog.fs->prog_data.fs->base.threads == 4;691692shader.coordinate_shader_start_in_final_thread_section =693v3d->prog.cs->prog_data.vs->base.single_seg;694shader.vertex_shader_start_in_final_thread_section =695v3d->prog.vs->prog_data.vs->base.single_seg;696shader.fragment_shader_start_in_final_thread_section =697v3d->prog.fs->prog_data.fs->base.single_seg;698#else699shader.coordinate_shader_4_way_threadable =700v3d->prog.cs->prog_data.vs->base.threads == 4;701shader.coordinate_shader_2_way_threadable =702v3d->prog.cs->prog_data.vs->base.threads == 2;703shader.vertex_shader_4_way_threadable =704v3d->prog.vs->prog_data.vs->base.threads == 4;705shader.vertex_shader_2_way_threadable =706v3d->prog.vs->prog_data.vs->base.threads == 2;707shader.fragment_shader_4_way_threadable =708v3d->prog.fs->prog_data.fs->base.threads == 4;709shader.fragment_shader_2_way_threadable =710v3d->prog.fs->prog_data.fs->base.threads == 2;711#endif712713shader.vertex_id_read_by_coordinate_shader =714v3d->prog.cs->prog_data.vs->uses_vid;715shader.instance_id_read_by_coordinate_shader =716v3d->prog.cs->prog_data.vs->uses_iid;717shader.vertex_id_read_by_vertex_shader =718v3d->prog.vs->prog_data.vs->uses_vid;719shader.instance_id_read_by_vertex_shader =720v3d->prog.vs->prog_data.vs->uses_iid;721722shader.address_of_default_attribute_values =723cl_address(v3d_resource(vtx->defaults)->bo,724vtx->defaults_offset);725}726727bool cs_loaded_any = false;728for (int i = 0; i < vtx->num_elements; i++) {729struct pipe_vertex_element *elem = &vtx->pipe[i];730struct pipe_vertex_buffer *vb =731&vertexbuf->vb[elem->vertex_buffer_index];732struct v3d_resource *rsc = v3d_resource(vb->buffer.resource);733734if (!rsc)735continue;736737const uint32_t size =738cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD);739cl_emit_with_prepacked(&job->indirect,740GL_SHADER_STATE_ATTRIBUTE_RECORD,741&vtx->attrs[i * size], attr) {742attr.stride = vb->stride;743attr.address = cl_address(rsc->bo,744vb->buffer_offset +745elem->src_offset);746attr.number_of_values_read_by_coordinate_shader =747v3d->prog.cs->prog_data.vs->vattr_sizes[i];748attr.number_of_values_read_by_vertex_shader =749v3d->prog.vs->prog_data.vs->vattr_sizes[i];750751/* GFXH-930: At least one attribute must be enabled752* and read by CS and VS. If we have attributes being753* consumed by the VS but not the CS, then set up a754* dummy load of the last attribute into the CS's VPM755* inputs. (Since CS is just dead-code-elimination756* compared to VS, we can't have CS loading but not757* VS).758*/759if (v3d->prog.cs->prog_data.vs->vattr_sizes[i])760cs_loaded_any = true;761if (i == vtx->num_elements - 1 && !cs_loaded_any) {762attr.number_of_values_read_by_coordinate_shader = 1;763}764#if V3D_VERSION >= 41765attr.maximum_index = 0xffffff;766#endif767}768STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size);769}770771if (num_elements_to_emit == 0) {772/* GFXH-930: At least one attribute must be enabled and read773* by CS and VS. If we have no attributes being consumed by774* the shader, set up a dummy to be loaded into the VPM.775*/776cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) {777/* Valid address of data whose value will be unused. */778attr.address = cl_address(job->indirect.bo, 0);779780attr.type = ATTRIBUTE_FLOAT;781attr.stride = 0;782attr.vec_size = 1;783784attr.number_of_values_read_by_coordinate_shader = 1;785attr.number_of_values_read_by_vertex_shader = 1;786}787num_elements_to_emit = 1;788}789790cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {791vcm.number_of_16_vertex_batches_for_binning = vpm_cfg_bin.Vc;792vcm.number_of_16_vertex_batches_for_rendering = vpm_cfg.Vc;793}794795#if V3D_VERSION >= 41796if (v3d->prog.gs) {797cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) {798state.address = cl_address(job->indirect.bo,799shader_rec_offset);800state.number_of_attribute_arrays = num_elements_to_emit;801}802} else {803cl_emit(&job->bcl, GL_SHADER_STATE, state) {804state.address = cl_address(job->indirect.bo,805shader_rec_offset);806state.number_of_attribute_arrays = num_elements_to_emit;807}808}809#else810assert(!v3d->prog.gs);811cl_emit(&job->bcl, GL_SHADER_STATE, state) {812state.address = cl_address(job->indirect.bo, shader_rec_offset);813state.number_of_attribute_arrays = num_elements_to_emit;814}815#endif816817v3d_bo_unreference(&cs_uniforms.bo);818v3d_bo_unreference(&vs_uniforms.bo);819if (gs_uniforms.bo)820v3d_bo_unreference(&gs_uniforms.bo);821if (gs_bin_uniforms.bo)822v3d_bo_unreference(&gs_bin_uniforms.bo);823v3d_bo_unreference(&fs_uniforms.bo);824}825826/**827* Updates the number of primitives generated from the number of vertices828* to draw. This only works when no GS is present, since otherwise the number829* of primitives generated cannot be determined in advance and we need to830* use the PRIMITIVE_COUNTS_FEEDBACK command instead, however, that requires831* a sync wait for the draw to complete, so we only use that when GS is present.832*/833static void834v3d_update_primitives_generated_counter(struct v3d_context *v3d,835const struct pipe_draw_info *info,836const struct pipe_draw_start_count_bias *draw)837{838assert(!v3d->prog.gs);839840if (!v3d->active_queries)841return;842843uint32_t prims = u_prims_for_vertices(info->mode, draw->count);844v3d->prims_generated += prims;845}846847static void848v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)849{850switch (v3d->zsa->ez_state) {851case V3D_EZ_UNDECIDED:852/* If the Z/S state didn't pick a direction but didn't853* disable, then go along with the current EZ state. This854* allows EZ optimization for Z func == EQUAL or NEVER.855*/856break;857858case V3D_EZ_LT_LE:859case V3D_EZ_GT_GE:860/* If the Z/S state picked a direction, then it needs to match861* the current direction if we've decided on one.862*/863if (job->ez_state == V3D_EZ_UNDECIDED)864job->ez_state = v3d->zsa->ez_state;865else if (job->ez_state != v3d->zsa->ez_state)866job->ez_state = V3D_EZ_DISABLED;867break;868869case V3D_EZ_DISABLED:870/* If the current Z/S state disables EZ because of a bad Z871* func or stencil operation, then we can't do any more EZ in872* this frame.873*/874job->ez_state = V3D_EZ_DISABLED;875break;876}877878/* If the FS affects the Z of the pixels, then it may update against879* the chosen EZ direction (though we could use880* ARB_conservative_depth's hints to avoid this)881*/882if (v3d->prog.fs->prog_data.fs->writes_z) {883job->ez_state = V3D_EZ_DISABLED;884}885886if (job->first_ez_state == V3D_EZ_UNDECIDED &&887(job->ez_state != V3D_EZ_DISABLED || job->draw_calls_queued == 0))888job->first_ez_state = job->ez_state;889}890891static uint32_t892v3d_hw_prim_type(enum pipe_prim_type prim_type)893{894switch (prim_type) {895case PIPE_PRIM_POINTS:896case PIPE_PRIM_LINES:897case PIPE_PRIM_LINE_LOOP:898case PIPE_PRIM_LINE_STRIP:899case PIPE_PRIM_TRIANGLES:900case PIPE_PRIM_TRIANGLE_STRIP:901case PIPE_PRIM_TRIANGLE_FAN:902return prim_type;903904case PIPE_PRIM_LINES_ADJACENCY:905case PIPE_PRIM_LINE_STRIP_ADJACENCY:906case PIPE_PRIM_TRIANGLES_ADJACENCY:907case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:908return 8 + (prim_type - PIPE_PRIM_LINES_ADJACENCY);909910default:911unreachable("Unsupported primitive type");912}913}914915static bool916v3d_check_compiled_shaders(struct v3d_context *v3d)917{918static bool warned[5] = { 0 };919920uint32_t failed_stage = MESA_SHADER_NONE;921if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) {922failed_stage = MESA_SHADER_VERTEX;923} else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) ||924(v3d->prog.gs && !v3d->prog.gs->resource)) {925failed_stage = MESA_SHADER_GEOMETRY;926} else if (v3d->prog.fs && !v3d->prog.fs->resource) {927failed_stage = MESA_SHADER_FRAGMENT;928}929930if (likely(failed_stage == MESA_SHADER_NONE))931return true;932933if (!warned[failed_stage]) {934fprintf(stderr,935"%s shader failed to compile. Expect corruption.\n",936_mesa_shader_stage_to_string(failed_stage));937warned[failed_stage] = true;938}939return false;940}941942static void943v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info,944unsigned drawid_offset,945const struct pipe_draw_indirect_info *indirect,946const struct pipe_draw_start_count_bias *draws,947unsigned num_draws)948{949if (num_draws > 1) {950util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws);951return;952}953954if (!indirect && (!draws[0].count || !info->instance_count))955return;956957struct v3d_context *v3d = v3d_context(pctx);958959if (!indirect &&960!info->primitive_restart &&961!u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count))962return;963964/* Fall back for weird desktop GL primitive restart values. */965if (info->primitive_restart &&966info->index_size) {967uint32_t mask = util_prim_restart_index_from_size(info->index_size);968if (info->restart_index != mask) {969util_draw_vbo_without_prim_restart(pctx, info, drawid_offset, indirect, &draws[0]);970return;971}972}973974if (info->mode >= PIPE_PRIM_QUADS && info->mode <= PIPE_PRIM_POLYGON) {975util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);976util_primconvert_draw_vbo(v3d->primconvert, info, drawid_offset, indirect, draws, num_draws);977perf_debug("Fallback conversion for %d %s vertices\n",978draws[0].count, u_prim_name(info->mode));979return;980}981982/* Before setting up the draw, flush anything writing to the resources983* that we read from or reading from resources we write to.984*/985for (int s = 0; s < PIPE_SHADER_COMPUTE; s++)986v3d_predraw_check_stage_inputs(pctx, s);987988if (indirect && indirect->buffer) {989v3d_flush_jobs_writing_resource(v3d, indirect->buffer,990V3D_FLUSH_DEFAULT, false);991}992993v3d_predraw_check_outputs(pctx);994995/* If transform feedback is active and we are switching primitive type996* we need to submit the job before drawing and update the vertex count997* written to TF based on the primitive type since we will need to998* know the exact vertex count if the application decides to call999* glDrawTransformFeedback() later.1000*/1001if (v3d->streamout.num_targets > 0 &&1002u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {1003v3d_update_primitive_counters(v3d);1004}10051006struct v3d_job *job = v3d_get_job_for_fbo(v3d);10071008/* If vertex texturing depends on the output of rendering, we need to1009* ensure that that rendering is complete before we run a coordinate1010* shader that depends on it.1011*1012* Given that doing that is unusual, for now we just block the binner1013* on the last submitted render, rather than tracking the last1014* rendering to each texture's BO.1015*/1016if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || (indirect && indirect->buffer)) {1017perf_debug("Blocking binner on last render "1018"due to vertex texturing or indirect drawing.\n");1019job->submit.in_sync_bcl = v3d->out_sync;1020}10211022/* We also need to ensure that compute is complete when render depends1023* on resources written by it.1024*/1025if (v3d->sync_on_last_compute_job) {1026job->submit.in_sync_bcl = v3d->out_sync;1027v3d->sync_on_last_compute_job = false;1028}10291030/* Mark SSBOs and images as being written. We don't actually know1031* which ones are read vs written, so just assume the worst.1032*/1033for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {1034u_foreach_bit(i, v3d->ssbo[s].enabled_mask) {1035v3d_job_add_write_resource(job,1036v3d->ssbo[s].sb[i].buffer);1037job->tmu_dirty_rcl = true;1038}10391040u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) {1041v3d_job_add_write_resource(job,1042v3d->shaderimg[s].si[i].base.resource);1043job->tmu_dirty_rcl = true;1044}1045}10461047/* Get space to emit our draw call into the BCL, using a branch to1048* jump to a new BO if necessary.1049*/1050v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */);10511052if (v3d->prim_mode != info->mode) {1053v3d->prim_mode = info->mode;1054v3d->dirty |= V3D_DIRTY_PRIM_MODE;1055}10561057v3d_start_draw(v3d);1058v3d_update_compiled_shaders(v3d, info->mode);1059if (!v3d_check_compiled_shaders(v3d))1060return;1061v3d_update_job_ez(v3d, job);10621063/* If this job was writing to transform feedback buffers before this1064* draw and we are reading from them here, then we need to wait for TF1065* to complete before we emit this draw.1066*1067* Notice this check needs to happen before we emit state for the1068* current draw call, where we update job->tf_enabled, so we can ensure1069* that we only check TF writes for prior draws.1070*/1071v3d_emit_wait_for_tf_if_needed(v3d, job);10721073#if V3D_VERSION >= 411074v3d41_emit_state(pctx);1075#else1076v3d33_emit_state(pctx);1077#endif10781079if (v3d->dirty & (V3D_DIRTY_VTXBUF |1080V3D_DIRTY_VTXSTATE |1081V3D_DIRTY_PRIM_MODE |1082V3D_DIRTY_RASTERIZER |1083V3D_DIRTY_COMPILED_CS |1084V3D_DIRTY_COMPILED_VS |1085V3D_DIRTY_COMPILED_GS_BIN |1086V3D_DIRTY_COMPILED_GS |1087V3D_DIRTY_COMPILED_FS |1088v3d->prog.cs->uniform_dirty_bits |1089v3d->prog.vs->uniform_dirty_bits |1090(v3d->prog.gs_bin ?1091v3d->prog.gs_bin->uniform_dirty_bits : 0) |1092(v3d->prog.gs ?1093v3d->prog.gs->uniform_dirty_bits : 0) |1094v3d->prog.fs->uniform_dirty_bits)) {1095v3d_emit_gl_shader_state(v3d, info);1096}10971098v3d->dirty = 0;10991100/* The Base Vertex/Base Instance packet sets those values to nonzero1101* for the next draw call only.1102*/1103if ((info->index_size && draws->index_bias) || info->start_instance) {1104cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) {1105base.base_instance = info->start_instance;1106base.base_vertex = info->index_size ? draws->index_bias : 0;1107}1108}11091110uint32_t prim_tf_enable = 0;1111#if V3D_VERSION < 401112/* V3D 3.x: The HW only processes transform feedback on primitives1113* with the flag set.1114*/1115if (v3d->streamout.num_targets)1116prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);1117#endif11181119if (!v3d->prog.gs)1120v3d_update_primitives_generated_counter(v3d, info, &draws[0]);11211122uint32_t hw_prim_type = v3d_hw_prim_type(info->mode);1123if (info->index_size) {1124uint32_t index_size = info->index_size;1125uint32_t offset = draws[0].start * index_size;1126struct pipe_resource *prsc;1127if (info->has_user_indices) {1128unsigned start_offset = draws[0].start * info->index_size;1129prsc = NULL;1130u_upload_data(v3d->uploader, start_offset,1131draws[0].count * info->index_size, 4,1132(char*)info->index.user + start_offset,1133&offset, &prsc);1134} else {1135prsc = info->index.resource;1136}1137struct v3d_resource *rsc = v3d_resource(prsc);11381139#if V3D_VERSION >= 401140cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) {1141ib.address = cl_address(rsc->bo, 0);1142ib.size = rsc->bo->size;1143}1144#endif11451146if (indirect && indirect->buffer) {1147cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) {1148prim.index_type = ffs(info->index_size) - 1;1149#if V3D_VERSION < 401150prim.address_of_indices_list =1151cl_address(rsc->bo, offset);1152#endif /* V3D_VERSION < 40 */1153prim.mode = hw_prim_type | prim_tf_enable;1154prim.enable_primitive_restarts = info->primitive_restart;11551156prim.number_of_draw_indirect_indexed_records = indirect->draw_count;11571158prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;1159prim.address = cl_address(v3d_resource(indirect->buffer)->bo,1160indirect->offset);1161}1162} else if (info->instance_count > 1) {1163cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) {1164prim.index_type = ffs(info->index_size) - 1;1165#if V3D_VERSION >= 401166prim.index_offset = offset;1167#else /* V3D_VERSION < 40 */1168prim.maximum_index = (1u << 31) - 1; /* XXX */1169prim.address_of_indices_list =1170cl_address(rsc->bo, offset);1171#endif /* V3D_VERSION < 40 */1172prim.mode = hw_prim_type | prim_tf_enable;1173prim.enable_primitive_restarts = info->primitive_restart;11741175prim.number_of_instances = info->instance_count;1176prim.instance_length = draws[0].count;1177}1178} else {1179cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) {1180prim.index_type = ffs(info->index_size) - 1;1181prim.length = draws[0].count;1182#if V3D_VERSION >= 401183prim.index_offset = offset;1184#else /* V3D_VERSION < 40 */1185prim.maximum_index = (1u << 31) - 1; /* XXX */1186prim.address_of_indices_list =1187cl_address(rsc->bo, offset);1188#endif /* V3D_VERSION < 40 */1189prim.mode = hw_prim_type | prim_tf_enable;1190prim.enable_primitive_restarts = info->primitive_restart;1191}1192}11931194if (info->has_user_indices)1195pipe_resource_reference(&prsc, NULL);1196} else {1197if (indirect && indirect->buffer) {1198cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {1199prim.mode = hw_prim_type | prim_tf_enable;1200prim.number_of_draw_indirect_array_records = indirect->draw_count;12011202prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2;1203prim.address = cl_address(v3d_resource(indirect->buffer)->bo,1204indirect->offset);1205}1206} else if (info->instance_count > 1) {1207struct pipe_stream_output_target *so =1208indirect && indirect->count_from_stream_output ?1209indirect->count_from_stream_output : NULL;1210uint32_t vert_count = so ?1211v3d_stream_output_target_get_vertex_count(so) :1212draws[0].count;1213cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {1214prim.mode = hw_prim_type | prim_tf_enable;1215prim.index_of_first_vertex = draws[0].start;1216prim.number_of_instances = info->instance_count;1217prim.instance_length = vert_count;1218}1219} else {1220struct pipe_stream_output_target *so =1221indirect && indirect->count_from_stream_output ?1222indirect->count_from_stream_output : NULL;1223uint32_t vert_count = so ?1224v3d_stream_output_target_get_vertex_count(so) :1225draws[0].count;1226cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {1227prim.mode = hw_prim_type | prim_tf_enable;1228prim.length = vert_count;1229prim.index_of_first_vertex = draws[0].start;1230}1231}1232}12331234/* A flush is required in between a TF draw and any following TF specs1235* packet, or the GPU may hang. Just flush each time for now.1236*/1237if (v3d->streamout.num_targets)1238cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush);12391240job->draw_calls_queued++;1241if (v3d->streamout.num_targets)1242job->tf_draw_calls_queued++;12431244/* Increment the TF offsets by how many verts we wrote. XXX: This1245* needs some clamping to the buffer size.1246*/1247for (int i = 0; i < v3d->streamout.num_targets; i++)1248v3d->streamout.offsets[i] += draws[0].count;12491250if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth_enabled) {1251struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);1252v3d_job_add_bo(job, rsc->bo);12531254job->load |= PIPE_CLEAR_DEPTH & ~job->clear;1255if (v3d->zsa->base.depth_writemask)1256job->store |= PIPE_CLEAR_DEPTH;1257rsc->initialized_buffers = PIPE_CLEAR_DEPTH;1258}12591260if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) {1261struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture);1262if (rsc->separate_stencil)1263rsc = rsc->separate_stencil;12641265v3d_job_add_bo(job, rsc->bo);12661267job->load |= PIPE_CLEAR_STENCIL & ~job->clear;1268if (v3d->zsa->base.stencil[0].writemask ||1269v3d->zsa->base.stencil[1].writemask) {1270job->store |= PIPE_CLEAR_STENCIL;1271}1272rsc->initialized_buffers |= PIPE_CLEAR_STENCIL;1273}12741275for (int i = 0; i < job->nr_cbufs; i++) {1276uint32_t bit = PIPE_CLEAR_COLOR0 << i;1277int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0;12781279if (job->store & bit || !job->cbufs[i])1280continue;1281struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture);12821283job->load |= bit & ~job->clear;1284if (v3d->blend->base.rt[blend_rt].colormask)1285job->store |= bit;1286v3d_job_add_bo(job, rsc->bo);1287}12881289if (job->referenced_size > 768 * 1024 * 1024) {1290perf_debug("Flushing job with %dkb to try to free up memory\n",1291job->referenced_size / 1024);1292v3d_flush(pctx);1293}12941295if (V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)1296v3d_flush(pctx);1297}12981299#if V3D_VERSION >= 411300#define V3D_CSD_CFG012_WG_COUNT_SHIFT 161301#define V3D_CSD_CFG012_WG_OFFSET_SHIFT 01302/* Allow this dispatch to start while the last one is still running. */1303#define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)1304/* Maximum supergroup ID. 6 bits. */1305#define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 201306/* Batches per supergroup minus 1. 8 bits. */1307#define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 121308/* Workgroups per supergroup, 0 means 16 */1309#define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 81310#define V3D_CSD_CFG3_WG_SIZE_SHIFT 013111312#define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)1313#define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)1314#define V3D_CSD_CFG5_THREADING (1 << 0)13151316static void1317v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)1318{1319struct v3d_context *v3d = v3d_context(pctx);1320struct v3d_screen *screen = v3d->screen;13211322v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);13231324v3d_update_compiled_cs(v3d);13251326if (!v3d->prog.compute->resource) {1327static bool warned = false;1328if (!warned) {1329fprintf(stderr,1330"Compute shader failed to compile. "1331"Expect corruption.\n");1332warned = true;1333}1334return;1335}13361337/* Some of the units of scale:1338*1339* - Batches of 16 work items (shader invocations) that will be queued1340* to the run on a QPU at once.1341*1342* - Workgroups composed of work items based on the shader's layout1343* declaration.1344*1345* - Supergroups of 1-16 workgroups. There can only be 16 supergroups1346* running at a time on the core, so we want to keep them large to1347* keep the QPUs busy, but a whole supergroup will sync at a barrier1348* so we want to keep them small if one is present.1349*/1350struct drm_v3d_submit_csd submit = { 0 };1351struct v3d_job *job = v3d_job_create(v3d);13521353/* Set up the actual number of workgroups, synchronously mapping the1354* indirect buffer if necessary to get the dimensions.1355*/1356if (info->indirect) {1357struct pipe_transfer *transfer;1358uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,1359info->indirect_offset,13603 * sizeof(uint32_t),1361PIPE_MAP_READ,1362&transfer);1363memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));1364pipe_buffer_unmap(pctx, transfer);13651366if (v3d->compute_num_workgroups[0] == 0 ||1367v3d->compute_num_workgroups[1] == 0 ||1368v3d->compute_num_workgroups[2] == 0) {1369/* Nothing to dispatch, so skip the draw (CSD can't1370* handle 0 workgroups).1371*/1372return;1373}1374} else {1375v3d->compute_num_workgroups[0] = info->grid[0];1376v3d->compute_num_workgroups[1] = info->grid[1];1377v3d->compute_num_workgroups[2] = info->grid[2];1378}13791380uint32_t num_wgs = 1;1381for (int i = 0; i < 3; i++) {1382num_wgs *= v3d->compute_num_workgroups[i];1383submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<1384V3D_CSD_CFG012_WG_COUNT_SHIFT);1385}13861387uint32_t wg_size = info->block[0] * info->block[1] * info->block[2];13881389struct v3d_compute_prog_data *compute =1390v3d->prog.compute->prog_data.compute;1391uint32_t wgs_per_sg =1392v3d_csd_choose_workgroups_per_supergroup(1393&v3d->screen->devinfo,1394compute->has_subgroups,1395compute->base.has_control_barrier,1396compute->base.threads,1397num_wgs, wg_size);13981399uint32_t batches_per_sg = DIV_ROUND_UP(wgs_per_sg * wg_size, 16);1400uint32_t whole_sgs = num_wgs / wgs_per_sg;1401uint32_t rem_wgs = num_wgs - whole_sgs * wgs_per_sg;1402uint32_t num_batches = batches_per_sg * whole_sgs +1403DIV_ROUND_UP(rem_wgs * wg_size, 16);14041405submit.cfg[3] |= (wgs_per_sg & 0xf) << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;1406submit.cfg[3] |=1407(batches_per_sg - 1) << V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT;1408submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;140914101411/* Number of batches the dispatch will invoke (minus 1). */1412submit.cfg[4] = num_batches - 1;14131414/* Make sure we didn't accidentally underflow. */1415assert(submit.cfg[4] != ~0);14161417v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);1418submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +1419v3d->prog.compute->offset);1420submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;1421if (v3d->prog.compute->prog_data.base->single_seg)1422submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;1423if (v3d->prog.compute->prog_data.base->threads == 4)1424submit.cfg[5] |= V3D_CSD_CFG5_THREADING;14251426if (v3d->prog.compute->prog_data.compute->shared_size) {1427v3d->compute_shared_memory =1428v3d_bo_alloc(v3d->screen,1429v3d->prog.compute->prog_data.compute->shared_size *1430wgs_per_sg,1431"shared_vars");1432}14331434struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,1435v3d->prog.compute,1436PIPE_SHADER_COMPUTE);1437v3d_job_add_bo(job, uniforms.bo);1438submit.cfg[6] = uniforms.bo->offset + uniforms.offset;14391440/* Pull some job state that was stored in a SUBMIT_CL struct out to1441* our SUBMIT_CSD struct1442*/1443submit.bo_handles = job->submit.bo_handles;1444submit.bo_handle_count = job->submit.bo_handle_count;14451446/* Serialize this in the rest of our command stream. */1447submit.in_sync = v3d->out_sync;1448submit.out_sync = v3d->out_sync;14491450if (!(V3D_DEBUG & V3D_DEBUG_NORAST)) {1451int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,1452&submit);1453static bool warned = false;1454if (ret && !warned) {1455fprintf(stderr, "CSD submit call returned %s. "1456"Expect corruption.\n", strerror(errno));1457warned = true;1458}1459}14601461v3d_job_free(v3d, job);14621463/* Mark SSBOs as being written.. we don't actually know which ones are1464* read vs written, so just assume the worst1465*/1466u_foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) {1467struct v3d_resource *rsc = v3d_resource(1468v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);1469rsc->writes++;1470rsc->compute_written = true;1471}14721473u_foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {1474struct v3d_resource *rsc = v3d_resource(1475v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);1476rsc->writes++;1477rsc->compute_written = true;1478}14791480v3d_bo_unreference(&uniforms.bo);1481v3d_bo_unreference(&v3d->compute_shared_memory);1482}1483#endif14841485/**1486* Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.1487*/1488static void1489v3d_draw_clear(struct v3d_context *v3d,1490unsigned buffers,1491const union pipe_color_union *color,1492double depth, unsigned stencil)1493{1494static const union pipe_color_union dummy_color = {};14951496/* The blitter util dereferences the color regardless, even though the1497* gallium clear API may not pass one in when only Z/S are cleared.1498*/1499if (!color)1500color = &dummy_color;15011502v3d_blitter_save(v3d);1503util_blitter_clear(v3d->blitter,1504v3d->framebuffer.width,1505v3d->framebuffer.height,1506util_framebuffer_get_num_layers(&v3d->framebuffer),1507buffers, color, depth, stencil,1508util_framebuffer_get_num_samples(&v3d->framebuffer) > 1);1509}15101511/**1512* Attempts to perform the GL clear by using the TLB's fast clear at the start1513* of the frame.1514*/1515static unsigned1516v3d_tlb_clear(struct v3d_job *job, unsigned buffers,1517const union pipe_color_union *color,1518double depth, unsigned stencil)1519{1520struct v3d_context *v3d = job->v3d;15211522if (job->draw_calls_queued) {1523/* If anything in the CL has drawn using the buffer, then the1524* TLB clear we're trying to add now would happen before that1525* drawing.1526*/1527buffers &= ~(job->load | job->store);1528}15291530/* GFXH-1461: If we were to emit a load of just depth or just stencil,1531* then the clear for the other may get lost. We need to decide now1532* if it would be possible to need to emit a load of just one after1533* we've set up our TLB clears.1534*/1535if (buffers & PIPE_CLEAR_DEPTHSTENCIL &&1536(buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL &&1537job->zsbuf &&1538util_format_is_depth_and_stencil(job->zsbuf->texture->format)) {1539buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;1540}15411542for (int i = 0; i < job->nr_cbufs; i++) {1543uint32_t bit = PIPE_CLEAR_COLOR0 << i;1544if (!(buffers & bit))1545continue;15461547struct pipe_surface *psurf = v3d->framebuffer.cbufs[i];1548struct v3d_surface *surf = v3d_surface(psurf);1549struct v3d_resource *rsc = v3d_resource(psurf->texture);15501551union util_color uc;1552uint32_t internal_size = 4 << surf->internal_bpp;15531554static union pipe_color_union swapped_color;1555if (v3d->swap_color_rb & (1 << i)) {1556swapped_color.f[0] = color->f[2];1557swapped_color.f[1] = color->f[1];1558swapped_color.f[2] = color->f[0];1559swapped_color.f[3] = color->f[3];1560color = &swapped_color;1561}15621563switch (surf->internal_type) {1564case V3D_INTERNAL_TYPE_8:1565util_pack_color(color->f, PIPE_FORMAT_R8G8B8A8_UNORM,1566&uc);1567memcpy(job->clear_color[i], uc.ui, internal_size);1568break;1569case V3D_INTERNAL_TYPE_8I:1570case V3D_INTERNAL_TYPE_8UI:1571job->clear_color[i][0] = ((color->ui[0] & 0xff) |1572(color->ui[1] & 0xff) << 8 |1573(color->ui[2] & 0xff) << 16 |1574(color->ui[3] & 0xff) << 24);1575break;1576case V3D_INTERNAL_TYPE_16F:1577util_pack_color(color->f, PIPE_FORMAT_R16G16B16A16_FLOAT,1578&uc);1579memcpy(job->clear_color[i], uc.ui, internal_size);1580break;1581case V3D_INTERNAL_TYPE_16I:1582case V3D_INTERNAL_TYPE_16UI:1583job->clear_color[i][0] = ((color->ui[0] & 0xffff) |1584color->ui[1] << 16);1585job->clear_color[i][1] = ((color->ui[2] & 0xffff) |1586color->ui[3] << 16);1587break;1588case V3D_INTERNAL_TYPE_32F:1589case V3D_INTERNAL_TYPE_32I:1590case V3D_INTERNAL_TYPE_32UI:1591memcpy(job->clear_color[i], color->ui, internal_size);1592break;1593}15941595rsc->initialized_buffers |= bit;1596}15971598unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL;1599if (zsclear) {1600struct v3d_resource *rsc =1601v3d_resource(v3d->framebuffer.zsbuf->texture);16021603if (zsclear & PIPE_CLEAR_DEPTH)1604job->clear_z = depth;1605if (zsclear & PIPE_CLEAR_STENCIL)1606job->clear_s = stencil;16071608rsc->initialized_buffers |= zsclear;1609}16101611job->draw_min_x = 0;1612job->draw_min_y = 0;1613job->draw_max_x = v3d->framebuffer.width;1614job->draw_max_y = v3d->framebuffer.height;1615job->clear |= buffers;1616job->store |= buffers;1617job->scissor.disabled = true;16181619v3d_start_draw(v3d);16201621return buffers;1622}16231624static void1625v3d_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,1626const union pipe_color_union *color, double depth, unsigned stencil)1627{1628struct v3d_context *v3d = v3d_context(pctx);1629struct v3d_job *job = v3d_get_job_for_fbo(v3d);16301631buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil);16321633if (buffers)1634v3d_draw_clear(v3d, buffers, color, depth, stencil);1635}16361637static void1638v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,1639const union pipe_color_union *color,1640unsigned x, unsigned y, unsigned w, unsigned h,1641bool render_condition_enabled)1642{1643fprintf(stderr, "unimpl: clear RT\n");1644}16451646static void1647v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,1648unsigned buffers, double depth, unsigned stencil,1649unsigned x, unsigned y, unsigned w, unsigned h,1650bool render_condition_enabled)1651{1652fprintf(stderr, "unimpl: clear DS\n");1653}16541655void1656v3dX(start_binning)(struct v3d_context *v3d, struct v3d_job *job)1657{1658v3d_start_binning(v3d, job);1659}16601661void1662v3dX(draw_init)(struct pipe_context *pctx)1663{1664pctx->draw_vbo = v3d_draw_vbo;1665pctx->clear = v3d_clear;1666pctx->clear_render_target = v3d_clear_render_target;1667pctx->clear_depth_stencil = v3d_clear_depth_stencil;1668#if V3D_VERSION >= 411669if (v3d_context(pctx)->screen->has_csd)1670pctx->launch_grid = v3d_launch_grid;1671#endif1672}167316741675