DxilResourceBinding.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilResourceBinding.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/DXIL/DxilResourceBinding.h"
  10. #include "llvm/IR/Constant.h"
  11. #include "dxc/DXIL/DxilShaderModel.h"
  12. #include "llvm/IR/DerivedTypes.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "dxc/DXIL/DxilResourceBase.h"
  15. #include "dxc/DXIL/DxilResource.h"
  16. #include "dxc/DXIL/DxilCBuffer.h"
  17. #include "dxc/DXIL/DxilSampler.h"
  18. #include "dxc/DXIL/DxilOperations.h"
  19. #include "dxc/DXIL/DxilInstructions.h"
  20. #include "dxc/DXIL/DxilUtil.h"
  21. using namespace llvm;
  22. namespace hlsl {
  23. bool DxilResourceBinding::operator==(const DxilResourceBinding &B) {
  24. return rangeLowerBound == B.rangeLowerBound &&
  25. rangeUpperBound == B.rangeUpperBound && spaceID == B.spaceID &&
  26. resourceClass == B.resourceClass;
  27. }
  28. bool DxilResourceBinding::operator!=(const DxilResourceBinding &B) {
  29. return !(*this == B) ;
  30. }
  31. namespace resource_helper {
  32. // The constant is as struct with int fields.
  33. // ShaderModel 6.6 has 4 fileds.
  34. llvm::Constant *getAsConstant(const DxilResourceBinding &B, llvm::Type *Ty,
  35. const ShaderModel &) {
  36. StructType *ST = cast<StructType>(Ty);
  37. switch (ST->getNumElements()) {
  38. case 4: {
  39. Constant *RawDwords[] = {
  40. ConstantInt::get(ST->getElementType(0), B.rangeLowerBound),
  41. ConstantInt::get(ST->getElementType(1), B.rangeUpperBound),
  42. ConstantInt::get(ST->getElementType(2), B.spaceID),
  43. ConstantInt::get(ST->getElementType(3), B.resourceClass)};
  44. return ConstantStruct::get(ST, RawDwords);
  45. } break;
  46. default:
  47. return nullptr;
  48. break;
  49. }
  50. return nullptr;
  51. }
  52. DxilResourceBinding loadBindingFromConstant(const llvm::Constant &C) {
  53. DxilResourceBinding B;
  54. // Ty Should match C.getType().
  55. Type *Ty = C.getType();
  56. StructType *ST = cast<StructType>(Ty);
  57. switch (ST->getNumElements()) {
  58. case 4: {
  59. if (isa<ConstantAggregateZero>(&C)) {
  60. B.rangeLowerBound = 0;
  61. B.rangeUpperBound = 0;
  62. B.spaceID = 0;
  63. B.resourceClass = 0;
  64. } else {
  65. const ConstantStruct *CS = cast<ConstantStruct>(&C);
  66. const Constant *rangeLowerBound = CS->getOperand(0);
  67. const Constant *rangeUpperBound = CS->getOperand(1);
  68. const Constant *spaceID = CS->getOperand(2);
  69. const Constant *resourceClass = CS->getOperand(3);
  70. B.rangeLowerBound = cast<ConstantInt>(rangeLowerBound)->getLimitedValue();
  71. B.rangeUpperBound = cast<ConstantInt>(rangeUpperBound)->getLimitedValue();
  72. B.spaceID = cast<ConstantInt>(spaceID)->getLimitedValue();
  73. B.resourceClass = cast<ConstantInt>(resourceClass)->getLimitedValue();
  74. }
  75. } break;
  76. default:
  77. B.resourceClass = (uint8_t)DXIL::ResourceClass::Invalid;
  78. break;
  79. }
  80. return B;
  81. }
  82. DxilResourceBinding loadBindingFromCreateHandleFromBinding(
  83. const DxilInst_CreateHandleFromBinding &createHandle, llvm::Type *Ty,
  84. const ShaderModel &) {
  85. Constant *B = cast<Constant>(createHandle.get_bind());
  86. return loadBindingFromConstant(*B);
  87. }
  88. DxilResourceBinding loadBindingFromResourceBase(DxilResourceBase *Res) {
  89. DxilResourceBinding B = {};
  90. B.resourceClass = (uint8_t)DXIL::ResourceClass::Invalid;
  91. if (!Res)
  92. return B;
  93. B.rangeLowerBound = Res->GetLowerBound();
  94. B.rangeUpperBound = Res->GetUpperBound();
  95. B.spaceID = Res->GetSpaceID();
  96. B.resourceClass = (uint8_t)Res->GetClass();
  97. return B;
  98. }
  99. } // namespace resource_helper
  100. } // namespace hlsl