1
// Copyright (c) 2012- 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 "base/basictypes.h"
24
#include "GPU/Common/ShaderCommon.h"
25
#include "GPU/Common/ShaderId.h"
26
#include "GPU/GLES/VertexShaderGenerator.h"
27
#include "GPU/GLES/FragmentShaderGenerator.h"
31
// Pre-fetched attrs and uniforms
46
LinkedShader(ShaderID VSID, Shader *vs, ShaderID FSID, Shader *fs, bool useHWTransform);
49
void use(const ShaderID &VSID, LinkedShader *previous);
51
void UpdateUniforms(u32 vertType, const ShaderID &VSID);
54
// Set to false if the VS failed, happens on Mali-400 a lot for complex shaders.
58
u32 availableUniforms;
61
// Present attributes in the shader.
62
int attrMask; // 1 << ATTR_ ... or-ed together.
64
int u_stencilReplaceValue;
72
int u_depthRange; // x,y = viewport xscale/xcenter. z,w=clipping minz/maxz (?)
75
int u_bone; // array, size is numBones
87
// Fragment processing inputs
101
int u_matambientalpha;
107
int u_lightatt[4]; // attenuation
108
int u_lightangle[4]; // spotlight cone angle (cosine)
109
int u_lightspotCoef[4]; // spotlight dropoff
110
int u_lightdiffuse[4]; // each light consist of vec4[3]
111
int u_lightspecular[4]; // attenuation
112
int u_lightambient[4]; // attenuation
116
DIRTY_PROJMATRIX = (1 << 0),
117
DIRTY_PROJTHROUGHMATRIX = (1 << 1),
118
DIRTY_FOGCOLOR = (1 << 2),
119
DIRTY_FOGCOEF = (1 << 3),
120
DIRTY_TEXENV = (1 << 4),
121
DIRTY_ALPHACOLORREF = (1 << 5),
123
// 1 << 6 is free! Wait, not anymore...
124
DIRTY_STENCILREPLACEVALUE = (1 << 6),
126
DIRTY_ALPHACOLORMASK = (1 << 7),
127
DIRTY_LIGHT0 = (1 << 8),
128
DIRTY_LIGHT1 = (1 << 9),
129
DIRTY_LIGHT2 = (1 << 10),
130
DIRTY_LIGHT3 = (1 << 11),
132
DIRTY_MATDIFFUSE = (1 << 12),
133
DIRTY_MATSPECULAR = (1 << 13),
134
DIRTY_MATEMISSIVE = (1 << 14),
135
DIRTY_AMBIENT = (1 << 15),
136
DIRTY_MATAMBIENTALPHA = (1 << 16),
138
DIRTY_SHADERBLEND = (1 << 17), // Used only for in-shader blending.
140
DIRTY_UVSCALEOFFSET = (1 << 18), // this will be dirtied ALL THE TIME... maybe we'll need to do "last value with this shader compares"
142
// Texclamp is fairly rare so let's share it's bit with DIRTY_DEPTHRANGE.
143
DIRTY_TEXCLAMP = (1 << 19),
144
DIRTY_DEPTHRANGE = (1 << 19),
146
DIRTY_WORLDMATRIX = (1 << 21),
147
DIRTY_VIEWMATRIX = (1 << 22), // Maybe we'll fold this into projmatrix eventually
148
DIRTY_TEXMATRIX = (1 << 23),
149
DIRTY_BONEMATRIX0 = (1 << 24),
150
DIRTY_BONEMATRIX1 = (1 << 25),
151
DIRTY_BONEMATRIX2 = (1 << 26),
152
DIRTY_BONEMATRIX3 = (1 << 27),
153
DIRTY_BONEMATRIX4 = (1 << 28),
154
DIRTY_BONEMATRIX5 = (1 << 29),
155
DIRTY_BONEMATRIX6 = (1 << 30),
156
DIRTY_BONEMATRIX7 = (1 << 31),
158
DIRTY_ALL = 0xFFFFFFFF
161
// Real public interface
165
Shader(const char *code, uint32_t glShaderType, bool useHWTransform);
169
bool Failed() const { return failed_; }
170
bool UseHWTransform() const { return useHWTransform_; } // only relevant for vtx shaders
172
std::string GetShaderString(DebugShaderStringType type, ShaderID id) const;
177
bool useHWTransform_;
181
class ShaderManager {
186
void ClearCache(bool deleteThem); // TODO: deleteThem currently not respected
188
// This is the old ApplyShader split into two parts, because of annoying information dependencies.
189
// If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
190
Shader *ApplyVertexShader(int prim, u32 vertType, ShaderID *VSID);
191
LinkedShader *ApplyFragmentShader(ShaderID VSID, Shader *vs, u32 vertType, int prim);
194
void DirtyUniform(u32 what) {
195
globalDirty_ |= what;
197
void DirtyLastShader(); // disables vertex arrays
199
int NumVertexShaders() const { return (int)vsCache_.size(); }
200
int NumFragmentShaders() const { return (int)fsCache_.size(); }
201
int NumPrograms() const { return (int)linkedShaderCache_.size(); }
203
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
204
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
206
void LoadAndPrecompile(const std::string &filename);
207
void Save(const std::string &filename);
211
Shader *CompileFragmentShader(ShaderID id);
212
Shader *CompileVertexShader(ShaderID id);
214
struct LinkedShaderCacheEntry {
215
LinkedShaderCacheEntry(Shader *vs_, Shader *fs_, LinkedShader *ls_)
216
: vs(vs_), fs(fs_), ls(ls_) { }
222
typedef std::vector<LinkedShaderCacheEntry> LinkedShaderCache;
224
LinkedShaderCache linkedShaderCache_;
226
bool lastVShaderSame_;
231
LinkedShader *lastShader_;
233
u32 shaderSwitchDirty_;
236
typedef std::map<ShaderID, Shader *> FSCache;
239
typedef std::map<ShaderID, Shader *> VSCache;
242
bool diskCacheDirty_;