OGLShaderProgram.h 4.7 KB

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