ShaderEffect.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2006-2012 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_EFFECT_H
  21. #define LOVE_GRAPHICS_EFFECT_H
  22. #include "common/Object.h"
  23. #include <string>
  24. #include <map>
  25. #include <vector>
  26. #include "OpenGL.h"
  27. #include "Image.h"
  28. #include "Canvas.h"
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. namespace opengl
  34. {
  35. // A GLSL shader
  36. class ShaderEffect : public Object, public Volatile
  37. {
  38. public:
  39. enum ShaderType
  40. {
  41. TYPE_VERTEX,
  42. TYPE_TESSCONTROL,
  43. TYPE_TESSEVAL,
  44. TYPE_GEOMETRY,
  45. TYPE_FRAGMENT,
  46. TYPE_MAX_ENUM
  47. };
  48. // thin wrapper for GLSL source code
  49. struct ShaderSource
  50. {
  51. std::string code;
  52. ShaderType type;
  53. };
  54. ShaderEffect(const std::vector<ShaderSource> &shadersources);
  55. virtual ~ShaderEffect();
  56. std::string getWarnings() const;
  57. virtual bool loadVolatile();
  58. virtual void unloadVolatile();
  59. void attach(bool temporary = false);
  60. static void detach();
  61. static std::string getGLSLVersion();
  62. static bool isSupported();
  63. static ShaderEffect *current; // pointer to currently active ShaderEffect
  64. void sendFloat(const std::string &name, int size, const GLfloat *vec, int count);
  65. void sendMatrix(const std::string &name, int size, const GLfloat *m, int count);
  66. void sendImage(const std::string &name, const Image &image);
  67. void sendCanvas(const std::string &name, const Canvas &canvas);
  68. private:
  69. GLint getUniformLocation(const std::string &name);
  70. void checkSetUniformError();
  71. GLuint createShader(const ShaderSource &source);
  72. void createProgram(const std::vector<GLuint> &shaderids);
  73. std::vector<ShaderSource> _shadersources; // all shader code attached to this ShaderEffect
  74. GLuint _program; // volatile
  75. // uniform location buffer
  76. std::map<std::string, GLint> _uniforms;
  77. static GLint _max_texture_units; // total max GPU texture units for shaders
  78. static std::vector<int> _texture_id_counters; // counts total number of textures bound to each texture unit in all shaders
  79. // texture unit pool for setting images
  80. std::map<std::string, GLint> _texture_unit_pool;
  81. std::vector<GLuint> _texture_id_list;
  82. GLint getTextureUnit(const std::string &name);
  83. void sendTexture(const std::string &name, GLuint texture);
  84. };
  85. } // opengl
  86. } // graphics
  87. } // love
  88. #endif // LOVE_GRAPHICS_EFFECT_H