Technique.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Copyright (c) 2008-2013 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 "GraphicsDefs.h"
  24. #include "HashTable.h"
  25. #include "Resource.h"
  26. namespace Urho3D
  27. {
  28. class ShaderVariation;
  29. /// Lighting mode of a pass.
  30. enum PassLightingMode
  31. {
  32. LIGHTING_UNLIT,
  33. LIGHTING_PERVERTEX,
  34. LIGHTING_PERPIXEL
  35. };
  36. /// %Material rendering pass, which defines shaders and render state.
  37. class URHO3D_API Pass : public RefCounted
  38. {
  39. public:
  40. /// Construct.
  41. Pass(StringHash type);
  42. /// Destruct.
  43. ~Pass();
  44. /// Set blend mode.
  45. void SetBlendMode(BlendMode mode);
  46. /// Set depth compare mode.
  47. void SetDepthTestMode(CompareMode mode);
  48. /// Set pass lighting mode, affects what shader variations will be attempted to be loaded.
  49. void SetLightingMode(PassLightingMode mode);
  50. /// Set depth write on/off.
  51. void SetDepthWrite(bool enable);
  52. /// Set alpha masking hint. Completely opaque draw calls will be performed before alpha masked.
  53. void SetAlphaMask(bool enable);
  54. /// Set vertex shader name.
  55. void SetVertexShader(const String& name);
  56. /// Set pixel shader name.
  57. void SetPixelShader(const String& name);
  58. /// Reset shader pointers.
  59. void ReleaseShaders();
  60. /// Mark shaders loaded this frame.
  61. void MarkShadersLoaded(unsigned frameNumber);
  62. /// Return pass type.
  63. const StringHash& GetType() const { return type_; }
  64. /// Return blend mode.
  65. BlendMode GetBlendMode() const { return blendMode_; }
  66. /// Return depth compare mode.
  67. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  68. /// Return pass lighting mode.
  69. PassLightingMode GetLightingMode() const { return lightingMode_; }
  70. /// Return last shaders loaded frame number.
  71. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  72. /// Return depth write mode.
  73. bool GetDepthWrite() const { return depthWrite_; }
  74. /// Return alpha masking hint.
  75. bool GetAlphaMask() const { return alphaMask_; }
  76. /// Return vertex shader name.
  77. const String& GetVertexShader() const { return vertexShaderName_; }
  78. /// Return pixel shader name.
  79. const String& GetPixelShader() const { return pixelShaderName_; }
  80. /// Return vertex shaders.
  81. Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; }
  82. /// Return pixel shaders.
  83. Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; }
  84. private:
  85. /// Pass type.
  86. StringHash type_;
  87. /// Blend mode.
  88. BlendMode blendMode_;
  89. /// Depth compare mode.
  90. CompareMode depthTestMode_;
  91. /// Lighting mode.
  92. PassLightingMode lightingMode_;
  93. /// Last shaders loaded frame number.
  94. unsigned shadersLoadedFrameNumber_;
  95. /// Depth write mode.
  96. bool depthWrite_;
  97. /// Alpha masking hint.
  98. bool alphaMask_;
  99. /// Vertex shader name.
  100. String vertexShaderName_;
  101. /// Pixel shader name.
  102. String pixelShaderName_;
  103. /// Vertex shaders.
  104. Vector<SharedPtr<ShaderVariation> > vertexShaders_;
  105. /// Pixel shaders.
  106. Vector<SharedPtr<ShaderVariation> > pixelShaders_;
  107. };
  108. /// %Material technique. Consists of several passes.
  109. class URHO3D_API Technique : public Resource
  110. {
  111. OBJECT(Technique);
  112. friend class Renderer;
  113. public:
  114. /// Construct.
  115. Technique(Context* context);
  116. /// Destruct.
  117. ~Technique();
  118. /// Register object factory.
  119. static void RegisterObject(Context* context);
  120. /// Load resource. Return true if successful.
  121. virtual bool Load(Deserializer& source);
  122. /// Set whether requires %Shader %Model 3.
  123. void SetIsSM3(bool enable);
  124. /// Create a new pass.
  125. Pass* CreatePass(StringHash type);
  126. /// Remove a pass.
  127. void RemovePass(StringHash type);
  128. /// Reset shader pointers in all passes.
  129. void ReleaseShaders();
  130. /// Return whether requires %Shader %Model 3.
  131. bool IsSM3() const { return isSM3_; }
  132. /// Return whether has a pass.
  133. bool HasPass(StringHash type) const { return passes_.Find(type.Value()) != 0; }
  134. /// Return a pass, or null if not found.
  135. Pass* GetPass(StringHash type) const
  136. {
  137. SharedPtr<Pass>* passPtr = passes_.Find(type.Value());
  138. return passPtr ? passPtr->Get() : 0;
  139. }
  140. private:
  141. /// Require %Shader %Model 3 flag.
  142. bool isSM3_;
  143. /// Passes.
  144. HashTable<SharedPtr<Pass>, 16> passes_;
  145. };
  146. }