Skip to content

Instantly share code, notes, and snippets.

@shaderjp
Last active September 28, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaderjp/15df605e428a464852b786857b0d6839 to your computer and use it in GitHub Desktop.
Save shaderjp/15df605e428a464852b786857b0d6839 to your computer and use it in GitHub Desktop.
DirectXTexのDDSロードサンプル
// Load texture from DDS.
{
std::unique_ptr<uint8_t[]> ddsData;
std::vector<D3D12_SUBRESOURCE_DATA> subresouceData;
ThrowIfFailed(LoadDDSTextureFromFile(m_device.Get(),
L"test.DDS",
&m_texture,
ddsData,
subresouceData));
D3D12_RESOURCE_DESC textureDesc = m_texture->GetDesc();
const UINT subresoucesize
= static_cast<UINT>(subresouceData.size());
const UINT64 uploadBufferSize
= GetRequiredIntermediateSize(m_texture.Get(), 0, subresoucesize);
// Create the GPU upload buffer.
ThrowIfFailed(m_device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&textureUploadHeap)));
UpdateSubresources(m_commandList.Get(),
m_texture.Get(),
textureUploadHeap.Get(),
0,
0,
subresoucesize,
&subresouceData[0]);
m_commandList->ResourceBarrier(1,
&CD3DX12_RESOURCE_BARRIER::Transition(m_texture.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
// Describe and create a SRV for the texture.
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = subresoucesize;
m_device->CreateShaderResourceView(m_texture.Get(),
&srvDesc,
m_srvHeap->GetCPUDescriptorHandleForHeapStart());
m_commandList->DiscardResource(textureUploadHeap.Get(), nullptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment