Initializing DX12 Textures After Allocation and Aliasing
Tags: directx, direct3d, d3d12, dx12
Published: 2020-03-19 21:25:35

---
title: Initializing DX12 Textures After Allocation and Aliasing
published: true
date: 2020-03-19 21:10:28 UTC
tags: directx,direct3d,d3d12,dx12
canonical_url: https://asawicki.info/news_1724_initializing_dx12_textures_after_allocation_and_aliasing
---

If you are a graphics programmer using Direct3D 12, you may wonder what's the initial content of a newly allocated buffer or texture. Microsoft admitted it was not clearly defined, but in practice such new memory is filled with zeros (unless you use the new flag `D3D12_HEAP_FLAG_CREATE_NOT_ZEROED`). See article [“Coming to DirectX 12: More control over memory allocation”](https://devblogs.microsoft.com/directx/coming-to-directx-12-more-control-over-memory-allocation/). This behavior has its pros and cons. Clearing all new memory makes sense, as the operating system surely doesn't want to disclose to us the data left by some other process, possibly containing passwords or other sensitive information. However, writing to a long memory region takes lots of time. Maybe that's one reason GPU memory allocation is so slow. I've seen large allocations taking even hundreds of milliseconds.

There are situations when the memory of your new buffer or texture is not zero, but may contain some random data. First case is **when you create a resource using CreatePlacedResource function** , inside a memory block that you might have used before for some other, already released resources. That's also what [D3D12 Memory Allocator](https://github.com/GPUOpen-LibrariesAndSDKs/D3D12MemoryAllocator) library does by default.

It is important to know that in this case you must initialize the resource in a specific way! The rules are described on page: [“ID3D12Device::CreatePlacedResource method”](https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-createplacedresource) and say: If your resource is a texture that has either `RENDER_TARGET` or `DEPTH_STENCIL` flag, you must initialize it after allocation and before any other usage using one of those methods: 1. a clear operation (`ClearRenderTargetView` or `ClearDepthStencilView`), 2. discard (`DiscardResource`), 3. copy to the entire resource as a destination (`CopyResource`, `CopyBufferRegion`, or `CopyTextureRegion`).

Please note that rendering to the texture as a Render Target or writing to it as an Unordered Access View is not on the list! It means that, for example, if you implement a posprocessing effect, you allocated an intermediate 1920x1080 texture, and you want to overwrite all its pixels by rendering a fullscreen quad or triangle (better to use one triangle - see article ["GCN Execution Patterns in Full Screen Passes"](https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in-full-screen-passes/)), then initializing the texture before your draw call seems redundant, but you still need to do it.

![Correct texture initialization after allocation](https://asawicki.info/files/initializing_dx12_textures_1.png)

What happens if you don't? Why are we asked to perform this initialization? Wouldn't we just see random colorful pixels if we use an uninitialized texture, which may or may not be a problem, depending on our use case? Not really... As I explained in my previous post [“Texture Compression: What Can It Mean?”](https://asawicki.info/news_1723_texture_compression_what_can_it_mean), a texture may be stored in video memory in some vendor-specific, compressed format. If the metadata of such compression are uninitialized, it might have consequences more severe than observing random colors. It's actually an undefined behavior. On one GPU everything may work fine, while on the other you may see graphical corruptions that even rendering to the texture as a Render Target cannot fix (or a total GPU crash maybe?) I've experienced this problem myself recently.

Thinking in terms of internal GPU texture compression also helps to explain why is this initialization required only for render-target and depth-stencil textures. GPUs use more aggressive compression techniques for those. Having the requirements for initialization defined like that implies that you can leave buffers and other textures uninitialized and just experience random data in their content without the danger of anything worse happening.

I feel that a side note on `ID3D12GraphicsCommandList::DiscardResource` function is needed, because many of you probably don't know it. Contrary to its name, this function doesn't release a resource or its memory. The meaning of this function is more like the mapping flag `D3D11_MAP_WRITE_DISCARD` from the old D3D11. It informs the driver that the current content of the resource might be garbage; we know about it, and we don't care, we don't need it, not going to read it, just going to fill the entire resource with a new content. Sometimes, calling this function may let the driver reach better performance. For example, it may skip downloading previous data from VRAM to the graphics chip. This is especially important and beneficial on tile-based, mobile GPUs. In some other cases, like the initialization of a newly allocated texture described here, it is required. Inside of it, driver might for example clear the metadata of its internal compression format. It is correct to call `DiscardResource` and then render to your new texture as a Render Target. It could also be potentially faster than doing `ClearRenderTargetView` instead of `DiscardResource`. By the way, if you happen to use Vulkan and still read that far, you might find it useful to know that the Vulkan equivalent of `DiscardResource` is an image memory barrier with `oldLayout = VK_IMAGE_LAYOUT_UNDEFINED`.

There is a second case when a resource may contain some random data. It happens **when you use memory aliasing**. This technique allows to save GPU memory by creating multiple resources in the same or overlapping region of a `ID3D12Heap`. It was not possible in old APIs (Direct3D 11, OpenGL) where each resource got its own implicit memory allocation. In Direct3D you can use `CreatePlacedResource` to put your resource in a specific heap, at a specific offset. It's not allowed to use aliasing resources at the same time. Sometimes you need some intermediate buffers or render targets only for a specific, short time during each frame. You can then reuse their memory for different resources needed in later part of the frame. That's the key idea of aliasing.

To do it correctly, you must do two things. First, between the usages you must issue a barrier of special type `D3D12_RESOURCE_BARRIER_TYPE_ALIASING`. Second, the resource to be used next (also called "ResourceAfter", as opposed to "ResourceBefore") needs to be initialized. The idea is like what I described before. You can find the rules of this initialization on page [“Memory Aliasing and Data Inheritance”](https://docs.microsoft.com/en-us/windows/win32/direct3d12/memory-aliasing-and-data-inheritance). This time however we are told to initialize every texture that has `RENDER_TARGET` or `DEPTH_STENCIL` flag with 1. a clear or 2. a copy operation to an entire subresource. `DiscardResource` is not allowed. Whether it's an omission or intentional, we have to stick to these rules, even if we feel such clears are redundant and will slow down our rendering. Otherwise we may experience hard to find bugs on some GPUs.

![Correct texture initialization after aliasing](https://asawicki.info/files/initializing_dx12_textures_2.png)