Technique.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "GraphicsDefs.h"
  25. #include "Resource.h"
  26. class PixelShader;
  27. class VertexShader;
  28. /// Material rendering pass, which defines shaders and render state
  29. class Pass
  30. {
  31. public:
  32. /// Construct with defaults
  33. Pass();
  34. /// Construct with pass type
  35. Pass(PassType type);
  36. /// Destruct
  37. ~Pass();
  38. /// Set alpha masking hint on/off
  39. void SetAlphaMask(bool enable);
  40. /// Set alpha test on/off
  41. void SetAlphaTest(bool enable);
  42. /// Set blend mode
  43. void SetBlendMode(BlendMode mode);
  44. /// Set depth compare mode
  45. void SetDepthTestMode(CompareMode mode);
  46. /// Set depth write on/off
  47. void SetDepthWrite(bool enable);
  48. /// Set vertex shader name
  49. void SetVertexShader(const std::string& name);
  50. /// Set pixel shader name
  51. void SetPixelShader(const std::string& name);
  52. /// Reset shader pointers
  53. void ReleaseShaders();
  54. /// Return pass type
  55. PassType GetType() const { return type_; }
  56. /// Return alpha masking hint
  57. bool GetAlphaMask() const { return alphaMask_; }
  58. /// Return alpha test mode
  59. bool GetAlphaTest() const { return alphaTest_; }
  60. /// Return blend mode
  61. BlendMode GetBlendMode() const { return blendMode_; }
  62. /// Return depth compare mode
  63. CompareMode GetDepthTestMode() const { return depthTestMode_; }
  64. /// Return depth write mode
  65. bool GetDepthWrite() const { return depthWrite_; }
  66. /// Return vertex shader name
  67. const std::string& GetVertexShaderName() const { return vertexShaderName_; }
  68. /// Return pixel shader name
  69. const std::string& GetPixelShaderName() const { return pixelShaderName_; }
  70. /// Return vertex shaders
  71. std::vector<SharedPtr<VertexShader> >& GetVertexShaders() { return vertexShaders_; }
  72. /// Return pixel shaders
  73. std::vector<SharedPtr<PixelShader> >& GetPixelShaders() { return pixelShaders_; }
  74. private:
  75. /// Pass type
  76. PassType type_;
  77. /// Alpha masking hint
  78. bool alphaMask_;
  79. /// Alpha test mode
  80. bool alphaTest_;
  81. /// Blend mode
  82. BlendMode blendMode_;
  83. /// Depth compare mode
  84. CompareMode depthTestMode_;
  85. /// Depth write mode
  86. bool depthWrite_;
  87. /// Vertex shader name
  88. std::string vertexShaderName_;
  89. /// Pixel shader name
  90. std::string pixelShaderName_;
  91. /// Vertex shaders
  92. std::vector<SharedPtr<VertexShader> > vertexShaders_;
  93. /// Pixel shaders
  94. std::vector<SharedPtr<PixelShader> > pixelShaders_;
  95. };
  96. /// Material technique. Consists of several passes
  97. class Technique : public Resource
  98. {
  99. OBJECT(Technique);
  100. friend class Renderer;
  101. public:
  102. /// Construct
  103. Technique(Context* context);
  104. /// Destruct
  105. ~Technique();
  106. /// Register object factory
  107. static void RegisterObject(Context* context);
  108. /// Load resource. Return true if successful
  109. virtual bool Load(Deserializer& source);
  110. /// Set whether requires Shader Model 3
  111. void SetIsSM3(bool enable);
  112. /// Create a new pass
  113. Pass* CreatePass(PassType pass);
  114. /// Remove a pass
  115. void RemovePass(PassType pass);
  116. /// Reset shader pointers in all passes
  117. void ReleaseShaders();
  118. /// Mark shaders loaded this frame
  119. void MarkShadersLoaded(unsigned frameNumber);
  120. /// Return whether has a pass
  121. bool HasPass(PassType pass) const
  122. {
  123. return passes_.find(pass) != passes_.end();
  124. }
  125. /// Return a pass
  126. Pass* GetPass(PassType pass)
  127. {
  128. std::map<PassType, Pass>::iterator i = passes_.find(pass);
  129. if (i != passes_.end())
  130. return &(i->second);
  131. else
  132. return 0;
  133. }
  134. /// Return whether requires Shader Model 3
  135. bool IsSM3() const { return isSM3_; }
  136. /// Return all passes
  137. const std::map<PassType, Pass>& GetPasses() const { return passes_; }
  138. /// Return last shaders loaded frame number
  139. unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; }
  140. /// Return name for pass
  141. static const std::string& GetPassName(PassType pass);
  142. private:
  143. /// Require Shader Model 3 flag
  144. bool isSM3_;
  145. /// Last shaders loaded frame number
  146. unsigned shadersLoadedFrameNumber_;
  147. /// Passes
  148. std::map<PassType, Pass> passes_;
  149. };