1
// Copyright (c) 2015- PPSSPP Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
20
#include "Common/Vulkan/VulkanLoader.h"
21
#include "GPU/Common/FramebufferCommon.h"
22
#include "GPU/GPUInterface.h"
23
#include "GPU/Common/GPUDebugInterface.h"
24
#include "GPU/Vulkan/VulkanUtil.h"
27
enum VulkanFBOColorDepth {
34
class TextureCacheVulkan;
35
class DrawEngineVulkan;
37
class ShaderManagerVulkan;
39
class VulkanPushBuffer;
41
struct PostShaderUniforms {
42
float texelDelta[2]; float pad[2];
43
float pixelDelta[2]; float pad0[2];
47
static const char *ub_post_shader =
53
// Simple struct for asynchronous PBO readbacks
54
// TODO: Probably will need a complete redesign.
55
struct AsyncPBOVulkan {
63
GEBufferFormat format;
67
struct CardboardSettings {
69
float leftEyeXPosition;
70
float rightEyeXPosition;
71
float screenYPosition;
76
class FramebufferManagerVulkan : public FramebufferManagerCommon {
78
FramebufferManagerVulkan(VulkanContext *vulkan);
79
~FramebufferManagerVulkan();
81
void SetTextureCache(TextureCacheVulkan *tc) {
84
void SetShaderManager(ShaderManagerVulkan *sm) {
87
void SetDrawEngine(DrawEngineVulkan *td) {
91
void DrawPixels(VirtualFramebuffer *vfb, int dstX, int dstY, const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height) override;
92
void DrawFramebufferToOutput(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, bool applyPostShader) override;
94
// If texture != 0, will bind it.
95
// x,y,w,h are relative to destW, destH which fill out the target completely.
96
void DrawTexture(VulkanTexture *texture, float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, VkPipeline pipeline, int uvRotation);
98
void DestroyAllFBOs(bool forceDelete);
100
virtual void Init() override;
102
void BeginFrameVulkan(); // there's a BeginFrame in the base class, which this calls
107
void CopyDisplayToOutput();
109
void ReformatFramebufferFrom(VirtualFramebuffer *vfb, GEBufferFormat old);
111
void BlitFramebufferDepth(VirtualFramebuffer *src, VirtualFramebuffer *dst);
113
// For use when texturing from a framebuffer. May create a duplicate if target.
114
VulkanTexture *GetFramebufferColor(u32 fbRawAddress, VirtualFramebuffer *framebuffer, int flags);
116
// Reads a rectangular subregion of a framebuffer to the right position in its backing memory.
117
void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) override;
118
void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) override;
120
std::vector<FramebufferInfo> GetFramebufferList();
122
bool NotifyStencilUpload(u32 addr, int size, bool skipZero = false) override;
124
void DestroyFramebuf(VirtualFramebuffer *vfb) override;
125
void ResizeFramebufFBO(VirtualFramebuffer *vfb, u16 w, u16 h, bool force = false, bool skipCopy = false) override;
127
bool GetFramebuffer(u32 fb_address, int fb_stride, GEBufferFormat format, GPUDebugBuffer &buffer);
128
bool GetDepthbuffer(u32 fb_address, int fb_stride, u32 z_address, int z_stride, GPUDebugBuffer &buffer);
129
bool GetStencilbuffer(u32 fb_address, int fb_stride, GPUDebugBuffer &buffer);
130
static bool GetDisplayFramebuffer(GPUDebugBuffer &buffer);
132
virtual void RebindFramebuffer() override;
134
// VulkanFBO *GetTempFBO(u16 w, u16 h, VulkanFBOColorDepth depth = VK_FBO_8888);
136
// Cardboard Settings Calculator
137
struct CardboardSettings * GetCardboardSettings(struct CardboardSettings * cardboardSettings);
140
// void BeginPassClear()
142
// If within a render pass, this will just issue a regular clear. If beginning a new render pass,
144
void NotifyClear(bool clearColor, bool clearAlpha, bool clearDepth, uint32_t color, float depth);
150
void DisableState() override {}
151
void ClearBuffer(bool keepState = false) override;
152
void FlushBeforeCopy() override;
153
void DecimateFBOs() override;
155
// Used by ReadFramebufferToMemory and later framebuffer block copies
156
void BlitFramebuffer(VirtualFramebuffer *dst, int dstX, int dstY, VirtualFramebuffer *src, int srcX, int srcY, int w, int h, int bpp) override;
158
void NotifyRenderFramebufferCreated(VirtualFramebuffer *vfb) override;
159
void NotifyRenderFramebufferSwitched(VirtualFramebuffer *prevVfb, VirtualFramebuffer *vfb, bool isClearingDepth) override;
160
void NotifyRenderFramebufferUpdated(VirtualFramebuffer *vfb, bool vfbFormatChanged) override;
161
bool CreateDownloadTempBuffer(VirtualFramebuffer *nvfb) override;
162
void UpdateDownloadTempBuffer(VirtualFramebuffer *nvfb) override;
167
// The returned texture does not need to be free'd, might be returned from a pool (currently single entry)
168
VulkanTexture *MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height);
171
VkCommandBuffer AllocFrameCommandBuffer();
172
void UpdatePostShaderUniforms(int bufferWidth, int bufferHeight, int renderWidth, int renderHeight);
174
void PackFramebufferAsync_(VirtualFramebuffer *vfb);
175
void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h);
177
VulkanContext *vulkan_;
179
// The command buffer of the current framebuffer pass being rendered to.
180
// One framebuffer can be used as a texturing source at multiple times in a frame,
181
// but then the contents have to be copied out into a new texture every time.
182
VkCommandBuffer curCmd_;
183
VkCommandBuffer cmdInit_;
185
// Used by DrawPixels
186
VulkanTexture *drawPixelsTex_;
187
GEBufferFormat drawPixelsTexFormat_;
192
TextureCacheVulkan *textureCache_;
193
ShaderManagerVulkan *shaderManager_;
194
DrawEngineVulkan *drawEngine_;
198
AsyncPBOVulkan *pixelBufObj_;
202
MAX_COMMAND_BUFFERS = 32,
206
VkCommandPool cmdPool_;
207
// Keep track of command buffers we allocated so we can reset or free them at an appropriate point.
208
VkCommandBuffer commandBuffers_[MAX_COMMAND_BUFFERS];
209
VulkanPushBuffer *push_;
210
int numCommandBuffers_;
211
int totalCommandBuffers_;
214
FrameData frameData_[2];
217
// This gets copied to the current frame's push buffer as needed.
218
PostShaderUniforms postUniforms_;
220
// Renderpasses, all combination of preserving or clearing fb contents
221
VkRenderPass rpLoadColorLoadDepth_;
222
VkRenderPass rpClearColorLoadDepth_;
223
VkRenderPass rpLoadColorClearDepth_;
224
VkRenderPass rpClearColorClearDepth_;
226
VkPipelineCache pipelineCache2D_;
229
VkShaderModule fsBasicTex_;
230
VkShaderModule vsBasicTex_;
231
VkPipeline pipelineBasicTex_;
234
VkPipeline pipelinePostShader_;
236
VkSampler linearSampler_;
237
VkSampler nearestSampler_;
239
// Simple 2D drawing engine.