DxilReduceMSAAToSingleSample.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilReduceMSAAToSingleSample.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 reduce all MSAA writes to single-sample writes //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/HLSL/DxilGenerationPass.h"
  12. #include "dxc/HLSL/DxilOperations.h"
  13. #include "dxc/HLSL/DxilInstructions.h"
  14. #include "dxc/HLSL/DxilModule.h"
  15. #include "dxc/HLSL/DxilPIXPasses.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/PassManager.h"
  18. #include "llvm/IR/Constants.h"
  19. using namespace llvm;
  20. using namespace hlsl;
  21. class DxilReduceMSAAToSingleSample : public ModulePass {
  22. public:
  23. static char ID; // Pass identification, replacement for typeid
  24. explicit DxilReduceMSAAToSingleSample() : ModulePass(ID) {}
  25. const char *getPassName() const override { return "HLSL DXIL Reduce all MSAA reads to single-sample reads"; }
  26. bool runOnModule(Module &M) override;
  27. };
  28. bool DxilReduceMSAAToSingleSample::runOnModule(Module &M)
  29. {
  30. DxilModule &DM = M.GetOrCreateDxilModule();
  31. LLVMContext & Ctx = M.getContext();
  32. OP *HlslOP = DM.GetOP();
  33. // FP16 type doesn't have its own identity, and is covered by float type...
  34. auto TextureLoadOverloads = std::vector<Type*>{ Type::getFloatTy(Ctx), Type::getInt16Ty(Ctx), Type::getInt32Ty(Ctx) };
  35. bool Modified = false;
  36. for (const auto & Overload : TextureLoadOverloads) {
  37. Function * TexLoadFunction = HlslOP->GetOpFunc(DXIL::OpCode::TextureLoad, Overload);
  38. auto TexLoadFunctionUses = TexLoadFunction->uses();
  39. for (auto FI = TexLoadFunctionUses.begin(); FI != TexLoadFunctionUses.end(); ) {
  40. auto & FunctionUse = *FI++;
  41. auto FunctionUser = FunctionUse.getUser();
  42. auto instruction = cast<Instruction>(FunctionUser);
  43. DxilInst_TextureLoad LoadInstruction(instruction);
  44. auto TextureHandle = LoadInstruction.get_srv();
  45. auto TextureHandleInst = cast<CallInst>(TextureHandle);
  46. DxilInst_CreateHandle createHandle(TextureHandleInst);
  47. // Dynamic rangeId is not supported
  48. if (isa<ConstantInt>(createHandle.get_rangeId())){
  49. unsigned rangeId = cast<ConstantInt>(createHandle.get_rangeId())->getLimitedValue();
  50. if (static_cast<DXIL::ResourceClass>(createHandle.get_resourceClass_val()) == DXIL::ResourceClass::SRV) {
  51. auto Resource = DM.GetSRV(rangeId);
  52. if (Resource.GetKind() == DXIL::ResourceKind::Texture2DMS || Resource.GetKind() == DXIL::ResourceKind::Texture2DMSArray) {
  53. // "2" is the mip-level/sample-index operand index:
  54. // https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst#textureload
  55. instruction->setOperand(2, HlslOP->GetI32Const(0));
  56. Modified = true;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return Modified;
  63. }
  64. char DxilReduceMSAAToSingleSample::ID = 0;
  65. ModulePass *llvm::createDxilReduceMSAAToSingleSamplePass() {
  66. return new DxilReduceMSAAToSingleSample();
  67. }
  68. INITIALIZE_PASS(DxilReduceMSAAToSingleSample, "hlsl-dxil-reduce-msaa-to-single", "HLSL DXIL Reduce all MSAA writes to single-sample writes", false, false)