OGLShaderProgram.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "../../Graphics/GPUObject.h"
  24. #include "../../Graphics/GraphicsDefs.h"
  25. #include "../../Container/HashMap.h"
  26. #include "../../Container/RefCounted.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. public:
  51. /// Construct.
  52. ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  53. /// Destruct.
  54. ~ShaderProgram();
  55. /// Mark the GPU resource destroyed on context destruction.
  56. virtual void OnDeviceLost();
  57. /// Release shader program.
  58. virtual void Release();
  59. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  60. bool Link();
  61. /// Return the vertex shader.
  62. ShaderVariation* GetVertexShader() const;
  63. /// Return the pixel shader.
  64. ShaderVariation* GetPixelShader() const;
  65. /// Return whether uses a shader parameter.
  66. bool HasParameter(StringHash param) const;
  67. /// Return whether uses a texture unit.
  68. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  69. /// Return the info for a shader parameter, or null if does not exist.
  70. const ShaderParameter* GetParameter(StringHash param) const;
  71. /// Return linker output.
  72. const String& GetLinkerOutput() const { return linkerOutput_; }
  73. /// Return all constant buffers.
  74. const SharedPtr<ConstantBuffer>* GetConstantBuffers() const { return &constantBuffers_[0]; }
  75. /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
  76. bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
  77. /// Clear a parameter source. Affects only the current shader program if appropriate.
  78. void ClearParameterSource(ShaderParameterGroup group);
  79. /// Clear all parameter sources from all shader programs by incrementing the global parameter source framenumber.
  80. static void ClearParameterSources();
  81. /// Clear a global parameter source when constant buffers change.
  82. static void ClearGlobalParameterSource(ShaderParameterGroup group);
  83. private:
  84. /// Vertex shader.
  85. WeakPtr<ShaderVariation> vertexShader_;
  86. /// Pixel shader.
  87. WeakPtr<ShaderVariation> pixelShader_;
  88. /// Shader parameters.
  89. HashMap<StringHash, ShaderParameter> shaderParameters_;
  90. /// Texture unit use.
  91. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  92. /// Constant buffers by binding index.
  93. SharedPtr<ConstantBuffer> constantBuffers_[MAX_SHADER_PARAMETER_GROUPS * 2];
  94. /// Remembered shader parameter sources for individual uniform mode.
  95. const void* parameterSources_[MAX_SHADER_PARAMETER_GROUPS];
  96. /// Shader link error string.
  97. String linkerOutput_;
  98. /// Shader parameter source framenumber.
  99. unsigned frameNumber_;
  100. /// Global shader parameter source framenumber.
  101. static unsigned globalFrameNumber;
  102. /// Remembered global shader parameter sources for constant buffer mode.
  103. static const void* globalParameterSources[MAX_SHADER_PARAMETER_GROUPS];
  104. };
  105. }