diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index f77a813837..b8c62f596e 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -215,11 +215,6 @@ void DrawEngineVulkan::DestroyDeviceObjects() { vulkan_->Delete().QueueDeletePipelineLayout(pipelineLayout_); if (descriptorSetLayout_ != VK_NULL_HANDLE) vulkan_->Delete().QueueDeleteDescriptorSetLayout(descriptorSetLayout_); - if (nullTexture_) { - nullTexture_->Destroy(); - delete nullTexture_; - nullTexture_ = nullptr; - } if (vertexCache_) { vertexCache_->Destroy(vulkan_); delete vertexCache_; @@ -265,29 +260,6 @@ void DrawEngineVulkan::BeginFrame() { // TODO: How can we make this nicer... ((TessellationDataTransferVulkan *)tessDataTransfer)->SetPushBuffer(frame->pushUBO); - if (!nullTexture_) { - VkCommandBuffer cmdInit = (VkCommandBuffer)draw_->GetNativeObject(Draw::NativeObject::INIT_COMMANDBUFFER); - nullTexture_ = new VulkanTexture(vulkan_, textureCache_->GetAllocator()); - nullTexture_->SetTag("Null"); - int w = 8; - int h = 8; - nullTexture_->CreateDirect(cmdInit, w, h, 1, VK_FORMAT_A8B8G8R8_UNORM_PACK32, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); - uint32_t bindOffset; - VkBuffer bindBuf; - uint32_t *data = (uint32_t *)frame->pushUBO->Push(w * h * 4, &bindOffset, &bindBuf); - for (int y = 0; y < h; y++) { - for (int x = 0; x < w; x++) { - // data[y*w + x] = ((x ^ y) & 1) ? 0xFF808080 : 0xFF000000; // gray/black checkerboard - data[y*w + x] = 0; // black - } - } - nullTexture_->UploadMip(cmdInit, 0, w, h, bindBuf, bindOffset, w); - nullTexture_->EndCreate(cmdInit); - } else { - nullTexture_->Touch(); - } - DirtyAllUBOs(); // Wipe the vertex cache if it's grown too large. @@ -796,7 +768,7 @@ void DrawEngineVulkan::DoFlush() { textureCache_->ApplyTexture(); textureCache_->GetVulkanHandles(imageView, sampler); if (imageView == VK_NULL_HANDLE) - imageView = nullTexture_->GetImageView(); + imageView = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::NULL_IMAGEVIEW); if (sampler == VK_NULL_HANDLE) sampler = nullSampler_; } @@ -902,7 +874,7 @@ void DrawEngineVulkan::DoFlush() { textureCache_->ApplyTexture(); textureCache_->GetVulkanHandles(imageView, sampler); if (imageView == VK_NULL_HANDLE) - imageView = nullTexture_->GetImageView(); + imageView = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::NULL_IMAGEVIEW); if (sampler == VK_NULL_HANDLE) sampler = nullSampler_; } diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index 529b3b8071..2459a7cb5a 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -267,8 +267,7 @@ private: VkImageView imageView = VK_NULL_HANDLE; VkSampler sampler = VK_NULL_HANDLE; - // Null texture - VulkanTexture *nullTexture_ = nullptr; + // For null texture VkSampler nullSampler_ = VK_NULL_HANDLE; DrawEngineVulkanStats stats_; diff --git a/ext/native/thin3d/thin3d.h b/ext/native/thin3d/thin3d.h index bc5927392c..323850fe43 100644 --- a/ext/native/thin3d/thin3d.h +++ b/ext/native/thin3d/thin3d.h @@ -258,6 +258,7 @@ enum class NativeObject { BOUND_TEXTURE0_IMAGEVIEW, BOUND_TEXTURE1_IMAGEVIEW, RENDER_MANAGER, + NULL_IMAGEVIEW, }; enum FBColorDepth { diff --git a/ext/native/thin3d/thin3d_vulkan.cpp b/ext/native/thin3d/thin3d_vulkan.cpp index d2fa0e1619..c71ff4f716 100644 --- a/ext/native/thin3d/thin3d_vulkan.cpp +++ b/ext/native/thin3d/thin3d_vulkan.cpp @@ -481,6 +481,8 @@ public: return (uintptr_t)boundImageView_[1]; case NativeObject::RENDER_MANAGER: return (uintptr_t)&renderManager_; + case NativeObject::NULL_IMAGEVIEW: + return (uintptr_t)GetNullTexture()->GetImageView(); default: Crash(); return 0; @@ -490,12 +492,15 @@ public: void HandleEvent(Event ev, int width, int height, void *param1, void *param2) override; private: + VulkanTexture *GetNullTexture(); VulkanContext *vulkan_ = nullptr; VulkanRenderManager renderManager_; VulkanDeviceAllocator *allocator_ = nullptr; + VulkanTexture *nullTexture_ = nullptr; + VKPipeline *curPipeline_ = nullptr; VKBuffer *curVBuffers_[4]{}; int curVBufferOffsets_[4]{}; @@ -609,6 +614,32 @@ static inline VkSamplerAddressMode AddressModeToVulkan(Draw::TextureAddressMode } } +VulkanTexture *VKContext::GetNullTexture() { + if (!nullTexture_) { + VkCommandBuffer cmdInit = renderManager_.GetInitCmd(); + nullTexture_ = new VulkanTexture(vulkan_, allocator_); + nullTexture_->SetTag("Null"); + int w = 8; + int h = 8; + nullTexture_->CreateDirect(cmdInit, w, h, 1, VK_FORMAT_A8B8G8R8_UNORM_PACK32, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); + uint32_t bindOffset; + VkBuffer bindBuf; + uint32_t *data = (uint32_t *)push_->Push(w * h * 4, &bindOffset, &bindBuf); + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + // data[y*w + x] = ((x ^ y) & 1) ? 0xFF808080 : 0xFF000000; // gray/black checkerboard + data[y*w + x] = 0; // black + } + } + nullTexture_->UploadMip(cmdInit, 0, w, h, bindBuf, bindOffset, w); + nullTexture_->EndCreate(cmdInit); + } else { + nullTexture_->Touch(); + } + return nullTexture_; +} + class VKSamplerState : public SamplerState { public: VKSamplerState(VulkanContext *vulkan, const SamplerStateDesc &desc) : vulkan_(vulkan) { @@ -791,6 +822,7 @@ VKContext::VKContext(VulkanContext *vulkan, bool splitSubmit) } VKContext::~VKContext() { + delete nullTexture_; allocator_->Destroy(); // We have to delete on queue, so this can free its queued deletions. vulkan_->Delete().QueueCallback([](void *ptr) { @@ -1121,7 +1153,7 @@ void VKContext::UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t offset, void VKContext::BindTextures(int start, int count, Texture **textures) { for (int i = start; i < start + count; i++) { boundTextures_[i] = static_cast(textures[i]); - boundImageView_[i] = boundTextures_[i] ? boundTextures_[i]->GetImageView() : VK_NULL_HANDLE; + boundImageView_[i] = boundTextures_[i] ? boundTextures_[i]->GetImageView() : GetNullTexture()->GetImageView(); } }