Path: blob/21.2-virgl/src/intel/vulkan/gfx7_cmd_buffer.c
4547 views
/*1* Copyright © 2015 Intel Corporation2*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 <assert.h>24#include <stdbool.h>25#include <string.h>26#include <unistd.h>27#include <fcntl.h>2829#include "anv_private.h"30#include "vk_format.h"3132#include "genxml/gen_macros.h"33#include "genxml/genX_pack.h"3435#if GFX_VERx10 == 7036static int64_t37clamp_int64(int64_t x, int64_t min, int64_t max)38{39if (x < min)40return min;41else if (x < max)42return x;43else44return max;45}4647void48gfx7_cmd_buffer_emit_scissor(struct anv_cmd_buffer *cmd_buffer)49{50struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;51uint32_t count = cmd_buffer->state.gfx.dynamic.scissor.count;52const VkRect2D *scissors = cmd_buffer->state.gfx.dynamic.scissor.scissors;5354/* Wa_1409725701:55* "The viewport-specific state used by the SF unit (SCISSOR_RECT) is56* stored as an array of up to 16 elements. The location of first57* element of the array, as specified by Pointer to SCISSOR_RECT, should58* be aligned to a 64-byte boundary.59*/60uint32_t alignment = 64;61struct anv_state scissor_state =62anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, count * 8, alignment);6364for (uint32_t i = 0; i < count; i++) {65const VkRect2D *s = &scissors[i];6667/* Since xmax and ymax are inclusive, we have to have xmax < xmin or68* ymax < ymin for empty clips. In case clip x, y, width height are all69* 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't70* what we want. Just special case empty clips and produce a canonical71* empty clip. */72static const struct GFX7_SCISSOR_RECT empty_scissor = {73.ScissorRectangleYMin = 1,74.ScissorRectangleXMin = 1,75.ScissorRectangleYMax = 0,76.ScissorRectangleXMax = 077};7879const int max = 0xffff;8081uint32_t y_min = s->offset.y;82uint32_t x_min = s->offset.x;83uint32_t y_max = s->offset.y + s->extent.height - 1;84uint32_t x_max = s->offset.x + s->extent.width - 1;8586/* Do this math using int64_t so overflow gets clamped correctly. */87if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {88y_min = clamp_int64((uint64_t) y_min,89cmd_buffer->state.render_area.offset.y, max);90x_min = clamp_int64((uint64_t) x_min,91cmd_buffer->state.render_area.offset.x, max);92y_max = clamp_int64((uint64_t) y_max, 0,93cmd_buffer->state.render_area.offset.y +94cmd_buffer->state.render_area.extent.height - 1);95x_max = clamp_int64((uint64_t) x_max, 0,96cmd_buffer->state.render_area.offset.x +97cmd_buffer->state.render_area.extent.width - 1);98} else if (fb) {99y_min = clamp_int64((uint64_t) y_min, 0, max);100x_min = clamp_int64((uint64_t) x_min, 0, max);101y_max = clamp_int64((uint64_t) y_max, 0, fb->height - 1);102x_max = clamp_int64((uint64_t) x_max, 0, fb->width - 1);103}104105struct GFX7_SCISSOR_RECT scissor = {106.ScissorRectangleYMin = y_min,107.ScissorRectangleXMin = x_min,108.ScissorRectangleYMax = y_max,109.ScissorRectangleXMax = x_max110};111112if (s->extent.width <= 0 || s->extent.height <= 0) {113GFX7_SCISSOR_RECT_pack(NULL, scissor_state.map + i * 8,114&empty_scissor);115} else {116GFX7_SCISSOR_RECT_pack(NULL, scissor_state.map + i * 8, &scissor);117}118}119120anv_batch_emit(&cmd_buffer->batch,121GFX7_3DSTATE_SCISSOR_STATE_POINTERS, ssp) {122ssp.ScissorRectPointer = scissor_state.offset;123}124}125#endif126127static uint32_t vk_to_intel_index_type(VkIndexType type)128{129switch (type) {130case VK_INDEX_TYPE_UINT8_EXT:131return INDEX_BYTE;132case VK_INDEX_TYPE_UINT16:133return INDEX_WORD;134case VK_INDEX_TYPE_UINT32:135return INDEX_DWORD;136default:137unreachable("invalid index type");138}139}140141static uint32_t restart_index_for_type(VkIndexType type)142{143switch (type) {144case VK_INDEX_TYPE_UINT8_EXT:145return UINT8_MAX;146case VK_INDEX_TYPE_UINT16:147return UINT16_MAX;148case VK_INDEX_TYPE_UINT32:149return UINT32_MAX;150default:151unreachable("invalid index type");152}153}154155void genX(CmdBindIndexBuffer)(156VkCommandBuffer commandBuffer,157VkBuffer _buffer,158VkDeviceSize offset,159VkIndexType indexType)160{161ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);162ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);163164cmd_buffer->state.gfx.dirty |= ANV_CMD_DIRTY_INDEX_BUFFER;165if (GFX_VERx10 == 75)166cmd_buffer->state.restart_index = restart_index_for_type(indexType);167cmd_buffer->state.gfx.gfx7.index_buffer = buffer;168cmd_buffer->state.gfx.gfx7.index_type = vk_to_intel_index_type(indexType);169cmd_buffer->state.gfx.gfx7.index_offset = offset;170}171172static uint32_t173get_depth_format(struct anv_cmd_buffer *cmd_buffer)174{175const struct anv_render_pass *pass = cmd_buffer->state.pass;176const struct anv_subpass *subpass = cmd_buffer->state.subpass;177178if (!subpass->depth_stencil_attachment)179return D16_UNORM;180181struct anv_render_pass_attachment *att =182&pass->attachments[subpass->depth_stencil_attachment->attachment];183184switch (att->format) {185case VK_FORMAT_D16_UNORM:186case VK_FORMAT_D16_UNORM_S8_UINT:187return D16_UNORM;188189case VK_FORMAT_X8_D24_UNORM_PACK32:190case VK_FORMAT_D24_UNORM_S8_UINT:191return D24_UNORM_X8_UINT;192193case VK_FORMAT_D32_SFLOAT:194case VK_FORMAT_D32_SFLOAT_S8_UINT:195return D32_FLOAT;196197default:198return D16_UNORM;199}200}201202void203genX(cmd_buffer_flush_dynamic_state)(struct anv_cmd_buffer *cmd_buffer)204{205struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;206struct anv_dynamic_state *d = &cmd_buffer->state.gfx.dynamic;207208if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY) {209uint32_t topology;210if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))211topology = pipeline->topology;212else213topology = genX(vk_to_intel_primitive_type)[d->primitive_topology];214215cmd_buffer->state.gfx.primitive_topology = topology;216}217218if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_PIPELINE |219ANV_CMD_DIRTY_RENDER_TARGETS |220ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH |221ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS |222ANV_CMD_DIRTY_DYNAMIC_CULL_MODE |223ANV_CMD_DIRTY_DYNAMIC_FRONT_FACE |224ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS_ENABLE |225ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY)) {226/* Take dynamic primitive topology in to account with227* 3DSTATE_SF::MultisampleRasterizationMode228*/229uint32_t ms_rast_mode = 0;230231if (cmd_buffer->state.gfx.pipeline->dynamic_states &232ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY) {233VkPrimitiveTopology primitive_topology =234cmd_buffer->state.gfx.dynamic.primitive_topology;235236VkPolygonMode dynamic_raster_mode =237genX(raster_polygon_mode)(cmd_buffer->state.gfx.pipeline,238primitive_topology);239240ms_rast_mode =241genX(ms_rasterization_mode)(pipeline, dynamic_raster_mode);242}243244uint32_t sf_dw[GENX(3DSTATE_SF_length)];245struct GENX(3DSTATE_SF) sf = {246GENX(3DSTATE_SF_header),247.DepthBufferSurfaceFormat = get_depth_format(cmd_buffer),248.LineWidth = d->line_width,249.GlobalDepthOffsetConstant = d->depth_bias.bias,250.GlobalDepthOffsetScale = d->depth_bias.slope,251.GlobalDepthOffsetClamp = d->depth_bias.clamp,252.FrontWinding = genX(vk_to_intel_front_face)[d->front_face],253.CullMode = genX(vk_to_intel_cullmode)[d->cull_mode],254.GlobalDepthOffsetEnableSolid = d->depth_bias_enable,255.GlobalDepthOffsetEnableWireframe = d->depth_bias_enable,256.GlobalDepthOffsetEnablePoint = d->depth_bias_enable,257.MultisampleRasterizationMode = ms_rast_mode,258};259GENX(3DSTATE_SF_pack)(NULL, sf_dw, &sf);260261anv_batch_emit_merge(&cmd_buffer->batch, sf_dw, pipeline->gfx7.sf);262}263264if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS |265ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE)) {266struct anv_state cc_state =267anv_cmd_buffer_alloc_dynamic_state(cmd_buffer,268GENX(COLOR_CALC_STATE_length) * 4,26964);270struct GENX(COLOR_CALC_STATE) cc = {271.BlendConstantColorRed = d->blend_constants[0],272.BlendConstantColorGreen = d->blend_constants[1],273.BlendConstantColorBlue = d->blend_constants[2],274.BlendConstantColorAlpha = d->blend_constants[3],275.StencilReferenceValue = d->stencil_reference.front & 0xff,276.BackfaceStencilReferenceValue = d->stencil_reference.back & 0xff,277};278GENX(COLOR_CALC_STATE_pack)(NULL, cc_state.map, &cc);279280anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_CC_STATE_POINTERS), ccp) {281ccp.ColorCalcStatePointer = cc_state.offset;282}283}284285if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE) {286anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_LINE_STIPPLE), ls) {287ls.LineStipplePattern = d->line_stipple.pattern;288ls.LineStippleInverseRepeatCount =2891.0f / MAX2(1, d->line_stipple.factor);290ls.LineStippleRepeatCount = d->line_stipple.factor;291}292}293294if (cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_PIPELINE |295ANV_CMD_DIRTY_RENDER_TARGETS |296ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK |297ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK |298ANV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE |299ANV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE |300ANV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP |301ANV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE |302ANV_CMD_DIRTY_DYNAMIC_STENCIL_OP)) {303uint32_t depth_stencil_dw[GENX(DEPTH_STENCIL_STATE_length)];304305struct GENX(DEPTH_STENCIL_STATE) depth_stencil = {306.StencilTestMask = d->stencil_compare_mask.front & 0xff,307.StencilWriteMask = d->stencil_write_mask.front & 0xff,308309.BackfaceStencilTestMask = d->stencil_compare_mask.back & 0xff,310.BackfaceStencilWriteMask = d->stencil_write_mask.back & 0xff,311312.StencilBufferWriteEnable =313(d->stencil_write_mask.front || d->stencil_write_mask.back) &&314d->stencil_test_enable,315316.DepthTestEnable = d->depth_test_enable,317.DepthBufferWriteEnable = d->depth_test_enable && d->depth_write_enable,318.DepthTestFunction = genX(vk_to_intel_compare_op)[d->depth_compare_op],319.StencilTestEnable = d->stencil_test_enable,320.StencilFailOp = genX(vk_to_intel_stencil_op)[d->stencil_op.front.fail_op],321.StencilPassDepthPassOp = genX(vk_to_intel_stencil_op)[d->stencil_op.front.pass_op],322.StencilPassDepthFailOp = genX(vk_to_intel_stencil_op)[d->stencil_op.front.depth_fail_op],323.StencilTestFunction = genX(vk_to_intel_compare_op)[d->stencil_op.front.compare_op],324.BackfaceStencilFailOp = genX(vk_to_intel_stencil_op)[d->stencil_op.back.fail_op],325.BackfaceStencilPassDepthPassOp = genX(vk_to_intel_stencil_op)[d->stencil_op.back.pass_op],326.BackfaceStencilPassDepthFailOp = genX(vk_to_intel_stencil_op)[d->stencil_op.back.depth_fail_op],327.BackfaceStencilTestFunction = genX(vk_to_intel_compare_op)[d->stencil_op.back.compare_op],328};329GENX(DEPTH_STENCIL_STATE_pack)(NULL, depth_stencil_dw, &depth_stencil);330331struct anv_state ds_state =332anv_cmd_buffer_merge_dynamic(cmd_buffer, depth_stencil_dw,333pipeline->gfx7.depth_stencil_state,334GENX(DEPTH_STENCIL_STATE_length), 64);335336anv_batch_emit(&cmd_buffer->batch,337GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS), dsp) {338dsp.PointertoDEPTH_STENCIL_STATE = ds_state.offset;339}340}341342if (cmd_buffer->state.gfx.gfx7.index_buffer &&343cmd_buffer->state.gfx.dirty & (ANV_CMD_DIRTY_PIPELINE |344ANV_CMD_DIRTY_INDEX_BUFFER |345ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_RESTART_ENABLE)) {346struct anv_buffer *buffer = cmd_buffer->state.gfx.gfx7.index_buffer;347uint32_t offset = cmd_buffer->state.gfx.gfx7.index_offset;348349#if GFX_VERx10 == 75350anv_batch_emit(&cmd_buffer->batch, GFX75_3DSTATE_VF, vf) {351vf.IndexedDrawCutIndexEnable = d->primitive_restart_enable;352vf.CutIndex = cmd_buffer->state.restart_index;353}354#endif355356anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_INDEX_BUFFER), ib) {357#if GFX_VERx10 != 75358ib.CutIndexEnable = d->primitive_restart_enable;359#endif360ib.IndexFormat = cmd_buffer->state.gfx.gfx7.index_type;361ib.MOCS = anv_mocs(cmd_buffer->device,362buffer->address.bo,363ISL_SURF_USAGE_INDEX_BUFFER_BIT);364365ib.BufferStartingAddress = anv_address_add(buffer->address, offset);366ib.BufferEndingAddress = anv_address_add(buffer->address,367buffer->size);368}369}370371/* 3DSTATE_WM in the hope we can avoid spawning fragment shaders372* threads or if we have dirty dynamic primitive topology state and373* need to toggle 3DSTATE_WM::MultisampleRasterizationMode dynamically.374*/375if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_COLOR_BLEND_STATE ||376cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY) {377const uint8_t color_writes = cmd_buffer->state.gfx.dynamic.color_writes;378379bool dirty_color_blend =380cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_COLOR_BLEND_STATE;381382bool dirty_primitive_topology =383cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY;384385VkPolygonMode dynamic_raster_mode;386VkPrimitiveTopology primitive_topology =387cmd_buffer->state.gfx.dynamic.primitive_topology;388dynamic_raster_mode =389genX(raster_polygon_mode)(cmd_buffer->state.gfx.pipeline,390primitive_topology);391392if (dirty_color_blend || dirty_primitive_topology) {393uint32_t dwords[GENX(3DSTATE_WM_length)];394struct GENX(3DSTATE_WM) wm = {395GENX(3DSTATE_WM_header),396397.ThreadDispatchEnable = pipeline->force_fragment_thread_dispatch ||398color_writes,399.MultisampleRasterizationMode =400genX(ms_rasterization_mode)(pipeline, dynamic_raster_mode),401};402GENX(3DSTATE_WM_pack)(NULL, dwords, &wm);403404anv_batch_emit_merge(&cmd_buffer->batch, dwords, pipeline->gfx7.wm);405}406407}408409if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS) {410genX(emit_multisample)(&cmd_buffer->batch,411cmd_buffer->state.gfx.dynamic.sample_locations.samples,412cmd_buffer->state.gfx.dynamic.sample_locations.locations);413}414415if (cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_COLOR_BLEND_STATE ||416cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_LOGIC_OP) {417const uint8_t color_writes = cmd_buffer->state.gfx.dynamic.color_writes;418bool dirty_color_blend =419cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_COLOR_BLEND_STATE;420421/* Blend states of each RT */422uint32_t surface_count = 0;423struct anv_pipeline_bind_map *map;424if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {425map = &pipeline->shaders[MESA_SHADER_FRAGMENT]->bind_map;426surface_count = map->surface_count;427}428429uint32_t blend_dws[GENX(BLEND_STATE_length) +430MAX_RTS * GENX(BLEND_STATE_ENTRY_length)];431uint32_t *dws = blend_dws;432memset(blend_dws, 0, sizeof(blend_dws));433434/* Skip this part */435dws += GENX(BLEND_STATE_length);436437bool dirty_logic_op =438cmd_buffer->state.gfx.dirty & ANV_CMD_DIRTY_DYNAMIC_LOGIC_OP;439440for (uint32_t i = 0; i < surface_count; i++) {441struct anv_pipeline_binding *binding = &map->surface_to_descriptor[i];442bool write_disabled =443dirty_color_blend && (color_writes & (1u << binding->index)) == 0;444struct GENX(BLEND_STATE_ENTRY) entry = {445.WriteDisableAlpha = write_disabled,446.WriteDisableRed = write_disabled,447.WriteDisableGreen = write_disabled,448.WriteDisableBlue = write_disabled,449.LogicOpFunction =450dirty_logic_op ? genX(vk_to_intel_logic_op)[d->logic_op] : 0,451};452GENX(BLEND_STATE_ENTRY_pack)(NULL, dws, &entry);453dws += GENX(BLEND_STATE_ENTRY_length);454}455456uint32_t num_dwords = GENX(BLEND_STATE_length) +457GENX(BLEND_STATE_ENTRY_length) * surface_count;458459struct anv_state blend_states =460anv_cmd_buffer_merge_dynamic(cmd_buffer, blend_dws,461pipeline->gfx7.blend_state, num_dwords, 64);462anv_batch_emit(&cmd_buffer->batch, GENX(3DSTATE_BLEND_STATE_POINTERS), bsp) {463bsp.BlendStatePointer = blend_states.offset;464}465}466467cmd_buffer->state.gfx.dirty = 0;468}469470void471genX(cmd_buffer_enable_pma_fix)(struct anv_cmd_buffer *cmd_buffer,472bool enable)473{474/* The NP PMA fix doesn't exist on gfx7 */475}476477478