Hi,
I have more of a broad abstraction design question that probably won't have one ultimate answer.
In many D3D12 samples (also in UE4), a buffer/texture is usually wrapped by a class that contains data about the width, height, stride, element count, .... and when the resource is created, a set of descriptors (SRV, UAV per mip, CBV, ...) are usually created depending on how the resource is going to be used (Rendertarget, Shader resource, Unordered Access, ...) and these descriptors are owned by that class. To give an example of the pattern that you usually come across (pseudo-code)
class Texture
{
void CreateTexture(int width, int height, int mipLevels)
{
CreateResource(width, height, ....);
m_Uav = AllocateDescriptors(mipLevels)
for each mip:
CreateUAV(m_Uav.offset(i));
CreateSRV();
CreateRTV();
....
}
int Width, Height, more properties ...
D3D12_CPU_DESCRIPTOR_HANDLE m_Rtv = {};
D3D12_CPU_DESCRIPTOR_HANDLE m_Uav = {};
D3D12_CPU_DESCRIPTOR_HANDLE m_Srv = {};
};
However, I find myself in a situation where I need different types of views for the resource and there is not a catch-all solution for it.
For example, I create a TextureCube and a SRV for it.
You could create
- a TextureCube SRV
- a Texture2DArray SRV
- Several Texture2D SRVs
This all depends on use and more than one view could be needed for the same resource.
Same for a depth buffer, if you want a depth buffer to be used for both writing and read_only, you need 2 separate descriptors (one with a READ_ONLY flag set).
I believe what makes views/descriptors so powerful, is that they provide you with different ways to interpret read the same data.
Having this "class wrapper" pretty much breaks this flexibility because all descriptors are created the same for the different types of resources you define in your abstraction and it is impossible to cover all uses.
Obviously, the solution would be to decouple the resource from the view but I wonder, how is this usually done?
Is a solution creating these descriptors on-the-fly, possibly even every single frame?
I suppose this is not specific to DirectX 12 and pretty much applies to any Graphics API
Thanks,
Simon