Shader.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2008-2017 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 "../Container/ArrayPtr.h"
  24. #include "../Resource/Resource.h"
  25. namespace Atomic
  26. {
  27. class ShaderVariation;
  28. /// %Shader resource consisting of several shader variations.
  29. class ATOMIC_API Shader : public Resource
  30. {
  31. ATOMIC_OBJECT(Shader, Resource);
  32. public:
  33. /// Construct.
  34. Shader(Context* context);
  35. /// Destruct.
  36. virtual ~Shader();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  40. virtual bool BeginLoad(Deserializer& source);
  41. /// Finish resource loading. Always called from the main thread. Return true if successful.
  42. virtual bool EndLoad();
  43. /// Return a variation with defines. Separate multiple defines with spaces.
  44. ShaderVariation* GetVariation(ShaderType type, const String& defines);
  45. /// Return a variation with defines. Separate multiple defines with spaces.
  46. ShaderVariation* GetVariation(ShaderType type, const char* defines);
  47. /// Return either vertex or pixel shader source code.
  48. const String& GetSourceCode(ShaderType type) const { return type == VS ? vsSourceCode_ : psSourceCode_; }
  49. /// Return the latest timestamp of the shader code and its includes.
  50. unsigned GetTimeStamp() const { return timeStamp_; }
  51. private:
  52. /// Process source code and include files. Return true if successful.
  53. bool ProcessSource(String& code, Deserializer& file);
  54. /// Sort the defines and strip extra spaces to prevent creation of unnecessary duplicate shader variations.
  55. String NormalizeDefines(const String& defines);
  56. /// Recalculate the memory used by the shader.
  57. void RefreshMemoryUse();
  58. /// Source code adapted for vertex shader.
  59. String vsSourceCode_;
  60. /// Source code adapted for pixel shader.
  61. String psSourceCode_;
  62. /// Vertex shader variations.
  63. HashMap<StringHash, SharedPtr<ShaderVariation> > vsVariations_;
  64. /// Pixel shader variations.
  65. HashMap<StringHash, SharedPtr<ShaderVariation> > psVariations_;
  66. /// Source code timestamp.
  67. unsigned timeStamp_;
  68. /// Number of unique variations so far.
  69. unsigned numVariations_;
  70. };
  71. }