MCPcopy Create free account
hub / github.com/TheRealMJP/DXRPathTracer / LoadTexture

Function LoadTexture

SampleFramework12/v1.02/Graphics/Textures.cpp:38–172  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36}
37
38void LoadTexture(Texture& texture, const wchar* filePath, bool forceSRGB)
39{
40 texture.Shutdown();
41 if(FileExists(filePath) == false)
42 throw Exception(MakeString(L"Texture file with path '%ls' does not exist", filePath));
43
44 DirectX::ScratchImage image;
45
46 const std::wstring extension = GetFileExtension(filePath);
47 if(extension == L"DDS" || extension == L"dds")
48 {
49 DXCall(DirectX::LoadFromDDSFile(filePath, DirectX::DDS_FLAGS_NONE, nullptr, image));
50 }
51 else if(extension == L"TGA" || extension == L"tga")
52 {
53 DirectX::ScratchImage tempImage;
54 DXCall(DirectX::LoadFromTGAFile(filePath, nullptr, tempImage));
55 DXCall(DirectX::GenerateMipMaps(*tempImage.GetImage(0, 0, 0), DirectX::TEX_FILTER_DEFAULT, 0, image, false));
56 }
57 else
58 {
59 DirectX::ScratchImage tempImage;
60 DXCall(DirectX::LoadFromWICFile(filePath, DirectX::WIC_FLAGS_NONE, nullptr, tempImage));
61 DXCall(DirectX::GenerateMipMaps(*tempImage.GetImage(0, 0, 0), DirectX::TEX_FILTER_DEFAULT, 0, image, false));
62 }
63
64 const DirectX::TexMetadata& metaData = image.GetMetadata();
65 DXGI_FORMAT format = metaData.format;
66 if(forceSRGB)
67 format = DirectX::MakeSRGB(format);
68
69 const bool is3D = metaData.dimension == DirectX::TEX_DIMENSION_TEXTURE3D;
70
71 D3D12_RESOURCE_DESC textureDesc = { };
72 textureDesc.MipLevels = uint16(metaData.mipLevels);
73 textureDesc.Format = format;
74 textureDesc.Width = uint32(metaData.width);
75 textureDesc.Height = uint32(metaData.height);
76 textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
77 textureDesc.DepthOrArraySize = is3D ? uint16(metaData.depth) : uint16(metaData.arraySize);
78 textureDesc.SampleDesc.Count = 1;
79 textureDesc.SampleDesc.Quality = 0;
80 textureDesc.Dimension = is3D ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D;
81 textureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
82 textureDesc.Alignment = 0;
83
84 ID3D12Device* device = DX12::Device;
85 DXCall(device->CreateCommittedResource(DX12::GetDefaultHeapProps(), D3D12_HEAP_FLAG_NONE, &textureDesc,
86 D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&texture.Resource)));
87 texture.Resource->SetName(filePath);
88
89 PersistentDescriptorAlloc srvAlloc = DX12::SRVDescriptorHeap.AllocatePersistent();
90 texture.SRV = srvAlloc.Index;
91
92 const D3D12_SHADER_RESOURCE_VIEW_DESC* srvDescPtr = nullptr;
93 D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = { };
94 if(metaData.IsCubemap())
95 {

Callers 2

InitializeMethod · 0.85
LoadMaterialResourcesFunction · 0.85

Calls 12

FileExistsFunction · 0.85
ExceptionClass · 0.85
MakeStringFunction · 0.85
GetFileExtensionFunction · 0.85
DXCallFunction · 0.85
GetDefaultHeapPropsFunction · 0.85
ResourceUploadBeginFunction · 0.85
MinFunction · 0.85
ResourceUploadEndFunction · 0.85
AllocatePersistentMethod · 0.80
IsCubemapMethod · 0.80
ShutdownMethod · 0.45

Tested by

no test coverage detected