OGLShaderProgram.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  29. {
  30. class Graphics;
  31. class ShaderVariation;
  32. /// %Shader parameter definition.
  33. struct ShaderParameter
  34. {
  35. /// Uniform location.
  36. int location_;
  37. /// Element type.
  38. unsigned type_;
  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. /// Mark the GPU resource destroyed on context destruction.
  49. virtual void OnDeviceLost();
  50. /// Release shader program.
  51. virtual void Release();
  52. /// Link the shaders and examine the uniforms and samplers used. Return true if successful.
  53. bool Link();
  54. /// Return the vertex shader.
  55. ShaderVariation* GetVertexShader() const;
  56. /// Return the pixel shader.
  57. ShaderVariation* GetPixelShader() const;
  58. /// Return whether uses a shader parameter.
  59. bool HasParameter(StringHash param) const;
  60. /// Return whether uses a texture unit.
  61. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  62. /// Return the info for a shader parameter, or null if does not exist.
  63. const ShaderParameter* GetParameter(StringHash param) const;
  64. /// Return whether successfully linked.
  65. bool IsLinked() const { return linked_; }
  66. /// Return linker output.
  67. const String& GetLinkerOutput() const { return linkerOutput_; }
  68. private:
  69. /// Vertex shader.
  70. WeakPtr<ShaderVariation> vertexShader_;
  71. /// Pixel shader.
  72. WeakPtr<ShaderVariation> pixelShader_;
  73. /// Shader parameters.
  74. HashMap<StringHash, ShaderParameter> shaderParameters_;
  75. /// Texture unit use.
  76. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  77. /// Shader link error string.
  78. String linkerOutput_;
  79. /// Linked flag.
  80. bool linked_;
  81. };
  82. }