ImGuiShaderUtils.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <Utils/ImGuiShaderUtils.h>
  9. #include <imgui/imgui.h>
  10. namespace AtomSampleViewer
  11. {
  12. namespace ImGuiShaderUtils
  13. {
  14. void DrawShaderVariantTable(const AZ::RPI::ShaderOptionGroupLayout* layout, AZ::RPI::ShaderVariantId requestedVariantId, AZ::RPI::ShaderVariantId selectedVariantId)
  15. {
  16. AZ::RPI::ShaderOptionGroup requestedShaderVariantOptions{layout, requestedVariantId};
  17. AZ::RPI::ShaderOptionGroup selectedShaderVariantOptions{layout, selectedVariantId};
  18. auto& shaderOptionDescriptors = layout->GetShaderOptions();
  19. // Note I tried using ImGui columns but they don't work so well, and I already had this manual formatting approach shelved elsewhere
  20. // so decided to just stick with this is it seems to be more reliable this way.
  21. int longestOptionNameLength = 0;
  22. for (auto& shaderOption : shaderOptionDescriptors)
  23. {
  24. longestOptionNameLength = AZStd::GetMax(longestOptionNameLength, aznumeric_cast<int>(shaderOption.GetName().GetStringView().size()));
  25. }
  26. AZStd::string header = AZStd::string::format("%-*s | Bits | Requested | Selected", longestOptionNameLength, "Option Name");
  27. AZStd::string divider{header.size(), '-'};
  28. ImGui::Text("%s", header.c_str());
  29. ImGui::Text("%s", divider.c_str());
  30. for (size_t i = 0; i < shaderOptionDescriptors.size(); ++i)
  31. {
  32. // TODO Consider giving ShaderOptionDescriptor its index value so we can use for-each.
  33. const AZ::RPI::ShaderOptionDescriptor& shaderOptionDesc = shaderOptionDescriptors[i];
  34. AZ::RPI::ShaderOptionIndex optionIndex{i};
  35. const char* optionName = shaderOptionDesc.GetName().GetCStr();
  36. AZ::RPI::ShaderOptionValue requestedValue = requestedShaderVariantOptions.GetValue(optionIndex);
  37. AZ::RPI::ShaderOptionValue selectedValue = selectedShaderVariantOptions.GetValue(optionIndex);
  38. AZStd::string optionNameLabel = AZStd::string::format("%-*s", longestOptionNameLength, optionName);
  39. AzFramework::StringFunc::Replace(optionNameLabel, ' ', '.');
  40. if (shaderOptionDesc.GetBitCount() == 1)
  41. {
  42. ImGui::Text("%s | %4d | %9d | %8d",
  43. optionNameLabel.c_str(),
  44. shaderOptionDesc.GetBitOffset(),
  45. requestedValue.GetIndex(),
  46. selectedValue.GetIndex());
  47. }
  48. else
  49. {
  50. ImGui::Text("%s | %2d-%-2d | %9d | %8d",
  51. optionNameLabel.c_str(),
  52. shaderOptionDesc.GetBitOffset(),
  53. shaderOptionDesc.GetBitOffset() + shaderOptionDesc.GetBitCount() - 1,
  54. requestedValue.GetIndex(),
  55. selectedValue.GetIndex());
  56. }
  57. }
  58. }
  59. } // namespace Utils
  60. } // namespace AtomSampleViewer