DxilReduceMSAAToSingleSample.cpp 3.5 KB

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