OGLShaderProgram.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Atomic
  29. {
  30. class ConstantBuffer;
  31. class Graphics;
  32. /// Linked shader program on the GPU.
  33. class ATOMIC_API ShaderProgram : public RefCounted, public GPUObject
  34. {
  35. ATOMIC_REFCOUNTED(ShaderProgram)
  36. public:
  37. /// Construct.
  38. ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  39. /// Destruct.
  40. ~ShaderProgram();
  41. /// Mark the GPU resource destroyed on context destruction.
  42. virtual void OnDeviceLost();
  43. /// Release shader program.
  44. virtual void Release();
  45. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  46. bool Link();
  47. /// Return the vertex shader.
  48. ShaderVariation* GetVertexShader() const;
  49. /// Return the pixel shader.
  50. ShaderVariation* GetPixelShader() const;
  51. /// Return whether uses a shader parameter.
  52. bool HasParameter(StringHash param) const;
  53. /// Return whether uses a texture unit.
  54. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  55. /// Return the info for a shader parameter, or null if does not exist.
  56. const ShaderParameter* GetParameter(StringHash param) const;
  57. /// Return linker output.
  58. const String& GetLinkerOutput() const { return linkerOutput_; }
  59. /// Return semantic to vertex attributes location mappings used by the shader.
  60. const HashMap<Pair<unsigned char, unsigned char>, unsigned>& GetVertexAttributes() const { return vertexAttributes_; }
  61. /// Return attribute location use bitmask.
  62. unsigned GetUsedVertexAttributes() const { return usedVertexAttributes_; }
  63. /// Return all constant buffers.
  64. const SharedPtr<ConstantBuffer>* GetConstantBuffers() const { return &constantBuffers_[0]; }
  65. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  66. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  67. /// Clear a parameter source. Affects only the current shader program if appropriate.
  68. void ClearParameterSource(ShaderParameterGroup group);
  69. /// Clear all parameter sources from all shader programs by incrementing the global parameter source framenumber.
  70. static void ClearParameterSources();
  71. /// Clear a global parameter source when constant buffers change.
  72. static void ClearGlobalParameterSource(ShaderParameterGroup group);
  73. private:
  74. /// Vertex shader.
  75. WeakPtr<ShaderVariation> vertexShader_;
  76. /// Pixel shader.
  77. WeakPtr<ShaderVariation> pixelShader_;
  78. /// Shader parameters.
  79. HashMap<StringHash, ShaderParameter> shaderParameters_;
  80. /// Texture unit use.
  81. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  82. /// Vertex attributes.
  83. HashMap<Pair<unsigned char, unsigned char>, unsigned> vertexAttributes_;
  84. /// Used vertex attribute location bitmask.
  85. unsigned usedVertexAttributes_;
  86. /// Constant buffers by binding index.
  87. SharedPtr<ConstantBuffer> constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
  88. /// Remembered shader parameter sources for individual uniform mode.
  89. const void* parameterSources_[MAX_SHADER_PARAMETER_GROUPS];
  90. /// Shader link error string.
  91. String linkerOutput_;
  92. /// Shader parameter source framenumber.
  93. unsigned frameNumber_;
  94. /// Global shader parameter source framenumber.
  95. static unsigned globalFrameNumber;
  96. /// Remembered global shader parameter sources for constant buffer mode.
  97. static const void* globalParameterSources[MAX_SHADER_PARAMETER_GROUPS];
  98. };
  99. }