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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef SPRING_SHADER_HDR
#define SPRING_SHADER_HDR
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include "ShaderStates.h"
constexpr size_t hashString(const char* str, size_t hash = 5381)
{
return (*str) ? hashString(str + 1, hash + (hash << 5) + *str) : hash;
}
struct fast_hash : public std::unary_function<int, size_t>
{
size_t operator()(const int a) const
{
return a;
}
};
namespace Shader {
struct IShaderObject {
public:
IShaderObject(unsigned int shType, const std::string& shSrcFile, const std::string& shSrcDefs = ""):
objID(0), type(shType), valid(false), srcFile(shSrcFile), rawDefStrs(shSrcDefs) {
}
virtual ~IShaderObject() {}
virtual void Compile(bool reloadFromDisk) {}
virtual void Release() {}
unsigned int GetObjID() const { return objID; }
unsigned int GetType() const { return type; }
bool IsValid() const { return valid; }
const std::string& GetLog() const { return log; }
void SetDefinitions(const std::string& defs) { modDefStrs = defs; }
protected:
unsigned int objID;
unsigned int type;
bool valid;
std::string srcFile;
std::string curShaderSrc;
std::string modDefStrs;
std::string rawDefStrs;
std::string log;
};
struct NullShaderObject: public Shader::IShaderObject {
public:
NullShaderObject(unsigned int shType, const std::string& shSrcFile) : IShaderObject(shType, shSrcFile) {}
};
struct ARBShaderObject: public Shader::IShaderObject {
public:
ARBShaderObject(unsigned int, const std::string&, const std::string& shSrcDefs = "");
void Compile(bool reloadFromDisk);
void Release();
};
struct GLSLShaderObject: public Shader::IShaderObject {
public:
GLSLShaderObject(unsigned int, const std::string&, const std::string& shSrcDefs = "");
void Compile(bool reloadFromDisk);
void Release();
};
struct IProgramObject : public SShaderFlagState {
public:
IProgramObject(const std::string& poName);
virtual ~IProgramObject() {}
virtual void Enable();
virtual void Disable();
virtual void Link() {}
virtual void Validate() {}
virtual void Release() = 0;
virtual void Reload(bool reloadFromDisk) = 0;
void PrintInfo();
private:
virtual int GetUniformLoc(const std::string& name) = 0;
virtual int GetUniformType(const int loc) = 0;
private:
UniformState* GetNewUniformState(const std::string name);
public:
int GetUniformLocation(const std::string& name) {
return GetUniformState(name)->GetLocation();
}
UniformState* GetUniformState(const std::string& name) {
const auto hash = hashString(name.c_str()); // never compiletime const (std::string is never a literal)
auto it = uniformStates.find(hash);
if (it != uniformStates.end())
return &it->second;
return GetNewUniformState(name);
}
UniformState* GetUniformState(const char* name) {
// (when inlined) hash might be compiletime const cause of constexpr of hashString
// WARNING: Cause of a bug in gcc, you _must_ assign the constexpr to a var before
// passing it to a function. I.e. foo.find(hashString(name)) would always
// be runtime evaluated (even when `name` is a literal)!
const auto hash = hashString(name);
auto it = uniformStates.find(hash);
if (it != uniformStates.end())
return &it->second;
return GetNewUniformState(name);
}
public:
template<typename TK, typename TV> inline void SetUniform(const TK& name, TV v0) { SetUniform(GetUniformState(name), v0); }
template<typename TK, typename TV> inline void SetUniform(const TK& name, TV v0, TV v1) { SetUniform(GetUniformState(name), v0, v1); }
template<typename TK, typename TV> inline void SetUniform(const TK& name, TV v0, TV v1, TV v2) { SetUniform(GetUniformState(name), v0, v1, v2); }
template<typename TK, typename TV> inline void SetUniform(const TK& name, TV v0, TV v1, TV v2, TV v3) { SetUniform(GetUniformState(name), v0, v1, v2, v3); }
template<typename TK, typename TV> inline void SetUniform2v(const TK& name, const TV* v) { SetUniform2v(GetUniformState(name), v); }
template<typename TK, typename TV> inline void SetUniform3v(const TK& name, const TV* v) { SetUniform3v(GetUniformState(name), v); }
template<typename TK, typename TV> inline void SetUniform4v(const TK& name, const TV* v) { SetUniform4v(GetUniformState(name), v); }
template<typename TK, typename TV> inline void SetUniformMatrix2x2(const TK& name, bool transp, const TV* v) { SetUniformMatrix2x2(GetUniformState(name), transp, v); }
template<typename TK, typename TV> inline void SetUniformMatrix3x3(const TK& name, bool transp, const TV* v) { SetUniformMatrix3x3(GetUniformState(name), transp, v); }
template<typename TK, typename TV> inline void SetUniformMatrix4x4(const TK& name, bool transp, const TV* v) { SetUniformMatrix4x4(GetUniformState(name), transp, v); }
virtual void SetUniform(UniformState* uState, int v0) { SetUniform1i(uState->GetLocation(), v0); }
virtual void SetUniform(UniformState* uState, float v0) { SetUniform1f(uState->GetLocation(), v0); }
virtual void SetUniform(UniformState* uState, int v0, int v1) { SetUniform2i(uState->GetLocation(), v0, v1); }
virtual void SetUniform(UniformState* uState, float v0, float v1) { SetUniform2f(uState->GetLocation(), v0, v1); }
virtual void SetUniform(UniformState* uState, int v0, int v1, int v2) { SetUniform3i(uState->GetLocation(), v0, v1, v2); }
virtual void SetUniform(UniformState* uState, float v0, float v1, float v2) { SetUniform3f(uState->GetLocation(), v0, v1, v2); }
virtual void SetUniform(UniformState* uState, int v0, int v1, int v2, int v3) { SetUniform4i(uState->GetLocation(), v0, v1, v2, v3); }
virtual void SetUniform(UniformState* uState, float v0, float v1, float v2, float v3) { SetUniform4f(uState->GetLocation(), v0, v1, v2, v3); }
virtual void SetUniform2v(UniformState* uState, const int* v) { SetUniform2iv(uState->GetLocation(), v); }
virtual void SetUniform2v(UniformState* uState, const float* v) { SetUniform2fv(uState->GetLocation(), v); }
virtual void SetUniform3v(UniformState* uState, const int* v) { SetUniform3iv(uState->GetLocation(), v); }
virtual void SetUniform3v(UniformState* uState, const float* v) { SetUniform3fv(uState->GetLocation(), v); }
virtual void SetUniform4v(UniformState* uState, const int* v) { SetUniform4iv(uState->GetLocation(), v); }
virtual void SetUniform4v(UniformState* uState, const float* v) { SetUniform4fv(uState->GetLocation(), v); }
virtual void SetUniformMatrix2x2(UniformState* uState, bool transp, const float* m) { SetUniformMatrix2fv(uState->GetLocation(), transp, m); }
virtual void SetUniformMatrix3x3(UniformState* uState, bool transp, const float* m) { SetUniformMatrix3fv(uState->GetLocation(), transp, m); }
virtual void SetUniformMatrix4x4(UniformState* uState, bool transp, const float* m) { SetUniformMatrix4fv(uState->GetLocation(), transp, m); }
virtual void SetUniformTarget(int) {}
virtual void SetUniformLocation(const std::string&) {}
virtual void SetUniform1i(int idx, int v0) = 0;
virtual void SetUniform2i(int idx, int v0, int v1) = 0;
virtual void SetUniform3i(int idx, int v0, int v1, int v2) = 0;
virtual void SetUniform4i(int idx, int v0, int v1, int v2, int v3) = 0;
virtual void SetUniform1f(int idx, float v0) = 0;
virtual void SetUniform2f(int idx, float v0, float v1) = 0;
virtual void SetUniform3f(int idx, float v0, float v1, float v2) = 0;
virtual void SetUniform4f(int idx, float v0, float v1, float v2, float v3) = 0;
virtual void SetUniform2iv(int idx, const int* v) = 0;
virtual void SetUniform3iv(int idx, const int* v) = 0;
virtual void SetUniform4iv(int idx, const int* v) = 0;
virtual void SetUniform2fv(int idx, const float* v) = 0;
virtual void SetUniform3fv(int idx, const float* v) = 0;
virtual void SetUniform4fv(int idx, const float* v) = 0;
virtual void SetUniformMatrix2fv(int idx, bool transp, const float* v) {}
virtual void SetUniformMatrix3fv(int idx, bool transp, const float* v) {}
virtual void SetUniformMatrix4fv(int idx, bool transp, const float* v) {}
//virtual void SetUniformMatrixArray4fv(int idx, int count, bool transp, const float* v) {}
//virtual void SetUniformMatrix2dv(int idx, bool transp, const double* v) {}
//virtual void SetUniformMatrix3dv(int idx, bool transp, const double* v) {}
//virtual void SetUniformMatrix4dv(int idx, bool transp, const double* v) {}
//virtual void SetUniformMatrixArray4dv(int idx, int count, bool transp, const double* v) {}
typedef std::vector<IShaderObject*> SOVec;
typedef SOVec::iterator SOVecIt;
typedef SOVec::const_iterator SOVecConstIt;
virtual void AttachShaderObject(IShaderObject* so) { shaderObjs.push_back(so); }
SOVec& GetAttachedShaderObjs() { return shaderObjs; }
void RecompileIfNeeded();
bool IsBound() const;
bool IsShaderAttached(const IShaderObject* so) const;
bool IsValid() const { return valid; }
unsigned int GetObjID() const { return objID; }
const std::string& GetLog() const { return log; }
protected:
std::string name;
std::string log;
unsigned int objID;
unsigned int curHash;
bool valid;
bool bound;
SOVec shaderObjs;
public:
std::unordered_map<std::size_t, UniformState, fast_hash> uniformStates;
};
struct NullProgramObject: public Shader::IProgramObject {
public:
NullProgramObject(const std::string& poName): IProgramObject(poName) {}
void Enable() {}
void Disable() {}
void Release() {}
void Reload(bool reloadFromDisk) {}
int GetUniformLoc(const std::string& name) { return -1; }
int GetUniformType(const int loc) { return -1; }
void SetUniform1i(int idx, int v0) {}
void SetUniform2i(int idx, int v0, int v1) {}
void SetUniform3i(int idx, int v0, int v1, int v2) {}
void SetUniform4i(int idx, int v0, int v1, int v2, int v3) {}
void SetUniform1f(int idx, float v0) {}
void SetUniform2f(int idx, float v0, float v1) {}
void SetUniform3f(int idx, float v0, float v1, float v2) {}
void SetUniform4f(int idx, float v0, float v1, float v2, float v3) {}
void SetUniform2iv(int idx, const int* v) {}
void SetUniform3iv(int idx, const int* v) {}
void SetUniform4iv(int idx, const int* v) {}
void SetUniform2fv(int idx, const float* v) {}
void SetUniform3fv(int idx, const float* v) {}
void SetUniform4fv(int idx, const float* v) {}
};
struct ARBProgramObject: public Shader::IProgramObject {
public:
ARBProgramObject(const std::string& poName);
void Enable();
void Disable();
void Link();
void Release();
void Reload(bool reloadFromDisk);
int GetUniformLoc(const std::string& name);
int GetUniformType(const int loc) { return -1; }
void SetUniformTarget(int target);
int GetUnitformTarget();
void SetUniform1i(int idx, int v0);
void SetUniform2i(int idx, int v0, int v1);
void SetUniform3i(int idx, int v0, int v1, int v2);
void SetUniform4i(int idx, int v0, int v1, int v2, int v3);
void SetUniform1f(int idx, float v0);
void SetUniform2f(int idx, float v0, float v1);
void SetUniform3f(int idx, float v0, float v1, float v2);
void SetUniform4f(int idx, float v0, float v1, float v2, float v3);
void SetUniform2iv(int idx, const int* v);
void SetUniform3iv(int idx, const int* v);
void SetUniform4iv(int idx, const int* v);
void SetUniform2fv(int idx, const float* v);
void SetUniform3fv(int idx, const float* v);
void SetUniform4fv(int idx, const float* v);
void AttachShaderObject(IShaderObject*);
private:
int uniformTarget;
};
struct GLSLProgramObject: public Shader::IProgramObject {
public:
GLSLProgramObject(const std::string& poName);
void Enable();
void Disable();
void Link();
void Validate();
void Release();
void Reload(bool reloadFromDisk);
int GetUniformType(const int loc);
int GetUniformLoc(const std::string& name);
void SetUniformLocation(const std::string&);
void SetUniform(UniformState* uState, int v0);
void SetUniform(UniformState* uState, float v0);
void SetUniform(UniformState* uState, int v0, int v1);
void SetUniform(UniformState* uState, float v0, float v1);
void SetUniform(UniformState* uState, int v0, int v1, int v2);
void SetUniform(UniformState* uState, float v0, float v1, float v2);
void SetUniform(UniformState* uState, int v0, int v1, int v2, int v3);
void SetUniform(UniformState* uState, float v0, float v1, float v2, float v3);
void SetUniform2v(UniformState* uState, const int* v);
void SetUniform2v(UniformState* uState, const float* v);
void SetUniform3v(UniformState* uState, const int* v);
void SetUniform3v(UniformState* uState, const float* v);
void SetUniform4v(UniformState* uState, const int* v);
void SetUniform4v(UniformState* uState, const float* v);
void SetUniformMatrix2x2(UniformState* uState, bool transp, const float* v);
//void SetUniformMatrix2x2(UniformState* uState, bool transp, const double* v);
void SetUniformMatrix3x3(UniformState* uState, bool transp, const float* v);
//void SetUniformMatrix3x3(UniformState* uState, bool transp, const double* v);
void SetUniformMatrix4x4(UniformState* uState, bool transp, const float* v);
//void SetUniformMatrix4x4(UniformState* uState, bool transp, const double* v);
void SetUniform1i(int idx, int v0);
void SetUniform2i(int idx, int v0, int v1);
void SetUniform3i(int idx, int v0, int v1, int v2);
void SetUniform4i(int idx, int v0, int v1, int v2, int v3);
void SetUniform1f(int idx, float v0);
void SetUniform2f(int idx, float v0, float v1);
void SetUniform3f(int idx, float v0, float v1, float v2);
void SetUniform4f(int idx, float v0, float v1, float v2, float v3);
void SetUniform2iv(int idx, const int* v);
void SetUniform3iv(int idx, const int* v);
void SetUniform4iv(int idx, const int* v);
void SetUniform2fv(int idx, const float* v);
void SetUniform3fv(int idx, const float* v);
void SetUniform4fv(int idx, const float* v);
void SetUniformMatrix2fv(int idx, bool transp, const float* v);
void SetUniformMatrix3fv(int idx, bool transp, const float* v);
void SetUniformMatrix4fv(int idx, bool transp, const float* v);
void SetUniformMatrixArray4fv(int idx, int count, bool transp, const float* v);
void SetUniformMatrix2dv(int idx, bool transp, const double* v);
void SetUniformMatrix3dv(int idx, bool transp, const double* v);
void SetUniformMatrix4dv(int idx, bool transp, const double* v);
void SetUniformMatrixArray4dv(int idx, int count, bool transp, const double* v);
void AttachShaderObject(IShaderObject*);
private:
std::vector<size_t> uniformLocs;
};
/*
struct GLSLARBProgramObject: public Shader::IProgramObject {
glCreateProgramObjectARB <==> glCreateProgram
glCreateShaderObjectARB <==> glCreateShader
glDeleteObjectARB <==> glDelete{Shader,Program}
glCompileShaderARB <==> glCompileShader
glShaderSourceARB <==> glShaderSource
glAttachObjectARB <==> glAttachShader
glDetachObjectARB <==> glDetachShader
glLinkProgramARB <==> glLinkProgram
glUseProgramObjectARB <==> glUseProgram
glUniform*ARB <==> glUniform*
glGetUniformLocationARB <==> glGetUniformLocation
};
*/
extern NullShaderObject* nullShaderObject;
extern NullProgramObject* nullProgramObject;
}
#endif
|