CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Vulkan/DrawEngineVulkan.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "ppsspp_config.h"18#include <algorithm>19#include <functional>2021#include "Common/Data/Convert/SmallDataConvert.h"22#include "Common/Profiler/Profiler.h"23#include "Common/GPU/Vulkan/VulkanRenderManager.h"2425#include "Common/Log.h"26#include "Common/MemoryUtil.h"27#include "Common/TimeUtil.h"28#include "Core/MemMap.h"29#include "Core/System.h"30#include "Core/Config.h"31#include "Core/CoreTiming.h"3233#include "GPU/Math3D.h"34#include "GPU/GPUState.h"35#include "GPU/ge_constants.h"3637#include "Common/GPU/Vulkan/VulkanContext.h"38#include "Common/GPU/Vulkan/VulkanMemory.h"3940#include "GPU/Common/SplineCommon.h"41#include "GPU/Common/TransformCommon.h"42#include "GPU/Common/VertexDecoderCommon.h"43#include "GPU/Common/SoftwareTransformCommon.h"44#include "GPU/Common/DrawEngineCommon.h"45#include "GPU/Common/ShaderUniforms.h"46#include "GPU/Debugger/Debugger.h"47#include "GPU/Vulkan/DrawEngineVulkan.h"48#include "GPU/Vulkan/TextureCacheVulkan.h"49#include "GPU/Vulkan/ShaderManagerVulkan.h"50#include "GPU/Vulkan/PipelineManagerVulkan.h"51#include "GPU/Vulkan/FramebufferManagerVulkan.h"52#include "GPU/Vulkan/GPU_Vulkan.h"5354using namespace PPSSPP_VK;5556enum {57TRANSFORMED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * sizeof(TransformedVertex)58};5960DrawEngineVulkan::DrawEngineVulkan(Draw::DrawContext *draw)61: draw_(draw) {62decOptions_.expandAllWeightsToFloat = false;63decOptions_.expand8BitNormalsToFloat = false;64indexGen.Setup(decIndex_);65}6667void DrawEngineVulkan::InitDeviceObjects() {68// All resources we need for PSP drawing. Usually only bindings 0 and 2-4 are populated.6970BindingType bindingTypes[VKRPipelineLayout::MAX_DESC_SET_BINDINGS] = {71BindingType::COMBINED_IMAGE_SAMPLER, // main72BindingType::COMBINED_IMAGE_SAMPLER, // framebuffer-read73BindingType::COMBINED_IMAGE_SAMPLER, // palette74BindingType::UNIFORM_BUFFER_DYNAMIC_ALL, // uniforms75BindingType::UNIFORM_BUFFER_DYNAMIC_VERTEX, // lights76BindingType::UNIFORM_BUFFER_DYNAMIC_VERTEX, // bones77BindingType::STORAGE_BUFFER_VERTEX, // tess78BindingType::STORAGE_BUFFER_VERTEX,79BindingType::STORAGE_BUFFER_VERTEX,80};8182VulkanContext *vulkan = (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT);83VkDevice device = vulkan->GetDevice();8485VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);86pipelineLayout_ = renderManager->CreatePipelineLayout(bindingTypes, ARRAY_SIZE(bindingTypes), draw_->GetDeviceCaps().geometryShaderSupported, "drawengine_layout");8788pushUBO_ = (VulkanPushPool *)draw_->GetNativeObject(Draw::NativeObject::PUSH_POOL);89pushVertex_ = new VulkanPushPool(vulkan, "pushVertex", 4 * 1024 * 1024, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);90pushIndex_ = new VulkanPushPool(vulkan, "pushIndex", 1 * 512 * 1024, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);9192VkSamplerCreateInfo samp{ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };93samp.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;94samp.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;95samp.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;96samp.magFilter = VK_FILTER_LINEAR;97samp.minFilter = VK_FILTER_LINEAR;98samp.maxLod = VK_LOD_CLAMP_NONE; // recommended by best practices, has no effect since we don't use mipmaps.99VkResult res = vkCreateSampler(device, &samp, nullptr, &samplerSecondaryLinear_);100samp.magFilter = VK_FILTER_NEAREST;101samp.minFilter = VK_FILTER_NEAREST;102res = vkCreateSampler(device, &samp, nullptr, &samplerSecondaryNearest_);103_dbg_assert_(VK_SUCCESS == res);104res = vkCreateSampler(device, &samp, nullptr, &nullSampler_);105_dbg_assert_(VK_SUCCESS == res);106107tessDataTransferVulkan = new TessellationDataTransferVulkan(vulkan);108tessDataTransfer = tessDataTransferVulkan;109110draw_->SetInvalidationCallback(std::bind(&DrawEngineVulkan::Invalidate, this, std::placeholders::_1));111}112113DrawEngineVulkan::~DrawEngineVulkan() {114DestroyDeviceObjects();115}116117void DrawEngineVulkan::DestroyDeviceObjects() {118if (!draw_) {119// We've already done this from LostDevice.120return;121}122123VulkanContext *vulkan = (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT);124VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);125126draw_->SetInvalidationCallback(InvalidationCallback());127128delete tessDataTransferVulkan;129tessDataTransfer = nullptr;130tessDataTransferVulkan = nullptr;131132pushUBO_ = nullptr;133134if (pushVertex_) {135pushVertex_->Destroy();136delete pushVertex_;137pushVertex_ = nullptr;138}139if (pushIndex_) {140pushIndex_->Destroy();141delete pushIndex_;142pushIndex_ = nullptr;143}144145if (samplerSecondaryNearest_ != VK_NULL_HANDLE)146vulkan->Delete().QueueDeleteSampler(samplerSecondaryNearest_);147if (samplerSecondaryLinear_ != VK_NULL_HANDLE)148vulkan->Delete().QueueDeleteSampler(samplerSecondaryLinear_);149if (nullSampler_ != VK_NULL_HANDLE)150vulkan->Delete().QueueDeleteSampler(nullSampler_);151152renderManager->DestroyPipelineLayout(pipelineLayout_);153}154155void DrawEngineVulkan::DeviceLost() {156DestroyDeviceObjects();157DirtyAllUBOs();158draw_ = nullptr;159}160161void DrawEngineVulkan::DeviceRestore(Draw::DrawContext *draw) {162draw_ = draw;163InitDeviceObjects();164}165166void DrawEngineVulkan::BeginFrame() {167lastPipeline_ = nullptr;168169// pushUBO is the thin3d push pool, don't need to BeginFrame again.170pushVertex_->BeginFrame();171pushIndex_->BeginFrame();172173tessDataTransferVulkan->SetPushPool(pushUBO_);174175DirtyAllUBOs();176}177178void DrawEngineVulkan::EndFrame() {179stats_.pushVertexSpaceUsed = (int)pushVertex_->GetUsedThisFrame();180stats_.pushIndexSpaceUsed = (int)pushIndex_->GetUsedThisFrame();181}182183void DrawEngineVulkan::DirtyAllUBOs() {184baseUBOOffset = 0;185lightUBOOffset = 0;186boneUBOOffset = 0;187baseBuf = VK_NULL_HANDLE;188lightBuf = VK_NULL_HANDLE;189boneBuf = VK_NULL_HANDLE;190dirtyUniforms_ = DIRTY_BASE_UNIFORMS | DIRTY_LIGHT_UNIFORMS | DIRTY_BONE_UNIFORMS;191imageView = VK_NULL_HANDLE;192sampler = VK_NULL_HANDLE;193gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);194}195196void DrawEngineVulkan::Invalidate(InvalidationCallbackFlags flags) {197if (flags & InvalidationCallbackFlags::COMMAND_BUFFER_STATE) {198// Nothing here anymore (removed the "frame descriptor set"199// If we add back "seldomly-changing" descriptors, we might use this again.200}201if (flags & InvalidationCallbackFlags::RENDER_PASS_STATE) {202// If have a new render pass, dirty our dynamic state so it gets re-set.203//204// Dirty everything that has dynamic state that will need re-recording.205gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_BLEND_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);206lastPipeline_ = nullptr;207}208}209210// The inline wrapper in the header checks for numDrawCalls_ == 0211void DrawEngineVulkan::DoFlush() {212VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);213214PROFILE_THIS_SCOPE("Flush");215216bool tess = gstate_c.submitType == SubmitType::HW_BEZIER || gstate_c.submitType == SubmitType::HW_SPLINE;217218bool textureNeedsApply = false;219if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) {220textureCache_->SetTexture();221gstate_c.Clean(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);222// NOTE: After this is set, we MUST call ApplyTexture before returning.223textureNeedsApply = true;224} else if (gstate.getTextureAddress(0) == (gstate.getFrameBufRawAddress() | 0x04000000)) {225// This catches the case of clearing a texture.226gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);227}228229GEPrimitiveType prim = prevPrim_;230231// Always use software for flat shading to fix the provoking index232// if the provoking vertex extension is not available.233bool provokingVertexOk = (tess || gstate.getShadeMode() != GE_SHADE_FLAT);234if (renderManager->GetVulkanContext()->GetDeviceFeatures().enabled.provokingVertex.provokingVertexLast) {235provokingVertexOk = true;236}237bool useHWTransform = CanUseHardwareTransform(prim) && provokingVertexOk;238239uint32_t ibOffset;240uint32_t vbOffset;241242// The optimization to avoid indexing isn't really worth it on Vulkan since it means creating more pipelines.243// This could be avoided with the new dynamic state extensions, but not available enough on mobile.244const bool forceIndexed = draw_->GetDeviceCaps().verySlowShaderCompiler;245246if (useHWTransform) {247VkBuffer vbuf = VK_NULL_HANDLE;248VkBuffer ibuf = VK_NULL_HANDLE;249if (decOptions_.applySkinInDecode && (lastVType_ & GE_VTYPE_WEIGHT_MASK)) {250// If software skinning, we're predecoding into "decoded". So make sure we're done, then push that content.251DecodeVerts(decoded_);252VkDeviceSize size = numDecodedVerts_ * dec_->GetDecVtxFmt().stride;253u8 *dest = (u8 *)pushVertex_->Allocate(size, 4, &vbuf, &vbOffset);254memcpy(dest, decoded_, size);255} else {256// Figure out how much pushbuffer space we need to allocate.257int vertsToDecode = ComputeNumVertsToDecode();258// Decode directly into the pushbuffer259u8 *dest = pushVertex_->Allocate(vertsToDecode * dec_->GetDecVtxFmt().stride, 4, &vbuf, &vbOffset);260DecodeVerts(dest);261}262263int vertexCount;264int maxIndex;265bool useElements;266DecodeIndsAndGetData(&prim, &vertexCount, &maxIndex, &useElements, false);267268bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;269if (gstate.isModeThrough()) {270gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);271} else {272gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);273}274275if (textureNeedsApply) {276textureCache_->ApplyTexture();277textureCache_->GetVulkanHandles(imageView, sampler);278if (imageView == VK_NULL_HANDLE)279imageView = (VkImageView)draw_->GetNativeObject(gstate_c.textureIsArray ? Draw::NativeObject::NULL_IMAGEVIEW_ARRAY : Draw::NativeObject::NULL_IMAGEVIEW);280if (sampler == VK_NULL_HANDLE)281sampler = nullSampler_;282}283284if (!lastPipeline_ || gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE) || prim != lastPrim_) {285if (prim != lastPrim_ || gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {286ConvertStateToVulkanKey(*framebufferManager_, shaderManager_, prim, pipelineKey_, dynState_);287}288289VulkanVertexShader *vshader = nullptr;290VulkanFragmentShader *fshader = nullptr;291VulkanGeometryShader *gshader = nullptr;292293shaderManager_->GetShaders(prim, dec_, &vshader, &fshader, &gshader, pipelineState_, true, useHWTessellation_, decOptions_.expandAllWeightsToFloat, decOptions_.applySkinInDecode);294_dbg_assert_msg_(vshader->UseHWTransform(), "Bad vshader");295VulkanPipeline *pipeline = pipelineManager_->GetOrCreatePipeline(renderManager, pipelineLayout_, pipelineKey_, &dec_->decFmt, vshader, fshader, gshader, true, 0, framebufferManager_->GetMSAALevel(), false);296if (!pipeline || !pipeline->pipeline) {297// Already logged, let's bail out.298ResetAfterDraw();299return;300}301BindShaderBlendTex(); // This might cause copies so important to do before BindPipeline.302303if (!renderManager->BindPipeline(pipeline->pipeline, pipeline->pipelineFlags, pipelineLayout_)) {304renderManager->ReportBadStateForDraw();305ResetAfterDraw();306return;307}308if (pipeline != lastPipeline_) {309if (lastPipeline_ && !(lastPipeline_->UsesBlendConstant() && pipeline->UsesBlendConstant())) {310gstate_c.Dirty(DIRTY_BLEND_STATE);311}312lastPipeline_ = pipeline;313}314ApplyDrawStateLate(renderManager, false, 0, pipeline->UsesBlendConstant());315gstate_c.Clean(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE);316gstate_c.Dirty(dirtyRequiresRecheck_);317dirtyRequiresRecheck_ = 0;318lastPipeline_ = pipeline;319}320lastPrim_ = prim;321322dirtyUniforms_ |= shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());323UpdateUBOs();324325int descCount = 6;326if (tess)327descCount = 9;328int descSetIndex;329PackedDescriptor *descriptors = renderManager->PushDescriptorSet(descCount, &descSetIndex);330descriptors[0].image.view = imageView;331descriptors[0].image.sampler = sampler;332333descriptors[1].image.view = boundSecondary_;334descriptors[1].image.sampler = samplerSecondaryNearest_;335336descriptors[2].image.view = boundDepal_;337descriptors[2].image.sampler = (boundDepal_ && boundDepalSmoothed_) ? samplerSecondaryLinear_ : samplerSecondaryNearest_;338339descriptors[3].buffer.buffer = baseBuf;340descriptors[3].buffer.range = sizeof(UB_VS_FS_Base);341descriptors[3].buffer.offset = 0;342343descriptors[4].buffer.buffer = lightBuf;344descriptors[4].buffer.range = sizeof(UB_VS_Lights);345descriptors[4].buffer.offset = 0;346347descriptors[5].buffer.buffer = boneBuf;348descriptors[5].buffer.range = sizeof(UB_VS_Bones);349descriptors[5].buffer.offset = 0;350if (tess) {351const VkDescriptorBufferInfo *bufInfo = tessDataTransferVulkan->GetBufferInfo();352for (int j = 0; j < 3; j++) {353descriptors[j + 6].buffer.buffer = bufInfo[j].buffer;354descriptors[j + 6].buffer.range = bufInfo[j].range;355descriptors[j + 6].buffer.offset = bufInfo[j].offset;356}357}358// TODO: Can we avoid binding all three when not needed? Same below for hardware transform.359// Think this will require different descriptor set layouts.360const uint32_t dynamicUBOOffsets[3] = {361baseUBOOffset, lightUBOOffset, boneUBOOffset,362};363if (useElements) {364if (!ibuf) {365ibOffset = (uint32_t)pushIndex_->Push(decIndex_, sizeof(uint16_t) * vertexCount, 4, &ibuf);366}367renderManager->DrawIndexed(descSetIndex, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, ibuf, ibOffset, vertexCount, 1);368} else {369renderManager->Draw(descSetIndex, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, vertexCount);370}371} else {372PROFILE_THIS_SCOPE("soft");373if (!decOptions_.applySkinInDecode) {374decOptions_.applySkinInDecode = true;375lastVType_ |= (1 << 26);376dec_ = GetVertexDecoder(lastVType_);377}378int prevDecodedVerts = numDecodedVerts_;379380DecodeVerts(decoded_);381int vertexCount = DecodeInds();382383bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;384if (gstate.isModeThrough()) {385gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);386} else {387gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);388}389390gpuStats.numUncachedVertsDrawn += vertexCount;391prim = IndexGenerator::GeneralPrim((GEPrimitiveType)drawInds_[0].prim);392393// At this point, the output is always an index triangle/line/point list, no strips/fans.394395u16 *inds = decIndex_;396SoftwareTransformResult result{};397SoftwareTransformParams params{};398params.decoded = decoded_;399params.transformed = transformed_;400params.transformedExpanded = transformedExpanded_;401params.fbman = framebufferManager_;402params.texCache = textureCache_;403// In Vulkan, we have to force drawing of primitives if !framebufferManager_->UseBufferedRendering() because Vulkan clears404// do not respect scissor rects.405params.allowClear = framebufferManager_->UseBufferedRendering();406params.allowSeparateAlphaClear = false;407408if (gstate.getShadeMode() == GE_SHADE_FLAT) {409if (!renderManager->GetVulkanContext()->GetDeviceFeatures().enabled.provokingVertex.provokingVertexLast) {410// If we can't have the hardware do it, we need to rotate the index buffer to simulate a different provoking vertex.411// We do this before line expansion etc.412IndexBufferProvokingLastToFirst(prim, inds, vertexCount);413}414}415params.flippedY = true;416params.usesHalfZ = true;417418// We need to update the viewport early because it's checked for flipping in SoftwareTransform.419// We don't have a "DrawStateEarly" in vulkan, so...420// TODO: Probably should eventually refactor this and feed the vp size into SoftwareTransform directly (Unknown's idea).421if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {422ViewportAndScissor vpAndScissor;423ConvertViewportAndScissor(framebufferManager_->UseBufferedRendering(),424framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),425framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),426vpAndScissor);427UpdateCachedViewportState(vpAndScissor);428}429430SoftwareTransform swTransform(params);431432const Lin::Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);433const Lin::Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);434swTransform.SetProjMatrix(gstate.projMatrix, gstate_c.vpWidth < 0, gstate_c.vpHeight < 0, trans, scale);435436swTransform.Transform(prim, dec_->VertexType(), dec_->GetDecVtxFmt(), numDecodedVerts_, &result);437// Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values.438// Games sometimes expect exact matches (see #12626, for example) for equal comparisons.439if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f)440result.action = SW_NOT_READY;441442if (result.action == SW_NOT_READY) {443// decIndex_ here is always equal to inds currently, but it may not be in the future.444swTransform.BuildDrawingParams(prim, vertexCount, dec_->VertexType(), inds, RemainingIndices(inds), numDecodedVerts_, VERTEX_BUFFER_MAX, &result);445}446447if (result.setSafeSize)448framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight);449450// Only here, where we know whether to clear or to draw primitives, should we actually set the current framebuffer! Because that gives use the opportunity451// to use a "pre-clear" render pass, for high efficiency on tilers.452if (result.action == SW_DRAW_INDEXED) {453if (textureNeedsApply) {454gstate_c.pixelMapped = result.pixelMapped;455textureCache_->ApplyTexture();456gstate_c.pixelMapped = false;457textureCache_->GetVulkanHandles(imageView, sampler);458if (imageView == VK_NULL_HANDLE)459imageView = (VkImageView)draw_->GetNativeObject(gstate_c.textureIsArray ? Draw::NativeObject::NULL_IMAGEVIEW_ARRAY : Draw::NativeObject::NULL_IMAGEVIEW);460if (sampler == VK_NULL_HANDLE)461sampler = nullSampler_;462}463if (!lastPipeline_ || gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE) || prim != lastPrim_) {464if (prim != lastPrim_ || gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {465ConvertStateToVulkanKey(*framebufferManager_, shaderManager_, prim, pipelineKey_, dynState_);466}467468VulkanVertexShader *vshader = nullptr;469VulkanFragmentShader *fshader = nullptr;470VulkanGeometryShader *gshader = nullptr;471472shaderManager_->GetShaders(prim, dec_, &vshader, &fshader, &gshader, pipelineState_, false, false, decOptions_.expandAllWeightsToFloat, true);473_dbg_assert_msg_(!vshader->UseHWTransform(), "Bad vshader");474VulkanPipeline *pipeline = pipelineManager_->GetOrCreatePipeline(renderManager, pipelineLayout_, pipelineKey_, &dec_->decFmt, vshader, fshader, gshader, false, 0, framebufferManager_->GetMSAALevel(), false);475if (!pipeline || !pipeline->pipeline) {476// Already logged, let's bail out.477ResetAfterDraw();478return;479}480BindShaderBlendTex(); // This might cause copies so super important to do before BindPipeline.481482if (!renderManager->BindPipeline(pipeline->pipeline, pipeline->pipelineFlags, pipelineLayout_)) {483renderManager->ReportBadStateForDraw();484ResetAfterDraw();485return;486}487if (pipeline != lastPipeline_) {488if (lastPipeline_ && !lastPipeline_->UsesBlendConstant() && pipeline->UsesBlendConstant()) {489gstate_c.Dirty(DIRTY_BLEND_STATE);490}491lastPipeline_ = pipeline;492}493ApplyDrawStateLate(renderManager, result.setStencil, result.stencilValue, pipeline->UsesBlendConstant());494gstate_c.Clean(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE);495gstate_c.Dirty(dirtyRequiresRecheck_);496dirtyRequiresRecheck_ = 0;497lastPipeline_ = pipeline;498}499500lastPrim_ = prim;501502dirtyUniforms_ |= shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());503504// Even if the first draw is through-mode, make sure we at least have one copy of these uniforms buffered505UpdateUBOs();506507int descCount = 6;508int descSetIndex;509PackedDescriptor *descriptors = renderManager->PushDescriptorSet(descCount, &descSetIndex);510descriptors[0].image.view = imageView;511descriptors[0].image.sampler = sampler;512descriptors[1].image.view = boundSecondary_;513descriptors[1].image.sampler = samplerSecondaryNearest_;514descriptors[2].image.view = boundDepal_;515descriptors[2].image.sampler = (boundDepal_ && boundDepalSmoothed_) ? samplerSecondaryLinear_ : samplerSecondaryNearest_;516descriptors[3].buffer.buffer = baseBuf;517descriptors[3].buffer.range = sizeof(UB_VS_FS_Base);518descriptors[4].buffer.buffer = lightBuf;519descriptors[4].buffer.range = sizeof(UB_VS_Lights);520descriptors[5].buffer.buffer = boneBuf;521descriptors[5].buffer.range = sizeof(UB_VS_Bones);522523const uint32_t dynamicUBOOffsets[3] = {524baseUBOOffset, lightUBOOffset, boneUBOOffset,525};526527PROFILE_THIS_SCOPE("renderman_q");528529VkBuffer vbuf, ibuf;530vbOffset = (uint32_t)pushVertex_->Push(result.drawBuffer, numDecodedVerts_ * sizeof(TransformedVertex), 4, &vbuf);531ibOffset = (uint32_t)pushIndex_->Push(inds, sizeof(short) * result.drawNumTrans, 4, &ibuf);532renderManager->DrawIndexed(descSetIndex, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, ibuf, ibOffset, result.drawNumTrans, 1);533} else if (result.action == SW_CLEAR) {534// Note: we won't get here if the clear is alpha but not color, or color but not alpha.535bool clearColor = gstate.isClearModeColorMask();536bool clearAlpha = gstate.isClearModeAlphaMask(); // and stencil537bool clearDepth = gstate.isClearModeDepthMask();538int mask = 0;539// The Clear detection takes care of doing a regular draw instead if separate masking540// of color and alpha is needed, so we can just treat them as the same.541if (clearColor || clearAlpha) mask |= Draw::FBChannel::FB_COLOR_BIT;542if (clearDepth) mask |= Draw::FBChannel::FB_DEPTH_BIT;543if (clearAlpha) mask |= Draw::FBChannel::FB_STENCIL_BIT;544// Note that since the alpha channel and the stencil channel are shared on the PSP,545// when we clear alpha, we also clear stencil to the same value.546draw_->Clear(mask, result.color, result.depth, result.color >> 24);547if (clearColor || clearAlpha) {548framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);549}550if (gstate_c.Use(GPU_USE_CLEAR_RAM_HACK) && gstate.isClearModeColorMask() && (gstate.isClearModeAlphaMask() || gstate.FrameBufFormat() == GE_FORMAT_565)) {551int scissorX1 = gstate.getScissorX1();552int scissorY1 = gstate.getScissorY1();553int scissorX2 = gstate.getScissorX2() + 1;554int scissorY2 = gstate.getScissorY2() + 1;555framebufferManager_->ApplyClearToMemory(scissorX1, scissorY1, scissorX2, scissorY2, result.color);556}557}558decOptions_.applySkinInDecode = g_Config.bSoftwareSkinning;559}560561ResetAfterDrawInline();562563framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);564565GPUDebug::NotifyDraw();566}567568void DrawEngineVulkan::ResetAfterDraw() {569indexGen.Reset();570numDecodedVerts_ = 0;571numDrawVerts_ = 0;572numDrawInds_ = 0;573vertexCountInDrawCalls_ = 0;574decodeIndsCounter_ = 0;575decodeVertsCounter_ = 0;576decOptions_.applySkinInDecode = g_Config.bSoftwareSkinning;577gstate_c.vertexFullAlpha = true;578}579580void DrawEngineVulkan::UpdateUBOs() {581if ((dirtyUniforms_ & DIRTY_BASE_UNIFORMS) || baseBuf == VK_NULL_HANDLE) {582baseUBOOffset = shaderManager_->PushBaseBuffer(pushUBO_, &baseBuf);583dirtyUniforms_ &= ~DIRTY_BASE_UNIFORMS;584}585if ((dirtyUniforms_ & DIRTY_LIGHT_UNIFORMS) || lightBuf == VK_NULL_HANDLE) {586lightUBOOffset = shaderManager_->PushLightBuffer(pushUBO_, &lightBuf);587dirtyUniforms_ &= ~DIRTY_LIGHT_UNIFORMS;588}589if ((dirtyUniforms_ & DIRTY_BONE_UNIFORMS) || boneBuf == VK_NULL_HANDLE) {590boneUBOOffset = shaderManager_->PushBoneBuffer(pushUBO_, &boneBuf);591dirtyUniforms_ &= ~DIRTY_BONE_UNIFORMS;592}593}594595void TessellationDataTransferVulkan::SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) {596// SSBOs that are not simply float1 or float2 need to be padded up to a float4 size. vec3 members597// also need to be 16-byte aligned, hence the padding.598struct TessData {599float pos[3]; float pad1;600float uv[2]; float pad2[2];601float color[4];602};603604int size = size_u * size_v;605606int ssboAlignment = vulkan_->GetPhysicalDeviceProperties().properties.limits.minStorageBufferOffsetAlignment;607uint8_t *data = (uint8_t *)push_->Allocate(size * sizeof(TessData), ssboAlignment, &bufInfo_[0].buffer, (uint32_t *)&bufInfo_[0].offset);608bufInfo_[0].range = size * sizeof(TessData);609610float *pos = (float *)(data);611float *tex = (float *)(data + offsetof(TessData, uv));612float *col = (float *)(data + offsetof(TessData, color));613int stride = sizeof(TessData) / sizeof(float);614615CopyControlPoints(pos, tex, col, stride, stride, stride, points, size, vertType);616617using Spline::Weight;618619// Weights U620data = (uint8_t *)push_->Allocate(weights.size_u * sizeof(Weight), ssboAlignment, &bufInfo_[1].buffer, (uint32_t *)&bufInfo_[1].offset);621memcpy(data, weights.u, weights.size_u * sizeof(Weight));622bufInfo_[1].range = weights.size_u * sizeof(Weight);623624// Weights V625data = (uint8_t *)push_->Allocate(weights.size_v * sizeof(Weight), ssboAlignment, &bufInfo_[2].buffer, (uint32_t *)&bufInfo_[2].offset);626memcpy(data, weights.v, weights.size_v * sizeof(Weight));627bufInfo_[2].range = weights.size_v * sizeof(Weight);628}629630631