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

Function LoadTexture

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

Source from the content-addressed store, hash-verified

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

Callers 3

InitializeMethod · 0.85
InitializeMethod · 0.85
LoadMaterialResourcesFunction · 0.85

Calls 13

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
TransitionResourceFunction · 0.85
AllocateMethod · 0.80
IsCubemapMethod · 0.80

Tested by

no test coverage detected