Shader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. void sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count) override;
  58. bool hasUniform(const std::string &name) const override;
  59. ptrdiff_t getHandle() const override;
  60. void setVideoTextures(love::graphics::Texture *ytexture, love::graphics::Texture *cbtexture, love::graphics::Texture *crtexture) override;
  61. void updatePointSize(float size);
  62. void updateBuiltinUniforms(love::graphics::Graphics *gfx, int viewportW, int viewportH);
  63. private:
  64. struct TextureUnit
  65. {
  66. GLuint texture = 0;
  67. TextureType type = TEXTURE_2D;
  68. bool isTexelBuffer = false;
  69. bool active = false;
  70. };
  71. struct BufferBinding
  72. {
  73. int bindingindex = 0;
  74. GLuint buffer = 0;
  75. };
  76. // Map active uniform names to their locations.
  77. void mapActiveUniforms();
  78. void updateUniform(const UniformInfo *info, int count, bool internalupdate);
  79. void sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count, bool internalupdate);
  80. void sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count, bool internalupdate);
  81. int getUniformTypeComponents(GLenum type) const;
  82. MatrixSize getMatrixSize(GLenum type) const;
  83. UniformType getUniformBaseType(GLenum type) const;
  84. TextureType getUniformTextureType(GLenum type) const;
  85. DataBaseType getUniformTexelBufferType(GLenum type) const;
  86. bool isDepthTextureType(GLenum type) const;
  87. void flushBatchedDraws() const;
  88. // Get any warnings or errors generated only by the shader program object.
  89. std::string getProgramWarnings() const;
  90. // volatile
  91. GLuint program;
  92. // Location values for any built-in uniform variables.
  93. GLint builtinUniforms[BUILTIN_MAX_ENUM];
  94. UniformInfo *builtinUniformInfo[BUILTIN_MAX_ENUM];
  95. // Location values for any generic vertex attribute variables.
  96. GLint builtinAttributes[ATTRIB_MAX_ENUM];
  97. std::map<std::string, GLint> attributes;
  98. // Uniform location buffer map
  99. std::map<std::string, UniformInfo> uniforms;
  100. // Texture unit pool for setting textures
  101. std::vector<TextureUnit> textureUnits;
  102. std::vector<int> storageBufferBindingIndexToActiveBinding;
  103. std::vector<BufferBinding> activeStorageBufferBindings;
  104. std::vector<std::pair<const UniformInfo *, int>> pendingUniformUpdates;
  105. float lastPointSize;
  106. }; // Shader
  107. } // opengl
  108. } // graphics
  109. } // love