mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-06-03 12:27:21 +12:00
[Shader JIT] Add caching
This commit is contained in:
parent
364443d66f
commit
4064abfdeb
9 changed files with 77 additions and 5 deletions
|
@ -1,22 +1,35 @@
|
|||
#pragma once
|
||||
#include "PICA/shader.hpp"
|
||||
|
||||
#if defined(PANDA3DS_DYNAPICA_SUPPORTED) && defined(PANDA3DS_X64_HOST)
|
||||
#define PANDA3DS_SHADER_JIT_SUPPORTED
|
||||
#include <unordered_map>
|
||||
#endif
|
||||
|
||||
class ShaderJIT {
|
||||
#ifdef PANDA3DS_SHADER_JIT_SUPPORTED
|
||||
using Hash = PICAShader::Hash;
|
||||
using ShaderCache = std::unordered_map<Hash, int>;
|
||||
|
||||
ShaderCache cache;
|
||||
void compileShader(PICAShader& shaderUnit);
|
||||
#endif
|
||||
|
||||
public:
|
||||
#if defined(PANDA3DS_DYNAPICA_SUPPORTED) && defined(PANDA3DS_X64_HOST)
|
||||
#define PANDA3DS_SHADER_JIT_SUPPORTED
|
||||
|
||||
#ifdef PANDA3DS_SHADER_JIT_SUPPORTED
|
||||
// Call this before starting to process a batch of vertices
|
||||
// This will read the PICA config (uploaded shader and shader operand descriptors) and search if we've already compiled this shader
|
||||
// If yes, it sets it as the active shader. if not, then it compiles it, adds it to the cache, and sets it as active,
|
||||
void prepare(PICAShader& shaderUnit);
|
||||
void reset();
|
||||
|
||||
static constexpr bool isAvailable() { return true; }
|
||||
#else
|
||||
void prepare(PICAShader& shaderUnit) {
|
||||
Helpers::panic("Vertex Loader JIT: Tried to load vertices with JIT on platform that does not support vertex loader jit");
|
||||
}
|
||||
|
||||
void reset() {}
|
||||
static constexpr bool isAvailable() { return false; }
|
||||
#endif
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@ class GPU {
|
|||
|
||||
Memory& mem;
|
||||
ShaderUnit shaderUnit;
|
||||
ShaderJIT shaderJIT; // Doesn't do anything if JIT is disabled or not supported
|
||||
|
||||
u8* vram = nullptr;
|
||||
MAKE_LOG_FUNCTION(log, gpuLogger)
|
||||
|
||||
|
|
|
@ -89,6 +89,17 @@ protected:
|
|||
std::array<CallInfo, 4> callInfo;
|
||||
ShaderType type;
|
||||
|
||||
// We use a hashmap for matching 3DS shaders to their equivalent compiled code in our shader cache in the shader JIT
|
||||
// We choose our hash type to be a 64-bit integer by default, as the collision chance is very tiny and generating it is decently optimal
|
||||
// Ideally we want to be able to support multiple different types of hash depending on compilation settings, but let's get this working first
|
||||
using Hash = u64;
|
||||
|
||||
Hash lastCodeHash = 0; // Last hash computed for the shader code (Used for the JIT caching mechanism)
|
||||
Hash lastOpdescHash = 0; // Last hash computed for the operand descriptors (Also used for the JIT)
|
||||
|
||||
bool codeHashDirty = false;
|
||||
bool opdescHashDirty = false;
|
||||
|
||||
friend class ShaderJIT;
|
||||
|
||||
private:
|
||||
|
@ -204,11 +215,15 @@ public:
|
|||
if (bufferIndex >= 4095) Helpers::panic("o no, shader upload overflew");
|
||||
bufferedShader[bufferIndex++] = word;
|
||||
bufferIndex &= 0xfff;
|
||||
|
||||
codeHashDirty = true; // Signal the JIT if necessary that the program hash has potentially changed
|
||||
}
|
||||
|
||||
void uploadDescriptor(u32 word) {
|
||||
operandDescriptors[opDescriptorIndex++] = word;
|
||||
opDescriptorIndex &= 0x7f;
|
||||
|
||||
opdescHashDirty = true; // Signal the JIT if necessary that the program hash has potentially changed
|
||||
}
|
||||
|
||||
void setFloatUniformIndex(u32 word) {
|
||||
|
@ -250,4 +265,7 @@ public:
|
|||
|
||||
void run();
|
||||
void reset();
|
||||
|
||||
Hash getCodeHash();
|
||||
Hash getOpdescHash();
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue