From f0f7327b90a65a5ca983719b07a9c66f93f1516e Mon Sep 17 00:00:00 2001
From: wheremyfoodat <gponiris2004@gmail.com>
Date: Wed, 14 Jun 2023 15:39:29 +0300
Subject: [PATCH] [PICA] Switch to xxh3 by default

---
 include/PICA/pica_hash.hpp    | 16 +++++++++++++
 include/PICA/shader.hpp       |  3 ++-
 src/core/PICA/pica_hash.cpp   | 42 +++++++++++++++++++++++++++++++++++
 src/core/PICA/shader_unit.cpp | 22 ------------------
 4 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/include/PICA/pica_hash.hpp b/include/PICA/pica_hash.hpp
index e69de29b..fa34e153 100644
--- a/include/PICA/pica_hash.hpp
+++ b/include/PICA/pica_hash.hpp
@@ -0,0 +1,16 @@
+#pragma once
+#include <cstdint>
+#include <cstddef>
+
+// Defines to pick which hash algorithm to use for the PICA (For hashing shaders, etc)
+// Please only define one of them
+//#define PANDA3DS_PICA_CITYHASH
+#define PANDA3DS_PICA_XXHASH3
+
+namespace PICAHash {
+	#if defined(PANDA3DS_PICA_CITYHASH) || defined(PANDA3DS_PICA_XXHASH3)
+	using HashType = std::uint64_t;
+	#endif
+
+	HashType computeHash(const char* buf, std::size_t len);
+} // namespace PICAHash
\ No newline at end of file
diff --git a/include/PICA/shader.hpp b/include/PICA/shader.hpp
index 94e055ba..5822dd83 100644
--- a/include/PICA/shader.hpp
+++ b/include/PICA/shader.hpp
@@ -5,6 +5,7 @@
 #include "helpers.hpp"
 #include "opengl.hpp"
 #include "PICA/float_types.hpp"
+#include "PICA/pica_hash.hpp"
 
 enum class ShaderType {
 	Vertex, Geometry
@@ -105,7 +106,7 @@ protected:
 	// 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;
+	using Hash = PICAHash::HashType;
 
 	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)
diff --git a/src/core/PICA/pica_hash.cpp b/src/core/PICA/pica_hash.cpp
index e69de29b..97e04ef3 100644
--- a/src/core/PICA/pica_hash.cpp
+++ b/src/core/PICA/pica_hash.cpp
@@ -0,0 +1,42 @@
+#include "PICA/pica_hash.hpp"
+#include "PICA/shader.hpp"
+
+#ifdef PANDA3DS_PICA_CITYHASH
+#include "cityhash.hpp"
+#elif defined(PANDA3DS_PICA_XXHASH3)
+#include "xxhash/xxhash.h"
+#else
+#error No hashing algorithm defined for the PICA (See pica_hash.hpp for details)
+#endif
+
+PICAHash::HashType PICAHash::computeHash(const char* buf, std::size_t len) {
+#ifdef PANDA3DS_PICA_CITYHASH
+	return CityHash::CityHash64(buf, len);
+#elif defined(PANDA3DS_PICA_XXHASH3)
+	return XXH3_64bits(buf, len);
+#else
+#error No hashing algorithm defined for PICAHash::computeHash
+#endif
+}
+
+PICAShader::Hash PICAShader::getCodeHash() {
+	// Hash the code again if the code changed
+	if (codeHashDirty) {
+		codeHashDirty = false;
+		lastCodeHash = PICAHash::computeHash((const char*)&loadedShader[0], loadedShader.size() * sizeof(loadedShader[0]));
+	}
+
+	// Return the code hash
+	return lastCodeHash;
+}
+
+PICAShader::Hash PICAShader::getOpdescHash() {
+	// Hash the code again if the operand descriptors changed
+	if (opdescHashDirty) {
+		opdescHashDirty = false;
+		lastOpdescHash = PICAHash::computeHash((const char*)&operandDescriptors[0], operandDescriptors.size() * sizeof(operandDescriptors[0]));
+	}
+
+	// Return the code hash
+	return lastOpdescHash;
+}
\ No newline at end of file
diff --git a/src/core/PICA/shader_unit.cpp b/src/core/PICA/shader_unit.cpp
index 29541bd0..6cbc2693 100644
--- a/src/core/PICA/shader_unit.cpp
+++ b/src/core/PICA/shader_unit.cpp
@@ -34,26 +34,4 @@ void PICAShader::reset() {
 
 	codeHashDirty = true;
 	opdescHashDirty = true;
-}
-
-PICAShader::Hash PICAShader::getCodeHash() {
-	// Hash the code again if the code changed
-	if (codeHashDirty) {
-		codeHashDirty = false;
-		lastCodeHash = CityHash::CityHash64((const char*)&loadedShader[0], loadedShader.size() * sizeof(loadedShader[0]));
-	}
-
-	// Return the code hash
-	return lastCodeHash;
-}
-
-PICAShader::Hash PICAShader::getOpdescHash() {
-	// Hash the code again if the operand descriptors changed
-	if (opdescHashDirty) {
-		opdescHashDirty = false;
-		lastOpdescHash = CityHash::CityHash64((const char*)&operandDescriptors[0], operandDescriptors.size() * sizeof(operandDescriptors[0]));
-	}
-
-	// Return the code hash
-	return lastOpdescHash;
 }
\ No newline at end of file