home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / d3dUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  4.2 KB  |  126 lines

  1.  
  2. #include "d3dUtil.h"
  3. #include <comdef.h>
  4. #include <fstream>
  5.  
  6. using Microsoft::WRL::ComPtr;
  7.  
  8. DxException::DxException(HRESULT hr, const std::wstring& functionName, const std::wstring& filename, int lineNumber) :
  9.     ErrorCode(hr),
  10.     FunctionName(functionName),
  11.     Filename(filename),
  12.     LineNumber(lineNumber)
  13. {
  14. }
  15.  
  16. bool d3dUtil::IsKeyDown(int vkeyCode)
  17. {
  18.     return (GetAsyncKeyState(vkeyCode) & 0x8000) != 0;
  19. }
  20.  
  21. ComPtr<ID3DBlob> d3dUtil::LoadBinary(const std::wstring& filename)
  22. {
  23.     std::ifstream fin(filename, std::ios::binary);
  24.  
  25.     fin.seekg(0, std::ios_base::end);
  26.     std::ifstream::pos_type size = (int)fin.tellg();
  27.     fin.seekg(0, std::ios_base::beg);
  28.  
  29.     ComPtr<ID3DBlob> blob;
  30.     ThrowIfFailed(D3DCreateBlob(size, blob.GetAddressOf()));
  31.  
  32.     fin.read((char*)blob->GetBufferPointer(), size);
  33.     fin.close();
  34.  
  35.     return blob;
  36. }
  37.  
  38. Microsoft::WRL::ComPtr<ID3D12Resource> d3dUtil::CreateDefaultBuffer(
  39.     ID3D12Device* device,
  40.     ID3D12GraphicsCommandList* cmdList,
  41.     const void* initData,
  42.     UINT64 byteSize,
  43.     Microsoft::WRL::ComPtr<ID3D12Resource>& uploadBuffer)
  44. {
  45.     ComPtr<ID3D12Resource> defaultBuffer;
  46.  
  47.     // Create the actual default buffer resource.
  48.     ThrowIfFailed(device->CreateCommittedResource(
  49.         &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  50.         D3D12_HEAP_FLAG_NONE,
  51.         &CD3DX12_RESOURCE_DESC::Buffer(byteSize),
  52.         D3D12_RESOURCE_STATE_COMMON,
  53.         nullptr,
  54.         IID_PPV_ARGS(defaultBuffer.GetAddressOf())));
  55.  
  56.     // In order to copy CPU memory data into our default buffer, we need to create
  57.     // an intermediate upload heap. 
  58.     ThrowIfFailed(device->CreateCommittedResource(
  59.         &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
  60.         D3D12_HEAP_FLAG_NONE,
  61.         &CD3DX12_RESOURCE_DESC::Buffer(byteSize),
  62.         D3D12_RESOURCE_STATE_GENERIC_READ,
  63.         nullptr,
  64.         IID_PPV_ARGS(uploadBuffer.GetAddressOf())));
  65.  
  66.  
  67.     // Describe the data we want to copy into the default buffer.
  68.     D3D12_SUBRESOURCE_DATA subResourceData = {};
  69.     subResourceData.pData = initData;
  70.     subResourceData.RowPitch = byteSize;
  71.     subResourceData.SlicePitch = subResourceData.RowPitch;
  72.  
  73.     // Schedule to copy the data to the default buffer resource.  At a high level, the helper function UpdateSubresources
  74.     // will copy the CPU memory into the intermediate upload heap.  Then, using ID3D12CommandList::CopySubresourceRegion,
  75.     // the intermediate upload heap data will be copied to mBuffer.
  76.     cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(), 
  77.         D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST));
  78.     UpdateSubresources<1>(cmdList, defaultBuffer.Get(), uploadBuffer.Get(), 0, 0, 1, &subResourceData);
  79.     cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(),
  80.         D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_GENERIC_READ));
  81.  
  82.     // Note: uploadBuffer has to be kept alive after the above function calls because
  83.     // the command list has not been executed yet that performs the actual copy.
  84.     // The caller can Release the uploadBuffer after it knows the copy has been executed.
  85.  
  86.  
  87.     return defaultBuffer;
  88. }
  89.  
  90. ComPtr<ID3DBlob> d3dUtil::CompileShader(
  91.     const std::wstring& filename,
  92.     const D3D_SHADER_MACRO* defines,
  93.     const std::string& entrypoint,
  94.     const std::string& target)
  95. {
  96.     UINT compileFlags = 0;
  97. #if defined(DEBUG) || defined(_DEBUG)  
  98.     compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  99. #endif
  100.  
  101.     HRESULT hr = S_OK;
  102.  
  103.     ComPtr<ID3DBlob> byteCode = nullptr;
  104.     ComPtr<ID3DBlob> errors;
  105.     hr = D3DCompileFromFile(filename.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE,
  106.         entrypoint.c_str(), target.c_str(), compileFlags, 0, &byteCode, &errors);
  107.  
  108.     if(errors != nullptr)
  109.         OutputDebugStringA((char*)errors->GetBufferPointer());
  110.  
  111.     ThrowIfFailed(hr);
  112.  
  113.     return byteCode;
  114. }
  115.  
  116. std::wstring DxException::ToString()const
  117. {
  118.     // Get the string description of the error code.
  119.     _com_error err(ErrorCode);
  120.     std::wstring msg = err.ErrorMessage();
  121.  
  122.     return FunctionName + L" failed in " + Filename + L"; line " + std::to_wstring(LineNumber) + L"; error: " + msg;
  123. }
  124.  
  125.  
  126.