Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/PresentationCommon.h
5668 views
1
// Copyright (c) 2012- 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/Common.h"
21
#include "Common/GPU/Shader.h"
22
23
struct CardboardSettings {
24
bool enabled;
25
float leftEyeXPosition;
26
float rightEyeXPosition;
27
float screenYPosition;
28
float screenWidth;
29
float screenHeight;
30
};
31
32
struct PostShaderUniforms {
33
float texelDelta[2]; float pixelDelta[2];
34
float time[4];
35
float timeDelta[4];
36
float setting[4];
37
float video; float pad[3];
38
float vr;
39
// Used on Direct3D9.
40
float gl_HalfPixel[4];
41
};
42
43
struct FRect {
44
float x;
45
float y;
46
float w;
47
float h;
48
};
49
50
struct Bounds; // from geom2d
51
struct DisplayLayoutConfig;
52
53
FRect GetScreenFrame(bool ignoreInsets, float pixelWidth, float pixelHeight);
54
void SetOverrideScreenFrame(const Bounds *bounds);
55
struct DisplayLayoutConfig;
56
void CalculateDisplayOutputRect(const DisplayLayoutConfig &config, FRect *rc, float origW, float origH, const FRect &frame, int rotation);
57
58
namespace Draw {
59
class Buffer;
60
class DrawContext;
61
class Framebuffer;
62
class Pipeline;
63
class SamplerState;
64
class ShaderModule;
65
class Texture;
66
}
67
68
struct ShaderInfo;
69
class TextureCacheCommon;
70
71
enum class OutputFlags {
72
LINEAR = 0x0000,
73
NEAREST = 0x0001,
74
RB_SWIZZLE = 0x0002,
75
BACKBUFFER_FLIPPED = 0x0004, // Viewport/scissor coordinates are y-flipped.
76
POSITION_FLIPPED = 0x0008, // Vertex position in the shader is y-flipped relative to the screen.
77
PILLARBOX = 0x0010, // Squeeze the image horizontally. Used for the DarkStalkers hack.
78
};
79
ENUM_CLASS_BITOPS(OutputFlags);
80
81
class PresentationCommon {
82
public:
83
PresentationCommon(Draw::DrawContext *draw);
84
~PresentationCommon();
85
86
void UpdateDisplaySize(int w, int h) {
87
pixelWidth_ = w;
88
pixelHeight_ = h;
89
}
90
91
// NOTE: Should be un-rotated width/height.
92
void UpdateRenderSize(int rw, int rh) {
93
renderWidth_ = rw;
94
renderHeight_ = rh;
95
}
96
void SetLanguage(ShaderLanguage lang) {
97
lang_ = lang;
98
}
99
100
bool HasPostShader() {
101
return usePostShader_;
102
}
103
104
bool UpdatePostShader(const DisplayLayoutConfig &config);
105
106
void BeginFrame(const DisplayLayoutConfig &config) {
107
if (restorePostShader_) {
108
UpdatePostShader(config);
109
restorePostShader_ = false;
110
}
111
presentedThisFrame_ = false;
112
}
113
bool PresentedThisFrame() const {
114
return presentedThisFrame_;
115
}
116
void NotifyPresent() {
117
// Something else did the present, skipping PresentationCommon.
118
// If you haven't called BindFramebufferAsRenderTarget, you must not set this.
119
presentedThisFrame_ = true;
120
}
121
122
void DeviceLost();
123
void DeviceRestore(Draw::DrawContext *draw);
124
125
void UpdateUniforms(bool hasVideo);
126
void SourceTexture(Draw::Texture *texture, int bufferWidth, int bufferHeight);
127
void SourceFramebuffer(Draw::Framebuffer *fb, int bufferWidth, int bufferHeight);
128
void CopyToOutput(const DisplayLayoutConfig &config, OutputFlags flags, int uvRotation, float u0, float v0, float u1, float v1);
129
130
void CalculateRenderResolution(const DisplayLayoutConfig &config, int *width, int *height, int *scaleFactor, bool *upscaling, bool *ssaa) const;
131
132
protected:
133
void CreateDeviceObjects();
134
void DestroyDeviceObjects();
135
136
void DestroyPostShader();
137
void DestroyStereoShader();
138
139
static void ShowPostShaderError(const std::string &errorString);
140
141
Draw::ShaderModule *CompileShaderModule(ShaderStage stage, ShaderLanguage lang, const std::string &src, std::string *errorString) const;
142
Draw::Pipeline *CreatePipeline(std::vector<Draw::ShaderModule *> shaders, bool postShader, const UniformBufferDesc *uniformDesc) const;
143
bool CompilePostShader(const ShaderInfo *shaderInfo, Draw::Pipeline **outPipeline) const;
144
bool BuildPostShader(const DisplayLayoutConfig &config, const ShaderInfo *shaderInfo, const ShaderInfo *next, Draw::Pipeline **outPipeline);
145
bool AllocateFramebuffer(int w, int h);
146
147
bool BindSource(int binding, bool bindStereo);
148
149
void GetCardboardSettings(const DisplayLayoutConfig &config, CardboardSettings *cardboardSettings) const;
150
void CalculatePostShaderUniforms(int bufferWidth, int bufferHeight, int targetWidth, int targetHeight, const ShaderInfo *shaderInfo, PostShaderUniforms *uniforms) const;
151
152
Draw::DrawContext *draw_;
153
Draw::Pipeline *texColor_ = nullptr;
154
Draw::Pipeline *texColorRBSwizzle_ = nullptr;
155
Draw::SamplerState *samplerNearest_ = nullptr;
156
Draw::SamplerState *samplerLinear_ = nullptr;
157
Draw::Buffer *vdata_ = nullptr;
158
159
std::vector<Draw::Pipeline *> postShaderPipelines_;
160
std::vector<Draw::Framebuffer *> postShaderFramebuffers_;
161
std::vector<ShaderInfo> postShaderInfo_;
162
std::vector<Draw::Framebuffer *> previousFramebuffers_;
163
164
Draw::Pipeline *stereoPipeline_ = nullptr;
165
ShaderInfo *stereoShaderInfo_ = nullptr;
166
167
int previousIndex_ = 0;
168
PostShaderUniforms previousUniforms_{};
169
170
Draw::Texture *srcTexture_ = nullptr;
171
Draw::Framebuffer *srcFramebuffer_ = nullptr;
172
int srcWidth_ = 0;
173
int srcHeight_ = 0;
174
bool hasVideo_ = false;
175
176
int pixelWidth_ = 0;
177
int pixelHeight_ = 0;
178
int renderWidth_ = 0;
179
int renderHeight_ = 0;
180
181
bool usePostShader_ = false;
182
bool restorePostShader_ = false;
183
bool presentedThisFrame_ = false;
184
ShaderLanguage lang_;
185
186
struct PrevFBO {
187
Draw::Framebuffer *fbo;
188
int w;
189
int h;
190
};
191
std::vector<PrevFBO> postShaderFBOUsage_;
192
};
193
194