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
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
2015-10-14 07:09:49 +00:00
|
|
|
#include "Globals.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <map>
|
|
|
|
#include "VertexShaderGenerator.h"
|
|
|
|
#include "FragmentShaderGenerator.h"
|
|
|
|
|
2012-12-21 15:49:42 +00:00
|
|
|
class Shader;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
struct ShaderID {
|
2015-10-14 07:09:49 +00:00
|
|
|
ShaderID() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(d); i++) {
|
|
|
|
d[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 09:18:45 +00:00
|
|
|
void set_invalid() {
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(d); i++) {
|
|
|
|
d[i] = 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 07:09:49 +00:00
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
u32 d[2];
|
|
|
|
bool operator < (const ShaderID &other) const {
|
|
|
|
for (size_t i = 0; i < sizeof(d) / sizeof(u32); i++) {
|
|
|
|
if (d[i] < other.d[i])
|
|
|
|
return true;
|
|
|
|
if (d[i] > other.d[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool operator == (const ShaderID &other) const {
|
|
|
|
for (size_t i = 0; i < sizeof(d) / sizeof(u32); i++) {
|
|
|
|
if (d[i] != other.d[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-14 07:09:49 +00:00
|
|
|
|
2015-10-14 16:01:40 +00:00
|
|
|
bool Bit(int bit) const {
|
2015-10-14 07:09:49 +00:00
|
|
|
return (d[bit >> 5] >> (bit & 31)) & 1;
|
|
|
|
}
|
|
|
|
// Does not handle crossing 32-bit boundaries
|
|
|
|
int Bits(int bit, int count) const {
|
|
|
|
const int mask = (1 << count) - 1;
|
|
|
|
return (d[bit >> 5] >> (bit & 31)) & mask;
|
|
|
|
}
|
|
|
|
void SetBit(int bit, bool value = true) {
|
|
|
|
if (value) {
|
|
|
|
d[bit >> 5] |= 1 << (bit & 31);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void SetBits(int bit, int count, int value) {
|
|
|
|
if (value != 0) {
|
|
|
|
const int mask = (1 << count) - 1;
|
|
|
|
d[bit >> 5] |= (value & mask) << (bit & 31);
|
|
|
|
}
|
|
|
|
}
|
2014-12-14 18:22:49 +00:00
|
|
|
};
|
|
|
|
|
2013-10-08 15:18:59 +00:00
|
|
|
// Pre-fetched attrs and uniforms
|
|
|
|
enum {
|
|
|
|
ATTR_POSITION = 0,
|
|
|
|
ATTR_TEXCOORD = 1,
|
|
|
|
ATTR_NORMAL = 2,
|
|
|
|
ATTR_W1 = 3,
|
|
|
|
ATTR_W2 = 4,
|
|
|
|
ATTR_COLOR0 = 5,
|
|
|
|
ATTR_COLOR1 = 6,
|
|
|
|
|
|
|
|
ATTR_COUNT,
|
|
|
|
};
|
|
|
|
|
|
|
|
class LinkedShader {
|
2012-12-21 15:49:42 +00:00
|
|
|
public:
|
2013-10-08 15:47:05 +00:00
|
|
|
LinkedShader(Shader *vs, Shader *fs, u32 vertType, bool useHWTransform, LinkedShader *previous);
|
2012-11-01 15:19:01 +00:00
|
|
|
~LinkedShader();
|
|
|
|
|
2013-10-08 15:47:05 +00:00
|
|
|
void use(u32 vertType, LinkedShader *previous);
|
2013-01-06 22:50:05 +00:00
|
|
|
void stop();
|
2013-11-12 15:39:35 +00:00
|
|
|
void UpdateUniforms(u32 vertType);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
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_;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
uint32_t program;
|
2013-11-12 15:39:35 +00:00
|
|
|
u32 availableUniforms;
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 dirtyUniforms;
|
|
|
|
|
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 (?)
|
|
|
|
|
2013-05-26 12:03:02 +00:00
|
|
|
#ifdef USE_BONE_ARRAY
|
2013-05-18 18:44:25 +00:00
|
|
|
int u_bone; // array, size is numBones
|
2013-05-26 12:03:02 +00:00
|
|
|
#else
|
|
|
|
int u_bone[8];
|
|
|
|
#endif
|
2013-05-18 18:44:25 +00:00
|
|
|
int numBones;
|
2013-10-08 15:18:59 +00:00
|
|
|
|
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
|
|
|
|
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;
|
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
|
2013-04-09 16:25:22 +00:00
|
|
|
int u_lightangle[4]; // spotlight cone angle (cosine)
|
|
|
|
int u_lightspotCoef[4]; // spotlight dropoff
|
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
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2014-09-13 10:11:34 +00:00
|
|
|
enum {
|
2012-11-01 15:19:01 +00:00
|
|
|
DIRTY_PROJMATRIX = (1 << 0),
|
2012-11-28 12:45:22 +00:00
|
|
|
DIRTY_PROJTHROUGHMATRIX = (1 << 1),
|
2015-08-26 11:26:08 +00:00
|
|
|
DIRTY_FOGCOLOR = (1 << 2),
|
|
|
|
DIRTY_FOGCOEF = (1 << 3),
|
|
|
|
DIRTY_TEXENV = (1 << 4),
|
|
|
|
DIRTY_ALPHACOLORREF = (1 << 5),
|
2013-12-02 23:09:48 +00:00
|
|
|
|
|
|
|
// 1 << 6 is free! Wait, not anymore...
|
|
|
|
DIRTY_STENCILREPLACEVALUE = (1 << 6),
|
|
|
|
|
2014-06-16 07:33:48 +00:00
|
|
|
DIRTY_ALPHACOLORMASK = (1 << 7),
|
2012-12-20 17:31:21 +00:00
|
|
|
DIRTY_LIGHT0 = (1 << 8),
|
|
|
|
DIRTY_LIGHT1 = (1 << 9),
|
|
|
|
DIRTY_LIGHT2 = (1 << 10),
|
|
|
|
DIRTY_LIGHT3 = (1 << 11),
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2012-12-20 17:31:21 +00:00
|
|
|
DIRTY_MATDIFFUSE = (1 << 12),
|
|
|
|
DIRTY_MATSPECULAR = (1 << 13),
|
|
|
|
DIRTY_MATEMISSIVE = (1 << 14),
|
|
|
|
DIRTY_AMBIENT = (1 << 15),
|
2012-12-20 13:10:42 +00:00
|
|
|
DIRTY_MATAMBIENTALPHA = (1 << 16),
|
2013-12-02 23:09:48 +00:00
|
|
|
|
2014-06-14 05:49:28 +00:00
|
|
|
DIRTY_SHADERBLEND = (1 << 17), // Used only for in-shader blending.
|
2013-12-02 23:09:48 +00:00
|
|
|
|
2012-12-05 03:45:28 +00:00
|
|
|
DIRTY_UVSCALEOFFSET = (1 << 18), // this will be dirtied ALL THE TIME... maybe we'll need to do "last value with this shader compares"
|
2015-08-26 11:26:08 +00:00
|
|
|
|
|
|
|
// Texclamp is fairly rare so let's share it's bit with DIRTY_DEPTHRANGE.
|
2014-06-07 19:21:52 +00:00
|
|
|
DIRTY_TEXCLAMP = (1 << 19),
|
2015-08-26 11:26:08 +00:00
|
|
|
DIRTY_DEPTHRANGE = (1 << 19),
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
DIRTY_WORLDMATRIX = (1 << 21),
|
2012-12-05 03:45:28 +00:00
|
|
|
DIRTY_VIEWMATRIX = (1 << 22), // Maybe we'll fold this into projmatrix eventually
|
|
|
|
DIRTY_TEXMATRIX = (1 << 23),
|
|
|
|
DIRTY_BONEMATRIX0 = (1 << 24),
|
|
|
|
DIRTY_BONEMATRIX1 = (1 << 25),
|
|
|
|
DIRTY_BONEMATRIX2 = (1 << 26),
|
|
|
|
DIRTY_BONEMATRIX3 = (1 << 27),
|
|
|
|
DIRTY_BONEMATRIX4 = (1 << 28),
|
|
|
|
DIRTY_BONEMATRIX5 = (1 << 29),
|
|
|
|
DIRTY_BONEMATRIX6 = (1 << 30),
|
|
|
|
DIRTY_BONEMATRIX7 = (1 << 31),
|
|
|
|
|
|
|
|
DIRTY_ALL = 0xFFFFFFFF
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Real public interface
|
|
|
|
|
2012-12-21 15:49:42 +00:00
|
|
|
class Shader {
|
|
|
|
public:
|
2014-12-14 19:16:46 +00:00
|
|
|
Shader(const char *code, uint32_t shaderType, bool useHWTransform, const ShaderID &shaderID);
|
2013-06-06 08:05:31 +00:00
|
|
|
~Shader();
|
2012-11-01 15:19:01 +00:00
|
|
|
uint32_t shader;
|
2012-12-05 04:13:36 +00:00
|
|
|
const std::string &source() const { return source_; }
|
2012-12-21 15:49:42 +00:00
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
bool Failed() const { return failed_; }
|
|
|
|
bool UseHWTransform() const { return useHWTransform_; }
|
2014-12-14 19:16:46 +00:00
|
|
|
const ShaderID &ID() const { return id_; }
|
2013-06-06 08:05:31 +00:00
|
|
|
|
2012-11-18 12:04:49 +00:00
|
|
|
private:
|
|
|
|
std::string source_;
|
2014-12-14 19:16:46 +00:00
|
|
|
ShaderID id_;
|
2013-06-06 08:05:31 +00:00
|
|
|
bool failed_;
|
|
|
|
bool useHWTransform_;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
class ShaderManager {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2013-02-04 22:09:01 +00:00
|
|
|
ShaderManager();
|
|
|
|
~ShaderManager();
|
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.
|
|
|
|
Shader *ApplyVertexShader(int prim, u32 vertType);
|
|
|
|
LinkedShader *ApplyFragmentShader(Shader *vs, int prim, u32 vertType);
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void DirtyShader();
|
2013-08-10 16:33:11 +00:00
|
|
|
void DirtyUniform(u32 what) {
|
|
|
|
globalDirty_ |= what;
|
|
|
|
}
|
2013-10-10 18:11:20 +00:00
|
|
|
void DirtyLastShader(); // disables vertex arrays
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-08-10 16:33:11 +00:00
|
|
|
int NumVertexShaders() const { return (int)vsCache_.size(); }
|
|
|
|
int NumFragmentShaders() const { return (int)fsCache_.size(); }
|
|
|
|
int NumPrograms() const { return (int)linkedShaderCache_.size(); }
|
2012-11-26 16:35:08 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
private:
|
|
|
|
void Clear();
|
2014-12-14 19:16:46 +00:00
|
|
|
static bool DebugAreShadersCompatibleForLinking(Shader *vs, Shader *fs);
|
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
|
|
|
|
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
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
ShaderID lastFSID_;
|
|
|
|
ShaderID lastVSID_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-08-10 16:33:11 +00:00
|
|
|
LinkedShader *lastShader_;
|
|
|
|
u32 globalDirty_;
|
|
|
|
u32 shaderSwitchDirty_;
|
2013-01-07 12:00:28 +00:00
|
|
|
char *codeBuffer_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
typedef std::map<ShaderID, Shader *> FSCache;
|
2013-08-10 16:33:11 +00:00
|
|
|
FSCache fsCache_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-12-14 18:22:49 +00:00
|
|
|
typedef std::map<ShaderID, Shader *> VSCache;
|
2013-08-10 16:33:11 +00:00
|
|
|
VSCache vsCache_;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|