SamplerState.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RHI.Reflect/SamplerState.h>
  9. #include <AzCore/Preprocessor/Enum.h>
  10. #include <AzCore/Preprocessor/EnumReflectUtils.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/RTTI/TypeInfo.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Utils/TypeHash.h>
  16. namespace AZ::RHI
  17. {
  18. AZ_ENUM_DEFINE_REFLECT_UTILITIES(FilterMode);
  19. AZ_ENUM_DEFINE_REFLECT_UTILITIES(ReductionType);
  20. AZ_ENUM_DEFINE_REFLECT_UTILITIES(AddressMode);
  21. AZ_ENUM_DEFINE_REFLECT_UTILITIES(ComparisonFunc);
  22. AZ_ENUM_DEFINE_REFLECT_UTILITIES(BorderColor);
  23. void ReflectSamplerStateEnums(ReflectContext* context)
  24. {
  25. if (auto serializeContext = azrtti_cast<SerializeContext*>(context))
  26. {
  27. FilterModeReflect(*serializeContext);
  28. ReductionTypeReflect(*serializeContext);
  29. AddressModeReflect(*serializeContext);
  30. ComparisonFuncReflect(*serializeContext);
  31. BorderColorReflect(*serializeContext);
  32. }
  33. if (auto behaviorContext = azrtti_cast<BehaviorContext*>(context))
  34. {
  35. FilterModeReflect(*behaviorContext);
  36. ReductionTypeReflect(*behaviorContext);
  37. AddressModeReflect(*behaviorContext);
  38. ComparisonFuncReflect(*behaviorContext);
  39. BorderColorReflect(*behaviorContext);
  40. }
  41. }
  42. void SamplerState::Reflect(AZ::ReflectContext* context)
  43. {
  44. if (auto serializeContext = azrtti_cast<SerializeContext*>(context))
  45. {
  46. serializeContext->Class<SamplerState>()
  47. ->Version(3)
  48. ->Field("m_anisotropyMax", &SamplerState::m_anisotropyMax)
  49. ->Field("m_anisotropyEnable", &SamplerState::m_anisotropyEnable)
  50. ->Field("m_filterMin", &SamplerState::m_filterMin)
  51. ->Field("m_filterMag", &SamplerState::m_filterMag)
  52. ->Field("m_filterMip", &SamplerState::m_filterMip)
  53. ->Field("m_reductionType", &SamplerState::m_reductionType)
  54. ->Field("m_comparisonFunc", &SamplerState::m_comparisonFunc)
  55. ->Field("m_addressU", &SamplerState::m_addressU)
  56. ->Field("m_addressV", &SamplerState::m_addressV)
  57. ->Field("m_addressW", &SamplerState::m_addressW)
  58. ->Field("m_mipLodMin", &SamplerState::m_mipLodMin)
  59. ->Field("m_mipLodMax", &SamplerState::m_mipLodMax)
  60. ->Field("m_mipLodBias", &SamplerState::m_mipLodBias)
  61. ->Field("m_borderColor", &SamplerState::m_borderColor)
  62. ;
  63. if (auto editContext = serializeContext->GetEditContext())
  64. {
  65. editContext->Class<SamplerState>("SamplerState", "")
  66. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  67. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  68. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_anisotropyMax, "Anisotropy Max", "Clamping value used if anisotropic filtering is enabled")
  69. ->Attribute(AZ::Edit::Attributes::Min, 1)
  70. ->Attribute(AZ::Edit::Attributes::Max, 16)
  71. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_anisotropyEnable, "Anisotropy Enable", "Enable anisotropic filtering to reduce blur when sampling textures on surfaces at extreme angles")
  72. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_filterMin, "Filter Min", "Minification filter used when sampling textures")
  73. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<FilterMode>())
  74. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_filterMag, "Filter Mag", "Magnification filter used when sampling textures")
  75. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<FilterMode>())
  76. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_filterMip, "Filter Mip", "Mipmap filter used when sampling textures")
  77. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<FilterMode>())
  78. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_reductionType, "Reduction Type", "Specifies the type of filter reduction")
  79. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<ReductionType>())
  80. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_comparisonFunc, "Comparison Func", "Function used to compare between texture samples")
  81. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<ComparisonFunc>())
  82. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_addressU, "Address U", "Specifies the method for addressing U texture coordinates outside of the 0 to 1 range")
  83. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<AddressMode>())
  84. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_addressV, "Address V", "Specifies the method for addressing V texture coordinates outside of the 0 to 1 range")
  85. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<AddressMode>())
  86. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_addressW, "Address W", "Specifies the method for addressing W texture coordinates outside of the 0 to 1 range")
  87. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<AddressMode>())
  88. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_mipLodMin, "Mip Lod Min", "Minimum mipmap level used for sampling textures")
  89. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  90. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_mipLodMax, "Mip Lod Max", "Maximum mipmap level used for sampling textures")
  91. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  92. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_mipLodBias, "Mip Lod Bias", "This value is added to the runtime selected mipmap level to adjust which mipmap is used for sampling textures")
  93. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  94. ->DataElement(AZ::Edit::UIHandlers::Default, &SamplerState::m_borderColor, "Border Color", "Border color used at the edges of sampled textures")
  95. ->Attribute(AZ::Edit::Attributes::EnumValues, AZ::Edit::GetEnumConstantsFromTraits<BorderColor>())
  96. ;
  97. }
  98. }
  99. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  100. {
  101. behaviorContext->Class<SamplerState>("SamplerState")
  102. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  103. ->Attribute(AZ::Script::Attributes::Category, "RHI")
  104. ->Attribute(AZ::Script::Attributes::Module, "rhi")
  105. ->Constructor()
  106. ->Constructor<const SamplerState&>()
  107. ->Property("anisotropyMax", BehaviorValueProperty(&SamplerState::m_anisotropyMax))
  108. ->Property("anisotropyEnable", BehaviorValueProperty(&SamplerState::m_anisotropyEnable))
  109. ->Property("filterMin", BehaviorValueProperty(&SamplerState::m_filterMin))
  110. ->Property("filterMag", BehaviorValueProperty(&SamplerState::m_filterMag))
  111. ->Property("filterMip", BehaviorValueProperty(&SamplerState::m_filterMip))
  112. ->Property("reductionType", BehaviorValueProperty(&SamplerState::m_reductionType))
  113. ->Property("comparisonFunc", BehaviorValueProperty(&SamplerState::m_comparisonFunc))
  114. ->Property("addressU", BehaviorValueProperty(&SamplerState::m_addressU))
  115. ->Property("addressV", BehaviorValueProperty(&SamplerState::m_addressV))
  116. ->Property("addressW", BehaviorValueProperty(&SamplerState::m_addressW))
  117. ->Property("mipLodMin", BehaviorValueProperty(&SamplerState::m_mipLodMin))
  118. ->Property("mipLodMax", BehaviorValueProperty(&SamplerState::m_mipLodMax))
  119. ->Property("mipLodBias", BehaviorValueProperty(&SamplerState::m_mipLodBias))
  120. ->Property("borderColor", BehaviorValueProperty(&SamplerState::m_borderColor))
  121. ;
  122. }
  123. }
  124. SamplerState SamplerState::Create(
  125. FilterMode filterModeMinMag,
  126. FilterMode filterModeMip,
  127. AddressMode addressMode,
  128. BorderColor borderColor)
  129. {
  130. SamplerState descriptor;
  131. descriptor.m_filterMin = descriptor.m_filterMag = filterModeMinMag;
  132. descriptor.m_filterMip = filterModeMip;
  133. descriptor.m_addressU = descriptor.m_addressV = descriptor.m_addressW = addressMode;
  134. descriptor.m_borderColor = borderColor;
  135. return descriptor;
  136. }
  137. SamplerState SamplerState::CreateAnisotropic(
  138. uint32_t anisotropyMax,
  139. AddressMode addressMode)
  140. {
  141. SamplerState descriptor;
  142. descriptor.m_anisotropyEnable = 1;
  143. descriptor.m_anisotropyMax = anisotropyMax;
  144. descriptor.m_addressU = descriptor.m_addressV = descriptor.m_addressW = addressMode;
  145. return descriptor;
  146. }
  147. HashValue64 SamplerState::GetHash(HashValue64 seed /*= 0*/) const
  148. {
  149. return TypeHash64(*this, seed);
  150. }
  151. }