Shader.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Resource/Resource.h"
  7. namespace Urho3D
  8. {
  9. class ShaderVariation;
  10. /// %Shader resource consisting of several shader variations.
  11. class URHO3D_API Shader : public Resource
  12. {
  13. URHO3D_OBJECT(Shader, Resource);
  14. public:
  15. /// Construct.
  16. explicit Shader(Context* context);
  17. /// Destruct.
  18. ~Shader() override;
  19. /// Register object factory.
  20. /// @nobind
  21. static void RegisterObject(Context* context);
  22. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  23. bool BeginLoad(Deserializer& source) override;
  24. /// Finish resource loading. Always called from the main thread. Return true if successful.
  25. bool EndLoad() override;
  26. /// Return a variation with defines. Separate multiple defines with spaces.
  27. ShaderVariation* GetVariation(ShaderType type, const String& defines);
  28. /// Return a variation with defines. Separate multiple defines with spaces.
  29. ShaderVariation* GetVariation(ShaderType type, const char* defines);
  30. /// Return either vertex or pixel shader source code.
  31. const String& GetSourceCode(ShaderType type) const { return type == VS ? vsSourceCode_ : psSourceCode_; }
  32. /// Return the latest timestamp of the shader code and its includes.
  33. unsigned GetTimeStamp() const { return timeStamp_; }
  34. private:
  35. /// Process source code and include files. Return true if successful.
  36. bool ProcessSource(String& code, Deserializer& source);
  37. /// Sort the defines and strip extra spaces to prevent creation of unnecessary duplicate shader variations.
  38. String NormalizeDefines(const String& defines);
  39. /// Recalculate the memory used by the shader.
  40. void RefreshMemoryUse();
  41. /// Source code adapted for vertex shader.
  42. String vsSourceCode_;
  43. /// Source code adapted for pixel shader.
  44. String psSourceCode_;
  45. /// Vertex shader variations.
  46. HashMap<StringHash, SharedPtr<ShaderVariation>> vsVariations_;
  47. /// Pixel shader variations.
  48. HashMap<StringHash, SharedPtr<ShaderVariation>> psVariations_;
  49. /// Source code timestamp.
  50. unsigned timeStamp_;
  51. /// Number of unique variations so far.
  52. unsigned numVariations_;
  53. };
  54. }