1 // Copyright (c) 2015- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #pragma once
19 
20 #include <map>
21 
22 #include "Common/Data/Collections/Hashmaps.h"
23 #include "GPU/GPUInterface.h"
24 #include "GPU/GPUState.h"
25 #include "Common/GPU/Vulkan/VulkanContext.h"
26 #include "GPU/Vulkan/TextureScalerVulkan.h"
27 #include "GPU/Common/TextureCacheCommon.h"
28 #include "GPU/Vulkan/VulkanUtil.h"
29 
30 struct VirtualFramebuffer;
31 class FramebufferManagerVulkan;
32 class DepalShaderCacheVulkan;
33 class ShaderManagerVulkan;
34 class DrawEngineVulkan;
35 
36 class VulkanContext;
37 class VulkanTexture;
38 class VulkanPushBuffer;
39 class VulkanDeviceAllocator;
40 
41 class SamplerCache {
42 public:
SamplerCache(VulkanContext * vulkan)43 	SamplerCache(VulkanContext *vulkan) : vulkan_(vulkan), cache_(16) {}
44 	~SamplerCache();
45 	VkSampler GetOrCreateSampler(const SamplerCacheKey &key);
46 
47 	void DeviceLost();
48 	void DeviceRestore(VulkanContext *vulkan);
49 
50 	std::vector<std::string> DebugGetSamplerIDs() const;
51 	std::string DebugGetSamplerString(std::string id, DebugShaderStringType stringType);
52 
53 private:
54 	VulkanContext *vulkan_;
55 	DenseHashMap<SamplerCacheKey, VkSampler, (VkSampler)VK_NULL_HANDLE> cache_;
56 };
57 
58 class Vulkan2D;
59 
60 class TextureCacheVulkan : public TextureCacheCommon {
61 public:
62 	TextureCacheVulkan(Draw::DrawContext *draw, VulkanContext *vulkan);
63 	~TextureCacheVulkan();
64 
65 	void StartFrame();
66 	void EndFrame();
67 
68 	void DeviceLost();
69 	void DeviceRestore(VulkanContext *vulkan, Draw::DrawContext *draw);
70 
71 	void SetFramebufferManager(FramebufferManagerVulkan *fbManager);
SetDepalShaderCache(DepalShaderCacheVulkan * dpCache)72 	void SetDepalShaderCache(DepalShaderCacheVulkan *dpCache) {
73 		depalShaderCache_ = dpCache;
74 	}
SetShaderManager(ShaderManagerVulkan * sm)75 	void SetShaderManager(ShaderManagerVulkan *sm) {
76 		shaderManagerVulkan_ = sm;
77 	}
SetDrawEngine(DrawEngineVulkan * td)78 	void SetDrawEngine(DrawEngineVulkan *td) {
79 		drawEngine_ = td;
80 	}
81 	void SetVulkan2D(Vulkan2D *vk2d);
SetPushBuffer(VulkanPushBuffer * push)82 	void SetPushBuffer(VulkanPushBuffer *push) {
83 		push_ = push;
84 	}
85 
ForgetLastTexture()86 	void ForgetLastTexture() override {}
InvalidateLastTexture()87 	void InvalidateLastTexture() override {}
88 
89 	void NotifyConfigChanged() override;
90 
GetVulkanHandles(VkImageView & imageView,VkSampler & sampler)91 	void GetVulkanHandles(VkImageView &imageView, VkSampler &sampler) {
92 		imageView = imageView_;
93 		sampler = curSampler_;
94 	}
95 
96 	bool GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level) override;
97 
98 	void GetStats(char *ptr, size_t size);
99 
GetAllocator()100 	VulkanDeviceAllocator *GetAllocator() { return allocator_; }
101 
102 	std::vector<std::string> DebugGetSamplerIDs() const;
103 	std::string DebugGetSamplerString(std::string id, DebugShaderStringType stringType);
104 
105 protected:
106 	void BindTexture(TexCacheEntry *entry) override;
107 	void Unbind() override;
108 	void ReleaseTexture(TexCacheEntry *entry, bool delete_them) override;
109 
110 private:
111 	void LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePtr, int rowPitch,  int level, int scaleFactor, VkFormat dstFmt);
112 	VkFormat GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const;
113 	static TexCacheEntry::TexStatus CheckAlpha(const u32 *pixelData, VkFormat dstFmt, int stride, int w, int h);
114 	void UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) override;
115 
116 	void ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, FramebufferNotificationChannel channel) override;
117 	void BuildTexture(TexCacheEntry *const entry) override;
118 
119 	void CompileScalingShader();
120 
121 	VulkanContext *vulkan_ = nullptr;
122 	VulkanDeviceAllocator *allocator_ = nullptr;
123 	VulkanPushBuffer *push_ = nullptr;
124 
125 	VulkanComputeShaderManager computeShaderManager_;
126 
127 	SamplerCache samplerCache_;
128 
129 	TextureScalerVulkan scaler;
130 
131 	FramebufferManagerVulkan *framebufferManagerVulkan_;
132 	DepalShaderCacheVulkan *depalShaderCache_;
133 	ShaderManagerVulkan *shaderManagerVulkan_;
134 	DrawEngineVulkan *drawEngine_;
135 	Vulkan2D *vulkan2D_;
136 
137 	std::string textureShader_;
138 	int maxScaleFactor_ = 255;
139 	VkShaderModule uploadCS_ = VK_NULL_HANDLE;
140 	VkShaderModule copyCS_ = VK_NULL_HANDLE;
141 
142 	// Bound state to emulate an API similar to the others
143 	VkImageView imageView_ = VK_NULL_HANDLE;
144 	VkSampler curSampler_ = VK_NULL_HANDLE;
145 
146 	VkSampler samplerNearest_ = VK_NULL_HANDLE;
147 };
148 
149 VkFormat getClutDestFormatVulkan(GEPaletteFormat format);
150