DxilAddPixelHitInstrumentation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilAddPixelHitInstrumentation.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. // Provides a pass to add instrumentation to determine pixel hit count and //
  9. // cost. Used by PIX. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "dxc/DXIL/DxilOperations.h"
  13. #include "dxc/DXIL/DxilInstructions.h"
  14. #include "dxc/DXIL/DxilModule.h"
  15. #include "dxc/DXIL/DxilUtil.h"
  16. #include "dxc/DxilPIXPasses/DxilPIXPasses.h"
  17. #include "dxc/HLSL/DxilGenerationPass.h"
  18. #include "llvm/IR/PassManager.h"
  19. #include "llvm/Transforms/Utils/Local.h"
  20. #include "PixPassHelpers.h"
  21. using namespace llvm;
  22. using namespace hlsl;
  23. class DxilAddPixelHitInstrumentation : public ModulePass {
  24. bool ForceEarlyZ = false;
  25. bool AddPixelCost = false;
  26. int RTWidth = 1024;
  27. int NumPixels = 128;
  28. int SVPositionIndex = -1;
  29. public:
  30. static char ID; // Pass identification, replacement for typeid
  31. explicit DxilAddPixelHitInstrumentation() : ModulePass(ID) {}
  32. const char *getPassName() const override { return "DXIL Constant Color Mod"; }
  33. void applyOptions(PassOptions O) override;
  34. bool runOnModule(Module &M) override;
  35. };
  36. void DxilAddPixelHitInstrumentation::applyOptions(PassOptions O) {
  37. GetPassOptionBool(O, "force-early-z", &ForceEarlyZ, false);
  38. GetPassOptionBool(O, "add-pixel-cost", &AddPixelCost, false);
  39. GetPassOptionInt(O, "rt-width", &RTWidth, 0);
  40. GetPassOptionInt(O, "num-pixels", &NumPixels, 0);
  41. GetPassOptionInt(O, "sv-position-index", &SVPositionIndex, 0);
  42. }
  43. bool DxilAddPixelHitInstrumentation::runOnModule(Module &M) {
  44. // This pass adds instrumentation for pixel hit counting and pixel cost.
  45. DxilModule &DM = M.GetOrCreateDxilModule();
  46. LLVMContext &Ctx = M.getContext();
  47. OP *HlslOP = DM.GetOP();
  48. // ForceEarlyZ is incompatible with the discard function (the Z has to be
  49. // tested/written, and may be written before the shader even runs)
  50. if (ForceEarlyZ) {
  51. DM.m_ShaderFlags.SetForceEarlyDepthStencil(true);
  52. }
  53. hlsl::DxilSignature &InputSignature = DM.GetInputSignature();
  54. auto &InputElements = InputSignature.GetElements();
  55. unsigned SV_Position_ID;
  56. auto SV_Position =
  57. std::find_if(InputElements.begin(), InputElements.end(),
  58. [](const std::unique_ptr<DxilSignatureElement> &Element) {
  59. return Element->GetSemantic()->GetKind() ==
  60. hlsl::DXIL::SemanticKind::Position;
  61. });
  62. // SV_Position, if present, has to have full mask, so we needn't worry
  63. // about the shader having selected components that don't include x or y.
  64. // If not present, we add it.
  65. if (SV_Position == InputElements.end()) {
  66. auto SVPosition =
  67. llvm::make_unique<DxilSignatureElement>(DXIL::SigPointKind::PSIn);
  68. SVPosition->Initialize("Position", hlsl::CompType::getF32(),
  69. hlsl::DXIL::InterpolationMode::Linear, 1, 4,
  70. SVPositionIndex == -1 ? 0 : SVPositionIndex, 0);
  71. SVPosition->AppendSemanticIndex(0);
  72. SVPosition->SetSigPointKind(DXIL::SigPointKind::PSIn);
  73. SVPosition->SetKind(hlsl::DXIL::SemanticKind::Position);
  74. auto index = InputSignature.AppendElement(std::move(SVPosition));
  75. SV_Position_ID = InputElements[index]->GetID();
  76. } else {
  77. SV_Position_ID = SV_Position->get()->GetID();
  78. }
  79. auto EntryPointFunction = DM.GetEntryFunction();
  80. auto &EntryBlock = EntryPointFunction->getEntryBlock();
  81. CallInst *HandleForUAV;
  82. {
  83. IRBuilder<> Builder(
  84. dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
  85. HandleForUAV = PIXPassHelpers::CreateUAV(DM, Builder, 0, "PIX_CountUAV_Handle");
  86. DM.ReEmitDxilResources();
  87. }
  88. // todo: is it a reasonable assumption that there will be a "Ret" in the entry
  89. // block, and that these are the only points from which the shader can exit
  90. // (except for a pixel-kill?)
  91. auto &Instructions = EntryBlock.getInstList();
  92. auto It = Instructions.begin();
  93. while (It != Instructions.end()) {
  94. auto ThisInstruction = It++;
  95. LlvmInst_Ret Ret(ThisInstruction);
  96. if (Ret) {
  97. // Check that there is at least one instruction preceding the Ret (no need
  98. // to instrument it if there isn't)
  99. if (ThisInstruction->getPrevNode() != nullptr) {
  100. // Start adding instructions right before the Ret:
  101. IRBuilder<> Builder(ThisInstruction);
  102. // ------------------------------------------------------------------------------------------------------------
  103. // Generate instructions to increment (by one) a UAV value corresponding
  104. // to the pixel currently being rendered
  105. // ------------------------------------------------------------------------------------------------------------
  106. // Useful constants
  107. Constant *Zero32Arg = HlslOP->GetU32Const(0);
  108. Constant *Zero8Arg = HlslOP->GetI8Const(0);
  109. Constant *One32Arg = HlslOP->GetU32Const(1);
  110. Constant *One8Arg = HlslOP->GetI8Const(1);
  111. UndefValue *UndefArg = UndefValue::get(Type::getInt32Ty(Ctx));
  112. Constant *NumPixelsByteOffsetArg = HlslOP->GetU32Const(NumPixels * 4);
  113. // Step 1: Convert SV_POSITION to UINT
  114. Value *XAsInt;
  115. Value *YAsInt;
  116. {
  117. auto LoadInputOpFunc =
  118. HlslOP->GetOpFunc(DXIL::OpCode::LoadInput, Type::getFloatTy(Ctx));
  119. Constant *LoadInputOpcode =
  120. HlslOP->GetU32Const((unsigned)DXIL::OpCode::LoadInput);
  121. Constant *SV_Pos_ID = HlslOP->GetU32Const(SV_Position_ID);
  122. auto XPos =
  123. Builder.CreateCall(LoadInputOpFunc,
  124. {LoadInputOpcode, SV_Pos_ID, Zero32Arg /*row*/,
  125. Zero8Arg /*column*/, UndefArg},
  126. "XPos");
  127. auto YPos =
  128. Builder.CreateCall(LoadInputOpFunc,
  129. {LoadInputOpcode, SV_Pos_ID, Zero32Arg /*row*/,
  130. One8Arg /*column*/, UndefArg},
  131. "YPos");
  132. XAsInt = Builder.CreateCast(Instruction::CastOps::FPToUI, XPos,
  133. Type::getInt32Ty(Ctx), "XIndex");
  134. YAsInt = Builder.CreateCast(Instruction::CastOps::FPToUI, YPos,
  135. Type::getInt32Ty(Ctx), "YIndex");
  136. }
  137. // Step 2: Calculate pixel index
  138. Value *Index;
  139. {
  140. Constant *RTWidthArg = HlslOP->GetI32Const(RTWidth);
  141. auto YOffset = Builder.CreateMul(YAsInt, RTWidthArg, "YOffset");
  142. auto Elementoffset =
  143. Builder.CreateAdd(XAsInt, YOffset, "ElementOffset");
  144. Index = Builder.CreateMul(Elementoffset, HlslOP->GetU32Const(4),
  145. "ByteIndex");
  146. }
  147. // Insert the UAV increment instruction:
  148. Function *AtomicOpFunc =
  149. HlslOP->GetOpFunc(OP::OpCode::AtomicBinOp, Type::getInt32Ty(Ctx));
  150. Constant *AtomicBinOpcode =
  151. HlslOP->GetU32Const((unsigned)OP::OpCode::AtomicBinOp);
  152. Constant *AtomicAdd =
  153. HlslOP->GetU32Const((unsigned)DXIL::AtomicBinOpCode::Add);
  154. {
  155. (void)Builder.CreateCall(
  156. AtomicOpFunc,
  157. {
  158. AtomicBinOpcode, // i32, ; opcode
  159. HandleForUAV, // %dx.types.Handle, ; resource handle
  160. AtomicAdd, // i32, ; binary operation code : EXCHANGE, IADD,
  161. // AND, OR, XOR, IMIN, IMAX, UMIN, UMAX
  162. Index, // i32, ; coordinate c0: byte offset
  163. UndefArg, // i32, ; coordinate c1 (unused)
  164. UndefArg, // i32, ; coordinate c2 (unused)
  165. One32Arg // i32); increment value
  166. },
  167. "UAVIncResult");
  168. }
  169. if (AddPixelCost) {
  170. // ------------------------------------------------------------------------------------------------------------
  171. // Generate instructions to increment a value corresponding to the
  172. // current pixel in the second half of the UAV, by an amount
  173. // proportional to the estimated average cost of each pixel in the
  174. // current draw call.
  175. // ------------------------------------------------------------------------------------------------------------
  176. // Step 1: Retrieve weight value from UAV; it will be placed after the
  177. // range we're writing to
  178. Value *Weight;
  179. {
  180. Function *LoadWeight = HlslOP->GetOpFunc(OP::OpCode::BufferLoad,
  181. Type::getInt32Ty(Ctx));
  182. Constant *LoadWeightOpcode =
  183. HlslOP->GetU32Const((unsigned)DXIL::OpCode::BufferLoad);
  184. Constant *OffsetIntoUAV = HlslOP->GetU32Const(NumPixels * 2 * 4);
  185. auto WeightStruct = Builder.CreateCall(
  186. LoadWeight,
  187. {
  188. LoadWeightOpcode, // i32 opcode
  189. HandleForUAV, // %dx.types.Handle, ; resource handle
  190. OffsetIntoUAV, // i32 c0: byte offset
  191. UndefArg // i32 c1: unused
  192. },
  193. "WeightStruct");
  194. Weight = Builder.CreateExtractValue(
  195. WeightStruct, static_cast<uint64_t>(0LL), "Weight");
  196. }
  197. // Step 2: Update write position ("Index") to second half of the UAV
  198. auto OffsetIndex = Builder.CreateAdd(Index, NumPixelsByteOffsetArg,
  199. "OffsetByteIndex");
  200. // Step 3: Increment UAV value by the weight
  201. (void)Builder.CreateCall(
  202. AtomicOpFunc,
  203. {
  204. AtomicBinOpcode, // i32, ; opcode
  205. HandleForUAV, // %dx.types.Handle, ; resource handle
  206. AtomicAdd, // i32, ; binary operation code : EXCHANGE, IADD,
  207. // AND, OR, XOR, IMIN, IMAX, UMIN, UMAX
  208. OffsetIndex, // i32, ; coordinate c0: byte offset
  209. UndefArg, // i32, ; coordinate c1 (unused)
  210. UndefArg, // i32, ; coordinate c2 (unused)
  211. Weight // i32); increment value
  212. },
  213. "UAVIncResult2");
  214. }
  215. }
  216. }
  217. }
  218. bool Modified = false;
  219. return Modified;
  220. }
  221. char DxilAddPixelHitInstrumentation::ID = 0;
  222. ModulePass *llvm::createDxilAddPixelHitInstrumentationPass() {
  223. return new DxilAddPixelHitInstrumentation();
  224. }
  225. INITIALIZE_PASS(DxilAddPixelHitInstrumentation,
  226. "hlsl-dxil-add-pixel-hit-instrmentation",
  227. "DXIL Count completed PS invocations and costs", false, false)