diff --git a/Common/GPU/Vulkan/VulkanBarrier.cpp b/Common/GPU/Vulkan/VulkanBarrier.cpp index 8a6bb0ef1a..822fd5640b 100644 --- a/Common/GPU/Vulkan/VulkanBarrier.cpp +++ b/Common/GPU/Vulkan/VulkanBarrier.cpp @@ -13,10 +13,16 @@ VulkanBarrierBatch::~VulkanBarrierBatch() { } void VulkanBarrierBatch::Flush(VkCommandBuffer cmd) { + // TODO: batch together buffer and image barriers if (!imageBarriers_.empty()) { vkCmdPipelineBarrier(cmd, srcStageMask_, dstStageMask_, dependencyFlags_, 0, nullptr, 0, nullptr, (uint32_t)imageBarriers_.size(), imageBarriers_.data()); } imageBarriers_.clear(); + if (!bufferBarriers_.empty()) { + vkCmdPipelineBarrier(cmd, srcStageMask_, dstStageMask_, dependencyFlags_, 0, nullptr, (uint32_t)bufferBarriers_.size(), bufferBarriers_.data(), 0, nullptr); + } + bufferBarriers_.clear(); + srcStageMask_ = 0; dstStageMask_ = 0; dependencyFlags_ = 0; @@ -51,6 +57,19 @@ void VulkanBarrierBatch::TransitionImage( imageBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; } +void VulkanBarrierBatch::TransitionBufferToShaderRead(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size) { + VkBufferMemoryBarrier &bufferBarrier = bufferBarriers_.push_uninitialized(); + bufferBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; + bufferBarrier.pNext = nullptr; + bufferBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + bufferBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + bufferBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + bufferBarrier.buffer = buffer; + bufferBarrier.offset = offset; + bufferBarrier.size = size; +} + void VulkanBarrierBatch::TransitionColorImageAuto( VkImage image, VkImageLayout *imageLayout, VkImageLayout newImageLayout, int baseMip, int numMipLevels, int numLayers) { _dbg_assert_(image != VK_NULL_HANDLE); diff --git a/Common/GPU/Vulkan/VulkanBarrier.h b/Common/GPU/Vulkan/VulkanBarrier.h index 884e0919c7..e41df8f9b1 100644 --- a/Common/GPU/Vulkan/VulkanBarrier.h +++ b/Common/GPU/Vulkan/VulkanBarrier.h @@ -52,10 +52,13 @@ public: void TransitionColorImageAuto(VKRImage *image, VkImageLayout newImageLayout); void TransitionDepthStencilImageAuto(VKRImage *image, VkImageLayout newImageLayout); + void TransitionBufferToShaderRead(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size); + void Flush(VkCommandBuffer cmd); private: FastVec imageBarriers_; + FastVec bufferBarriers_; VkPipelineStageFlags srcStageMask_ = 0; VkPipelineStageFlags dstStageMask_ = 0; VkDependencyFlags dependencyFlags_ = 0; diff --git a/Common/GPU/Vulkan/VulkanDescSet.h b/Common/GPU/Vulkan/VulkanDescSet.h index bf62bec4fa..5123ba3460 100644 --- a/Common/GPU/Vulkan/VulkanDescSet.h +++ b/Common/GPU/Vulkan/VulkanDescSet.h @@ -12,6 +12,7 @@ enum class BindingType { STORAGE_BUFFER_VERTEX, STORAGE_BUFFER_COMPUTE, STORAGE_IMAGE_COMPUTE, + UNIFORM_BUFFER_COMPUTE, }; // Only appropriate for use in a per-frame pool. diff --git a/Common/GPU/Vulkan/VulkanMemory.cpp b/Common/GPU/Vulkan/VulkanMemory.cpp index 23af6ebf72..451960bf1b 100644 --- a/Common/GPU/Vulkan/VulkanMemory.cpp +++ b/Common/GPU/Vulkan/VulkanMemory.cpp @@ -176,3 +176,29 @@ void VulkanPushPool::GetDebugString(char *buffer, size_t bufSize) const { snprintf(buffer, bufSize, "Pool %s: %s / %s (%d extra blocks)", name_, NiceSizeFormat(used).c_str(), NiceSizeFormat(capacity).c_str(), (int)blocks_.size() - 3); } + +void VulkanBuffer::Create(VulkanContext *vulkan, const char *name, VkDeviceSize size, VkBufferUsageFlags usage) { + if (buffer_) { + Destroy(vulkan); + } + + VkBufferCreateInfo b{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + b.size = size; + b.usage = usage; + b.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + VmaAllocationCreateInfo allocCreateInfo{}; + + allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; + allocCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; // required to not get memory where we have to manually flush. + VmaAllocationInfo allocInfo{}; + + VkResult result = vmaCreateBuffer(vulkan->Allocator(), &b, &allocCreateInfo, &buffer_, &allocation_, &allocInfo); + _dbg_assert_(result == VK_SUCCESS); + vulkan->SetDebugName(buffer_, VK_OBJECT_TYPE_BUFFER, name); +} + +void VulkanBuffer::Destroy(VulkanContext *vulkan) { + if (buffer_) { + vulkan->Delete().QueueDeleteBufferAllocation(buffer_, allocation_); + } +} diff --git a/Common/GPU/Vulkan/VulkanMemory.h b/Common/GPU/Vulkan/VulkanMemory.h index 14e3b78436..f7e265046f 100644 --- a/Common/GPU/Vulkan/VulkanMemory.h +++ b/Common/GPU/Vulkan/VulkanMemory.h @@ -94,3 +94,18 @@ private: int curBlockIndex_ = -1; const char *name_; }; + +// Just a plain old static buffer in GPU memory. You can upload to it +// using a PushPool. +class VulkanBuffer { +public: + // Can be called again to resize/recreate. Will not preserve any data. + void Create(VulkanContext *vulkan, const char *name, VkDeviceSize size, VkBufferUsageFlags usage); + void Destroy(VulkanContext *vulkan); + + VkBuffer Buffer() const { return buffer_; } + +private: + VkBuffer buffer_; + VmaAllocation allocation_; +}; diff --git a/Common/GPU/Vulkan/VulkanRenderManager.cpp b/Common/GPU/Vulkan/VulkanRenderManager.cpp index 711e2a3fb5..10abc5cbec 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.cpp +++ b/Common/GPU/Vulkan/VulkanRenderManager.cpp @@ -1780,6 +1780,10 @@ VKRPipelineLayout *VulkanRenderManager::CreatePipelineLayout(BindingType *bindin bindings[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; bindings[i].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; break; + case BindingType::UNIFORM_BUFFER_COMPUTE: + bindings[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + bindings[i].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; + break; default: UNREACHABLE(); break; @@ -1962,12 +1966,22 @@ void VKRPipelineLayout::FlushDescSets(VulkanContext *vulkan, int frame, QueuePro _dbg_assert_(data[i].buffer.buffer != VK_NULL_HANDLE); bufferInfo[numBuffers].buffer = data[i].buffer.buffer; bufferInfo[numBuffers].range = data[i].buffer.range; - bufferInfo[numBuffers].offset = 0; // This is supplied by the dynamic offset. + bufferInfo[numBuffers].offset = 0; // This is supplied by the dynamic offset, if available. writes[numWrites].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; writes[numWrites].pBufferInfo = &bufferInfo[numBuffers]; writes[numWrites].pImageInfo = nullptr; numBuffers++; break; + case BindingType::UNIFORM_BUFFER_COMPUTE: + _dbg_assert_(data[i].buffer.buffer != VK_NULL_HANDLE); + bufferInfo[numBuffers].buffer = data[i].buffer.buffer; + bufferInfo[numBuffers].range = data[i].buffer.range; + bufferInfo[numBuffers].offset = 0; // This is supplied by the dynamic offset, if available. + writes[numWrites].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + writes[numWrites].pBufferInfo = &bufferInfo[numBuffers]; + writes[numWrites].pImageInfo = nullptr; + numBuffers++; + break; } writes[numWrites].dstBinding = i; writes[numWrites].dstSet = d.set; diff --git a/GPU/Common/PostShader.cpp b/GPU/Common/PostShader.cpp index 6335625dfd..66bdfa25ab 100644 --- a/GPU/Common/PostShader.cpp +++ b/GPU/Common/PostShader.cpp @@ -212,6 +212,16 @@ void LoadPostShaderInfo(Draw::DrawContext *draw, const std::vector &direct info.scaleFactor = 0; section.Get("Name", &info.name); section.Get("Scale", &info.scaleFactor); + std::string cbufferFilename; + if (section.Get("ConstantBuffer", &cbufferFilename)) { + Path cbufferPath = path / cbufferFilename; + std::string temp; + if (File::ReadBinaryFileToString(cbufferPath, &temp)) { + info.constantBuffer.assign(temp.begin(), temp.end()); + } else { + ERROR_LOG(Log::G3D, "Failed to read constant buffer file '%s' for texture shader '%s'", cbufferPath.c_str(), info.name.c_str()); + } + } if (section.Get("Compute", &temp)) { info.computeShaderFile = path / temp; info.computeShaderFiles.push_back(info.computeShaderFile); diff --git a/GPU/Common/PostShader.h b/GPU/Common/PostShader.h index 280a04dac5..7a8231afde 100644 --- a/GPU/Common/PostShader.h +++ b/GPU/Common/PostShader.h @@ -90,6 +90,9 @@ struct TextureShaderInfo { // Upscaling shaders have a fixed scale factor. int scaleFactor; + // Some shaders need to pass some data to the shader, and this is the buffer for that. It's up to the shader to interpret it. + std::vector constantBuffer; + bool operator == (const std::string &other) const { return name == other; } diff --git a/GPU/GPU.vcxproj b/GPU/GPU.vcxproj index 96206312cf..1ae4f38491 100644 --- a/GPU/GPU.vcxproj +++ b/GPU/GPU.vcxproj @@ -492,6 +492,62 @@ + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + true true @@ -516,8 +572,192 @@ true true + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + true true @@ -526,6 +766,110 @@ true true + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + true true @@ -542,6 +886,46 @@ true true + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + + + true + true + true + true + true + true + diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index 04c674b20a..389b88b0b5 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -538,6 +538,69 @@ Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + @@ -549,5 +612,86 @@ Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 0ebe937b2d..624dff7c47 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -62,7 +62,8 @@ const char *uploadShader = R"( #extension GL_ARB_separate_shader_objects : enable // 8x8 is the most common compute shader workgroup size, and works great on all major -// hardware vendors. +// hardware vendors. TODO: However, we should probably change to 16x16, as Qualcomm now has +// support for bigger groups... layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in; uniform layout(set = 0, binding = 0, rgba8) writeonly image2D img; @@ -76,6 +77,8 @@ layout(push_constant) uniform Params { int height; } params; +#define CBUFFER_BINDING 4 + uint readColoru(uvec2 p) { return buf.data[p.y * params.width + p.x]; } @@ -251,14 +254,16 @@ void TextureCacheVulkan::DeviceRestore(Draw::DrawContext *draw) { VkResult res = vkCreateSampler(vulkan->GetDevice(), &samp, nullptr, &samplerNearest_); _assert_(res == VK_SUCCESS); - CompileScalingShader(); + VkCommandBuffer cmdInit = (VkCommandBuffer)draw_->GetNativeObject(Draw::NativeObject::INIT_COMMANDBUFFER); + CompileScalingShader(cmdInit); computeShaderManager_.DeviceRestore(draw); } void TextureCacheVulkan::NotifyConfigChanged() { TextureCacheCommon::NotifyConfigChanged(); - CompileScalingShader(); + VkCommandBuffer cmdInit = (VkCommandBuffer)draw_->GetNativeObject(Draw::NativeObject::INIT_COMMANDBUFFER); + CompileScalingShader(cmdInit); } static std::string ReadShaderSrc(const Path &filename) { @@ -294,6 +299,7 @@ void TextureCacheVulkan::ClearScalingShaders(VulkanContext *vulkan) { multipassScratchDescs_.clear(); multipassStageDescs_.clear(); textureScalePipeline_ = TextureScalePipelineType::NONE; + textureSceleCBuffer_.Destroy(vulkan); } bool TextureCacheVulkan::CompileMultipassShader(VulkanContext *vulkan, const TextureShaderInfo &shaderInfo, std::string *error) { @@ -356,7 +362,7 @@ bool TextureCacheVulkan::CompileMultipassShader(VulkanContext *vulkan, const Tex return true; } -void TextureCacheVulkan::CompileScalingShader() { +void TextureCacheVulkan::CompileScalingShader(VkCommandBuffer cmdInit) { if (!draw_) { // Something is very wrong. return; @@ -402,6 +408,18 @@ void TextureCacheVulkan::CompileScalingShader() { textureScalePipeline_ = TextureScalePipelineType::SINGLE_PASS; } + if (!shaderInfo->constantBuffer.empty()) { + textureSceleCBuffer_.Create(vulkan, "TextureScale CBuffer", shaderInfo->constantBuffer.size(), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); + VulkanPushPool *pushPool = drawEngine_->GetPushBufferForTextureData(); + VkBuffer srcBuf; + VkDeviceSize offset = pushPool->Push(shaderInfo->constantBuffer.data(), shaderInfo->constantBuffer.size(), vulkan->GetPhysicalDeviceProperties().properties.limits.minUniformBufferOffsetAlignment, &srcBuf); + VkBufferCopy copyRegion{offset, 0, shaderInfo->constantBuffer.size()}; + vkCmdCopyBuffer(cmdInit, srcBuf, textureSceleCBuffer_.Buffer(), 1, ©Region); + VulkanBarrierBatch barrier; + barrier.TransitionBufferToShaderRead(textureSceleCBuffer_.Buffer(), 0, shaderInfo->constantBuffer.size()); + barrier.Flush(cmdInit); + } + textureShader_ = g_Config.sTextureShaderName; shaderScaleFactor_ = shaderInfo->scaleFactor; } diff --git a/GPU/Vulkan/TextureCacheVulkan.h b/GPU/Vulkan/TextureCacheVulkan.h index 79c1cf3d3f..dd01b2ac78 100644 --- a/GPU/Vulkan/TextureCacheVulkan.h +++ b/GPU/Vulkan/TextureCacheVulkan.h @@ -121,7 +121,7 @@ private: void BuildTexture(TexCacheEntry *const entry) override; - void CompileScalingShader(); + void CompileScalingShader(VkCommandBuffer cmdInit); bool CompileMultipassShader(VulkanContext *vulkan, const TextureShaderInfo &shaderInfo, std::string *error); void ClearScalingShaders(VulkanContext *vulkan); bool HasScalingShader() const; @@ -146,6 +146,8 @@ private: VkSampler curSampler_ = VK_NULL_HANDLE; VkSampler samplerNearest_ = VK_NULL_HANDLE; + + VulkanBuffer textureSceleCBuffer_; }; VkFormat getClutDestFormatVulkan(GEPaletteFormat format); diff --git a/assets/shaders/defaultshaders.ini b/assets/shaders/defaultshaders.ini index f322a6a8b4..bd30fdaf6a 100644 --- a/assets/shaders/defaultshaders.ini +++ b/assets/shaders/defaultshaders.ini @@ -260,6 +260,13 @@ Name=NNEDI3 Single (4x) Author=tritical, mpv community Compute=tex_nnedi3_4x_single.csh Scale=4 +[TexSmiley] +Type=Texture +Name=Smiley (2x) +Author=PPSSPP contributors +Compute=tex_smiley_2x.csh +Scale=2 +ConstantBuffer=smiley_16x16_rgba.bin [RedBlue] Type=StereoToMono Name=Red/Blue glasses (anaglyph) diff --git a/assets/shaders/smiley.py b/assets/shaders/smiley.py new file mode 100644 index 0000000000..eedf77d773 --- /dev/null +++ b/assets/shaders/smiley.py @@ -0,0 +1,53 @@ +import struct + +# Vibe coded nonsense to initialize a cbuffer + +# Constants +WIDTH, HEIGHT = 16, 16 +OUTPUT_FILE = "smiley_16x16_rgba.bin" + +# Define the colors (RGBA uint32) +# Format: 0xAABBGGRR (Little Endian will write this as R, G, B, A) +YELLOW = 0xFF00FFFF # Bright Yellow +BLACK = 0xFF000000 # Opaque Black +TRANS = 0x00000000 # Fully Transparent + +# A simple 16x16 map +# Y = Yellow, B = Black, . = Transparent +smiley_map = [ + " YYYY ", + " YYYBYYYY ", + " YYYYYYYYYY ", + " YYYYYYYYYYYY ", + " YYYBYYYYBBYYY ", + " YYYBYYYYBBYYY ", + "YYYYYYYYYYYYYYYY", + "YYYYYYYYYYYYYYYY", + "YYYYBYYYYYYBYYYY", + "YYYYYBBYBBBYYYYY", + " YYYYYYBBBYYYYY ", + " YYYYYYYYYYYYYY ", + " YYYYYYYYYYYY ", + " YYYYYYYYYY ", + " YYYYYYYY ", + " YYYY ", +] + +def generate_binary(): + with open(OUTPUT_FILE, "wb") as f: + for row in smiley_map: + for char in row: + if char == "Y": + color = YELLOW + elif char == "B": + color = BLACK + else: + color = TRANS + + # 'I' is unsigned int (32-bit), '<' is Little Endian + f.write(struct.pack('