* Loads a texture from disk, creates a D3D12 GPU resource, and uploads the texels to the GPU. * Returns the index of the texture in the resources.textures array. * Uses format DXGI_FORMAT_R8G8B8A8_UNORM. * Does not make mips and puts the texture in state D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE. * An upload buffer is created and not freed until cleanup. */
| 80 | * An upload buffer is created and not freed until cleanup. |
| 81 | */ |
| 82 | bool LoadAndCreateTexture(D3D12Global &d3d, D3D12Resources &resources, Texture &texture, int &index) |
| 83 | { |
| 84 | // Load the texture from disk |
| 85 | if (!LoadTexture(texture)) return false; |
| 86 | |
| 87 | // Create the texture resource on the default heap |
| 88 | ID3D12Resource* textureResource = nullptr; |
| 89 | { |
| 90 | // Describe the texture resource |
| 91 | D3D12_RESOURCE_DESC desc = {}; |
| 92 | desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 93 | desc.Alignment = 0; |
| 94 | desc.Width = texture.width; |
| 95 | desc.Height = texture.height; |
| 96 | desc.DepthOrArraySize = 1; |
| 97 | desc.MipLevels = 1; |
| 98 | desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 99 | desc.SampleDesc.Count = 1; |
| 100 | desc.SampleDesc.Quality = 0; |
| 101 | desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; |
| 102 | desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 103 | |
| 104 | // Create the texture resource |
| 105 | HRESULT hr = d3d.device->CreateCommittedResource(&defaultHeapProps, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&textureResource)); |
| 106 | if (FAILED(hr)) return false; |
| 107 | #if defined(RTXGI_NAME_D3D_OBJECTS) |
| 108 | std::string name = "Texture: "; |
| 109 | name.append(texture.name); |
| 110 | std::wstring n = std::wstring(name.begin(), name.end()); |
| 111 | textureResource->SetName(n.c_str()); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | UINT rowPitch = RTXGI_ALIGN(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, texture.width * texture.stride); |
| 116 | ID3D12Resource* uploadBuffer = nullptr; |
| 117 | |
| 118 | // Create an upload buffer and copy texels into it |
| 119 | { |
| 120 | UINT uploadBufferSize = (rowPitch * texture.height); |
| 121 | |
| 122 | // Describe the upload buffer |
| 123 | D3D12_RESOURCE_DESC desc = {}; |
| 124 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 125 | desc.Alignment = 0; |
| 126 | desc.Width = uploadBufferSize; |
| 127 | desc.Height = 1; |
| 128 | desc.DepthOrArraySize = 1; |
| 129 | desc.MipLevels = 1; |
| 130 | desc.Format = DXGI_FORMAT_UNKNOWN; |
| 131 | desc.SampleDesc.Count = 1; |
| 132 | desc.SampleDesc.Quality = 0; |
| 133 | desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 134 | desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 135 | |
| 136 | // Create the upload buffer |
| 137 | HRESULT hr = d3d.device->CreateCommittedResource(&uploadHeapProps, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&uploadBuffer)); |
| 138 | if (FAILED(hr)) return false; |
| 139 | #if RTXGI_NAME_D3D_OBJECTS |
no test coverage detected