OGLShaderProgram.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. };
  38. /// Linked shader program on the GPU.
  39. class ShaderProgram : public RefCounted, public GPUObject
  40. {
  41. public:
  42. /// Construct.
  43. ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader);
  44. /// Destruct.
  45. ~ShaderProgram();
  46. /// Release shader program.
  47. virtual void Release();
  48. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  49. bool Link();
  50. /// Return the vertex shader.
  51. ShaderVariation* GetVertexShader() const;
  52. /// Return the pixel shader.
  53. ShaderVariation* GetPixelShader() const;
  54. /// Return whether uses a shader parameter.
  55. bool HasParameter(StringHash param) const;
  56. /// Return whether uses a texture unit.
  57. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  58. /// Return the info for a shader parameter, or null if does not exist.
  59. const ShaderParameter* GetParameter(StringHash param) const;
  60. /// Return the vertex attribute bindings.
  61. const int* GetAttributeLocations() const { return attributeLocations_; }
  62. /// Return whether successfully linked.
  63. bool IsLinked() const { return linked_; }
  64. /// Return linker output.
  65. const String& GetLinkerOutput() const { return linkerOutput_; }
  66. private:
  67. /// Vertex shader.
  68. WeakPtr<ShaderVariation> vertexShader_;
  69. /// Pixel shader.
  70. WeakPtr<ShaderVariation> pixelShader_;
  71. /// Shader parameters.
  72. HashMap<StringHash, ShaderParameter> shaderParameters_;
  73. /// Texture unit use.
  74. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  75. /// Vertex attribute bindings.
  76. int attributeLocations_[MAX_VERTEX_ELEMENTS];
  77. /// Shader link error string.
  78. String linkerOutput_;
  79. /// Linked flag.
  80. bool linked_;
  81. };