OGLShaderProgram.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../../Container/HashMap.h"
  24. #include "../../Container/RefCounted.h"
  25. #include "../../Graphics/GPUObject.h"
  26. #include "../../Graphics/GraphicsDefs.h"
  27. #include "../../Graphics/ShaderVariation.h"
  28. namespace Urho3D
  29. {
  30. class ConstantBuffer;
  31. class Graphics;
  32. /// Linked shader program on the GPU.
  33. class URHO3D_API ShaderProgram : public RefCounted, public GPUObject
  34. {
  35. public:
  36. /// Construct.
  37. ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  38. /// Destruct.
  39. virtual ~ShaderProgram() override;
  40. /// Mark the GPU resource destroyed on context destruction.
  41. virtual void OnDeviceLost() override;
  42. /// Release shader program.
  43. virtual void Release() override;
  44. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  45. bool Link();
  46. /// Return the vertex shader.
  47. ShaderVariation* GetVertexShader() const;
  48. /// Return the pixel shader.
  49. ShaderVariation* GetPixelShader() const;
  50. /// Return whether uses a shader parameter.
  51. bool HasParameter(StringHash param) const;
  52. /// Return whether uses a texture unit.
  53. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  54. /// Return the info for a shader parameter, or null if does not exist.
  55. const ShaderParameter* GetParameter(StringHash param) const;
  56. /// Return linker output.
  57. const String& GetLinkerOutput() const { return linkerOutput_; }
  58. /// Return semantic to vertex attributes location mappings used by the shader.
  59. const HashMap<Pair<unsigned char, unsigned char>, unsigned>& GetVertexAttributes() const { return vertexAttributes_; }
  60. /// Return attribute location use bitmask.
  61. unsigned GetUsedVertexAttributes() const { return usedVertexAttributes_; }
  62. /// Return all constant buffers.
  63. const SharedPtr<ConstantBuffer>* GetConstantBuffers() const { return &constantBuffers_[0]; }
  64. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  65. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  66. /// Clear a parameter source. Affects only the current shader program if appropriate.
  67. void ClearParameterSource(ShaderParameterGroup group);
  68. /// Clear all parameter sources from all shader programs by incrementing the global parameter source framenumber.
  69. static void ClearParameterSources();
  70. /// Clear a global parameter source when constant buffers change.
  71. static void ClearGlobalParameterSource(ShaderParameterGroup group);
  72. private:
  73. /// Vertex shader.
  74. WeakPtr<ShaderVariation> vertexShader_;
  75. /// Pixel shader.
  76. WeakPtr<ShaderVariation> pixelShader_;
  77. /// Shader parameters.
  78. HashMap<StringHash, ShaderParameter> shaderParameters_;
  79. /// Texture unit use.
  80. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  81. /// Vertex attributes.
  82. HashMap<Pair<unsigned char, unsigned char>, unsigned> vertexAttributes_;
  83. /// Used vertex attribute location bitmask.
  84. unsigned usedVertexAttributes_;
  85. /// Constant buffers by binding index.
  86. SharedPtr<ConstantBuffer> constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
  87. /// Remembered shader parameter sources for individual uniform mode.
  88. const void* parameterSources_[MAX_SHADER_PARAMETER_GROUPS];
  89. /// Shader link error string.
  90. String linkerOutput_;
  91. /// Shader parameter source framenumber.
  92. unsigned frameNumber_;
  93. /// Global shader parameter source framenumber.
  94. static unsigned globalFrameNumber;
  95. /// Remembered global shader parameter sources for constant buffer mode.
  96. static const void* globalParameterSources[MAX_SHADER_PARAMETER_GROUPS];
  97. };
  98. }