OGLShaderProgram.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "GPUObject.h"
  25. #include "GraphicsDefs.h"
  26. #include "HashMap.h"
  27. #include "RefCounted.h"
  28. class Graphics;
  29. class ShaderVariation;
  30. /// %Shader parameter definition.
  31. struct ShaderParameter
  32. {
  33. /// Uniform location.
  34. int location_;
  35. /// Element type.
  36. unsigned type_;
  37. /// Last parameter source.
  38. const void* lastSource_;
  39. };
  40. /// Linked shader program on the GPU.
  41. class ShaderProgram : public RefCounted, public GPUObject
  42. {
  43. public:
  44. /// Construct.
  45. ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  46. /// Destruct.
  47. ~ShaderProgram();
  48. /// Release shader program.
  49. virtual void Release();
  50. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  51. bool Link();
  52. /// Check whether needs a parameter update.
  53. bool NeedParameterUpdate(StringHash param, const void* source, unsigned frame);
  54. /// Clear a specific remembered parameter source.
  55. void ClearParameterSource(StringHash param);
  56. /// Return the vertex shader.
  57. ShaderVariation* GetVertexShader() const;
  58. /// Return the pixel shader.
  59. ShaderVariation* GetPixelShader() const;
  60. /// Return whether uses a shader parameter.
  61. bool HasParameter(StringHash param) const;
  62. /// Return whether uses a texture unit.
  63. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  64. /// Return the info for a shader parameter, or null if does not exist.
  65. const ShaderParameter* GetParameter(StringHash param) const;
  66. /// Return the vertex attribute bindings.
  67. const int* GetAttributeLocations() const { return attributeLocations_; }
  68. /// Return whether successfully linked.
  69. bool IsLinked() const { return linked_; }
  70. /// Return linker output.
  71. const String& GetLinkerOutput() const { return linkerOutput_; }
  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. /// Shader parameters global frame number.
  80. unsigned lastParameterFrame_;
  81. /// Texture unit use.
  82. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  83. /// Vertex attribute bindings.
  84. int attributeLocations_[MAX_VERTEX_ELEMENTS];
  85. /// Shader link error string.
  86. String linkerOutput_;
  87. /// Linked flag.
  88. bool linked_;
  89. };