Shader.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_GRAPHICS_SHADER_H
  21. #define LOVE_GRAPHICS_SHADER_H
  22. // LOVE
  23. #include "common/Object.h"
  24. #include "common/StringMap.h"
  25. #include "OpenGL.h"
  26. #include "Texture.h"
  27. // STL
  28. #include <string>
  29. #include <map>
  30. #include <vector>
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. namespace opengl
  36. {
  37. class Canvas;
  38. // A GLSL shader
  39. class Shader : public Object, public Volatile
  40. {
  41. public:
  42. enum ShaderStage
  43. {
  44. STAGE_VERTEX,
  45. STAGE_PIXEL,
  46. STAGE_MAX_ENUM
  47. };
  48. // Built-in uniform (extern) variables.
  49. enum BuiltinUniform
  50. {
  51. BUILTIN_SCREEN_SIZE,
  52. BUILTIN_MAX_ENUM
  53. };
  54. // Types of potential uniform (extern) variables used in love's shaders.
  55. enum UniformType
  56. {
  57. UNIFORM_FLOAT,
  58. UNIFORM_INT,
  59. UNIFORM_BOOL,
  60. UNIFORM_SAMPLER,
  61. UNIFORM_UNKNOWN,
  62. UNIFORM_MAX_ENUM
  63. };
  64. struct ShaderSource
  65. {
  66. std::string vertex;
  67. std::string pixel;
  68. };
  69. // Pointer to currently active Shader.
  70. static Shader *current;
  71. // Pointer to the default Shader.
  72. static Shader *defaultShader;
  73. // Default shader code (a shader is always required internally.)
  74. static ShaderSource defaultCode[1]; // TODO: RENDERER_MAX_ENUM
  75. /**
  76. * Creates a new Shader using a list of source codes.
  77. * Source must contain either vertex or pixel shader code, or both.
  78. **/
  79. Shader(const ShaderSource &source);
  80. virtual ~Shader();
  81. // Implements Volatile
  82. virtual bool loadVolatile();
  83. virtual void unloadVolatile();
  84. /**
  85. * Binds this Shader's program to be used when rendering.
  86. *
  87. * @param temporary True if we just want to send values to the shader with no intention of rendering.
  88. **/
  89. void attach(bool temporary = false);
  90. /**
  91. * Detach the currently bound Shader.
  92. * Causes the GPU rendering pipeline to use fixed functionality in place of shader programs.
  93. **/
  94. static void detach();
  95. /**
  96. * Returns any warnings this Shader may have generated.
  97. **/
  98. std::string getWarnings() const;
  99. /**
  100. * Send at least one integer or int-vector value to this Shader as a uniform.
  101. *
  102. * @param name The name of the uniform variable in the source code.
  103. * @param size Number of elements in each vector to send.
  104. * A value of 1 indicates a single-component vector (an int).
  105. * @param vec Pointer to the integer or int-vector values.
  106. * @param count Number of integer or int-vector values.
  107. **/
  108. void sendInt(const std::string &name, int size, const GLint *vec, int count);
  109. /**
  110. * Send at least one float or vector value to this Shader as a uniform.
  111. *
  112. * @param name The name of the uniform variable in the source code.
  113. * @param size Number of elements in each vector to send.
  114. * A value of 1 indicates a single-component vector (a float).
  115. * @param vec Pointer to the float or float-vector values.
  116. * @param count Number of float or float-vector values.
  117. **/
  118. void sendFloat(const std::string &name, int size, const GLfloat *vec, int count);
  119. /**
  120. * Send at least one matrix to this Shader as a uniform.
  121. *
  122. * @param name The name of the uniform variable in the source code.
  123. * @param size Number of rows/columns in the matrix.
  124. * @param m Pointer to the first element of the first matrix.
  125. * @param count Number of matrices to send.
  126. **/
  127. void sendMatrix(const std::string &name, int size, const GLfloat *m, int count);
  128. /**
  129. * Send a texture to this Shader as a uniform.
  130. *
  131. * @param name The name of the uniform variable in the source code.
  132. **/
  133. void sendTexture(const std::string &name, Texture *texture);
  134. /**
  135. * Gets the type, number of components, and number of array elements of
  136. * an active 'extern' (uniform) variable in the shader. If a uniform
  137. * variable with the specified name doesn't exist, returns UNIFORM_UNKNOWN
  138. * and sets the 'components' and 'count' values to 0.
  139. *
  140. * @param name The name of the uniform variable in the source code.
  141. * @param[out] components Number of components of the variable (2 for vec2.)
  142. * @param[out] count Number of array elements, if the variable is an array.
  143. * @return The base type of the uniform variable.
  144. **/
  145. UniformType getExternVariable(const std::string &name, int &components, int &count);
  146. /**
  147. * Internal use only.
  148. **/
  149. bool hasVertexAttrib(VertexAttribID attrib) const;
  150. bool hasBuiltinUniform(BuiltinUniform builtin) const;
  151. bool sendBuiltinFloat(BuiltinUniform builtin, int size, const GLfloat *m, int count);
  152. void checkSetScreenParams();
  153. const std::map<std::string, Object *> &getBoundRetainables() const;
  154. static std::string getGLSLVersion();
  155. static bool isSupported();
  156. static bool getConstant(const char *in, UniformType &out);
  157. static bool getConstant(UniformType in, const char *&out);
  158. private:
  159. // Represents a single uniform/extern shader variable.
  160. struct Uniform
  161. {
  162. GLint location;
  163. GLint count;
  164. GLenum type;
  165. UniformType baseType;
  166. std::string name;
  167. };
  168. // Map active uniform names to their locations.
  169. void mapActiveUniforms();
  170. const Uniform &getUniform(const std::string &name) const;
  171. int getUniformTypeSize(GLenum type) const;
  172. UniformType getUniformBaseType(GLenum type) const;
  173. void checkSetUniformError(const Uniform &u, int size, int count, UniformType sendtype) const;
  174. GLuint compileCode(ShaderStage stage, const std::string &code);
  175. int getTextureUnit(const std::string &name);
  176. void retainObject(const std::string &name, Object *object);
  177. // Get any warnings or errors generated only by the shader program object.
  178. std::string getProgramWarnings() const;
  179. // Source code used for this Shader.
  180. ShaderSource shaderSource;
  181. // Shader compiler warning strings for individual shader stages.
  182. std::map<ShaderStage, std::string> shaderWarnings;
  183. // volatile
  184. GLuint program;
  185. // Location values for any built-in uniform variables.
  186. GLint builtinUniforms[BUILTIN_MAX_ENUM];
  187. // Location values for any generic vertex attribute variables.
  188. GLint builtinAttributes[ATTRIB_MAX_ENUM];
  189. // Uniform location buffer map
  190. std::map<std::string, Uniform> uniforms;
  191. // Texture unit pool for setting images
  192. std::map<std::string, GLint> texUnitPool; // texUnitPool[name] = textureunit
  193. std::vector<GLuint> activeTexUnits; // activeTexUnits[textureunit-1] = textureid
  194. // Uniform name to retainable objects
  195. std::map<std::string, Object*> boundRetainables;
  196. // Pointer to the active Canvas when the screen params were last checked.
  197. Canvas *lastCanvas;
  198. OpenGL::Viewport lastViewport;
  199. // Max GPU texture units available for sent images
  200. static GLint maxTexUnits;
  201. // Counts total number of textures bound to each texture unit in all shaders
  202. static std::vector<int> textureCounters;
  203. static StringMap<ShaderStage, STAGE_MAX_ENUM>::Entry stageNameEntries[];
  204. static StringMap<ShaderStage, STAGE_MAX_ENUM> stageNames;
  205. static StringMap<UniformType, UNIFORM_MAX_ENUM>::Entry uniformTypeEntries[];
  206. static StringMap<UniformType, UNIFORM_MAX_ENUM> uniformTypes;
  207. // Names for the generic vertex attributes used by love.
  208. static StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry attribNameEntries[];
  209. static StringMap<VertexAttribID, ATTRIB_MAX_ENUM> attribNames;
  210. // Names for the built-in uniform variables.
  211. static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM>::Entry builtinNameEntries[];
  212. static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM> builtinNames;
  213. };
  214. } // opengl
  215. } // graphics
  216. } // love
  217. #endif // LOVE_GRAPHICS_SHADER_H