xref: /freebsd/contrib/llvm-project/llvm/lib/MC/DXContainerPSVInfo.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 //===- llvm/MC/DXContainerPSVInfo.cpp - DXContainer PSVInfo -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/DXContainerPSVInfo.h"
10 #include "llvm/BinaryFormat/DXContainer.h"
11 #include "llvm/Support/raw_ostream.h"
12 
13 using namespace llvm;
14 using namespace llvm::mcdxbc;
15 using namespace llvm::dxbc::PSV;
16 
17 void PSVRuntimeInfo::write(raw_ostream &OS, uint32_t Version) const {
18   uint32_t InfoSize;
19   uint32_t BindingSize;
20   switch (Version) {
21   case 0:
22     InfoSize = sizeof(dxbc::PSV::v0::RuntimeInfo);
23     BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
24     break;
25   case 1:
26     InfoSize = sizeof(dxbc::PSV::v1::RuntimeInfo);
27     BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
28     break;
29   case 2:
30   default:
31     InfoSize = sizeof(dxbc::PSV::v2::RuntimeInfo);
32     BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo);
33   }
34   uint32_t InfoSizeSwapped = InfoSize;
35   if (sys::IsBigEndianHost)
36     sys::swapByteOrder(InfoSizeSwapped);
37   // Write the size of the info.
38   OS.write(reinterpret_cast<const char *>(&InfoSizeSwapped), sizeof(uint32_t));
39   // Write the info itself.
40   OS.write(reinterpret_cast<const char *>(&BaseData), InfoSize);
41 
42   uint32_t ResourceCount = static_cast<uint32_t>(Resources.size());
43   uint32_t BindingSizeSwapped = BindingSize;
44   if (sys::IsBigEndianHost) {
45     sys::swapByteOrder(ResourceCount);
46     sys::swapByteOrder(BindingSizeSwapped);
47   }
48 
49   OS.write(reinterpret_cast<const char *>(&ResourceCount), sizeof(uint32_t));
50   OS.write(reinterpret_cast<const char *>(&BindingSizeSwapped), sizeof(uint32_t));
51 
52   for (const auto &Res : Resources)
53     OS.write(reinterpret_cast<const char *>(&Res), BindingSize);
54 }
55