~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to GPU/Vulkan/FramebufferVulkan.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "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"
 
25
 
 
26
// TODO: Remove?
 
27
enum VulkanFBOColorDepth {
 
28
        VK_FBO_8888,
 
29
        VK_FBO_565,
 
30
        VK_FBO_4444,
 
31
        VK_FBO_5551,
 
32
};
 
33
 
 
34
class TextureCacheVulkan;
 
35
class DrawEngineVulkan;
 
36
class VulkanContext;
 
37
class ShaderManagerVulkan;
 
38
class VulkanTexture;
 
39
class VulkanPushBuffer;
 
40
 
 
41
struct PostShaderUniforms {
 
42
        float texelDelta[2]; float pad[2];
 
43
        float pixelDelta[2]; float pad0[2];
 
44
        float time[4];
 
45
};
 
46
 
 
47
static const char *ub_post_shader =
 
48
R"(     vec2 texelDelta;
 
49
        vec2 pixelDelta;
 
50
        vec4 time;
 
51
)";
 
52
 
 
53
// Simple struct for asynchronous PBO readbacks
 
54
// TODO: Probably will need a complete redesign.
 
55
struct AsyncPBOVulkan {
 
56
        //  handle;
 
57
        u32 maxSize;
 
58
 
 
59
        u32 fb_address;
 
60
        u32 stride;
 
61
        u32 height;
 
62
        u32 size;
 
63
        GEBufferFormat format;
 
64
        bool reading;
 
65
};
 
66
 
 
67
struct CardboardSettings {
 
68
        bool enabled;
 
69
        float leftEyeXPosition;
 
70
        float rightEyeXPosition;
 
71
        float screenYPosition;
 
72
        float screenWidth;
 
73
        float screenHeight;
 
74
};
 
75
 
 
76
class FramebufferManagerVulkan : public FramebufferManagerCommon {
 
77
public:
 
78
        FramebufferManagerVulkan(VulkanContext *vulkan);
 
79
        ~FramebufferManagerVulkan();
 
80
 
 
81
        void SetTextureCache(TextureCacheVulkan *tc) {
 
82
                textureCache_ = tc;
 
83
        }
 
84
        void SetShaderManager(ShaderManagerVulkan *sm) {
 
85
                shaderManager_ = sm;
 
86
        }
 
87
        void SetDrawEngine(DrawEngineVulkan *td) {
 
88
                drawEngine_ = td;
 
89
        }
 
90
 
 
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;
 
93
 
 
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);
 
97
 
 
98
        void DestroyAllFBOs(bool forceDelete);
 
99
 
 
100
        virtual void Init() override;
 
101
 
 
102
        void BeginFrameVulkan();  // there's a BeginFrame in the base class, which this calls
 
103
        void EndFrame();
 
104
 
 
105
        void Resized();
 
106
        void DeviceLost();
 
107
        void CopyDisplayToOutput();
 
108
        int GetLineWidth();
 
109
        void ReformatFramebufferFrom(VirtualFramebuffer *vfb, GEBufferFormat old);
 
110
 
 
111
        void BlitFramebufferDepth(VirtualFramebuffer *src, VirtualFramebuffer *dst);
 
112
 
 
113
        // For use when texturing from a framebuffer.  May create a duplicate if target.
 
114
        VulkanTexture *GetFramebufferColor(u32 fbRawAddress, VirtualFramebuffer *framebuffer, int flags);
 
115
 
 
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;
 
119
 
 
120
        std::vector<FramebufferInfo> GetFramebufferList();
 
121
 
 
122
        bool NotifyStencilUpload(u32 addr, int size, bool skipZero = false) override;
 
123
 
 
124
        void DestroyFramebuf(VirtualFramebuffer *vfb) override;
 
125
        void ResizeFramebufFBO(VirtualFramebuffer *vfb, u16 w, u16 h, bool force = false, bool skipCopy = false) override;
 
126
 
 
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);
 
131
 
 
132
        virtual void RebindFramebuffer() override;
 
133
 
 
134
        // VulkanFBO *GetTempFBO(u16 w, u16 h, VulkanFBOColorDepth depth = VK_FBO_8888);
 
135
 
 
136
        // Cardboard Settings Calculator
 
137
        struct CardboardSettings * GetCardboardSettings(struct CardboardSettings * cardboardSettings);
 
138
 
 
139
        // Pass management
 
140
        // void BeginPassClear()
 
141
 
 
142
        // If within a render pass, this will just issue a regular clear. If beginning a new render pass,
 
143
        // do that.
 
144
        void NotifyClear(bool clearColor, bool clearAlpha, bool clearDepth, uint32_t color, float depth);
 
145
        void NotifyDraw() {
 
146
                DoNotifyDraw();
 
147
        }
 
148
 
 
149
protected:
 
150
        void DisableState() override {}
 
151
        void ClearBuffer(bool keepState = false) override;
 
152
        void FlushBeforeCopy() override;
 
153
        void DecimateFBOs() override;
 
154
 
 
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;
 
157
 
 
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;
 
163
 
 
164
 
 
165
private:
 
166
 
 
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);
 
169
        void DoNotifyDraw();
 
170
 
 
171
        VkCommandBuffer AllocFrameCommandBuffer();
 
172
        void UpdatePostShaderUniforms(int bufferWidth, int bufferHeight, int renderWidth, int renderHeight);
 
173
 
 
174
        void PackFramebufferAsync_(VirtualFramebuffer *vfb);
 
175
        void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h);
 
176
 
 
177
        VulkanContext *vulkan_;
 
178
 
 
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_;
 
184
 
 
185
        // Used by DrawPixels
 
186
        VulkanTexture *drawPixelsTex_;
 
187
        GEBufferFormat drawPixelsTexFormat_;
 
188
 
 
189
        u8 *convBuf_;
 
190
        u32 convBufSize_;
 
191
 
 
192
        TextureCacheVulkan *textureCache_;
 
193
        ShaderManagerVulkan *shaderManager_;
 
194
        DrawEngineVulkan *drawEngine_;
 
195
 
 
196
        bool resized_;
 
197
 
 
198
        AsyncPBOVulkan *pixelBufObj_;
 
199
        int currentPBO_;
 
200
 
 
201
        enum {
 
202
                MAX_COMMAND_BUFFERS = 32,
 
203
        };
 
204
 
 
205
        struct FrameData {
 
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_;
 
212
        };
 
213
 
 
214
        FrameData frameData_[2];
 
215
        int curFrame_;
 
216
 
 
217
        // This gets copied to the current frame's push buffer as needed.
 
218
        PostShaderUniforms postUniforms_;
 
219
 
 
220
        // Renderpasses, all combination of preserving or clearing fb contents
 
221
        VkRenderPass rpLoadColorLoadDepth_;
 
222
        VkRenderPass rpClearColorLoadDepth_;
 
223
        VkRenderPass rpLoadColorClearDepth_;
 
224
        VkRenderPass rpClearColorClearDepth_;
 
225
 
 
226
        VkPipelineCache pipelineCache2D_;
 
227
 
 
228
        // Basic shaders
 
229
        VkShaderModule fsBasicTex_;
 
230
        VkShaderModule vsBasicTex_;
 
231
        VkPipeline pipelineBasicTex_;
 
232
 
 
233
        // Postprocessing
 
234
        VkPipeline pipelinePostShader_;
 
235
 
 
236
        VkSampler linearSampler_;
 
237
        VkSampler nearestSampler_;
 
238
 
 
239
        // Simple 2D drawing engine.
 
240
        Vulkan2D vulkan2D_;
 
241
};