microsoft / DirectXTK12

The DirectX Tool Kit (aka DirectXTK12) is a collection of helper classes for writing DirectX 12 code in C++
https://walbourn.github.io/directx-tool-kit-for-directx-12/
MIT License
1.48k stars 393 forks source link

How to know a resource is dependent or independent? #42

Closed HppZ closed 5 years ago

walbourn commented 5 years ago

I think you are asking if something should be created in Initialize, in CreateDevice / CreateDeviceDependentResources, -or- inside CreateResources / CreateWindowSizeDependentResources in the directx-vs-templates?

Or do you mean something else?

HppZ commented 5 years ago

Yes, that is exactly what I mean.

walbourn commented 5 years ago

The basic reason for organizing it this way is that some things don't depend on a Direct3D device, such as creating your audio graph. My audio samples therefore initialize the XAudio2 engine in Initialize which only called once for a given program instance.

Lots of Direct3D resources require the Direct3D device: textures, vertex buffers, index buffers, etc. In my samples, I typically do model loading, creating spritebatch instances, etc. in CreateDeviceDependentResources.

Some resources depend on both the Direct3D device and is based on the presentation window size. This includes the size of the swap chain, render targets, offscreen render targets, etc. In my samples, I typically handle font loading here if it depends on resolution (on Xbox it would be 1080p vs. 4k), but mostly it's the swapchain and this is done in CreateWindowSizeDependentResources.

When the user changes the size of the presentation window (resizing the window, maximize/restore), then CreateWindowSizeDependentResources is called to re-create any size-dependent resources.

If the system encounters a 'lost device' scenario (i.e. getting DXGI_ERROR_DEVICE_REMOVED from DXGI Present), then both CreateDeviceDependentResources and CreateWindowSizeDependentResources will get called again.