File: ShaderStates.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (116 lines) | stat: -rw-r--r-- 2,812 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "Rendering/Shaders/ShaderStates.h"
#include "System/Sync/HsiehHash.h"
#include "System/UnorderedSet.hpp"


static spring::unordered_set<int> samplerTypes{
#ifndef HEADLESS
	GL_SAMPLER_1D,
	GL_SAMPLER_2D,
	GL_SAMPLER_3D,
	GL_SAMPLER_CUBE,
	GL_SAMPLER_1D_SHADOW,
	GL_SAMPLER_2D_SHADOW,
	GL_SAMPLER_1D_ARRAY,
	GL_SAMPLER_2D_ARRAY,
	GL_SAMPLER_1D_ARRAY_SHADOW,
	GL_SAMPLER_2D_ARRAY_SHADOW,
	GL_SAMPLER_2D_MULTISAMPLE,
	GL_SAMPLER_2D_MULTISAMPLE_ARRAY,
	GL_SAMPLER_CUBE_SHADOW,
	GL_SAMPLER_BUFFER,
	GL_SAMPLER_2D_RECT,
	GL_SAMPLER_2D_RECT_SHADOW,

	GL_INT_SAMPLER_1D,
	GL_INT_SAMPLER_2D,
	GL_INT_SAMPLER_3D,
	GL_INT_SAMPLER_CUBE,
	GL_INT_SAMPLER_1D_ARRAY,
	GL_INT_SAMPLER_2D_ARRAY,
	GL_INT_SAMPLER_2D_MULTISAMPLE,
	GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
	GL_INT_SAMPLER_BUFFER,
	GL_INT_SAMPLER_2D_RECT,

	GL_UNSIGNED_INT_SAMPLER_1D,
	GL_UNSIGNED_INT_SAMPLER_2D,
	GL_UNSIGNED_INT_SAMPLER_3D,
	GL_UNSIGNED_INT_SAMPLER_CUBE,
	GL_UNSIGNED_INT_SAMPLER_1D_ARRAY,
	GL_UNSIGNED_INT_SAMPLER_2D_ARRAY,
	GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE,
	GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
	GL_UNSIGNED_INT_SAMPLER_BUFFER,
	GL_UNSIGNED_INT_SAMPLER_2D_RECT,
#endif
};


namespace Shader {
	int UniformState::Hash(const int v0, const int v1, const int v2, const int v3) const
	{
		int hash = ~0;
		hash += v0 ^ (hash * 33);
		hash += v1 ^ (hash * 33);
		hash += v2 ^ (hash * 33);
		hash += v3 ^ (hash * 33);
		return hash;
	}


	int UniformState::Hash(const int* v, int count) const
	{
		int hash = ~0;
		for (int n = 0; n < count; ++n) {
			hash += v[n] ^ (hash * 33);
		}
		return hash;
	}


	bool UniformState::IsLocationValid() const
	{
	#ifdef HEADLESS
		// our stub headers are outdated and are missing GL_INVALID_INDEX
		return false;
	#else
		return (location != GL_INVALID_INDEX);
	#endif
	}


#ifdef DEBUG
	void UniformState::AssertType(int type) const
	{
		int utype = this->type;
		if (samplerTypes.find(utype) != samplerTypes.end())
			utype = GL_INT;
		assert(type == utype || utype == -1);
	}
#endif


	unsigned int ShaderFlags::CalcHash() const {
		unsigned int hash = 997;

		for (const auto& p: bitFlags) {
			hash = HsiehHash(p.first, (p.second).first, hash);
			hash = HsiehHash(reinterpret_cast<const uint8_t*>(&((p.second).second)), sizeof((p.second).second), hash);
		}
		for (const auto& p: intFlags) {
			hash = HsiehHash(p.first, (p.second).first, hash);
			hash = HsiehHash(reinterpret_cast<const uint8_t*>(&((p.second).second)), sizeof((p.second).second), hash);
		}
		for (const auto& p: fltFlags) {
			hash = HsiehHash(p.first, (p.second).first, hash);
			hash = HsiehHash(reinterpret_cast<const uint8_t*>(&((p.second).second)), sizeof((p.second).second), hash);
		}

		assert(hash != 0);
		return hash;
	}
}