File: ShaderHandler.h

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (85 lines) | stat: -rw-r--r-- 2,346 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef SPRING_SHADERHANDLER_HDR
#define SPRING_SHADERHANDLER_HDR

#include <string>
#include <unordered_map>
#include "Rendering/GL/myGL.h" //GLuint

namespace Shader {
	struct IProgramObject;
	struct IShaderObject;
};

class CShaderHandler {
public:
	typedef std::unordered_map<std::string, Shader::IProgramObject*> ProgramObjMap;
	typedef std::unordered_map<std::string, Shader::IProgramObject*>::iterator ProgramObjMapIt;
	typedef std::unordered_map<std::string, ProgramObjMap> ProgramTable;

	~CShaderHandler();

	static CShaderHandler* GetInstance(unsigned int instanceValue);
	static void FreeInstance(CShaderHandler*);

	void ReloadAll();
	bool ReleaseProgramObjects(const std::string& poClass);
	void ReleaseProgramObjectsMap(ProgramObjMap& poMap);

	Shader::IProgramObject* GetProgramObject(const std::string& poClass, const std::string& poName);
	Shader::IProgramObject* CreateProgramObject(const std::string& poClass, const std::string& poName, bool arbProgram);
	/**
	 * @param soName The filepath to the shader.
	 * @param soDefs Additional preprocessor flags passed as header.
	 * @param soType In case of an ARB shader it must be either GL_VERTEX_PROGRAM_ARB & GL_FRAGMENT_PROGRAM_ARB.
	 *               In case of an GLSL shader it can be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, ...
	 */
	Shader::IShaderObject* CreateShaderObject(const std::string& soName, const std::string& soDefs, int soType);


	struct ShaderCache {
	public:
		void Clear() { cache.clear(); }

		unsigned int Find(unsigned int hash) {
			const auto it = cache.find(hash);

			GLuint id = 0;

			if (it != cache.end()) {
				id = it->second;
				cache.erase(it);
			}

			return id;
		}

		bool Push(unsigned int hash, unsigned int objID) {
			const auto it = cache.find(hash);

			if (it == cache.end()) {
				cache[hash] = objID;
				return true;
			}

			return false;
		}

	private:
		std::unordered_map<size_t, GLuint> cache;
	};

	const ShaderCache& GetShaderCache() const { return shaderCache; }
	      ShaderCache& GetShaderCache()       { return shaderCache; }

private:
	// all created programs, by name
	ProgramTable programObjects;
	// all (re)loaded program ID's, by hash
	ShaderCache shaderCache;
};

#define shaderHandler (CShaderHandler::GetInstance(1))

#endif