Shader.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2006-2020 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. #pragma once
  21. // LOVE
  22. #include "graphics/Shader.h"
  23. #include "graphics/Graphics.h"
  24. #include "graphics/Volatile.h"
  25. #include "OpenGL.h"
  26. // STL
  27. #include <string>
  28. #include <map>
  29. #include <vector>
  30. namespace love
  31. {
  32. namespace graphics
  33. {
  34. namespace opengl
  35. {
  36. // A GLSL shader
  37. class Shader final : public love::graphics::Shader, public Volatile
  38. {
  39. public:
  40. /**
  41. * Creates a new Shader using a list of source codes.
  42. * Source must contain either vertex or pixel shader code, or both.
  43. **/
  44. Shader(love::graphics::ShaderStage *vertex, love::graphics::ShaderStage *pixel);
  45. virtual ~Shader();
  46. // Implements Volatile
  47. bool loadVolatile() override;
  48. void unloadVolatile() override;
  49. // Implements Shader.
  50. void attach() override;
  51. std::string getWarnings() const override;
  52. int getVertexAttributeIndex(const std::string &name) override;
  53. const UniformInfo *getUniformInfo(const std::string &name) const override;
  54. const UniformInfo *getUniformInfo(BuiltinUniform builtin) const override;
  55. void updateUniform(const UniformInfo *info, int count) override;
  56. void sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count) override;
  57. bool hasUniform(const std::string &name) const override;
  58. ptrdiff_t getHandle() const override;
  59. void setVideoTextures(love::graphics::Texture *ytexture, love::graphics::Texture *cbtexture, love::graphics::Texture *crtexture) override;
  60. void updatePointSize(float size);
  61. void updateBuiltinUniforms(love::graphics::Graphics *gfx, int viewportW, int viewportH);
  62. private:
  63. struct TextureUnit
  64. {
  65. GLuint texture = 0;
  66. TextureType type = TEXTURE_2D;
  67. bool active = false;
  68. };
  69. // Map active uniform names to their locations.
  70. void mapActiveUniforms();
  71. void updateUniform(const UniformInfo *info, int count, bool internalupdate);
  72. void sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count, bool internalupdate);
  73. int getUniformTypeComponents(GLenum type) const;
  74. MatrixSize getMatrixSize(GLenum type) const;
  75. UniformType getUniformBaseType(GLenum type) const;
  76. TextureType getUniformTextureType(GLenum type) const;
  77. bool isDepthTextureType(GLenum type) const;
  78. void flushBatchedDraws() const;
  79. // Get any warnings or errors generated only by the shader program object.
  80. std::string getProgramWarnings() const;
  81. // volatile
  82. GLuint program;
  83. // Location values for any built-in uniform variables.
  84. GLint builtinUniforms[BUILTIN_MAX_ENUM];
  85. UniformInfo *builtinUniformInfo[BUILTIN_MAX_ENUM];
  86. // Location values for any generic vertex attribute variables.
  87. GLint builtinAttributes[ATTRIB_MAX_ENUM];
  88. std::map<std::string, GLint> attributes;
  89. // Uniform location buffer map
  90. std::map<std::string, UniformInfo> uniforms;
  91. // Texture unit pool for setting textures
  92. std::vector<TextureUnit> textureUnits;
  93. std::vector<std::pair<const UniformInfo *, int>> pendingUniformUpdates;
  94. float lastPointSize;
  95. }; // Shader
  96. } // opengl
  97. } // graphics
  98. } // love