Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/ShaderManagerGLES.h
3187 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 <vector>
21
22
#include "Common/Data/Collections/Hashmaps.h"
23
#include "Common/GPU/OpenGL/GLRenderManager.h"
24
#include "GPU/Common/ShaderCommon.h"
25
#include "GPU/Common/ShaderId.h"
26
#include "GPU/Common/VertexShaderGenerator.h"
27
#include "GPU/Common/FragmentShaderGenerator.h"
28
29
class DrawEngineGLES;
30
class Shader;
31
class Path;
32
struct ShaderLanguageDesc;
33
34
namespace File {
35
class IOFile;
36
}
37
38
class LinkedShader {
39
public:
40
LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, FShaderID FSID, Shader *fs, bool useHWTransform, bool preloading = false);
41
~LinkedShader();
42
43
void use(const ShaderID &VSID) const;
44
void UpdateUniforms(const ShaderID &VSID, bool useBufferedRendering, const ShaderLanguageDesc &shaderLanguage);
45
void Delete();
46
47
GLRenderManager *render_;
48
Shader *vs_;
49
// Set to false if the VS failed, happens on Mali-400 a lot for complex shaders.
50
bool useHWTransform_;
51
52
GLRProgram *program;
53
uint64_t availableUniforms;
54
uint64_t dirtyUniforms = 0;
55
56
// Present attributes in the shader.
57
int attrMask; // 1 << ATTR_ ... or-ed together.
58
59
int u_stencilReplaceValue;
60
int u_tex;
61
int u_proj;
62
int u_proj_lens;
63
int u_proj_through;
64
int u_texenv;
65
int u_view;
66
int u_texmtx;
67
int u_world;
68
int u_depthRange; // x,y = viewport xscale/xcenter. z,w=clipping minz/maxz (?)
69
int u_cullRangeMin;
70
int u_cullRangeMax;
71
int u_rotation;
72
int u_mipBias;
73
int u_scaleX;
74
int u_scaleY;
75
76
#ifdef USE_BONE_ARRAY
77
int u_bone; // array, size is numBones
78
#else
79
int u_bone[8];
80
#endif
81
int numBones;
82
83
// Shader blending.
84
int u_fbotex;
85
int u_blendFixA;
86
int u_blendFixB;
87
int u_fbotexSize;
88
89
// Shader depal
90
int u_pal; // the texture
91
int u_depal_mask_shift_off_fmt; // the params
92
93
// Fragment processing inputs
94
int u_alphacolorref;
95
int u_alphacolormask;
96
int u_colorWriteMask;
97
int u_testtex;
98
int u_fogcolor;
99
int u_fogcoef;
100
101
// Texturing
102
int u_uvscaleoffset;
103
int u_texclamp;
104
int u_texclampoff;
105
int u_texNoAlphaMul;
106
107
// Lighting
108
int u_lightControl;
109
int u_ambient;
110
int u_matambientalpha;
111
int u_matdiffuse;
112
int u_matspecular;
113
int u_matemissive;
114
int u_lightpos[4];
115
int u_lightdir[4];
116
int u_lightatt[4]; // attenuation
117
int u_lightangle_spotCoef[4]; // spotlight cone angle (cosine) (x), spotlight dropoff (y)
118
int u_lightdiffuse[4]; // each light consist of vec4[3]
119
int u_lightspecular[4]; // attenuation
120
int u_lightambient[4]; // attenuation
121
122
// Spline Tessellation
123
int u_tess_points; // Control Points
124
int u_tess_weights_u;
125
int u_tess_weights_v;
126
int u_spline_counts;
127
};
128
129
// Real public interface
130
131
struct ShaderDescGLES {
132
uint32_t glShaderType;
133
uint32_t attrMask;
134
uint64_t uniformMask;
135
bool useHWTransform;
136
};
137
138
class Shader {
139
public:
140
Shader(GLRenderManager *render, const char *code, const std::string &desc, const ShaderDescGLES &params);
141
~Shader();
142
GLRShader *shader;
143
144
bool UseHWTransform() const { return useHWTransform_; } // only relevant for vtx shaders
145
146
std::string GetShaderString(DebugShaderStringType type, ShaderID id) const;
147
148
uint32_t GetAttrMask() const { return attrMask_; }
149
uint64_t GetUniformMask() const { return uniformMask_; }
150
151
private:
152
GLRenderManager *render_;
153
std::string source_;
154
bool useHWTransform_;
155
bool isFragment_;
156
uint32_t attrMask_; // only used in vertex shaders
157
uint64_t uniformMask_;
158
};
159
160
class VertexDecoder;
161
162
class ShaderManagerGLES : public ShaderManagerCommon {
163
public:
164
ShaderManagerGLES(Draw::DrawContext *draw);
165
~ShaderManagerGLES();
166
167
void ClearShaders() override;
168
169
// This is the old ApplyShader split into two parts, because of annoying information dependencies.
170
// If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
171
Shader *ApplyVertexShader(bool useHWTransform, bool useHWTessellation, u32 vertexType, bool weightsAsFloat, bool useSkinInDecode, VShaderID *VSID);
172
LinkedShader *ApplyFragmentShader(VShaderID VSID, Shader *vs, const ComputedPipelineState &pipelineState, bool useBufferedRendering);
173
174
void DeviceLost() override;
175
void DeviceRestore(Draw::DrawContext *draw) override;
176
177
void DirtyLastShader() override;
178
179
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
180
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
181
int GetNumPrograms() const { return (int)linkedShaderCache_.size(); }
182
183
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type) override;
184
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) override;
185
186
static bool LoadCacheFlags(File::IOFile &f, DrawEngineGLES *drawEngine);
187
bool LoadCache(File::IOFile &f);
188
void SaveCache(const Path &filename, DrawEngineGLES *drawEngine);
189
190
private:
191
void Clear();
192
Shader *CompileFragmentShader(FShaderID id);
193
Shader *CompileVertexShader(VShaderID id);
194
195
struct LinkedShaderCacheEntry {
196
LinkedShaderCacheEntry(Shader *vs_, Shader *fs_, LinkedShader *ls_)
197
: vs(vs_), fs(fs_), ls(ls_) { }
198
199
Shader *vs;
200
Shader *fs;
201
LinkedShader *ls;
202
};
203
typedef std::vector<LinkedShaderCacheEntry> LinkedShaderCache;
204
205
GLRenderManager *render_;
206
LinkedShaderCache linkedShaderCache_;
207
208
bool lastVShaderSame_ = false;
209
210
FShaderID lastFSID_;
211
VShaderID lastVSID_;
212
213
LinkedShader *lastShader_ = nullptr;
214
u64 shaderSwitchDirtyUniforms_ = 0;
215
char *codeBuffer_;
216
217
typedef DenseHashMap<FShaderID, Shader *> FSCache;
218
FSCache fsCache_;
219
220
typedef DenseHashMap<VShaderID, Shader *> VSCache;
221
VSCache vsCache_;
222
};
223
224