Add shader cache

This commit is contained in:
wheremyfoodat 2024-03-02 20:41:23 +02:00
parent fdfb012aa1
commit 67fe3214fe
3 changed files with 106 additions and 58 deletions

View file

@ -1,9 +1,13 @@
#pragma once
#include <array>
#include <cstring>
#include <functional>
#include <span>
#include <unordered_map>
#include "PICA/float_types.hpp"
#include "PICA/pica_hash.hpp"
#include "PICA/pica_vertex.hpp"
#include "PICA/regs.hpp"
#include "PICA/shader_gen.hpp"
@ -17,6 +21,32 @@
// More circular dependencies!
class GPU;
namespace PICA {
struct FragmentConfig {
u32 texUnitConfig;
u32 texEnvUpdateBuffer;
// TODO: This should probably be a uniform
u32 texEnvBufferColor;
// There's 6 TEV stages, and each one is configured via 5 word-sized registers
std::array<u32, 5 * 6> tevConfigs;
// Hash function and equality operator required by std::unordered_map
bool operator==(const FragmentConfig& config) const {
return std::memcmp(this, &config, sizeof(FragmentConfig)) == 0;
}
};
} // namespace PICA
// Override std::hash for our fragment config class
template <>
struct std::hash<PICA::FragmentConfig> {
std::size_t operator()(const PICA::FragmentConfig& config) const noexcept {
return PICAHash::computeHash((const char*)&config, sizeof(config));
}
};
class RendererGL final : public Renderer {
GLStateManager gl = {};
@ -26,20 +56,23 @@ class RendererGL final : public Renderer {
OpenGL::VertexArray vao;
OpenGL::VertexBuffer vbo;
// TEV configuration uniform locations
GLint textureEnvSourceLoc = -1;
GLint textureEnvOperandLoc = -1;
GLint textureEnvCombinerLoc = -1;
GLint textureEnvColorLoc = -1;
GLint textureEnvScaleLoc = -1;
// Data
struct {
// TEV configuration uniform locations
GLint textureEnvSourceLoc = -1;
GLint textureEnvOperandLoc = -1;
GLint textureEnvCombinerLoc = -1;
GLint textureEnvColorLoc = -1;
GLint textureEnvScaleLoc = -1;
// Uniform of PICA registers
GLint picaRegLoc = -1;
// Uniform of PICA registers
GLint picaRegLoc = -1;
// Depth configuration uniform locations
GLint depthOffsetLoc = -1;
GLint depthScaleLoc = -1;
GLint depthmapEnableLoc = -1;
// Depth configuration uniform locations
GLint depthOffsetLoc = -1;
GLint depthScaleLoc = -1;
GLint depthmapEnableLoc = -1;
} ubershaderData;
float oldDepthScale = -1.0;
float oldDepthOffset = 0.0;
@ -48,6 +81,7 @@ class RendererGL final : public Renderer {
SurfaceCache<DepthBuffer, 16, true> depthBufferCache;
SurfaceCache<ColourBuffer, 16, true> colourBufferCache;
SurfaceCache<Texture, 256, true> textureCache;
bool usingUbershader = false;
// Dummy VAO/VBO for blitting the final output
OpenGL::VertexArray dummyVAO;
@ -58,9 +92,11 @@ class RendererGL final : public Renderer {
OpenGL::Framebuffer screenFramebuffer;
OpenGL::Texture blankTexture;
std::unordered_map<PICA::FragmentConfig, OpenGL::Program> shaderCache;
OpenGL::Framebuffer getColourFBO();
OpenGL::Texture getTexture(Texture& tex);
OpenGL::Program getSpecializedShader();
OpenGL::Program& getSpecializedShader();
PICA::ShaderGen::FragmentGenerator fragShaderGen;
@ -99,4 +135,4 @@ class RendererGL final : public Renderer {
// Take a screenshot of the screen and store it in a file
void screenshot(const std::string& name) override;
};
};