SkSLCPPUniformCTypes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkSLUniformCTypes_DEFINED
  8. #define SkSLUniformCTypes_DEFINED
  9. #include "src/sksl/SkSLContext.h"
  10. #include "src/sksl/SkSLString.h"
  11. #include "src/sksl/ir/SkSLType.h"
  12. #include "src/sksl/ir/SkSLVariable.h"
  13. namespace SkSL {
  14. // This uses templates to define dirtyExpression(), saveState() and setUniform(). Each template can
  15. // reference token names formatted ${name} that are replaced with the actual values passed into the
  16. // functions.
  17. //
  18. // dirtyExpression() and saveState() support the following tokens:
  19. // - ${newVar} replaced with value of newValueVarName (1st argument)
  20. // - ${oldVar} replaced with value of oldValueVarName (2nd argument)
  21. //
  22. // setUniform() supports these tokens:
  23. // - ${pdman} replaced with value of pdmanName (1st argument)
  24. // - ${uniform} replaced with value of uniformHandleName (2nd argument)
  25. // - ${var} replaced with value of valueVarName (3rd argument)
  26. //
  27. // All templates and C++ snippets should produce valid expressions, but do not need to include
  28. // semicolons or newlines, which will be handled by the code generation itself.
  29. class UniformCTypeMapper {
  30. public:
  31. // Create a templated mapper that does not support state tracking
  32. UniformCTypeMapper(Layout::CType ctype, const std::vector<String>& skslTypes,
  33. const char* setUniformFormat)
  34. : UniformCTypeMapper(ctype, skslTypes, setUniformFormat, false, "", "", "") { }
  35. // Create a templated mapper that provides extra patterns for the state
  36. // tracking expressions.
  37. UniformCTypeMapper(Layout::CType ctype, const std::vector<String>& skslTypes,
  38. const String& setUniformFormat, const String& defaultValue,
  39. const String& dirtyExpressionFormat, const String& saveStateFormat)
  40. : UniformCTypeMapper(ctype, skslTypes, setUniformFormat,
  41. true, defaultValue, dirtyExpressionFormat, saveStateFormat) { }
  42. // Returns nullptr if the type and layout are not supported; the returned pointer's ownership
  43. // is not transfered to the caller.
  44. //
  45. // The returned mapper can support tracking even if tracking is disabled based on the flags in
  46. // the layout.
  47. static const UniformCTypeMapper* Get(const Context& context, const Type& type,
  48. const Layout& layout);
  49. static const UniformCTypeMapper* Get(const Context& context, const Variable& variable) {
  50. return Get(context, variable.fType, variable.fModifiers.fLayout);
  51. }
  52. // The C++ type name that this mapper applies to
  53. Layout::CType ctype() const {
  54. return fCType;
  55. }
  56. // The sksl type names that the mapper's ctype can be mapped to
  57. const std::vector<String>& supportedTypeNames() const {
  58. return fSKSLTypes;
  59. }
  60. // Whether or not this handler knows how to write state tracking code
  61. // for the uniform variables
  62. bool supportsTracking() const {
  63. return fSupportsTracking;
  64. }
  65. // What the C++ class fields are initialized to in the GLSLFragmentProcessor The empty string
  66. // implies the no-arg constructor is suitable. This is not used if supportsTracking() returns
  67. // false.
  68. //
  69. // The returned snippet will be a valid as the lhs of an assignment.
  70. const String& defaultValue() const {
  71. return fDefaultValue;
  72. }
  73. // Return a boolean expression that returns true if the variables specified by newValueVarName
  74. // and oldValueVarName have different values. This is ignored if supportsTracking() returns
  75. // false.
  76. //
  77. // The returned snippet will be a valid expression to be inserted into the condition of an 'if'
  78. // statement.
  79. String dirtyExpression(const String& newValueVarName, const String& oldValueVarName) const;
  80. // Return a statement that stores the value of newValueVarName into the variable specified by
  81. // oldValueVarName. This is ignored if supportsTracking() returns false.
  82. //
  83. // The returned snippet will be a valid expression.
  84. String saveState(const String& newValueVarName, const String& oldValueVarName) const;
  85. // Return a statement that invokes the appropriate setX method on the GrGLSLProgramDataManager
  86. // specified by pdmanName, where the uniform is provided by the expression stored in
  87. // uniformHandleName, and valueVarName is the variable name pointing to the ctype instance
  88. // holding the new value.
  89. //
  90. // The returned snippet will be a valid expression.
  91. String setUniform(const String& pdmanName, const String& uniformHandleName,
  92. const String& valueVarName) const;
  93. // True if the setUniform() template only uses the value variable once in its expression. The
  94. // variable does not necessarily get inlined if this returns true, since a local variable may be
  95. // needed if state tracking is employed for a particular uniform.
  96. bool canInlineUniformValue() const {
  97. return fInlineValue;
  98. }
  99. private:
  100. UniformCTypeMapper(Layout::CType ctype, const std::vector<String>& skslTypes,
  101. const String& setUniformFormat, bool enableTracking, const String& defaultValue,
  102. const String& dirtyExpressionFormat, const String& saveStateFormat);
  103. Layout::CType fCType;
  104. std::vector<String> fSKSLTypes;
  105. String fUniformTemplate;
  106. bool fInlineValue; // Cached value calculated from fUniformTemplate
  107. bool fSupportsTracking;
  108. String fDefaultValue;
  109. String fDirtyExpressionTemplate;
  110. String fSaveStateTemplate;
  111. };
  112. } // namespace
  113. #endif // SkSLUniformCTypes_DEFINED