2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-08-31 15:13:18 +00:00
|
|
|
#include <vector>
|
2015-10-14 15:45:21 +00:00
|
|
|
|
2020-10-05 18:58:33 +00:00
|
|
|
#include "Common/Data/Collections/Hashmaps.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/OpenGL/GLRenderManager.h"
|
2021-05-05 23:31:38 +00:00
|
|
|
#include "Common/File/Path.h"
|
2015-10-14 15:45:21 +00:00
|
|
|
#include "GPU/Common/ShaderCommon.h"
|
2015-10-24 21:24:06 +00:00
|
|
|
#include "GPU/Common/ShaderId.h"
|
2020-11-01 12:02:53 +00:00
|
|
|
#include "GPU/Common/VertexShaderGenerator.h"
|
2020-10-31 17:56:44 +00:00
|
|
|
#include "GPU/Common/FragmentShaderGenerator.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-21 15:49:42 +00:00
|
|
|
class Shader;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-08 15:18:59 +00:00
|
|
|
class LinkedShader {
|
2012-12-21 15:49:42 +00:00
|
|
|
public:
|
2017-11-18 23:43:58 +00:00
|
|
|
LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, FShaderID FSID, Shader *fs, bool useHWTransform, bool preloading = false);
|
2012-11-01 15:19:01 +00:00
|
|
|
~LinkedShader();
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
void use(const ShaderID &VSID);
|
2020-04-04 18:03:07 +00:00
|
|
|
void UpdateUniforms(u32 vertType, const ShaderID &VSID, bool useBufferedRendering);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
GLRenderManager *render_;
|
2014-03-24 09:55:07 +00:00
|
|
|
Shader *vs_;
|
2013-06-06 08:05:31 +00:00
|
|
|
// Set to false if the VS failed, happens on Mali-400 a lot for complex shaders.
|
|
|
|
bool useHWTransform_;
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
GLRProgram *program;
|
2017-01-23 22:15:54 +00:00
|
|
|
uint64_t availableUniforms;
|
2017-12-01 19:17:51 +00:00
|
|
|
uint64_t dirtyUniforms = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-08 15:18:59 +00:00
|
|
|
// Present attributes in the shader.
|
|
|
|
int attrMask; // 1 << ATTR_ ... or-ed together.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-02 23:09:48 +00:00
|
|
|
int u_stencilReplaceValue;
|
2012-11-01 15:19:01 +00:00
|
|
|
int u_tex;
|
|
|
|
int u_proj;
|
2012-11-28 12:45:22 +00:00
|
|
|
int u_proj_through;
|
2012-11-01 15:19:01 +00:00
|
|
|
int u_texenv;
|
2012-12-20 13:10:42 +00:00
|
|
|
int u_view;
|
|
|
|
int u_texmtx;
|
|
|
|
int u_world;
|
2015-08-26 11:26:08 +00:00
|
|
|
int u_depthRange; // x,y = viewport xscale/xcenter. z,w=clipping minz/maxz (?)
|
2018-09-17 05:52:43 +00:00
|
|
|
int u_cullRangeMin;
|
|
|
|
int u_cullRangeMax;
|
2015-08-26 11:26:08 +00:00
|
|
|
|
2018-04-10 10:22:02 +00:00
|
|
|
#ifdef USE_BONE_ARRAY
|
|
|
|
int u_bone; // array, size is numBones
|
|
|
|
#else
|
|
|
|
int u_bone[8];
|
|
|
|
#endif
|
|
|
|
int numBones;
|
|
|
|
|
2014-05-11 16:51:35 +00:00
|
|
|
// Shader blending.
|
|
|
|
int u_fbotex;
|
|
|
|
int u_blendFixA;
|
|
|
|
int u_blendFixB;
|
2014-06-14 05:49:28 +00:00
|
|
|
int u_fbotexSize;
|
2014-05-11 16:51:35 +00:00
|
|
|
|
2018-04-13 18:00:14 +00:00
|
|
|
// Shader depal
|
|
|
|
int u_pal; // the texture
|
2020-10-22 20:36:02 +00:00
|
|
|
int u_depal_mask_shift_off_fmt; // the params
|
2018-04-13 18:00:14 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Fragment processing inputs
|
2012-12-19 17:35:37 +00:00
|
|
|
int u_alphacolorref;
|
2014-06-16 07:33:48 +00:00
|
|
|
int u_alphacolormask;
|
2020-11-08 22:17:06 +00:00
|
|
|
int u_colorWriteMask;
|
2014-08-24 22:26:38 +00:00
|
|
|
int u_testtex;
|
2012-11-01 15:19:01 +00:00
|
|
|
int u_fogcolor;
|
2012-11-28 12:45:22 +00:00
|
|
|
int u_fogcoef;
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Texturing
|
|
|
|
int u_uvscaleoffset;
|
2014-06-07 19:21:52 +00:00
|
|
|
int u_texclamp;
|
2014-06-08 20:52:05 +00:00
|
|
|
int u_texclampoff;
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2012-12-05 03:45:28 +00:00
|
|
|
// Lighting
|
2012-12-20 17:31:21 +00:00
|
|
|
int u_ambient;
|
2012-12-20 13:10:42 +00:00
|
|
|
int u_matambientalpha;
|
2012-12-20 17:31:21 +00:00
|
|
|
int u_matdiffuse;
|
|
|
|
int u_matspecular;
|
|
|
|
int u_matemissive;
|
|
|
|
int u_lightpos[4];
|
|
|
|
int u_lightdir[4];
|
|
|
|
int u_lightatt[4]; // attenuation
|
2018-03-12 10:07:51 +00:00
|
|
|
int u_lightangle_spotCoef[4]; // spotlight cone angle (cosine) (x), spotlight dropoff (y)
|
2012-12-20 17:31:21 +00:00
|
|
|
int u_lightdiffuse[4]; // each light consist of vec4[3]
|
|
|
|
int u_lightspecular[4]; // attenuation
|
|
|
|
int u_lightambient[4]; // attenuation
|
2017-01-08 18:37:21 +00:00
|
|
|
|
2018-06-29 15:21:30 +00:00
|
|
|
// Spline Tessellation
|
|
|
|
int u_tess_points; // Control Points
|
|
|
|
int u_tess_weights_u;
|
|
|
|
int u_tess_weights_v;
|
2018-07-14 00:40:13 +00:00
|
|
|
int u_spline_counts;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Real public interface
|
|
|
|
|
2012-12-21 15:49:42 +00:00
|
|
|
class Shader {
|
|
|
|
public:
|
2018-01-20 16:46:34 +00:00
|
|
|
Shader(GLRenderManager *render, const char *code, const std::string &desc, uint32_t glShaderType, bool useHWTransform, uint32_t attrMask, uint64_t uniformMask);
|
2013-06-06 08:05:31 +00:00
|
|
|
~Shader();
|
2017-11-18 23:43:58 +00:00
|
|
|
GLRShader *shader;
|
2012-12-21 15:49:42 +00:00
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
bool Failed() const { return failed_; }
|
2018-01-10 09:48:46 +00:00
|
|
|
bool UseHWTransform() const { return useHWTransform_; } // only relevant for vtx shaders
|
2013-06-06 08:05:31 +00:00
|
|
|
|
2016-01-17 19:29:34 +00:00
|
|
|
std::string GetShaderString(DebugShaderStringType type, ShaderID id) const;
|
2015-10-14 15:45:21 +00:00
|
|
|
|
2017-11-19 10:08:34 +00:00
|
|
|
uint32_t GetAttrMask() const { return attrMask_; }
|
2017-11-19 10:23:16 +00:00
|
|
|
uint64_t GetUniformMask() const { return uniformMask_; }
|
2017-11-19 10:08:34 +00:00
|
|
|
|
2012-11-18 12:04:49 +00:00
|
|
|
private:
|
2017-11-18 23:43:58 +00:00
|
|
|
GLRenderManager *render_;
|
2012-11-18 12:04:49 +00:00
|
|
|
std::string source_;
|
2013-06-06 08:05:31 +00:00
|
|
|
bool failed_;
|
|
|
|
bool useHWTransform_;
|
2015-10-14 15:45:21 +00:00
|
|
|
bool isFragment_;
|
2017-11-19 10:08:34 +00:00
|
|
|
uint32_t attrMask_; // only used in vertex shaders
|
2017-11-19 10:23:16 +00:00
|
|
|
uint64_t uniformMask_;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2017-01-23 19:48:23 +00:00
|
|
|
class ShaderManagerGLES : public ShaderManagerCommon {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2018-05-29 21:07:22 +00:00
|
|
|
ShaderManagerGLES(Draw::DrawContext *draw);
|
2017-01-21 21:16:30 +00:00
|
|
|
~ShaderManagerGLES();
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void ClearCache(bool deleteThem); // TODO: deleteThem currently not respected
|
2014-03-24 09:55:07 +00:00
|
|
|
|
|
|
|
// This is the old ApplyShader split into two parts, because of annoying information dependencies.
|
|
|
|
// If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
|
2021-01-10 16:51:15 +00:00
|
|
|
Shader *ApplyVertexShader(bool useHWTransform, bool useHWTessellation, u32 vertType, bool weightsAsFloat, VShaderID *VSID);
|
2020-04-04 18:14:32 +00:00
|
|
|
LinkedShader *ApplyFragmentShader(VShaderID VSID, Shader *vs, u32 vertType, bool useBufferedRendering);
|
2014-03-24 09:55:07 +00:00
|
|
|
|
2018-05-29 21:07:22 +00:00
|
|
|
void DeviceLost();
|
|
|
|
void DeviceRestore(Draw::DrawContext *draw);
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void DirtyShader();
|
2018-07-28 09:09:01 +00:00
|
|
|
void DirtyLastShader() override;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-02-08 17:07:34 +00:00
|
|
|
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
|
|
|
|
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
|
|
|
|
int GetNumPrograms() const { return (int)linkedShaderCache_.size(); }
|
2012-11-26 16:35:08 +00:00
|
|
|
|
2015-10-14 15:45:21 +00:00
|
|
|
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
|
|
|
|
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void Load(const Path &filename);
|
2017-12-03 19:30:00 +00:00
|
|
|
bool ContinuePrecompile(float sliceTime = 1.0f / 60.0f);
|
2018-10-31 03:32:12 +00:00
|
|
|
void CancelPrecompile();
|
2021-05-05 23:31:38 +00:00
|
|
|
void Save(const Path &filename);
|
2016-01-17 16:11:51 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
private:
|
|
|
|
void Clear();
|
2017-12-02 17:07:27 +00:00
|
|
|
Shader *CompileFragmentShader(FShaderID id);
|
|
|
|
Shader *CompileVertexShader(VShaderID id);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-06-29 20:49:38 +00:00
|
|
|
struct LinkedShaderCacheEntry {
|
|
|
|
LinkedShaderCacheEntry(Shader *vs_, Shader *fs_, LinkedShader *ls_)
|
|
|
|
: vs(vs_), fs(fs_), ls(ls_) { }
|
|
|
|
|
|
|
|
Shader *vs;
|
|
|
|
Shader *fs;
|
|
|
|
LinkedShader *ls;
|
|
|
|
};
|
|
|
|
typedef std::vector<LinkedShaderCacheEntry> LinkedShaderCache;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
GLRenderManager *render_;
|
2013-08-10 16:33:11 +00:00
|
|
|
LinkedShaderCache linkedShaderCache_;
|
2014-03-24 09:55:07 +00:00
|
|
|
|
2014-05-04 23:31:57 +00:00
|
|
|
bool lastVShaderSame_;
|
2014-03-24 09:55:07 +00:00
|
|
|
|
2017-12-02 17:07:27 +00:00
|
|
|
FShaderID lastFSID_;
|
|
|
|
VShaderID lastVSID_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-10-30 22:29:18 +00:00
|
|
|
LinkedShader *lastShader_ = nullptr;
|
|
|
|
u64 shaderSwitchDirtyUniforms_ = 0;
|
2013-01-07 12:00:28 +00:00
|
|
|
char *codeBuffer_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-12-02 17:07:27 +00:00
|
|
|
typedef DenseHashMap<FShaderID, Shader *, nullptr> FSCache;
|
2013-08-10 16:33:11 +00:00
|
|
|
FSCache fsCache_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-12-02 17:07:27 +00:00
|
|
|
typedef DenseHashMap<VShaderID, Shader *, nullptr> VSCache;
|
2013-08-10 16:33:11 +00:00
|
|
|
VSCache vsCache_;
|
2016-01-17 19:29:34 +00:00
|
|
|
|
2020-10-30 22:29:18 +00:00
|
|
|
bool diskCacheDirty_ = false;
|
2017-12-03 19:30:00 +00:00
|
|
|
struct {
|
|
|
|
std::vector<VShaderID> vert;
|
|
|
|
std::vector<FShaderID> frag;
|
|
|
|
std::vector<std::pair<VShaderID, FShaderID>> link;
|
|
|
|
|
|
|
|
size_t vertPos = 0;
|
|
|
|
size_t fragPos = 0;
|
|
|
|
size_t linkPos = 0;
|
|
|
|
double start;
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
vert.clear();
|
|
|
|
frag.clear();
|
|
|
|
link.clear();
|
|
|
|
vertPos = 0;
|
|
|
|
fragPos = 0;
|
|
|
|
linkPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Done() {
|
|
|
|
return vertPos >= vert.size() && fragPos >= frag.size() && linkPos >= link.size();
|
|
|
|
}
|
|
|
|
} diskCachePending_;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|