dxcapi.impl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // dxcapi.impl.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. // Provides support for DXC API implementations. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef __DXCAPI_IMPL__
  12. #define __DXCAPI_IMPL__
  13. #include "dxc/dxcapi.h"
  14. #include "dxc/Support/microcom.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. // Simple adaptor for IStream. Can probably do better.
  17. class raw_stream_ostream : public llvm::raw_ostream {
  18. private:
  19. CComPtr<hlsl::AbstractMemoryStream> m_pStream;
  20. void write_impl(const char *Ptr, size_t Size) override {
  21. ULONG cbWritten;
  22. IFT(m_pStream->Write(Ptr, Size, &cbWritten));
  23. }
  24. uint64_t current_pos() const { return m_pStream->GetPosition(); }
  25. public:
  26. raw_stream_ostream(hlsl::AbstractMemoryStream* pStream) : m_pStream(pStream) { }
  27. ~raw_stream_ostream() override {
  28. flush();
  29. }
  30. };
  31. class DxcOperationResult : public IDxcOperationResult {
  32. private:
  33. DXC_MICROCOM_REF_FIELD(m_dwRef)
  34. DxcOperationResult(_In_opt_ IDxcBlob *pResultBlob,
  35. _In_opt_ IDxcBlobEncoding *pErrorBlob, HRESULT status)
  36. : m_dwRef(0), m_status(status), m_result(pResultBlob),
  37. m_errors(pErrorBlob) {}
  38. public:
  39. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  40. HRESULT m_status;
  41. CComPtr<IDxcBlob> m_result;
  42. CComPtr<IDxcBlobEncoding> m_errors;
  43. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) {
  44. return DoBasicQueryInterface<IDxcOperationResult>(this, iid, ppvObject);
  45. }
  46. static HRESULT CreateFromResultErrorStatus(_In_opt_ IDxcBlob *pResultBlob,
  47. _In_opt_ IDxcBlobEncoding *pErrorBlob,
  48. HRESULT status,
  49. _COM_Outptr_ IDxcOperationResult **ppResult) {
  50. *ppResult = nullptr;
  51. CComPtr<DxcOperationResult> result = new (std::nothrow) DxcOperationResult(pResultBlob, pErrorBlob, status);
  52. if (result.p == nullptr) return E_OUTOFMEMORY;
  53. *ppResult = result.Detach();
  54. return S_OK;
  55. }
  56. static HRESULT
  57. CreateFromUtf8Strings(_In_opt_z_ LPCSTR pErrorStr,
  58. _In_opt_z_ LPCSTR pResultStr, HRESULT status,
  59. _COM_Outptr_ IDxcOperationResult **pResult) {
  60. *pResult = nullptr;
  61. CComPtr<IDxcBlobEncoding> resultBlob;
  62. CComPtr<IDxcBlobEncoding> errorBlob;
  63. CComPtr<DxcOperationResult> result;
  64. HRESULT hr = S_OK;
  65. if (pErrorStr != nullptr) {
  66. hr = hlsl::DxcCreateBlobWithEncodingOnHeapCopy(
  67. pErrorStr, strlen(pErrorStr), CP_UTF8, &errorBlob);
  68. if (FAILED(hr)) {
  69. return hr;
  70. }
  71. }
  72. if (pResultStr != nullptr) {
  73. hr = hlsl::DxcCreateBlobWithEncodingOnHeap(
  74. pResultStr, strlen(pResultStr), CP_UTF8, &resultBlob);
  75. if (FAILED(hr)) {
  76. return hr;
  77. }
  78. }
  79. return CreateFromResultErrorStatus(resultBlob, errorBlob, status, pResult);
  80. }
  81. __override HRESULT STDMETHODCALLTYPE GetStatus(_Out_ HRESULT *pStatus) {
  82. if (pStatus == nullptr)
  83. return E_INVALIDARG;
  84. *pStatus = m_status;
  85. return S_OK;
  86. }
  87. __override HRESULT STDMETHODCALLTYPE
  88. GetResult(_COM_Outptr_result_maybenull_ IDxcBlob **ppResult) {
  89. return m_result.CopyTo(ppResult);
  90. }
  91. __override HRESULT STDMETHODCALLTYPE
  92. GetErrorBuffer(_COM_Outptr_result_maybenull_ IDxcBlobEncoding **ppErrors) {
  93. return m_errors.CopyTo(ppErrors);
  94. }
  95. };
  96. #endif