DxilContainerReader.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilContainerReader.h //
  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. // Helper class for reading from dxil container. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "dxc/Support/Global.h"
  13. #include "dxc/Support/WinAdapter.h"
  14. #include "dxc/DxilContainer/DxilContainer.h"
  15. #include "dxc/DxilContainer/DxilContainerReader.h"
  16. namespace hlsl {
  17. HRESULT DxilContainerReader::Load(_In_ const void* pContainer, _In_ uint32_t containerSizeInBytes) {
  18. if (pContainer == nullptr) {
  19. return E_FAIL;
  20. }
  21. const DxilContainerHeader *pHeader = IsDxilContainerLike(pContainer, containerSizeInBytes);
  22. if (pHeader == nullptr) {
  23. return E_FAIL;
  24. }
  25. if (!IsValidDxilContainer(pHeader, containerSizeInBytes)) {
  26. return E_FAIL;
  27. }
  28. m_pContainer = pContainer;
  29. m_uContainerSize = containerSizeInBytes;
  30. m_pHeader = pHeader;
  31. return S_OK;
  32. }
  33. HRESULT DxilContainerReader::GetVersion(_Out_ DxilContainerVersion *pResult) {
  34. if (pResult == nullptr) return E_POINTER;
  35. if (!IsLoaded()) return E_NOT_VALID_STATE;
  36. *pResult = m_pHeader->Version;
  37. return S_OK;
  38. }
  39. HRESULT DxilContainerReader::GetPartCount(_Out_ uint32_t *pResult) {
  40. if (pResult == nullptr) return E_POINTER;
  41. if (!IsLoaded()) return E_NOT_VALID_STATE;
  42. *pResult = m_pHeader->PartCount;
  43. return S_OK;
  44. }
  45. HRESULT DxilContainerReader::GetPartContent(uint32_t idx, _Outptr_ const void **ppResult, _Out_ uint32_t *pResultSize) {
  46. if (ppResult == nullptr) return E_POINTER;
  47. *ppResult = nullptr;
  48. if (!IsLoaded()) return E_NOT_VALID_STATE;
  49. if (idx >= m_pHeader->PartCount) return E_BOUNDS;
  50. const DxilPartHeader *pPart = GetDxilContainerPart(m_pHeader, idx);
  51. *ppResult = GetDxilPartData(pPart);
  52. if (pResultSize != nullptr) {
  53. *pResultSize = pPart->PartSize;
  54. }
  55. return S_OK;
  56. }
  57. HRESULT DxilContainerReader::GetPartFourCC(uint32_t idx, _Out_ uint32_t *pResult) {
  58. if (pResult == nullptr) return E_POINTER;
  59. if (!IsLoaded()) return E_NOT_VALID_STATE;
  60. if (idx >= m_pHeader->PartCount) return E_BOUNDS;
  61. const DxilPartHeader *pPart = GetDxilContainerPart(m_pHeader, idx);
  62. *pResult = pPart->PartFourCC;
  63. return S_OK;
  64. }
  65. HRESULT DxilContainerReader::FindFirstPartKind(uint32_t kind, _Out_ uint32_t *pResult) {
  66. if (pResult == nullptr) return E_POINTER;
  67. *pResult = 0;
  68. if (!IsLoaded()) return E_NOT_VALID_STATE;
  69. DxilPartIterator it = std::find_if(begin(m_pHeader), end(m_pHeader), DxilPartIsType(kind));
  70. *pResult = (it == end(m_pHeader)) ? DXIL_CONTAINER_BLOB_NOT_FOUND : it.index;
  71. return S_OK;
  72. }
  73. } // namespace hlsl