diff --git a/Image-Loading-and-Displaying-Examples.md b/Image-Loading-and-Displaying-Examples.md index c474127..92e3fc2 100644 --- a/Image-Loading-and-Displaying-Examples.md +++ b/Image-Loading-and-Displaying-Examples.md @@ -348,20 +348,7 @@ ImGui::End(); ## Example for DirectX12 users -It should be noted that the DirectX12 API is substantially more complex than previous iterations, and assumes that you will be writing your own code to manage various things such as resource allocation. As such, it's a lot harder to provide a "one size fits all" example than with some APIs, and in all probability the code below will need modification to work with any existing engine code. It should however work if inserted into the Dear ImGui DirectX12 example project, with the caveat that the `CreateDeviceD3D()` function will need to be modified slightly to allocate two descriptors in the SRV resource descriptor heap as follows: - -```cpp -{ - D3D12_DESCRIPTOR_HEAP_DESC desc = {}; - desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - desc.NumDescriptors = 2; // <-- Set this value to 2 (the first descriptor is used for the built-in font texture, the second for our new texture) - desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - if (g_pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&g_pd3dSrvDescHeap)) != S_OK) - return false; -} -``` - -If you want to use this code to load more than one texture (and don't have your own mechanism for allocating descriptors), then you will need to increase `NumDescriptors` accordingly and also increment `descriptor_index` for each texture loaded. +It should be noted that the DirectX12 API is substantially more complex than previous iterations, and assumes that you will be writing your own code to manage various things such as resource allocation. As such, it's a lot harder to provide a "one size fits all" example than with some APIs, and in all probability the code below will need modification to work with any existing engine code. It should however work if inserted into the Dear ImGui DirectX12 example project. Since 1.91.5, the DirectX12 backend was changed to accept a callback for allocating/free SRV descriptor, so this is the responsability of the app. The DirectX12 example creates a free list (see `int APP_SRV_HEAP_SIZE = 64;' near top of the file). The actual implementation itself is as follows - firstly, at the top of one of your source files add: ```cpp @@ -565,14 +552,11 @@ int my_image_height = 0; ID3D12Resource* my_texture = NULL; // Get CPU/GPU handles for the shader resource view -// Normally your engine will have some sort of allocator for these - here we assume that there's an SRV descriptor heap in -// g_pd3dSrvDescHeap with at least two descriptors allocated, and descriptor 1 is unused -UINT handle_increment = g_pd3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); -int descriptor_index = 1; // The descriptor table index to use (not normally a hard-coded constant, but in this case we'll assume we have slot 1 reserved for us) -D3D12_CPU_DESCRIPTOR_HANDLE my_texture_srv_cpu_handle = g_pd3dSrvDescHeap->GetCPUDescriptorHandleForHeapStart(); -my_texture_srv_cpu_handle.ptr += (handle_increment * descriptor_index); -D3D12_GPU_DESCRIPTOR_HANDLE my_texture_srv_gpu_handle = g_pd3dSrvDescHeap->GetGPUDescriptorHandleForHeapStart(); -my_texture_srv_gpu_handle.ptr += (handle_increment * descriptor_index); +// Normally your engine will have some sort of allocator for these. +// In our example we use a simple ExampleDescriptorHeapAllocator helper. +D3D12_CPU_DESCRIPTOR_HANDLE my_texture_srv_cpu_handle; +D3D12_GPU_DESCRIPTOR_HANDLE my_texture_srv_gpu_handle; +g_pd3dSrvDescHeapAlloc.Alloc(&my_texture_srv_cpu_handle, my_texture_srv_gpu_handle); // Load the texture from a file bool ret = LoadTextureFromFile("../../MyImage01.jpg", g_pd3dDevice, my_texture_srv_cpu_handle, &my_texture, &my_image_width, &my_image_height);