ppsspp/GPU/GLES/ShaderManagerGLES.h

229 lines
6.2 KiB
C
Raw Normal View History

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
// 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
#include "Common/Data/Collections/Hashmaps.h"
#include "Common/GPU/OpenGL/GLRenderManager.h"
#include "Common/File/Path.h"
2015-10-14 15:45:21 +00:00
#include "GPU/Common/ShaderCommon.h"
#include "GPU/Common/ShaderId.h"
#include "GPU/Common/VertexShaderGenerator.h"
#include "GPU/Common/FragmentShaderGenerator.h"
2012-11-01 15:19:01 +00:00
class Shader;
2012-11-01 15:19:01 +00:00
class LinkedShader {
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);
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_;
Shader *vs_;
// 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;
uint64_t availableUniforms;
uint64_t dirtyUniforms = 0;
2012-11-01 15:19:01 +00:00
// Present attributes in the shader.
int attrMask; // 1 << ATTR_ ... or-ed together.
2012-11-01 15:19:01 +00:00
int u_stencilReplaceValue;
2012-11-01 15:19:01 +00:00
int u_tex;
int u_proj;
int u_proj_through;
2012-11-01 15:19:01 +00:00
int u_texenv;
int u_view;
int u_texmtx;
int u_world;
int u_depthRange; // x,y = viewport xscale/xcenter. z,w=clipping minz/maxz (?)
int u_cullRangeMin;
int u_cullRangeMax;
#ifdef USE_BONE_ARRAY
int u_bone; // array, size is numBones
#else
int u_bone[8];
#endif
int numBones;
// Shader blending.
int u_fbotex;
int u_blendFixA;
int u_blendFixB;
int u_fbotexSize;
// Shader depal
int u_pal; // the texture
int u_depal_mask_shift_off_fmt; // the params
2012-11-01 15:19:01 +00:00
// Fragment processing inputs
int u_alphacolorref;
int u_alphacolormask;
int u_colorWriteMask;
int u_testtex;
2012-11-01 15:19:01 +00:00
int u_fogcolor;
int u_fogcoef;
// Texturing
int u_uvscaleoffset;
int u_texclamp;
int u_texclampoff;
// Lighting
2012-12-20 17:31:21 +00:00
int u_ambient;
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
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
// Spline Tessellation
int u_tess_points; // Control Points
int u_tess_weights_u;
int u_tess_weights_v;
int u_spline_counts;
2012-11-01 15:19:01 +00:00
};
// Real public interface
class Shader {
public:
Shader(GLRenderManager *render, const char *code, const std::string &desc, uint32_t glShaderType, bool useHWTransform, uint32_t attrMask, uint64_t uniformMask);
~Shader();
2017-11-18 23:43:58 +00:00
GLRShader *shader;
bool Failed() const { return failed_; }
bool UseHWTransform() const { return useHWTransform_; } // only relevant for vtx shaders
std::string GetShaderString(DebugShaderStringType type, ShaderID id) const;
2015-10-14 15:45:21 +00:00
uint32_t GetAttrMask() const { return attrMask_; }
uint64_t GetUniformMask() const { return uniformMask_; }
private:
2017-11-18 23:43:58 +00:00
GLRenderManager *render_;
std::string source_;
bool failed_;
bool useHWTransform_;
2015-10-14 15:45:21 +00:00
bool isFragment_;
uint32_t attrMask_; // only used in vertex shaders
uint64_t uniformMask_;
2012-11-01 15:19:01 +00:00
};
class ShaderManagerGLES : public ShaderManagerCommon {
2012-11-01 15:19:01 +00:00
public:
ShaderManagerGLES(Draw::DrawContext *draw);
~ShaderManagerGLES();
2012-11-01 15:19:01 +00:00
void ClearCache(bool deleteThem); // TODO: deleteThem currently not respected
// This is the old ApplyShader split into two parts, because of annoying information dependencies.
// If you call ApplyVertexShader, you MUST call ApplyFragmentShader soon afterwards.
Shader *ApplyVertexShader(bool useHWTransform, bool useHWTessellation, u32 vertType, bool weightsAsFloat, VShaderID *VSID);
LinkedShader *ApplyFragmentShader(VShaderID VSID, Shader *vs, u32 vertType, bool useBufferedRendering);
void DeviceLost();
void DeviceRestore(Draw::DrawContext *draw);
2012-11-01 15:19:01 +00:00
void DirtyShader();
void DirtyLastShader() override;
2012-11-01 15:19:01 +00:00
int GetNumVertexShaders() const { return (int)vsCache_.size(); }
int GetNumFragmentShaders() const { return (int)fsCache_.size(); }
int GetNumPrograms() const { return (int)linkedShaderCache_.size(); }
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);
void Load(const Path &filename);
bool ContinuePrecompile(float sliceTime = 1.0f / 60.0f);
void CancelPrecompile();
void Save(const Path &filename);
2012-11-01 15:19:01 +00:00
private:
void Clear();
Shader *CompileFragmentShader(FShaderID id);
Shader *CompileVertexShader(VShaderID id);
2012-11-01 15:19:01 +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_;
LinkedShaderCache linkedShaderCache_;
bool lastVShaderSame_;
FShaderID lastFSID_;
VShaderID lastVSID_;
2012-11-01 15:19:01 +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
typedef DenseHashMap<FShaderID, Shader *, nullptr> FSCache;
FSCache fsCache_;
2012-11-01 15:19:01 +00:00
typedef DenseHashMap<VShaderID, Shader *, nullptr> VSCache;
VSCache vsCache_;
bool diskCacheDirty_ = false;
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
};