CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/ShaderManagerGLES.h
Views: 1401
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 "Common/File/Path.h"
25
#include "GPU/Common/ShaderCommon.h"
26
#include "GPU/Common/ShaderId.h"
27
#include "GPU/Common/VertexShaderGenerator.h"
28
#include "GPU/Common/FragmentShaderGenerator.h"
29
30
class DrawEngineGLES;
31
class Shader;
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 ShaderManagerGLES : public ShaderManagerCommon {
161
public:
162
ShaderManagerGLES(Draw::DrawContext *draw);
163
~ShaderManagerGLES();
164
165
void ClearShaders() override;
166
167
// This is the old ApplyShader split into two parts, because of annoying information dependencies.
168
// If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
169
Shader *ApplyVertexShader(bool useHWTransform, bool useHWTessellation, VertexDecoder *vertexDecoder, bool weightsAsFloat, bool useSkinInDecode, VShaderID *VSID);
170
LinkedShader *ApplyFragmentShader(VShaderID VSID, Shader *vs, const ComputedPipelineState &pipelineState, bool useBufferedRendering);
171
172
void DeviceLost() override;
173
void DeviceRestore(Draw::DrawContext *draw) override;
174
175
void DirtyLastShader() override;
176
177
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
178
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
179
int GetNumPrograms() const { return (int)linkedShaderCache_.size(); }
180
181
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type) override;
182
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) override;
183
184
static bool LoadCacheFlags(File::IOFile &f, DrawEngineGLES *drawEngine);
185
bool LoadCache(File::IOFile &f);
186
void SaveCache(const Path &filename, DrawEngineGLES *drawEngine);
187
188
private:
189
void Clear();
190
Shader *CompileFragmentShader(FShaderID id);
191
Shader *CompileVertexShader(VShaderID id);
192
193
struct LinkedShaderCacheEntry {
194
LinkedShaderCacheEntry(Shader *vs_, Shader *fs_, LinkedShader *ls_)
195
: vs(vs_), fs(fs_), ls(ls_) { }
196
197
Shader *vs;
198
Shader *fs;
199
LinkedShader *ls;
200
};
201
typedef std::vector<LinkedShaderCacheEntry> LinkedShaderCache;
202
203
GLRenderManager *render_;
204
LinkedShaderCache linkedShaderCache_;
205
206
bool lastVShaderSame_ = false;
207
208
FShaderID lastFSID_;
209
VShaderID lastVSID_;
210
211
LinkedShader *lastShader_ = nullptr;
212
u64 shaderSwitchDirtyUniforms_ = 0;
213
char *codeBuffer_;
214
215
typedef DenseHashMap<FShaderID, Shader *> FSCache;
216
FSCache fsCache_;
217
218
typedef DenseHashMap<VShaderID, Shader *> VSCache;
219
VSCache vsCache_;
220
};
221
222