1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 //#include <boost/functional/hash.hpp>
4 #include "Rendering/Shaders/ShaderStates.h"
5 #include "System/Sync/HsiehHash.h"
6 #include <unordered_set>
7 
8 
9 static std::unordered_set<int> samplerTypes{
10 #ifndef HEADLESS
11 	GL_SAMPLER_1D,
12 	GL_SAMPLER_2D,
13 	GL_SAMPLER_3D,
14 	GL_SAMPLER_CUBE,
15 	GL_SAMPLER_1D_SHADOW,
16 	GL_SAMPLER_2D_SHADOW,
17 	GL_SAMPLER_1D_ARRAY,
18 	GL_SAMPLER_2D_ARRAY,
19 	GL_SAMPLER_1D_ARRAY_SHADOW,
20 	GL_SAMPLER_2D_ARRAY_SHADOW,
21 	GL_SAMPLER_2D_MULTISAMPLE,
22 	GL_SAMPLER_2D_MULTISAMPLE_ARRAY,
23 	GL_SAMPLER_CUBE_SHADOW,
24 	GL_SAMPLER_BUFFER,
25 	GL_SAMPLER_2D_RECT,
26 	GL_SAMPLER_2D_RECT_SHADOW,
27 
28 	GL_INT_SAMPLER_1D,
29 	GL_INT_SAMPLER_2D,
30 	GL_INT_SAMPLER_3D,
31 	GL_INT_SAMPLER_CUBE,
32 	GL_INT_SAMPLER_1D_ARRAY,
33 	GL_INT_SAMPLER_2D_ARRAY,
34 	GL_INT_SAMPLER_2D_MULTISAMPLE,
35 	GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
36 	GL_INT_SAMPLER_BUFFER,
37 	GL_INT_SAMPLER_2D_RECT,
38 
39 	GL_UNSIGNED_INT_SAMPLER_1D,
40 	GL_UNSIGNED_INT_SAMPLER_2D,
41 	GL_UNSIGNED_INT_SAMPLER_3D,
42 	GL_UNSIGNED_INT_SAMPLER_CUBE,
43 	GL_UNSIGNED_INT_SAMPLER_1D_ARRAY,
44 	GL_UNSIGNED_INT_SAMPLER_2D_ARRAY,
45 	GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE,
46 	GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
47 	GL_UNSIGNED_INT_SAMPLER_BUFFER,
48 	GL_UNSIGNED_INT_SAMPLER_2D_RECT,
49 #endif
50 };
51 
52 
53 namespace Shader {
Hash(const int v0,const int v1,const int v2,const int v3) const54 	int UniformState::Hash(const int v0, const int v1, const int v2, const int v3) const
55 	{
56 		int hash = ~0;
57 		hash += v0 ^ (hash * 33);
58 		hash += v1 ^ (hash * 33);
59 		hash += v2 ^ (hash * 33);
60 		hash += v3 ^ (hash * 33);
61 		return hash;
62 	}
63 
64 
Hash(const int * v,int count) const65 	int UniformState::Hash(const int* v, int count) const
66 	{
67 		int hash = ~0;
68 		for (int n = 0; n < count; ++n) {
69 			hash += v[n] ^ (hash * 33);
70 		}
71 		return hash;
72 	}
73 
74 
IsLocationValid() const75 	bool UniformState::IsLocationValid() const
76 	{
77 	#ifdef HEADLESS
78 		// our stub headers are outdated and are missing GL_INVALID_INDEX
79 		return false;
80 	#else
81 		return (location != GL_INVALID_INDEX);
82 	#endif
83 	}
84 
85 
86 #ifdef DEBUG
AssertType(int type) const87 	void UniformState::AssertType(int type) const
88 	{
89 		int utype = this->type;
90 		if (samplerTypes.find(utype) != samplerTypes.end())
91 			utype = GL_INT;
92 		assert(type == utype || utype == -1);
93 	}
94 #endif
95 
96 
GetHash()97 	unsigned int SShaderFlagState::GetHash()
98 	{
99 		if (updates != lastUpdates) {
100 			const std::string defs = GetString();
101 
102 			lastUpdates = updates;
103 			lastHash = HsiehHash(&defs[0], defs.length(), 0);
104 		}
105 		return lastHash;
106 	}
107 }
108