ShaderEffect.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 fragment shader
  36. class ShaderEffect : public Object, public Volatile
  37. {
  38. public:
  39. ShaderEffect(const std::string &vertcode, const std::string &fragcode);
  40. virtual ~ShaderEffect();
  41. std::string getWarnings() const;
  42. virtual bool loadVolatile();
  43. virtual void unloadVolatile();
  44. void attach();
  45. static void detach();
  46. static std::string getGLSLVersion();
  47. static bool isSupported();
  48. static ShaderEffect *current;
  49. void sendFloat(const std::string &name, int size, const GLfloat *vec, int count);
  50. void sendMatrix(const std::string &name, int size, const GLfloat *m, int count);
  51. void sendImage(const std::string &name, const Image &image);
  52. void sendCanvas(const std::string &name, const Canvas &canvas);
  53. private:
  54. GLint getUniformLocation(const std::string &name);
  55. void checkSetUniformError();
  56. GLuint createShader(GLenum type, const std::string &code);
  57. void createProgram(const std::vector<GLuint> &shaders);
  58. GLuint _program;
  59. std::string _vertcode;
  60. std::string _fragcode; // volatile and stuff
  61. // uniform location buffer
  62. std::map<std::string, GLint> _uniforms;
  63. // texture unit pool for setting images
  64. static GLint _current_texture_unit;
  65. static GLint _max_texture_units;
  66. std::map<std::string, GLint> _texture_unit_pool;
  67. GLint getTextureUnit(const std::string &name);
  68. void sendTexture(const std::string &name, GLuint texture);
  69. };
  70. } // opengl
  71. } // graphics
  72. } // love
  73. #endif // LOVE_GRAPHICS_EFFECT_H