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/.
|
|
|
|
|
2015-10-14 09:18:45 +00:00
|
|
|
#if defined(_WIN32) && defined(SHADERLOG)
|
2013-07-29 03:43:25 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2013-01-22 20:57:47 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-10 07:27:16 +00:00
|
|
|
#include <cmath>
|
2015-10-14 09:18:45 +00:00
|
|
|
#include <cstdio>
|
2018-09-10 07:27:16 +00:00
|
|
|
#include <map>
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Data/Convert/SmallDataConvert.h"
|
2022-12-27 22:58:20 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/OpenGL/GLDebugLog.h"
|
|
|
|
#include "Common/GPU/OpenGL/GLFeatures.h"
|
2022-12-27 22:58:20 +00:00
|
|
|
#include "Common/LogReporting.h"
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Math/math_util.h"
|
|
|
|
#include "Common/Math/lin/matrix4x4.h"
|
2020-10-04 08:04:01 +00:00
|
|
|
#include "Common/Profiler/Profiler.h"
|
2020-11-04 08:18:35 +00:00
|
|
|
#include "Common/GPU/Shader.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/thin3d.h"
|
|
|
|
#include "Common/GPU/OpenGL/GLRenderManager.h"
|
2021-10-23 20:44:10 +00:00
|
|
|
#include "Common/System/Display.h"
|
2023-06-30 15:15:49 +00:00
|
|
|
#include "Common/System/OSD.h"
|
2022-08-27 15:33:37 +00:00
|
|
|
#include "Common/VR/PPSSPPVR.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-08-15 10:25:39 +00:00
|
|
|
#include "Common/Log.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2013-10-01 16:33:32 +00:00
|
|
|
#include "Core/Config.h"
|
2018-03-13 01:06:46 +00:00
|
|
|
#include "Core/System.h"
|
2013-12-29 21:32:55 +00:00
|
|
|
#include "GPU/Math3D.h"
|
2013-06-06 08:05:31 +00:00
|
|
|
#include "GPU/GPUState.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
2018-09-18 05:27:25 +00:00
|
|
|
#include "GPU/Common/ShaderUniforms.h"
|
2017-01-21 21:16:30 +00:00
|
|
|
#include "GPU/GLES/ShaderManagerGLES.h"
|
2016-04-10 08:27:28 +00:00
|
|
|
#include "GPU/GLES/DrawEngineGLES.h"
|
2018-09-18 05:27:25 +00:00
|
|
|
#include "GPU/GLES/FramebufferManagerGLES.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2019-10-22 20:58:10 +00:00
|
|
|
using namespace Lin;
|
|
|
|
|
2021-09-12 00:53:50 +00:00
|
|
|
Shader::Shader(GLRenderManager *render, const char *code, const std::string &desc, const ShaderDescGLES ¶ms)
|
|
|
|
: render_(render), useHWTransform_(params.useHWTransform), attrMask_(params.attrMask), uniformMask_(params.uniformMask) {
|
2015-05-25 22:39:27 +00:00
|
|
|
PROFILE_THIS_SCOPE("shadercomp");
|
2021-09-12 00:53:50 +00:00
|
|
|
isFragment_ = params.glShaderType == GL_FRAGMENT_SHADER;
|
2012-11-18 12:04:49 +00:00
|
|
|
source_ = code;
|
2013-01-22 20:57:47 +00:00
|
|
|
#ifdef SHADERLOG
|
2015-10-14 09:18:45 +00:00
|
|
|
#ifdef _WIN32
|
2013-08-26 17:00:16 +00:00
|
|
|
OutputDebugStringUTF8(code);
|
2015-10-14 09:18:45 +00:00
|
|
|
#else
|
|
|
|
printf("%s\n", code);
|
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2021-09-12 00:53:50 +00:00
|
|
|
shader = render->CreateShader(params.glShaderType, source_, desc);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 08:05:31 +00:00
|
|
|
Shader::~Shader() {
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->DeleteShader(shader);
|
2013-06-06 08:05:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, FShaderID FSID, Shader *fs, bool useHWTransform, bool preloading)
|
|
|
|
: render_(render), useHWTransform_(useHWTransform) {
|
2015-05-25 22:39:27 +00:00
|
|
|
PROFILE_THIS_SCOPE("shaderlink");
|
|
|
|
|
2023-09-21 14:41:09 +00:00
|
|
|
_assert_(render);
|
2023-05-01 10:11:34 +00:00
|
|
|
_assert_(vs);
|
|
|
|
_assert_(fs);
|
|
|
|
|
2014-03-24 09:55:07 +00:00
|
|
|
vs_ = vs;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
std::vector<GLRShader *> shaders;
|
|
|
|
shaders.push_back(vs->shader);
|
|
|
|
shaders.push_back(fs->shader);
|
|
|
|
|
|
|
|
std::vector<GLRProgram::Semantic> semantics;
|
2022-09-30 09:31:32 +00:00
|
|
|
semantics.reserve(7);
|
2017-11-18 23:43:58 +00:00
|
|
|
semantics.push_back({ ATTR_POSITION, "position" });
|
|
|
|
semantics.push_back({ ATTR_TEXCOORD, "texcoord" });
|
2021-10-24 19:27:47 +00:00
|
|
|
if (useHWTransform_)
|
|
|
|
semantics.push_back({ ATTR_NORMAL, "normal" });
|
|
|
|
else
|
|
|
|
semantics.push_back({ ATTR_NORMAL, "fog" });
|
2018-04-10 09:22:58 +00:00
|
|
|
semantics.push_back({ ATTR_W1, "w1" });
|
|
|
|
semantics.push_back({ ATTR_W2, "w2" });
|
2017-11-18 23:43:58 +00:00
|
|
|
semantics.push_back({ ATTR_COLOR0, "color0" });
|
|
|
|
semantics.push_back({ ATTR_COLOR1, "color1" });
|
|
|
|
|
|
|
|
std::vector<GLRProgram::UniformLocQuery> queries;
|
|
|
|
queries.push_back({ &u_tex, "tex" });
|
2022-08-24 08:51:35 +00:00
|
|
|
queries.push_back({ &u_pal, "pal" });
|
|
|
|
queries.push_back({ &u_testtex, "testtex" });
|
|
|
|
queries.push_back({ &u_fbotex, "fbotex" });
|
2017-11-18 23:43:58 +00:00
|
|
|
|
|
|
|
queries.push_back({ &u_proj, "u_proj" });
|
2022-09-04 19:12:18 +00:00
|
|
|
queries.push_back({ &u_proj_lens, "u_proj_lens" });
|
2017-11-18 23:43:58 +00:00
|
|
|
queries.push_back({ &u_proj_through, "u_proj_through" });
|
|
|
|
queries.push_back({ &u_texenv, "u_texenv" });
|
|
|
|
queries.push_back({ &u_fogcolor, "u_fogcolor" });
|
|
|
|
queries.push_back({ &u_fogcoef, "u_fogcoef" });
|
|
|
|
queries.push_back({ &u_alphacolorref, "u_alphacolorref" });
|
|
|
|
queries.push_back({ &u_alphacolormask, "u_alphacolormask" });
|
2020-11-08 22:17:06 +00:00
|
|
|
queries.push_back({ &u_colorWriteMask, "u_colorWriteMask" });
|
2017-11-18 23:43:58 +00:00
|
|
|
queries.push_back({ &u_stencilReplaceValue, "u_stencilReplaceValue" });
|
|
|
|
queries.push_back({ &u_blendFixA, "u_blendFixA" });
|
|
|
|
queries.push_back({ &u_blendFixB, "u_blendFixB" });
|
|
|
|
queries.push_back({ &u_fbotexSize, "u_fbotexSize" });
|
2014-05-11 16:51:35 +00:00
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Transform
|
2017-11-18 23:43:58 +00:00
|
|
|
queries.push_back({ &u_view, "u_view" });
|
|
|
|
queries.push_back({ &u_world, "u_world" });
|
|
|
|
queries.push_back({ &u_texmtx, "u_texmtx" });
|
|
|
|
|
2018-04-10 10:22:02 +00:00
|
|
|
if (VSID.Bit(VS_BIT_ENABLE_BONES))
|
|
|
|
numBones = TranslateNumBones(VSID.Bits(VS_BIT_BONES, 3) + 1);
|
|
|
|
else
|
|
|
|
numBones = 0;
|
2017-11-18 23:43:58 +00:00
|
|
|
queries.push_back({ &u_depthRange, "u_depthRange" });
|
2018-09-17 05:52:43 +00:00
|
|
|
queries.push_back({ &u_cullRangeMin, "u_cullRangeMin" });
|
|
|
|
queries.push_back({ &u_cullRangeMax, "u_cullRangeMax" });
|
2021-10-23 20:44:10 +00:00
|
|
|
queries.push_back({ &u_rotation, "u_rotation" });
|
2022-08-27 15:33:37 +00:00
|
|
|
|
2022-10-17 16:36:54 +00:00
|
|
|
// These two are only used for VR, but let's always query them for simplicity.
|
|
|
|
queries.push_back({ &u_scaleX, "u_scaleX" });
|
|
|
|
queries.push_back({ &u_scaleY, "u_scaleY" });
|
2013-07-27 15:32:24 +00:00
|
|
|
|
2018-04-10 10:22:02 +00:00
|
|
|
#ifdef USE_BONE_ARRAY
|
|
|
|
queries.push_back({ &u_bone, "u_bone" });
|
|
|
|
#else
|
|
|
|
static const char * const boneNames[8] = { "u_bone0", "u_bone1", "u_bone2", "u_bone3", "u_bone4", "u_bone5", "u_bone6", "u_bone7", };
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
queries.push_back({ &u_bone[i], boneNames[i] });
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Lighting, texturing
|
2017-11-18 23:43:58 +00:00
|
|
|
queries.push_back({ &u_ambient, "u_ambient" });
|
|
|
|
queries.push_back({ &u_matambientalpha, "u_matambientalpha" });
|
|
|
|
queries.push_back({ &u_matdiffuse, "u_matdiffuse" });
|
|
|
|
queries.push_back({ &u_matspecular, "u_matspecular" });
|
|
|
|
queries.push_back({ &u_matemissive, "u_matemissive" });
|
|
|
|
queries.push_back({ &u_uvscaleoffset, "u_uvscaleoffset" });
|
|
|
|
queries.push_back({ &u_texclamp, "u_texclamp" });
|
|
|
|
queries.push_back({ &u_texclampoff, "u_texclampoff" });
|
2023-05-09 16:47:13 +00:00
|
|
|
queries.push_back({ &u_texNoAlphaMul, "u_texNoAlphaMul" });
|
2022-09-25 16:30:27 +00:00
|
|
|
queries.push_back({ &u_lightControl, "u_lightControl" });
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2012-12-20 17:31:21 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2017-11-18 23:43:58 +00:00
|
|
|
static const char * const lightPosNames[4] = { "u_lightpos0", "u_lightpos1", "u_lightpos2", "u_lightpos3", };
|
|
|
|
queries.push_back({ &u_lightpos[i], lightPosNames[i] });
|
|
|
|
static const char * const lightdir_names[4] = { "u_lightdir0", "u_lightdir1", "u_lightdir2", "u_lightdir3", };
|
|
|
|
queries.push_back({ &u_lightdir[i], lightdir_names[i] });
|
|
|
|
static const char * const lightatt_names[4] = { "u_lightatt0", "u_lightatt1", "u_lightatt2", "u_lightatt3", };
|
|
|
|
queries.push_back({ &u_lightatt[i], lightatt_names[i] });
|
2018-03-12 10:07:51 +00:00
|
|
|
static const char * const lightangle_spotCoef_names[4] = { "u_lightangle_spotCoef0", "u_lightangle_spotCoef1", "u_lightangle_spotCoef2", "u_lightangle_spotCoef3", };
|
|
|
|
queries.push_back({ &u_lightangle_spotCoef[i], lightangle_spotCoef_names[i] });
|
2017-11-18 23:43:58 +00:00
|
|
|
|
|
|
|
static const char * const lightambient_names[4] = { "u_lightambient0", "u_lightambient1", "u_lightambient2", "u_lightambient3", };
|
|
|
|
queries.push_back({ &u_lightambient[i], lightambient_names[i] });
|
|
|
|
static const char * const lightdiffuse_names[4] = { "u_lightdiffuse0", "u_lightdiffuse1", "u_lightdiffuse2", "u_lightdiffuse3", };
|
|
|
|
queries.push_back({ &u_lightdiffuse[i], lightdiffuse_names[i] });
|
|
|
|
static const char * const lightspecular_names[4] = { "u_lightspecular0", "u_lightspecular1", "u_lightspecular2", "u_lightspecular3", };
|
|
|
|
queries.push_back({ &u_lightspecular[i], lightspecular_names[i] });
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2017-01-28 21:33:31 +00:00
|
|
|
|
|
|
|
// We need to fetch these unconditionally, gstate_c.spline or bezier will not be set if we
|
|
|
|
// create this shader at load time from the shader cache.
|
2018-06-29 15:21:30 +00:00
|
|
|
queries.push_back({ &u_tess_points, "u_tess_points" });
|
|
|
|
queries.push_back({ &u_tess_weights_u, "u_tess_weights_u" });
|
|
|
|
queries.push_back({ &u_tess_weights_v, "u_tess_weights_v" });
|
2018-07-14 00:40:13 +00:00
|
|
|
queries.push_back({ &u_spline_counts, "u_spline_counts" });
|
2020-10-22 20:36:02 +00:00
|
|
|
queries.push_back({ &u_depal_mask_shift_off_fmt, "u_depal_mask_shift_off_fmt" });
|
2022-07-30 19:33:24 +00:00
|
|
|
queries.push_back({ &u_mipBias, "u_mipBias" });
|
2012-12-20 17:31:21 +00:00
|
|
|
|
2017-11-19 10:08:34 +00:00
|
|
|
attrMask = vs->GetAttrMask();
|
2017-11-19 10:23:16 +00:00
|
|
|
availableUniforms = vs->GetUniformMask() | fs->GetUniformMask();
|
2013-11-12 15:39:35 +00:00
|
|
|
|
2017-11-19 11:25:57 +00:00
|
|
|
std::vector<GLRProgram::Initializer> initialize;
|
2022-09-30 09:31:32 +00:00
|
|
|
initialize.reserve(7);
|
2022-09-11 11:27:17 +00:00
|
|
|
initialize.push_back({ &u_tex, 0, TEX_SLOT_PSP_TEXTURE });
|
|
|
|
initialize.push_back({ &u_fbotex, 0, TEX_SLOT_SHADERBLEND_SRC });
|
|
|
|
initialize.push_back({ &u_testtex, 0, TEX_SLOT_ALPHATEST });
|
|
|
|
initialize.push_back({ &u_pal, 0, TEX_SLOT_CLUT }); // CLUT
|
|
|
|
initialize.push_back({ &u_tess_points, 0, TEX_SLOT_SPLINE_POINTS }); // Control Points
|
|
|
|
initialize.push_back({ &u_tess_weights_u, 0, TEX_SLOT_SPLINE_WEIGHTS_U });
|
|
|
|
initialize.push_back({ &u_tess_weights_v, 0, TEX_SLOT_SPLINE_WEIGHTS_V });
|
2017-11-19 11:25:57 +00:00
|
|
|
|
2022-09-18 20:16:59 +00:00
|
|
|
GLRProgramFlags flags{};
|
2022-12-13 15:53:53 +00:00
|
|
|
flags.supportDualSource = gstate_c.Use(GPU_USE_DUALSOURCE_BLEND);
|
2022-10-17 06:30:27 +00:00
|
|
|
if (!VSID.Bit(VS_BIT_IS_THROUGH) && gstate_c.Use(GPU_USE_DEPTH_CLAMP)) {
|
2022-09-18 20:18:02 +00:00
|
|
|
flags.useClipDistance0 = true;
|
2022-10-17 06:30:27 +00:00
|
|
|
if (VSID.Bit(VS_BIT_VERTEX_RANGE_CULLING) && gstate_c.Use(GPU_USE_CLIP_DISTANCE))
|
2022-10-06 04:17:17 +00:00
|
|
|
flags.useClipDistance1 = true;
|
2022-10-17 06:30:27 +00:00
|
|
|
} else if (VSID.Bit(VS_BIT_VERTEX_RANGE_CULLING) && gstate_c.Use(GPU_USE_CLIP_DISTANCE)) {
|
2022-09-18 20:16:59 +00:00
|
|
|
flags.useClipDistance0 = true;
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:52:19 +00:00
|
|
|
program = render->CreateProgram(shaders, semantics, queries, initialize, nullptr, flags);
|
2017-01-28 21:33:31 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// The rest, use the "dirty" mechanism.
|
2017-01-23 22:15:54 +00:00
|
|
|
dirtyUniforms = DIRTY_ALL_UNIFORMS;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 06:12:18 +00:00
|
|
|
void LinkedShader::Delete() {
|
|
|
|
program->SetDeleteCallback([](void *thiz) {
|
|
|
|
LinkedShader *ls = (LinkedShader *)thiz;
|
|
|
|
delete ls;
|
|
|
|
}, this);
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->DeleteProgram(program);
|
2023-01-11 06:12:18 +00:00
|
|
|
program = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedShader::~LinkedShader() {
|
|
|
|
_assert_(program == nullptr);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 14:14:41 +00:00
|
|
|
// Utility
|
2017-11-18 23:43:58 +00:00
|
|
|
static inline void SetFloatUniform(GLRenderManager *render, GLint *uniform, float value) {
|
|
|
|
render->SetUniformF(uniform, 1, &value);
|
|
|
|
}
|
|
|
|
|
2018-03-12 10:07:51 +00:00
|
|
|
static inline void SetFloatUniform2(GLRenderManager *render, GLint *uniform, float value[2]) {
|
|
|
|
render->SetUniformF(uniform, 2, value);
|
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static inline void SetColorUniform3(GLRenderManager *render, GLint *uniform, u32 color) {
|
2017-08-20 11:53:39 +00:00
|
|
|
float f[4];
|
|
|
|
Uint8x4ToFloat4(f, color);
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 3, f);
|
2012-12-19 14:14:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetColorUniform3Alpha(GLRenderManager *render, GLint *uniform, u32 color, u8 alpha) {
|
2017-08-20 11:53:39 +00:00
|
|
|
float f[4];
|
|
|
|
Uint8x3ToFloat4_AlphaUint8(f, color, alpha);
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 4, f);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:02 +00:00
|
|
|
// This passes colors unscaled (e.g. 0 - 255 not 0 - 1.)
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetColorUniform3Alpha255(GLRenderManager *render, GLint *uniform, u32 color, u8 alpha) {
|
2017-11-21 14:42:01 +00:00
|
|
|
if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) {
|
2013-07-20 17:14:36 +00:00
|
|
|
const float col[4] = {
|
2014-06-16 07:33:48 +00:00
|
|
|
(float)((color & 0xFF) >> 0) * (1.0f / 255.0f),
|
2013-07-20 17:14:36 +00:00
|
|
|
(float)((color & 0xFF00) >> 8) * (1.0f / 255.0f),
|
|
|
|
(float)((color & 0xFF0000) >> 16) * (1.0f / 255.0f),
|
|
|
|
(float)alpha * (1.0f / 255.0f)
|
|
|
|
};
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 4, col);
|
2013-07-20 17:14:36 +00:00
|
|
|
} else {
|
|
|
|
const float col[4] = {
|
2014-06-16 07:33:48 +00:00
|
|
|
(float)((color & 0xFF) >> 0),
|
|
|
|
(float)((color & 0xFF00) >> 8),
|
|
|
|
(float)((color & 0xFF0000) >> 16),
|
2014-11-09 12:34:23 +00:00
|
|
|
(float)alpha
|
2013-07-20 17:14:36 +00:00
|
|
|
};
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 4, col);
|
2013-07-20 17:14:36 +00:00
|
|
|
}
|
2013-05-07 14:52:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetColorUniform3iAlpha(GLRenderManager *render, GLint *uniform, u32 color, u8 alpha) {
|
2014-06-16 07:33:48 +00:00
|
|
|
const int col[4] = {
|
2014-06-16 08:02:49 +00:00
|
|
|
(int)((color & 0xFF) >> 0),
|
|
|
|
(int)((color & 0xFF00) >> 8),
|
|
|
|
(int)((color & 0xFF0000) >> 16),
|
|
|
|
(int)alpha,
|
2014-06-16 07:33:48 +00:00
|
|
|
};
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformI(uniform, 4, col);
|
2014-06-16 07:33:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetColorUniform3ExtraFloat(GLRenderManager *render, GLint *uniform, u32 color, float extra) {
|
2013-01-14 19:43:18 +00:00
|
|
|
const float col[4] = {
|
|
|
|
((color & 0xFF)) / 255.0f,
|
|
|
|
((color & 0xFF00) >> 8) / 255.0f,
|
|
|
|
((color & 0xFF0000) >> 16) / 255.0f,
|
|
|
|
extra
|
|
|
|
};
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 4, col);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetFloat24Uniform3(GLRenderManager *render, GLint *uniform, const uint32_t data[3]) {
|
2017-08-20 11:53:39 +00:00
|
|
|
float f[4];
|
|
|
|
ExpandFloat24x3ToFloat4(f, data);
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformF(uniform, 3, f);
|
2014-04-18 12:30:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 17:21:07 +00:00
|
|
|
static void SetFloat24Uniform3Normalized(GLRenderManager *render, GLint *uniform, const uint32_t data[3]) {
|
|
|
|
float f[4];
|
|
|
|
ExpandFloat24x3ToFloat4AndNormalize(f, data);
|
|
|
|
render->SetUniformF(uniform, 3, f);
|
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetFloatUniform4(GLRenderManager *render, GLint *uniform, float data[4]) {
|
|
|
|
render->SetUniformF(uniform, 4, data);
|
2015-08-26 11:26:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:43:58 +00:00
|
|
|
static void SetMatrix4x3(GLRenderManager *render, GLint *uniform, const float *m4x3) {
|
2013-05-18 19:18:04 +00:00
|
|
|
float m4x4[16];
|
2020-10-22 22:44:35 +00:00
|
|
|
ConvertMatrix4x3To4x4Transposed(m4x4, m4x3);
|
2017-11-18 23:43:58 +00:00
|
|
|
render->SetUniformM4x4(uniform, m4x4);
|
2012-12-21 10:08:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 18:03:07 +00:00
|
|
|
static inline void ScaleProjMatrix(Matrix4x4 &in, bool useBufferedRendering) {
|
2015-11-12 14:22:20 +00:00
|
|
|
float yOffset = gstate_c.vpYOffset;
|
2020-04-04 18:03:07 +00:00
|
|
|
if (!useBufferedRendering) {
|
2015-11-12 14:22:20 +00:00
|
|
|
// GL upside down is a pain as usual.
|
|
|
|
yOffset = -yOffset;
|
|
|
|
}
|
2016-01-16 08:17:27 +00:00
|
|
|
const Vec3 trans(gstate_c.vpXOffset, yOffset, gstate_c.vpZOffset);
|
2016-01-03 20:11:05 +00:00
|
|
|
const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale);
|
2015-03-23 06:35:24 +00:00
|
|
|
in.translateAndScale(trans, scale);
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:51:45 +00:00
|
|
|
static inline void FlipProjMatrix(Matrix4x4 &in, bool useBufferedRendering) {
|
|
|
|
|
|
|
|
const bool invertedY = useBufferedRendering ? (gstate_c.vpHeight < 0) : (gstate_c.vpHeight > 0);
|
|
|
|
if (invertedY) {
|
|
|
|
in[1] = -in[1];
|
|
|
|
in[5] = -in[5];
|
|
|
|
in[9] = -in[9];
|
|
|
|
in[13] = -in[13];
|
|
|
|
}
|
|
|
|
const bool invertedX = gstate_c.vpWidth < 0;
|
|
|
|
if (invertedX) {
|
|
|
|
in[0] = -in[0];
|
|
|
|
in[4] = -in[4];
|
|
|
|
in[8] = -in[8];
|
|
|
|
in[12] = -in[12];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 15:05:30 +00:00
|
|
|
static inline bool GuessVRDrawingHUD(bool is2D, bool flatScreen) {
|
|
|
|
|
|
|
|
bool hud = true;
|
2023-04-05 20:49:46 +00:00
|
|
|
//HUD can be disabled in settings
|
|
|
|
if (!g_Config.bRescaleHUD) hud = false;
|
2022-11-25 15:05:30 +00:00
|
|
|
//HUD cannot be rendered in flatscreen
|
2023-04-05 20:49:46 +00:00
|
|
|
else if (flatScreen) hud = false;
|
2022-11-25 15:05:30 +00:00
|
|
|
//HUD has to be 2D
|
|
|
|
else if (!is2D) hud = false;
|
|
|
|
//HUD has to be blended
|
|
|
|
else if (!gstate.isAlphaBlendEnabled()) hud = false;
|
|
|
|
//HUD cannot be rendered with clear color mask
|
|
|
|
else if (gstate.isClearModeColorMask()) hud = false;
|
2022-11-29 20:25:13 +00:00
|
|
|
//HUD cannot be rendered with depth color mask
|
|
|
|
else if (gstate.isClearModeDepthMask()) hud = false;
|
2022-11-25 15:05:30 +00:00
|
|
|
//HUD texture has to contain alpha channel
|
|
|
|
else if (!gstate.isTextureAlphaUsed()) hud = false;
|
2023-07-02 13:05:29 +00:00
|
|
|
//HUD texture cannot be in 5551 format
|
|
|
|
else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_5551) hud = false;
|
2023-01-05 21:38:50 +00:00
|
|
|
//HUD texture cannot be in CLUT16 format
|
|
|
|
else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT16) hud = false;
|
|
|
|
//HUD texture cannot be in CLUT32 format
|
|
|
|
else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT32) hud = false;
|
2022-12-02 14:16:41 +00:00
|
|
|
//HUD cannot have full texture alpha
|
2023-02-05 07:38:43 +00:00
|
|
|
else if (gstate_c.textureFullAlpha && gstate.getTextureFormat() != GETextureFormat::GE_TFMT_CLUT4) hud = false;
|
2022-12-02 14:16:41 +00:00
|
|
|
//HUD must have full vertex alpha
|
2022-12-02 15:47:26 +00:00
|
|
|
else if (!gstate_c.vertexFullAlpha && gstate.getDepthTestFunction() == GE_COMP_NEVER) hud = false;
|
2022-11-25 15:05:30 +00:00
|
|
|
//HUD cannot render FB screenshot
|
2022-12-17 16:52:04 +00:00
|
|
|
else if (gstate_c.curTextureHeight % 68 <= 1) hud = false;
|
2022-12-17 15:51:05 +00:00
|
|
|
//HUD cannot be rendered with add function
|
|
|
|
else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_ADD) hud = false;
|
2022-12-02 16:45:29 +00:00
|
|
|
//HUD cannot be rendered with replace function
|
|
|
|
else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_REPLACE) hud = false;
|
|
|
|
//HUD cannot be rendered with full clear color mask
|
|
|
|
else if ((gstate.getClearModeColorMask() == 0xFFFFFF) && (gstate.getColorMask() == 0xFFFFFF)) hud = false;
|
2022-11-25 15:05:30 +00:00
|
|
|
|
|
|
|
return hud;
|
|
|
|
}
|
|
|
|
|
2024-04-05 14:04:31 +00:00
|
|
|
void LinkedShader::use(const ShaderID &VSID) const {
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->BindProgram(program);
|
|
|
|
// Note that we no longer track attr masks here - we do it for the input layouts instead.
|
2012-12-21 20:49:09 +00:00
|
|
|
}
|
2012-12-19 14:14:41 +00:00
|
|
|
|
2022-12-14 21:48:17 +00:00
|
|
|
void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRendering, const ShaderLanguageDesc &shaderLanguage) {
|
2017-01-08 14:01:01 +00:00
|
|
|
u64 dirty = dirtyUniforms & availableUniforms;
|
2013-11-13 09:02:58 +00:00
|
|
|
dirtyUniforms = 0;
|
2022-08-27 15:33:37 +00:00
|
|
|
|
2022-10-18 10:23:23 +00:00
|
|
|
// Analyze scene
|
|
|
|
bool is2D, flatScreen;
|
|
|
|
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
|
|
|
|
is2D = Is2DVRObject(gstate.projMatrix, gstate.isModeThrough());
|
|
|
|
flatScreen = IsFlatVRScene();
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:39:35 +00:00
|
|
|
if (!dirty)
|
2012-12-19 14:14:41 +00:00
|
|
|
return;
|
|
|
|
|
2018-04-13 18:00:14 +00:00
|
|
|
if (dirty & DIRTY_DEPAL) {
|
|
|
|
int indexMask = gstate.getClutIndexMask();
|
|
|
|
int indexShift = gstate.getClutIndexShift();
|
|
|
|
int indexOffset = gstate.getClutIndexStartPos() >> 4;
|
|
|
|
int format = gstate_c.depalFramebufferFormat;
|
|
|
|
uint32_t val = BytesToUint32(indexMask, indexShift, indexOffset, format);
|
|
|
|
// Poke in a bilinear filter flag in the top bit.
|
|
|
|
val |= gstate.isMagnifyFilteringEnabled() << 31;
|
2020-11-11 22:09:48 +00:00
|
|
|
render_->SetUniformUI1(&u_depal_mask_shift_off_fmt, val);
|
2018-04-13 18:00:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 10:23:23 +00:00
|
|
|
// Set HUD mode
|
2022-10-17 16:36:54 +00:00
|
|
|
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
|
2023-01-06 07:26:14 +00:00
|
|
|
if (GuessVRDrawingHUD(is2D, flatScreen)) {
|
2024-08-27 06:22:58 +00:00
|
|
|
float aspect = 480.0f / 272.0f * (IsImmersiveVRMode() ? 0.5f : 1.0f);
|
2024-07-22 11:33:03 +00:00
|
|
|
render_->SetUniformF1(&u_scaleX, g_Config.fHeadUpDisplayScale * aspect);
|
2022-11-25 14:58:51 +00:00
|
|
|
render_->SetUniformF1(&u_scaleY, g_Config.fHeadUpDisplayScale);
|
2022-08-27 15:33:37 +00:00
|
|
|
} else {
|
|
|
|
render_->SetUniformF1(&u_scaleX, 1.0f);
|
|
|
|
render_->SetUniformF1(&u_scaleY, 1.0f);
|
|
|
|
}
|
2022-07-27 17:47:11 +00:00
|
|
|
}
|
2022-08-02 18:58:16 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Update any dirty uniforms before we draw
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_PROJMATRIX) {
|
2022-10-17 16:36:54 +00:00
|
|
|
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
|
2024-06-03 16:29:06 +00:00
|
|
|
Matrix4x4 vrProjection;
|
2022-08-27 15:33:37 +00:00
|
|
|
if (flatScreen || is2D) {
|
2024-06-03 16:29:06 +00:00
|
|
|
memcpy(&vrProjection, gstate.projMatrix, 16 * sizeof(float));
|
2022-08-27 15:33:37 +00:00
|
|
|
} else {
|
2024-06-03 16:29:06 +00:00
|
|
|
UpdateVRProjection(gstate.projMatrix, vrProjection.m);
|
2022-08-27 15:33:37 +00:00
|
|
|
}
|
2023-07-02 18:29:59 +00:00
|
|
|
UpdateVRParams(gstate.projMatrix);
|
2016-02-07 04:28:45 +00:00
|
|
|
|
2024-06-03 16:29:06 +00:00
|
|
|
FlipProjMatrix(vrProjection, useBufferedRendering);
|
|
|
|
ScaleProjMatrix(vrProjection, useBufferedRendering);
|
2016-02-07 04:28:45 +00:00
|
|
|
|
2024-06-03 16:29:06 +00:00
|
|
|
render_->SetUniformM4x4(&u_proj_lens, vrProjection.m);
|
2022-09-04 19:12:18 +00:00
|
|
|
}
|
2016-02-07 04:28:45 +00:00
|
|
|
|
2022-09-04 19:12:18 +00:00
|
|
|
Matrix4x4 flippedMatrix;
|
|
|
|
memcpy(&flippedMatrix, gstate.projMatrix, 16 * sizeof(float));
|
2015-03-23 06:35:24 +00:00
|
|
|
|
2022-09-04 19:12:18 +00:00
|
|
|
FlipProjMatrix(flippedMatrix, useBufferedRendering);
|
|
|
|
ScaleProjMatrix(flippedMatrix, useBufferedRendering);
|
|
|
|
|
|
|
|
render_->SetUniformM4x4(&u_proj, flippedMatrix.m);
|
2023-02-25 12:09:44 +00:00
|
|
|
render_->SetUniformF1(&u_rotation, useBufferedRendering ? 0 : (float)g_display.rotation);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2022-10-10 15:16:52 +00:00
|
|
|
if (dirty & DIRTY_PROJTHROUGHMATRIX) {
|
2012-11-28 12:45:22 +00:00
|
|
|
Matrix4x4 proj_through;
|
2015-10-31 23:23:24 +00:00
|
|
|
if (useBufferedRendering) {
|
|
|
|
proj_through.setOrtho(0.0f, gstate_c.curRTWidth, 0.0f, gstate_c.curRTHeight, 0.0f, 1.0f);
|
|
|
|
} else {
|
|
|
|
proj_through.setOrtho(0.0f, gstate_c.curRTWidth, gstate_c.curRTHeight, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->SetUniformM4x4(&u_proj_through, proj_through.getReadPtr());
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_TEXENV) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_texenv, gstate.texenvcolor);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2023-01-05 16:05:42 +00:00
|
|
|
if (dirty & DIRTY_TEX_ALPHA_MUL) {
|
2023-02-03 07:51:35 +00:00
|
|
|
bool doTextureAlpha = gstate.isTextureAlphaUsed();
|
|
|
|
if (gstate_c.textureFullAlpha && gstate.getTextureFunction() != GE_TEXFUNC_REPLACE) {
|
|
|
|
doTextureAlpha = false;
|
|
|
|
}
|
2023-05-09 16:47:13 +00:00
|
|
|
float noAlphaMul[2] = { doTextureAlpha ? 0.0f : 1.0f, gstate.isColorDoublingEnabled() ? 2.0f : 1.0f };
|
|
|
|
render_->SetUniformF(&u_texNoAlphaMul, 2, noAlphaMul);
|
2023-01-09 10:19:50 +00:00
|
|
|
}
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_ALPHACOLORREF) {
|
2022-10-10 15:51:32 +00:00
|
|
|
if (shaderLanguage.bitwiseOps) {
|
2022-10-10 15:16:52 +00:00
|
|
|
render_->SetUniformUI1(&u_alphacolorref, gstate.getColorTestRef() | ((gstate.getAlphaTestRef() & gstate.getAlphaTestMask()) << 24));
|
|
|
|
} else {
|
|
|
|
SetColorUniform3Alpha255(render_, &u_alphacolorref, gstate.getColorTestRef(), gstate.getAlphaTestRef() & gstate.getAlphaTestMask());
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2014-06-16 07:33:48 +00:00
|
|
|
if (dirty & DIRTY_ALPHACOLORMASK) {
|
2022-09-25 08:33:14 +00:00
|
|
|
render_->SetUniformUI1(&u_alphacolormask, gstate.getColorTestMask() | (gstate.getAlphaTestMask() << 24));
|
2013-02-24 21:42:23 +00:00
|
|
|
}
|
2020-11-08 22:17:06 +00:00
|
|
|
if (dirty & DIRTY_COLORWRITEMASK) {
|
|
|
|
render_->SetUniformUI1(&u_colorWriteMask, ~((gstate.pmska << 24) | (gstate.pmskc & 0xFFFFFF)));
|
|
|
|
}
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_FOGCOLOR) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_fogcolor, gstate.fogcolor);
|
2024-08-07 07:25:12 +00:00
|
|
|
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
|
2022-10-17 17:00:38 +00:00
|
|
|
SetVRCompat(VR_COMPAT_FOG_COLOR, gstate.fogcolor);
|
|
|
|
}
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2023-05-08 20:01:14 +00:00
|
|
|
if (dirty & DIRTY_FOGCOEF) {
|
|
|
|
float fogcoef[2] = {
|
|
|
|
getFloat24(gstate.fog1),
|
|
|
|
getFloat24(gstate.fog2),
|
|
|
|
};
|
|
|
|
// The PSP just ignores infnan here (ignoring IEEE), so take it down to a valid float.
|
|
|
|
// Workaround for https://github.com/hrydgard/ppsspp/issues/5384#issuecomment-38365988
|
|
|
|
if (my_isnanorinf(fogcoef[0])) {
|
|
|
|
// Not really sure what a sensible value might be, but let's try 64k.
|
|
|
|
fogcoef[0] = std::signbit(fogcoef[0]) ? -65535.0f : 65535.0f;
|
|
|
|
}
|
|
|
|
if (my_isnanorinf(fogcoef[1])) {
|
|
|
|
fogcoef[1] = std::signbit(fogcoef[1]) ? -65535.0f : 65535.0f;
|
2014-06-07 22:22:46 +00:00
|
|
|
}
|
2023-05-08 20:01:14 +00:00
|
|
|
render_->SetUniformF(&u_fogcoef, 2, fogcoef);
|
2012-11-28 12:45:22 +00:00
|
|
|
}
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_UVSCALEOFFSET) {
|
2023-06-15 09:57:30 +00:00
|
|
|
float widthFactor = 1.0f;
|
|
|
|
float heightFactor = 1.0f;
|
|
|
|
if (gstate_c.textureIsFramebuffer) {
|
|
|
|
const float invW = 1.0f / (float)gstate_c.curTextureWidth;
|
|
|
|
const float invH = 1.0f / (float)gstate_c.curTextureHeight;
|
|
|
|
const int w = gstate.getTextureWidth(0);
|
|
|
|
const int h = gstate.getTextureHeight(0);
|
|
|
|
widthFactor = (float)w * invW;
|
|
|
|
heightFactor = (float)h * invH;
|
|
|
|
}
|
2013-07-09 23:22:15 +00:00
|
|
|
float uvscaleoff[4];
|
2020-12-13 15:04:16 +00:00
|
|
|
if (gstate_c.submitType == SubmitType::HW_BEZIER || gstate_c.submitType == SubmitType::HW_SPLINE) {
|
2017-01-23 16:44:13 +00:00
|
|
|
// When we are generating UV coordinates through the bezier/spline, we need to apply the scaling.
|
|
|
|
// However, this is missing a check that we're not getting our UV:s supplied for us in the vertices.
|
|
|
|
uvscaleoff[0] = gstate_c.uv.uScale * widthFactor;
|
|
|
|
uvscaleoff[1] = gstate_c.uv.vScale * heightFactor;
|
2017-01-08 18:37:21 +00:00
|
|
|
uvscaleoff[2] = gstate_c.uv.uOff * widthFactor;
|
|
|
|
uvscaleoff[3] = gstate_c.uv.vOff * heightFactor;
|
|
|
|
} else {
|
|
|
|
uvscaleoff[0] = widthFactor;
|
|
|
|
uvscaleoff[1] = heightFactor;
|
|
|
|
uvscaleoff[2] = 0.0f;
|
|
|
|
uvscaleoff[3] = 0.0f;
|
|
|
|
}
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->SetUniformF(&u_uvscaleoffset, 4, uvscaleoff);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:26:08 +00:00
|
|
|
if ((dirty & DIRTY_TEXCLAMP) && u_texclamp != -1) {
|
2014-06-07 19:21:52 +00:00
|
|
|
const float invW = 1.0f / (float)gstate_c.curTextureWidth;
|
|
|
|
const float invH = 1.0f / (float)gstate_c.curTextureHeight;
|
|
|
|
const int w = gstate.getTextureWidth(0);
|
|
|
|
const int h = gstate.getTextureHeight(0);
|
|
|
|
const float widthFactor = (float)w * invW;
|
|
|
|
const float heightFactor = (float)h * invH;
|
|
|
|
|
2014-06-08 02:30:46 +00:00
|
|
|
// First wrap xy, then half texel xy (for clamp.)
|
|
|
|
const float texclamp[4] = {
|
2014-06-07 19:21:52 +00:00
|
|
|
widthFactor,
|
|
|
|
heightFactor,
|
2014-06-08 02:30:46 +00:00
|
|
|
invW * 0.5f,
|
|
|
|
invH * 0.5f,
|
2014-06-07 19:21:52 +00:00
|
|
|
};
|
2014-06-08 20:52:05 +00:00
|
|
|
const float texclampoff[2] = {
|
|
|
|
gstate_c.curTextureXOffset * invW,
|
|
|
|
gstate_c.curTextureYOffset * invH,
|
|
|
|
};
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->SetUniformF(&u_texclamp, 4, texclamp);
|
2014-06-08 21:24:38 +00:00
|
|
|
if (u_texclampoff != -1) {
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->SetUniformF(&u_texclampoff, 2, texclampoff);
|
2014-06-08 21:24:38 +00:00
|
|
|
}
|
2014-06-07 19:21:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 08:43:12 +00:00
|
|
|
if ((dirty & DIRTY_MIPBIAS) && u_mipBias != -1) {
|
2022-07-30 19:33:24 +00:00
|
|
|
float mipBias = (float)gstate.getTexLevelOffset16() * (1.0 / 16.0f);
|
|
|
|
mipBias = (mipBias + 0.5f) / (float)(gstate.getTextureMaxLevel() + 1);
|
|
|
|
|
|
|
|
render_->SetUniformF(&u_mipBias, 1, &mipBias);
|
|
|
|
}
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Transform
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_WORLDMATRIX) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetMatrix4x3(render_, &u_world, gstate.worldMatrix);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2023-07-02 18:29:59 +00:00
|
|
|
if (dirty & DIRTY_VIEWMATRIX) {
|
2022-10-17 16:36:54 +00:00
|
|
|
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
|
2022-08-27 15:33:37 +00:00
|
|
|
float leftEyeView[16];
|
|
|
|
float rightEyeView[16];
|
|
|
|
ConvertMatrix4x3To4x4Transposed(leftEyeView, gstate.viewMatrix);
|
|
|
|
ConvertMatrix4x3To4x4Transposed(rightEyeView, gstate.viewMatrix);
|
2022-11-03 19:00:30 +00:00
|
|
|
if (!is2D) {
|
2022-09-12 16:46:04 +00:00
|
|
|
UpdateVRView(leftEyeView, rightEyeView);
|
2022-08-27 15:33:37 +00:00
|
|
|
}
|
2022-09-05 18:49:25 +00:00
|
|
|
render_->SetUniformM4x4Stereo("u_view", &u_view, leftEyeView, rightEyeView);
|
2022-08-27 15:33:37 +00:00
|
|
|
} else {
|
|
|
|
SetMatrix4x3(render_, &u_view, gstate.viewMatrix);
|
2022-07-25 16:47:54 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & DIRTY_TEXMATRIX) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetMatrix4x3(render_, &u_texmtx, gstate.tgenMatrix);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2018-09-17 06:40:30 +00:00
|
|
|
if (dirty & DIRTY_DEPTHRANGE) {
|
2016-01-19 04:53:38 +00:00
|
|
|
// Since depth is [-1, 1] mapping to [minz, maxz], this is easyish.
|
|
|
|
float vpZScale = gstate.getViewportZScale();
|
|
|
|
float vpZCenter = gstate.getViewportZCenter();
|
2016-01-03 20:11:05 +00:00
|
|
|
|
2016-01-19 04:53:38 +00:00
|
|
|
// These are just the reverse of the formulas in GPUStateUtils.
|
2023-12-12 17:48:22 +00:00
|
|
|
float halfActualZRange = InfToZero(gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f);
|
|
|
|
float inverseDepthScale = InfToZero(gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f);
|
2016-01-19 04:53:38 +00:00
|
|
|
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
|
2016-01-19 04:26:31 +00:00
|
|
|
float viewZScale = halfActualZRange;
|
|
|
|
float viewZCenter = minz + halfActualZRange;
|
2016-01-03 20:11:05 +00:00
|
|
|
|
2022-10-17 06:30:27 +00:00
|
|
|
if (!gstate_c.Use(GPU_USE_ACCURATE_DEPTH)) {
|
2016-02-07 04:18:50 +00:00
|
|
|
viewZScale = vpZScale;
|
|
|
|
viewZCenter = vpZCenter;
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:41:44 +00:00
|
|
|
float data[4] = { viewZScale, viewZCenter, gstate_c.vpZOffset, inverseDepthScale };
|
2017-11-18 23:43:58 +00:00
|
|
|
SetFloatUniform4(render_, &u_depthRange, data);
|
2015-08-26 11:26:08 +00:00
|
|
|
}
|
2018-09-17 05:52:43 +00:00
|
|
|
if (dirty & DIRTY_CULLRANGE) {
|
2018-09-18 05:27:25 +00:00
|
|
|
float minValues[4], maxValues[4];
|
2020-04-04 18:03:07 +00:00
|
|
|
CalcCullRange(minValues, maxValues, !useBufferedRendering, true);
|
2018-09-17 05:52:43 +00:00
|
|
|
SetFloatUniform4(render_, &u_cullRangeMin, minValues);
|
|
|
|
SetFloatUniform4(render_, &u_cullRangeMax, maxValues);
|
|
|
|
}
|
2013-05-18 18:44:25 +00:00
|
|
|
|
2013-12-02 23:09:48 +00:00
|
|
|
if (dirty & DIRTY_STENCILREPLACEVALUE) {
|
2017-11-18 23:43:58 +00:00
|
|
|
float f = (float)gstate.getStencilTestRef() * (1.0f / 255.0f);
|
|
|
|
render_->SetUniformF(&u_stencilReplaceValue, 1, &f);
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2018-04-10 10:22:02 +00:00
|
|
|
float bonetemp[16];
|
|
|
|
for (int i = 0; i < numBones; i++) {
|
|
|
|
if (dirty & (DIRTY_BONEMATRIX0 << i)) {
|
2020-10-23 16:37:37 +00:00
|
|
|
ConvertMatrix4x3To4x4Transposed(bonetemp, gstate.boneMatrix + 12 * i);
|
2018-04-10 10:22:02 +00:00
|
|
|
render_->SetUniformM4x4(&u_bone[i], bonetemp);
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2014-06-14 05:49:28 +00:00
|
|
|
if (dirty & DIRTY_SHADERBLEND) {
|
2014-06-17 06:25:56 +00:00
|
|
|
if (u_blendFixA != -1) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_blendFixA, gstate.getFixA());
|
2014-06-17 06:25:56 +00:00
|
|
|
}
|
|
|
|
if (u_blendFixB != -1) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_blendFixB, gstate.getFixB());
|
2014-06-17 06:25:56 +00:00
|
|
|
}
|
2014-06-14 05:49:28 +00:00
|
|
|
|
|
|
|
const float fbotexSize[2] = {
|
|
|
|
1.0f / (float)gstate_c.curRTRenderWidth,
|
|
|
|
1.0f / (float)gstate_c.curRTRenderHeight,
|
|
|
|
};
|
|
|
|
if (u_fbotexSize != -1) {
|
2017-11-18 23:43:58 +00:00
|
|
|
render_->SetUniformF(&u_fbotexSize, 2, fbotexSize);
|
2014-06-14 05:49:28 +00:00
|
|
|
}
|
2014-05-11 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 13:10:42 +00:00
|
|
|
// Lighting
|
2022-09-25 16:30:27 +00:00
|
|
|
if (dirty & DIRTY_LIGHT_CONTROL) {
|
|
|
|
render_->SetUniformUI1(&u_lightControl, PackLightControlBits());
|
|
|
|
}
|
2013-11-13 09:02:58 +00:00
|
|
|
if (dirty & DIRTY_AMBIENT) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3Alpha(render_, &u_ambient, gstate.ambientcolor, gstate.getAmbientA());
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2013-11-13 09:02:58 +00:00
|
|
|
if (dirty & DIRTY_MATAMBIENTALPHA) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3Alpha(render_, &u_matambientalpha, gstate.materialambient, gstate.getMaterialAmbientA());
|
2012-12-20 13:10:42 +00:00
|
|
|
}
|
2013-11-13 09:02:58 +00:00
|
|
|
if (dirty & DIRTY_MATDIFFUSE) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_matdiffuse, gstate.materialdiffuse);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2013-11-13 09:02:58 +00:00
|
|
|
if (dirty & DIRTY_MATEMISSIVE) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3(render_, &u_matemissive, gstate.materialemissive);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2013-11-13 09:02:58 +00:00
|
|
|
if (dirty & DIRTY_MATSPECULAR) {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetColorUniform3ExtraFloat(render_, &u_matspecular, gstate.materialspecular, getFloat24(gstate.materialspecularcoef));
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
2012-12-20 13:10:42 +00:00
|
|
|
|
2012-12-20 17:31:21 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2013-11-12 15:39:35 +00:00
|
|
|
if (dirty & (DIRTY_LIGHT0 << i)) {
|
2014-11-05 08:57:22 +00:00
|
|
|
if (gstate.isDirectionalLight(i)) {
|
2023-01-11 17:21:07 +00:00
|
|
|
// Prenormalize for cheaper calculations in shader
|
|
|
|
SetFloat24Uniform3Normalized(render_, &u_lightpos[i], &gstate.lpos[i * 3]);
|
2014-11-05 08:57:22 +00:00
|
|
|
} else {
|
2017-11-18 23:43:58 +00:00
|
|
|
SetFloat24Uniform3(render_, &u_lightpos[i], &gstate.lpos[i * 3]);
|
2013-06-20 21:30:21 +00:00
|
|
|
}
|
2023-01-11 17:21:07 +00:00
|
|
|
if (u_lightdir[i] != -1) SetFloat24Uniform3Normalized(render_, &u_lightdir[i], &gstate.ldir[i * 3]);
|
2017-11-18 23:43:58 +00:00
|
|
|
if (u_lightatt[i] != -1) SetFloat24Uniform3(render_, &u_lightatt[i], &gstate.latt[i * 3]);
|
2018-03-12 10:07:51 +00:00
|
|
|
if (u_lightangle_spotCoef[i] != -1) {
|
|
|
|
float lightangle_spotCoef[2] = { getFloat24(gstate.lcutoff[i]), getFloat24(gstate.lconv[i]) };
|
|
|
|
SetFloatUniform2(render_, &u_lightangle_spotCoef[i], lightangle_spotCoef);
|
|
|
|
}
|
2017-11-18 23:43:58 +00:00
|
|
|
if (u_lightambient[i] != -1) SetColorUniform3(render_, &u_lightambient[i], gstate.lcolor[i * 3]);
|
|
|
|
if (u_lightdiffuse[i] != -1) SetColorUniform3(render_, &u_lightdiffuse[i], gstate.lcolor[i * 3 + 1]);
|
|
|
|
if (u_lightspecular[i] != -1) SetColorUniform3(render_, &u_lightspecular[i], gstate.lcolor[i * 3 + 2]);
|
2012-12-20 17:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 18:37:21 +00:00
|
|
|
|
2017-03-20 02:51:44 +00:00
|
|
|
if (dirty & DIRTY_BEZIERSPLINE) {
|
2018-07-14 00:40:13 +00:00
|
|
|
if (u_spline_counts != -1) {
|
2018-09-22 10:20:30 +00:00
|
|
|
render_->SetUniformI1(&u_spline_counts, gstate_c.spline_num_points_u);
|
2018-07-10 16:09:20 +00:00
|
|
|
}
|
2017-01-08 18:37:21 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 04:41:09 +00:00
|
|
|
static constexpr size_t CODE_BUFFER_SIZE = 32768;
|
|
|
|
|
2018-05-29 21:07:22 +00:00
|
|
|
ShaderManagerGLES::ShaderManagerGLES(Draw::DrawContext *draw)
|
2020-11-06 08:01:13 +00:00
|
|
|
: ShaderManagerCommon(draw), fsCache_(16), vsCache_(16) {
|
2018-05-29 21:07:22 +00:00
|
|
|
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
2022-10-06 04:41:09 +00:00
|
|
|
codeBuffer_ = new char[CODE_BUFFER_SIZE];
|
2015-10-14 09:18:45 +00:00
|
|
|
lastFSID_.set_invalid();
|
|
|
|
lastVSID_.set_invalid();
|
2013-02-04 22:09:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 21:16:30 +00:00
|
|
|
ShaderManagerGLES::~ShaderManagerGLES() {
|
2013-02-04 22:09:01 +00:00
|
|
|
delete [] codeBuffer_;
|
|
|
|
}
|
|
|
|
|
2017-01-21 21:16:30 +00:00
|
|
|
void ShaderManagerGLES::Clear() {
|
2013-10-12 08:39:50 +00:00
|
|
|
DirtyLastShader();
|
2013-08-10 16:33:11 +00:00
|
|
|
for (auto iter = linkedShaderCache_.begin(); iter != linkedShaderCache_.end(); ++iter) {
|
2023-01-11 06:12:18 +00:00
|
|
|
iter->ls->Delete();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2017-12-02 17:07:27 +00:00
|
|
|
fsCache_.Iterate([&](const FShaderID &key, Shader *shader) {
|
2017-08-20 17:29:43 +00:00
|
|
|
delete shader;
|
|
|
|
});
|
2017-12-02 17:07:27 +00:00
|
|
|
vsCache_.Iterate([&](const VShaderID &key, Shader *shader) {
|
2017-08-20 17:29:43 +00:00
|
|
|
delete shader;
|
|
|
|
});
|
2013-08-10 16:33:11 +00:00
|
|
|
linkedShaderCache_.clear();
|
2017-08-20 17:29:43 +00:00
|
|
|
fsCache_.Clear();
|
|
|
|
vsCache_.Clear();
|
2023-09-24 09:50:24 +00:00
|
|
|
DirtyLastShader();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 14:59:35 +00:00
|
|
|
void ShaderManagerGLES::ClearShaders() {
|
2016-01-17 19:29:34 +00:00
|
|
|
// TODO: Recreate all from the diskcache when we come back.
|
2012-11-01 15:19:01 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2018-05-29 21:07:22 +00:00
|
|
|
void ShaderManagerGLES::DeviceLost() {
|
|
|
|
Clear();
|
2022-08-24 03:15:30 +00:00
|
|
|
render_ = nullptr;
|
|
|
|
draw_ = nullptr;
|
2018-05-29 21:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderManagerGLES::DeviceRestore(Draw::DrawContext *draw) {
|
|
|
|
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
2018-12-23 20:06:29 +00:00
|
|
|
draw_ = draw;
|
2018-05-29 21:07:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 09:50:24 +00:00
|
|
|
void ShaderManagerGLES::DirtyLastShader() {
|
2012-11-01 15:19:01 +00:00
|
|
|
// Forget the last shader ID
|
2015-10-14 09:18:45 +00:00
|
|
|
lastFSID_.set_invalid();
|
|
|
|
lastVSID_.set_invalid();
|
2017-09-20 15:30:34 +00:00
|
|
|
gstate_c.Dirty(DIRTY_ALL_UNIFORMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE);
|
2017-01-30 13:04:20 +00:00
|
|
|
shaderSwitchDirtyUniforms_ = 0;
|
2015-10-14 09:18:45 +00:00
|
|
|
lastShader_ = nullptr;
|
2015-03-16 02:10:33 +00:00
|
|
|
lastVShaderSame_ = false;
|
2013-04-07 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 12:38:22 +00:00
|
|
|
// Can only fail by failing to generate the code (bad FSID).
|
|
|
|
// Any actual failures driver-side happens later in the render manager.
|
2017-12-02 17:07:27 +00:00
|
|
|
Shader *ShaderManagerGLES::CompileFragmentShader(FShaderID FSID) {
|
2017-11-19 10:23:16 +00:00
|
|
|
uint64_t uniformMask;
|
2020-10-21 21:09:34 +00:00
|
|
|
std::string errorString;
|
2022-12-02 12:49:28 +00:00
|
|
|
FragmentShaderFlags flags;
|
|
|
|
if (!GenerateFragmentShader(FSID, codeBuffer_, draw_->GetShaderLanguageDesc(), draw_->GetBugs(), &uniformMask, &flags, &errorString)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG_REPORT(Log::G3D, "FS shader gen error: %s (%s: %08x:%08x)", errorString.c_str(), "GLES", FSID.d[0], FSID.d[1]);
|
2016-01-17 19:29:34 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-06 04:41:09 +00:00
|
|
|
_assert_msg_(strlen(codeBuffer_) < CODE_BUFFER_SIZE, "FS length error: %d", (int)strlen(codeBuffer_));
|
2018-01-10 09:48:46 +00:00
|
|
|
std::string desc = FragmentShaderDesc(FSID);
|
2021-09-12 00:53:50 +00:00
|
|
|
ShaderDescGLES params{ GL_FRAGMENT_SHADER, 0, uniformMask };
|
|
|
|
return new Shader(render_, codeBuffer_, desc, params);
|
2016-01-17 19:29:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 12:38:22 +00:00
|
|
|
// Can only fail by failing to generate the code (bad VSID).
|
|
|
|
// Any actual failures driver-side happens later in the render manager.
|
2017-12-02 17:07:27 +00:00
|
|
|
Shader *ShaderManagerGLES::CompileVertexShader(VShaderID VSID) {
|
2016-01-17 19:29:34 +00:00
|
|
|
bool useHWTransform = VSID.Bit(VS_BIT_USE_HW_TRANSFORM);
|
2017-11-19 10:08:34 +00:00
|
|
|
uint32_t attrMask;
|
2017-11-19 10:23:16 +00:00
|
|
|
uint64_t uniformMask;
|
2020-10-21 21:09:34 +00:00
|
|
|
std::string errorString;
|
2022-12-02 12:49:28 +00:00
|
|
|
VertexShaderFlags flags;
|
|
|
|
if (!GenerateVertexShader(VSID, codeBuffer_, draw_->GetShaderLanguageDesc(), draw_->GetBugs(), &attrMask, &uniformMask, &flags, &errorString)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG_REPORT(Log::G3D, "VS shader gen error: %s (%s: %08x:%08x)", errorString.c_str(), "GLES", VSID.d[0], VSID.d[1]);
|
2020-10-21 21:09:34 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-06 04:41:09 +00:00
|
|
|
_assert_msg_(strlen(codeBuffer_) < CODE_BUFFER_SIZE, "VS length error: %d", (int)strlen(codeBuffer_));
|
2018-01-10 09:48:46 +00:00
|
|
|
std::string desc = VertexShaderDesc(VSID);
|
2021-09-12 00:53:50 +00:00
|
|
|
ShaderDescGLES params{ GL_VERTEX_SHADER, attrMask, uniformMask };
|
|
|
|
params.useHWTransform = useHWTransform;
|
|
|
|
return new Shader(render_, codeBuffer_, desc, params);
|
2014-12-14 19:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 21:48:17 +00:00
|
|
|
Shader *ShaderManagerGLES::ApplyVertexShader(bool useHWTransform, bool useHWTessellation, VertexDecoder *decoder, bool weightsAsFloat, bool useSkinInDecode, VShaderID *VSID) {
|
2017-01-30 13:04:20 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_VERTEXSHADER_STATE)) {
|
|
|
|
gstate_c.Clean(DIRTY_VERTEXSHADER_STATE);
|
2022-12-14 21:48:17 +00:00
|
|
|
ComputeVertexShaderID(VSID, decoder, useHWTransform, useHWTessellation, weightsAsFloat, useSkinInDecode);
|
2017-01-30 13:04:20 +00:00
|
|
|
} else {
|
|
|
|
*VSID = lastVSID_;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-04-12 00:40:39 +00:00
|
|
|
if (lastShader_ != nullptr && *VSID == lastVSID_) {
|
2014-05-04 23:31:57 +00:00
|
|
|
lastVShaderSame_ = true;
|
2014-12-14 19:16:46 +00:00
|
|
|
return lastShader_->vs_; // Already all set.
|
2014-03-24 09:55:07 +00:00
|
|
|
} else {
|
2014-05-04 23:31:57 +00:00
|
|
|
lastVShaderSame_ = false;
|
2012-12-21 20:49:09 +00:00
|
|
|
}
|
2016-01-17 19:29:34 +00:00
|
|
|
lastVSID_ = *VSID;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2023-09-11 10:02:56 +00:00
|
|
|
Shader *vs;
|
|
|
|
if (vsCache_.Get(*VSID, &vs)) {
|
|
|
|
return vs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertex shader not in cache. Let's compile it.
|
|
|
|
vs = CompileVertexShader(*VSID);
|
2023-09-18 12:38:22 +00:00
|
|
|
if (!vs) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Vertex shader generation failed, falling back to software transform");
|
2023-09-11 10:02:56 +00:00
|
|
|
if (!g_Config.bHideSlowWarnings) {
|
2023-12-20 09:34:34 +00:00
|
|
|
auto gr = GetI18NCategory(I18NCat::GRAPHICS);
|
2023-09-11 10:02:56 +00:00
|
|
|
g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("hardware transform error - falling back to software"), 2.5f);
|
2013-06-06 08:05:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:02:56 +00:00
|
|
|
// TODO: Look for existing shader with the appropriate ID, use that instead of generating a new one - however, need to make sure
|
|
|
|
// that that shader ID is not used when computing the linked shader ID below, because then IDs won't match
|
|
|
|
// next time and we'll do this over and over...
|
|
|
|
|
|
|
|
// Can still work with software transform.
|
|
|
|
VShaderID vsidTemp;
|
|
|
|
ComputeVertexShaderID(&vsidTemp, decoder, false, false, weightsAsFloat, true);
|
|
|
|
vs = CompileVertexShader(vsidTemp);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2023-09-11 10:02:56 +00:00
|
|
|
|
|
|
|
vsCache_.Insert(*VSID, vs);
|
2014-03-24 09:55:07 +00:00
|
|
|
return vs;
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:48:17 +00:00
|
|
|
LinkedShader *ShaderManagerGLES::ApplyFragmentShader(VShaderID VSID, Shader *vs, const ComputedPipelineState &pipelineState, bool useBufferedRendering) {
|
2020-04-12 00:40:39 +00:00
|
|
|
uint64_t dirty = gstate_c.GetDirtyUniforms();
|
|
|
|
if (dirty) {
|
|
|
|
if (lastShader_)
|
|
|
|
lastShader_->dirtyUniforms |= dirty;
|
|
|
|
shaderSwitchDirtyUniforms_ |= dirty;
|
|
|
|
gstate_c.CleanUniforms();
|
|
|
|
}
|
|
|
|
|
2017-12-02 17:07:27 +00:00
|
|
|
FShaderID FSID;
|
2017-03-19 10:32:29 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_FRAGMENTSHADER_STATE)) {
|
|
|
|
gstate_c.Clean(DIRTY_FRAGMENTSHADER_STATE);
|
2022-09-02 20:07:42 +00:00
|
|
|
ComputeFragmentShaderID(&FSID, pipelineState, draw_->GetBugs());
|
2017-03-19 10:32:29 +00:00
|
|
|
} else {
|
|
|
|
FSID = lastFSID_;
|
|
|
|
}
|
|
|
|
|
2014-06-01 01:55:00 +00:00
|
|
|
if (lastVShaderSame_ && FSID == lastFSID_) {
|
2022-12-14 21:48:17 +00:00
|
|
|
lastShader_->UpdateUniforms(VSID, useBufferedRendering, draw_->GetShaderLanguageDesc());
|
2014-03-24 09:55:07 +00:00
|
|
|
return lastShader_;
|
2014-05-04 23:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastFSID_ = FSID;
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2023-09-11 10:02:56 +00:00
|
|
|
Shader *fs;
|
2023-09-11 10:06:06 +00:00
|
|
|
if (!fsCache_.Get(FSID, &fs)) {
|
2012-11-01 15:19:01 +00:00
|
|
|
// Fragment shader not in cache. Let's compile it.
|
2020-11-04 23:48:35 +00:00
|
|
|
// Can't really tell if we succeeded since the compile is on the GPU thread later.
|
2021-08-29 19:50:43 +00:00
|
|
|
// Could fail to generate, in which case we're kinda screwed.
|
2016-01-17 19:29:34 +00:00
|
|
|
fs = CompileFragmentShader(FSID);
|
2023-09-11 10:06:06 +00:00
|
|
|
if (!fs) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Failed to generate fragment shader with ID %08x:%08x", FSID.d[0], FSID.d[1]);
|
2023-09-11 10:06:06 +00:00
|
|
|
// Still insert it so we don't end up spamming generation.
|
|
|
|
}
|
2017-08-20 17:29:43 +00:00
|
|
|
fsCache_.Insert(FSID, fs);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-06-06 08:05:31 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Okay, we have both shaders. Let's see if there's a linked one.
|
2015-10-14 09:18:45 +00:00
|
|
|
LinkedShader *ls = nullptr;
|
2013-06-29 20:49:38 +00:00
|
|
|
|
2017-01-30 13:04:20 +00:00
|
|
|
u64 switchDirty = shaderSwitchDirtyUniforms_;
|
2013-08-10 16:33:11 +00:00
|
|
|
for (auto iter = linkedShaderCache_.begin(); iter != linkedShaderCache_.end(); ++iter) {
|
2013-06-29 20:49:38 +00:00
|
|
|
// Deferred dirtying! Let's see if we can make this even more clever later.
|
2015-07-26 20:38:40 +00:00
|
|
|
iter->ls->dirtyUniforms |= switchDirty;
|
2013-06-29 20:49:38 +00:00
|
|
|
|
|
|
|
if (iter->vs == vs && iter->fs == fs) {
|
|
|
|
ls = iter->ls;
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 13:04:20 +00:00
|
|
|
shaderSwitchDirtyUniforms_ = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-10-14 09:18:45 +00:00
|
|
|
if (ls == nullptr) {
|
2023-05-09 15:50:43 +00:00
|
|
|
_dbg_assert_(FSID.Bit(FS_BIT_LMODE) == VSID.Bit(VS_BIT_LMODE));
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_(FSID.Bit(FS_BIT_FLATSHADE) == VSID.Bit(VS_BIT_FLATSHADE));
|
2017-11-30 23:07:25 +00:00
|
|
|
|
2023-05-01 10:11:34 +00:00
|
|
|
if (vs == nullptr || fs == nullptr) {
|
2023-09-11 10:06:06 +00:00
|
|
|
// Can't draw. This shouldn't really happen (but can happen if fragment shader generation fails)
|
2023-05-01 10:11:34 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-14 19:16:46 +00:00
|
|
|
// Check if we can link these.
|
2017-11-18 23:43:58 +00:00
|
|
|
ls = new LinkedShader(render_, VSID, vs, FSID, fs, vs->UseHWTransform());
|
|
|
|
ls->use(VSID);
|
2013-06-29 20:49:38 +00:00
|
|
|
const LinkedShaderCacheEntry entry(vs, fs, ls);
|
2013-08-10 16:33:11 +00:00
|
|
|
linkedShaderCache_.push_back(entry);
|
2012-11-01 15:19:01 +00:00
|
|
|
} else {
|
2017-11-18 23:43:58 +00:00
|
|
|
ls->use(VSID);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2022-12-14 21:48:17 +00:00
|
|
|
ls->UpdateUniforms(VSID, useBufferedRendering, draw_->GetShaderLanguageDesc());
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-08-10 16:33:11 +00:00
|
|
|
lastShader_ = ls;
|
2012-11-01 15:19:01 +00:00
|
|
|
return ls;
|
|
|
|
}
|
2015-10-14 15:45:21 +00:00
|
|
|
|
2016-01-17 19:29:34 +00:00
|
|
|
std::string Shader::GetShaderString(DebugShaderStringType type, ShaderID id) const {
|
2015-10-14 15:45:21 +00:00
|
|
|
switch (type) {
|
|
|
|
case SHADER_STRING_SOURCE_CODE:
|
|
|
|
return source_;
|
|
|
|
case SHADER_STRING_SHORT_DESC:
|
2019-02-16 14:54:56 +00:00
|
|
|
return isFragment_ ? FragmentShaderDesc(FShaderID(id)) : VertexShaderDesc(VShaderID(id));
|
2015-10-14 15:45:21 +00:00
|
|
|
default:
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 21:16:30 +00:00
|
|
|
std::vector<std::string> ShaderManagerGLES::DebugGetShaderIDs(DebugShaderType type) {
|
2015-10-14 15:45:21 +00:00
|
|
|
std::string id;
|
|
|
|
std::vector<std::string> ids;
|
|
|
|
switch (type) {
|
|
|
|
case SHADER_TYPE_VERTEX:
|
2023-12-29 07:27:56 +00:00
|
|
|
vsCache_.Iterate([&](const VShaderID &id, Shader *shader) {
|
|
|
|
std::string idstr;
|
|
|
|
id.ToString(&idstr);
|
|
|
|
ids.push_back(idstr);
|
|
|
|
});
|
2015-10-14 15:45:21 +00:00
|
|
|
break;
|
|
|
|
case SHADER_TYPE_FRAGMENT:
|
2023-12-29 07:27:56 +00:00
|
|
|
fsCache_.Iterate([&](const FShaderID &id, Shader *shader) {
|
|
|
|
std::string idstr;
|
|
|
|
id.ToString(&idstr);
|
|
|
|
ids.push_back(idstr);
|
|
|
|
});
|
2015-10-14 15:45:21 +00:00
|
|
|
break;
|
2015-11-04 11:46:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
2015-10-14 15:45:21 +00:00
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2017-01-21 21:16:30 +00:00
|
|
|
std::string ShaderManagerGLES::DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) {
|
2015-10-14 15:45:21 +00:00
|
|
|
ShaderID shaderId;
|
|
|
|
shaderId.FromString(id);
|
|
|
|
switch (type) {
|
|
|
|
case SHADER_TYPE_VERTEX:
|
|
|
|
{
|
2023-09-11 10:02:56 +00:00
|
|
|
Shader *vs;
|
|
|
|
if (vsCache_.Get(VShaderID(shaderId), &vs) && vs) {
|
|
|
|
return vs->GetShaderString(stringType, shaderId);
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2015-10-14 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case SHADER_TYPE_FRAGMENT:
|
|
|
|
{
|
2023-09-11 10:02:56 +00:00
|
|
|
Shader *fs;
|
|
|
|
if (fsCache_.Get(FShaderID(shaderId), &fs) && fs) {
|
|
|
|
return fs->GetShaderString(stringType, shaderId);
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2015-10-14 15:45:21 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 16:11:51 +00:00
|
|
|
|
|
|
|
// Shader pseudo-cache.
|
|
|
|
//
|
|
|
|
// We simply store the IDs of the shaders used during gameplay. On next startup of
|
|
|
|
// the same game, we simply compile all the shaders from the start, so we don't have to
|
|
|
|
// compile them on the fly later. Ideally we would store the actual compiled shaders
|
|
|
|
// rather than just their IDs, but OpenGL does not support this, except for a few obscure
|
|
|
|
// vendor-specific extensions.
|
|
|
|
//
|
|
|
|
// If things like GPU supported features have changed since the last time, we discard the cache
|
|
|
|
// as sometimes these features might have an effect on the ID bits.
|
|
|
|
|
2022-12-14 06:26:46 +00:00
|
|
|
enum class CacheDetectFlags {
|
|
|
|
EQUAL_DEPTH = 1,
|
|
|
|
};
|
|
|
|
|
2016-01-17 16:11:51 +00:00
|
|
|
#define CACHE_HEADER_MAGIC 0x83277592
|
2024-10-29 12:19:58 +00:00
|
|
|
#define CACHE_VERSION 37
|
2023-01-10 14:27:05 +00:00
|
|
|
|
2016-01-17 16:11:51 +00:00
|
|
|
struct CacheHeader {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t version;
|
2022-10-17 06:27:49 +00:00
|
|
|
uint32_t useFlags;
|
2022-12-14 06:26:46 +00:00
|
|
|
uint32_t detectFlags;
|
2016-01-17 16:11:51 +00:00
|
|
|
int numVertexShaders;
|
|
|
|
int numFragmentShaders;
|
|
|
|
int numLinkedPrograms;
|
|
|
|
};
|
|
|
|
|
2022-12-14 06:26:46 +00:00
|
|
|
bool ShaderManagerGLES::LoadCacheFlags(File::IOFile &f, DrawEngineGLES *drawEngine) {
|
|
|
|
CacheHeader header;
|
|
|
|
if (!f.ReadArray(&header, 1)) {
|
|
|
|
return false;
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2022-12-14 06:26:46 +00:00
|
|
|
if (header.magic != CACHE_HEADER_MAGIC || header.version != CACHE_VERSION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((header.detectFlags & (uint32_t)CacheDetectFlags::EQUAL_DEPTH) != 0) {
|
|
|
|
drawEngine->SetEverUsedExactEqualDepth(true);
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2022-12-14 06:26:46 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderManagerGLES::LoadCache(File::IOFile &f) {
|
2023-09-24 09:20:27 +00:00
|
|
|
// TODO: Get rid of this struct.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-05 14:04:31 +00:00
|
|
|
bool Done() const {
|
2023-09-24 09:20:27 +00:00
|
|
|
return vertPos >= vert.size() && fragPos >= frag.size() && linkPos >= link.size();
|
|
|
|
}
|
|
|
|
} diskCachePending_;
|
|
|
|
|
2022-12-14 06:26:46 +00:00
|
|
|
u64 sz = f.GetSize();
|
|
|
|
f.Seek(0, SEEK_SET);
|
2016-01-17 16:11:51 +00:00
|
|
|
CacheHeader header;
|
2016-01-21 07:36:06 +00:00
|
|
|
if (!f.ReadArray(&header, 1)) {
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2022-12-14 06:26:46 +00:00
|
|
|
// We don't recheck the version, done in LoadCacheFlags().
|
|
|
|
if (header.useFlags != gstate_c.GetUseFlags()) {
|
|
|
|
return false;
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2017-12-03 19:30:00 +00:00
|
|
|
diskCachePending_.start = time_now_d();
|
|
|
|
diskCachePending_.Clear();
|
2016-01-17 19:53:58 +00:00
|
|
|
|
2017-08-28 13:22:18 +00:00
|
|
|
// Sanity check the file contents
|
2017-12-02 17:14:19 +00:00
|
|
|
if (header.numFragmentShaders > 1000 || header.numVertexShaders > 1000 || header.numLinkedPrograms > 1000) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Corrupt shader cache file header, aborting.");
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2017-12-02 17:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also make sure the size makes sense, in case there's corruption.
|
|
|
|
u64 expectedSize = sizeof(header);
|
|
|
|
expectedSize += header.numVertexShaders * sizeof(VShaderID);
|
|
|
|
expectedSize += header.numFragmentShaders * sizeof(FShaderID);
|
|
|
|
expectedSize += header.numLinkedPrograms * (sizeof(VShaderID) + sizeof(FShaderID));
|
|
|
|
if (sz != expectedSize) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Shader cache file is wrong size: %lld instead of %lld", sz, expectedSize);
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2017-12-02 17:14:19 +00:00
|
|
|
}
|
2017-08-28 13:22:18 +00:00
|
|
|
|
2017-12-03 19:30:00 +00:00
|
|
|
diskCachePending_.vert.resize(header.numVertexShaders);
|
|
|
|
if (!f.ReadArray(&diskCachePending_.vert[0], header.numVertexShaders)) {
|
|
|
|
diskCachePending_.vert.clear();
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2017-12-03 19:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
diskCachePending_.frag.resize(header.numFragmentShaders);
|
|
|
|
if (!f.ReadArray(&diskCachePending_.frag[0], header.numFragmentShaders)) {
|
|
|
|
diskCachePending_.vert.clear();
|
|
|
|
diskCachePending_.frag.clear();
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2017-12-03 19:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < header.numLinkedPrograms; i++) {
|
|
|
|
VShaderID vsid;
|
|
|
|
FShaderID fsid;
|
|
|
|
if (!f.ReadArray(&vsid, 1)) {
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2016-01-21 07:36:06 +00:00
|
|
|
}
|
2017-12-03 19:30:00 +00:00
|
|
|
if (!f.ReadArray(&fsid, 1)) {
|
2022-12-14 06:26:46 +00:00
|
|
|
return false;
|
2017-12-03 19:30:00 +00:00
|
|
|
}
|
2022-09-30 09:35:28 +00:00
|
|
|
diskCachePending_.link.emplace_back(vsid, fsid);
|
2017-12-03 19:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &pending = diskCachePending_;
|
|
|
|
if (pending.Done()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-13 01:06:46 +00:00
|
|
|
PSP_SetLoading("Compiling shaders...");
|
|
|
|
|
2020-09-24 21:52:03 +00:00
|
|
|
double start = time_now_d();
|
2017-12-03 19:30:00 +00:00
|
|
|
|
|
|
|
for (size_t &i = pending.vertPos; i < pending.vert.size(); i++) {
|
|
|
|
const VShaderID &id = pending.vert[i];
|
2023-09-11 10:02:56 +00:00
|
|
|
if (!vsCache_.ContainsKey(id)) {
|
2017-12-01 10:32:16 +00:00
|
|
|
if (id.Bit(VS_BIT_IS_THROUGH) && id.Bit(VS_BIT_USE_HW_TRANSFORM)) {
|
|
|
|
// Clearly corrupt, bailing.
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG_REPORT(Log::G3D, "Corrupt shader cache: Both IS_THROUGH and USE_HW_TRANSFORM set.");
|
2017-12-03 19:30:00 +00:00
|
|
|
pending.Clear();
|
|
|
|
return false;
|
2017-12-01 10:32:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 13:22:18 +00:00
|
|
|
Shader *vs = CompileVertexShader(id);
|
2023-09-18 12:38:22 +00:00
|
|
|
if (!vs) {
|
2017-08-28 13:22:18 +00:00
|
|
|
// Give up on using the cache, just bail. We can't safely create the fallback shaders here
|
|
|
|
// without trying to deduce the vertType from the VSID.
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Failed to compile a vertex shader loading from cache. Skipping rest of shader cache.");
|
2017-12-03 19:30:00 +00:00
|
|
|
pending.Clear();
|
|
|
|
return false;
|
2017-08-28 13:22:18 +00:00
|
|
|
}
|
2017-08-25 20:49:51 +00:00
|
|
|
vsCache_.Insert(id, vs);
|
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::G3D, "Duplicate vertex shader found in GL shader cache, ignoring");
|
2017-08-25 20:49:51 +00:00
|
|
|
}
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2017-12-03 19:30:00 +00:00
|
|
|
|
|
|
|
for (size_t &i = pending.fragPos; i < pending.frag.size(); i++) {
|
|
|
|
const FShaderID &id = pending.frag[i];
|
2023-09-11 10:02:56 +00:00
|
|
|
if (!fsCache_.ContainsKey(id)) {
|
2023-09-18 12:38:22 +00:00
|
|
|
Shader *fs = CompileFragmentShader(id);
|
|
|
|
if (!fs) {
|
|
|
|
// Give up on using the cache - something went wrong.
|
|
|
|
// We'll still keep the shaders we generated so far around.
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::G3D, "Failed to compile a fragment shader loading from cache. Skipping rest of shader cache.");
|
2023-09-18 12:38:22 +00:00
|
|
|
pending.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fsCache_.Insert(id, fs);
|
2017-08-25 20:49:51 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::G3D, "Duplicate fragment shader found in GL shader cache, ignoring");
|
2017-08-25 20:49:51 +00:00
|
|
|
}
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2017-12-03 19:30:00 +00:00
|
|
|
|
2023-12-12 12:13:43 +00:00
|
|
|
linkedShaderCache_.reserve(pending.link.size() - pending.linkPos);
|
2017-12-03 19:30:00 +00:00
|
|
|
for (size_t &i = pending.linkPos; i < pending.link.size(); i++) {
|
|
|
|
const VShaderID &vsid = pending.link[i].first;
|
|
|
|
const FShaderID &fsid = pending.link[i].second;
|
2023-09-11 10:02:56 +00:00
|
|
|
Shader *vs = nullptr;
|
|
|
|
Shader *fs = nullptr;
|
|
|
|
vsCache_.Get(vsid, &vs);
|
|
|
|
fsCache_.Get(fsid, &fs);
|
2017-08-20 17:29:43 +00:00
|
|
|
if (vs && fs) {
|
2017-11-18 23:43:58 +00:00
|
|
|
LinkedShader *ls = new LinkedShader(render_, vsid, vs, fsid, fs, vs->UseHWTransform(), true);
|
2017-08-20 17:29:43 +00:00
|
|
|
LinkedShaderCacheEntry entry(vs, fs, ls);
|
2016-01-21 07:36:06 +00:00
|
|
|
linkedShaderCache_.push_back(entry);
|
|
|
|
}
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
2017-12-03 19:30:00 +00:00
|
|
|
|
|
|
|
// Okay, finally done. Time to report status.
|
|
|
|
double finish = time_now_d();
|
2016-01-17 19:53:58 +00:00
|
|
|
|
2024-07-14 12:42:59 +00:00
|
|
|
NOTICE_LOG(Log::G3D, "Precompile: Compiled and linked %d programs (%d vertex, %d fragment) in %0.1f milliseconds", (int)pending.link.size(), (int)pending.vert.size(), (int)pending.frag.size(), 1000 * (finish - pending.start));
|
2017-12-03 19:30:00 +00:00
|
|
|
pending.Clear();
|
|
|
|
|
|
|
|
return true;
|
2016-01-17 16:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 06:26:46 +00:00
|
|
|
void ShaderManagerGLES::SaveCache(const Path &filename, DrawEngineGLES *drawEngine) {
|
2016-01-17 21:53:06 +00:00
|
|
|
if (linkedShaderCache_.empty()) {
|
2016-01-17 21:11:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-07-14 12:42:59 +00:00
|
|
|
INFO_LOG(Log::G3D, "Saving the shader cache to '%s'", filename.c_str());
|
2016-01-17 16:11:51 +00:00
|
|
|
FILE *f = File::OpenCFile(filename, "wb");
|
|
|
|
if (!f) {
|
|
|
|
// Can't save, give up for now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CacheHeader header;
|
|
|
|
header.magic = CACHE_HEADER_MAGIC;
|
|
|
|
header.version = CACHE_VERSION;
|
2022-12-14 06:26:46 +00:00
|
|
|
header.detectFlags = 0;
|
|
|
|
if (drawEngine->EverUsedExactEqualDepth())
|
|
|
|
header.detectFlags |= (uint32_t)CacheDetectFlags::EQUAL_DEPTH;
|
2022-12-13 15:53:53 +00:00
|
|
|
header.useFlags = gstate_c.GetUseFlags();
|
2017-02-08 17:07:34 +00:00
|
|
|
header.numVertexShaders = GetNumVertexShaders();
|
|
|
|
header.numFragmentShaders = GetNumFragmentShaders();
|
|
|
|
header.numLinkedPrograms = GetNumPrograms();
|
2016-01-17 16:11:51 +00:00
|
|
|
fwrite(&header, 1, sizeof(header), f);
|
2017-08-20 17:29:43 +00:00
|
|
|
vsCache_.Iterate([&](const ShaderID &id, Shader *shader) {
|
2016-01-17 16:11:51 +00:00
|
|
|
fwrite(&id, 1, sizeof(id), f);
|
2017-08-20 17:29:43 +00:00
|
|
|
});
|
|
|
|
fsCache_.Iterate([&](const ShaderID &id, Shader *shader) {
|
2016-01-17 16:11:51 +00:00
|
|
|
fwrite(&id, 1, sizeof(id), f);
|
2017-08-20 17:29:43 +00:00
|
|
|
});
|
2023-12-12 14:38:50 +00:00
|
|
|
for (const auto &iter : linkedShaderCache_) {
|
2016-01-17 16:11:51 +00:00
|
|
|
ShaderID vsid, fsid;
|
2017-08-20 17:29:43 +00:00
|
|
|
vsCache_.Iterate([&](const ShaderID &id, Shader *shader) {
|
|
|
|
if (iter.vs == shader)
|
|
|
|
vsid = id;
|
|
|
|
});
|
|
|
|
fsCache_.Iterate([&](const ShaderID &id, Shader *shader) {
|
|
|
|
if (iter.fs == shader)
|
|
|
|
fsid = id;
|
|
|
|
});
|
2016-01-17 16:11:51 +00:00
|
|
|
fwrite(&vsid, 1, sizeof(vsid), f);
|
|
|
|
fwrite(&fsid, 1, sizeof(fsid), f);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|