RI::createTexture, RI::destroyTexture¶
What it does¶
RI::createTexture(device, memory, offset, desc) creates a 2D, cube, or 3D texture described
by desc, placed in memory at offset. Like a
buffer, it allocates no memory of its own: the bytes belong to memory. Get the required size,
alignment, and memory kinds first with textureRequirements.
RI::destroyTexture(device, texture) destroys the texture without freeing memory.
In desc:
shape:e2D(default),eCube, ore3D(shapes);width,height,depth(3D only),layers(array layers; 6 per cube),mips(mip levels), andsamples(1, 2, 4, or 8 for MSAA, 2D only);format: one of the formats;usage: a bitmask of everything the texture will be used for (see the table below);viewFormats: additional formats that views of the texture may use, such as the sRGB version offormat. It is empty by default, which lets drivers keep compression.
Creating a texture also resets it: its initial contents are undefined, but the texture is in a
valid state and ready for any use. RI records that
reset itself, submits it on the graphics queue, and waits for it to finish. Because
createTexture costs a short submission, create textures at load time rather than in the middle
of a frame.
The returned RI::Texture holds its properties (shape, dimensions, mip and layer counts, sample
count, format, and usage) and view, a ready-made view of the whole texture in
its own format, used for attachments, descriptors, clears, and discards.
TextureUsage |
Lets you | Vulkan usage | D3D12 flag |
|---|---|---|---|
eSampled |
read it in shaders (ReadView, Texture2D<T>) |
SAMPLED |
not DENY_SHADER_RESOURCE |
eStorage |
read and write it in shaders (ReadWriteView, RWTexture2D<T>) |
STORAGE |
ALLOW_UNORDERED_ACCESS |
eRenderTarget |
render to it (color attachment) | COLOR_ATTACHMENT |
ALLOW_RENDER_TARGET |
eDepthStencil |
use it as the depth or stencil attachment | DEPTH_STENCIL_ATTACHMENT |
ALLOW_DEPTH_STENCIL |
eCopySrc / eCopyDst |
copy from / to it (copies) | TRANSFER_SRC / TRANSFER_DST |
- |
eReadOnlyDepthSampling |
sample it in shaders while it is a read-only depth attachment; needs eSampled, eDepthStencil, and a depth format |
- | - |
The usage flags also determine the layout each backend keeps the texture in between operations
(Image layouts are RI's business). Set every flag you need and no
more, because unused flags can disable hardware compression. On Vulkan, eStorage,
eRenderTarget, and eDepthStencil also add TRANSFER_DST for clears.
Shapes¶
- 2D: one image per layer; setting
layers > 1creates a 2D array (Texture2DArrayin HLSL). - Cube: a 2D array whose layers are faces, 6 per cube in the order +X, −X, +Y, −Y, +Z, −Z
on both backends (for example,
layers = 12is an array of two cubes). Cube textures must be square and cannot be multisampled. A view whose layer range covers whole cubes (baseLayerandlayerCountare multiples of 6) reads asTextureCube(6 layers) orTextureCubeArray; a view covering a subset of faces is a 2D array. Storage writes (RWTexture2DArray) and render targets always see the faces as a 2D array, so a geometry shader can render all six faces in one draw. - 3D:
depthslices ofwidth×height; a 3D texture has one layer, cannot be multisampled, and cannot use a depth format. Views read and write the whole volume (Texture3D,RWTexture3D). Where a 2D array counts layers, a 3D texture counts slices: a view'sbaseLayerandlayerCountselect slices of one mip to render to, and copies move slices.
| Vulkan | D3D12 | |
|---|---|---|
| Cube | 2D image with CUBE_COMPATIBLE; a cube view to read, a 2D-array view to write and render |
2D array; TEXTURECUBE(ARRAY) shader views |
| 3D | 3D image, with 2D_ARRAY_COMPATIBLE when it is a render target; render targets are 2D-array views of slices |
TEXTURE3D; render-target views pick WSlices |
RI::isDepthFormat(format) returns whether format is a depth format, and
RI::hasStencil(format) returns whether it also has a stencil component.
RI::bytesPerTexel(format) returns the byte size of one texel for color formats, or 0 for
depth-stencil formats.
Signature¶
// <RI/RI.hpp>, namespace RI
Texture createTexture(Device &device, const Memory &memory, uint64 offset, const TextureDesc &desc);
From the header: Creates the texture and resets it (contents undefined, ready for use), as a discard would (P69, R16). The reset is submitted on the graphics queue at once.
// <RI/RI.hpp>, namespace RI
struct Texture {
TextureShape shape = TextureShape::e2D;
uint32 width = 0;
uint32 height = 0;
uint32 depth = 1; // 3D textures
uint32 layers = 1;
uint32 mips = 1;
uint32 samples = 1;
Format format = Format::eUndefined;
TextureUsage usage = TextureUsage::eNone;
TextureView view; // the whole texture, in its own format
// internal: backend-private state (not part of the API)
};
From the header: A texture placed in user memory at offset (P31). It owns no memory. Its image layouts are RI's business (P16, L1).
| Field | Type | Default | Description |
|---|---|---|---|
shape |
TextureShape |
TextureShape::e2D |
|
width |
uint32 |
0 |
|
height |
uint32 |
0 |
|
depth |
uint32 |
1 |
3D textures |
layers |
uint32 |
1 |
|
mips |
uint32 |
1 |
|
samples |
uint32 |
1 |
|
format |
Format |
Format::eUndefined |
|
usage |
TextureUsage |
TextureUsage::eNone |
|
view |
TextureView |
the whole texture, in its own format |
// <RI/RI.hpp>, namespace RI
struct TextureDesc {
TextureShape shape = TextureShape::e2D;
uint32 width = 1;
uint32 height = 1;
uint32 depth = 1; // 3D textures only
uint32 layers = 1; // cube textures: 6 per cube
uint32 mips = 1;
uint32 samples = 1; // MSAA sample count: 1, 2, 4, 8
Format format = Format::eUndefined;
TextureUsage usage = TextureUsage::eNone;
// Other formats views may use (P74). Empty: views use `format` only, which
// lets drivers keep compression.
std::vector<Format> viewFormats;
std::string_view name; // empty: unnamed (P106)
};
| Field | Type | Default | Description |
|---|---|---|---|
shape |
TextureShape |
TextureShape::e2D |
|
width |
uint32 |
1 |
|
height |
uint32 |
1 |
|
depth |
uint32 |
1 |
3D textures only |
layers |
uint32 |
1 |
cube textures: 6 per cube |
mips |
uint32 |
1 |
|
samples |
uint32 |
1 |
MSAA sample count: 1, 2, 4, 8 |
format |
Format |
Format::eUndefined |
|
usage |
TextureUsage |
TextureUsage::eNone |
|
viewFormats |
std::vector<Format> |
Other formats views may use (P74). Empty: views use format only, which lets drivers keep compression. |
|
name |
std::string_view |
empty: unnamed (P106) |
From the header: The shape of a texture (P102). Cube textures are 2D arrays whose layers are faces, 6 per cube in the order +X, -X, +Y, -Y, +Z, -Z. 3D textures have depth slices instead of layers: views, copies and render targets count slices where a 2D array counts layers.
| Value | Meaning |
|---|---|
e2D |
|
e3D |
|
eCube |
// <RI/RI.hpp>, namespace RI
enum class TextureUsage : uint32 {
eNone = 0,
eSampled = rvl::eBit(0), // ReadView: Texture2D<...> reads
eStorage = rvl::eBit(1), // ReadWriteView: RWTexture2D<...>
eRenderTarget = rvl::eBit(2), // color attachment
eDepthStencil = rvl::eBit(3), // depth/stencil attachment
eCopySrc = rvl::eBit(4),
eCopyDst = rvl::eBit(5),
// Sampled by shaders while it is the read-only depth attachment of a
// rendering section (P75, P97). Needs eSampled and eDepthStencil. Without
// it, sampled depth rests ready for fast sampling, and shaders must not
// sample it during a read-only depth section.
eReadOnlyDepthSampling = rvl::eBit(6),
};
From the header: What a texture may be used for (P58). Also decides, inside each backend, the layout the texture rests in between operations (L1, A').
| Value | Meaning |
|---|---|
eNone |
|
eSampled |
ReadView: Texture2D<...> reads |
eStorage |
ReadWriteView: RWTexture2D<...> |
eRenderTarget |
color attachment |
eDepthStencil |
depth/stencil attachment |
eCopySrc |
|
eCopyDst |
|
eReadOnlyDepthSampling |
// <RI/RI.hpp>, namespace RI
enum class Format : uint16 {
eUndefined,
// Color
eR8Unorm,
eRG8Unorm,
eRGBA8Unorm,
eRGBA8Srgb,
eBGRA8Unorm,
eBGRA8Srgb,
eRGB10A2Unorm,
eR11G11B10Float,
eR16Float,
eRG16Float,
eRGBA16Float,
eR32Float,
eRG32Float,
eRGBA32Float,
eR32Uint,
eRG32Uint,
eRGBA32Uint,
// Depth and stencil
eD16Unorm,
eD32Float,
eD24UnormS8Uint, // not on AMD Vulkan: check formatSupported (P58 (2))
eD32FloatS8Uint,
};
| Value | Meaning |
|---|---|
eUndefined |
|
eR8Unorm |
|
eRG8Unorm |
|
eRGBA8Unorm |
|
eRGBA8Srgb |
|
eBGRA8Unorm |
|
eBGRA8Srgb |
|
eRGB10A2Unorm |
|
eR11G11B10Float |
|
eR16Float |
|
eRG16Float |
|
eRGBA16Float |
|
eR32Float |
|
eRG32Float |
|
eRGBA32Float |
|
eR32Uint |
|
eRG32Uint |
|
eRGBA32Uint |
|
eD16Unorm |
|
eD32Float |
|
eD24UnormS8Uint |
not on AMD Vulkan: check formatSupported (P58 (2)) |
eD32FloatS8Uint |
Example¶
// A color target that shaders also sample, with an sRGB view format (P74).
const RI::TextureDesc colorDesc{
.width = kWidth,
.height = kHeight,
.format = RI::Format::eRGBA8Unorm,
.usage = RI::TextureUsage::eRenderTarget | RI::TextureUsage::eSampled,
.viewFormats = {RI::Format::eRGBA8Srgb},
};
// A depth-stencil target that shaders also read (soft particles, SSAO, ...).
const RI::TextureDesc depthDesc{.width = kWidth, .height = kHeight, .format = depthFormat, .usage = depthUsage};
// A texture compute shaders write, with a full mip chain.
const RI::TextureDesc storageDesc{
.width = 512,
.height = 512,
.mips = 10,
.format = RI::Format::eRGBA16Float,
.usage = RI::TextureUsage::eStorage | RI::TextureUsage::eSampled,
};
RI::Memory memory = RI::allocateMemory(device, RI::MemoryKind::eGPU, scratchOffset + scratchReq.size);
RI::Texture color = RI::createTexture(device, memory, 0, colorDesc);
RI::Texture depth = RI::createTexture(device, memory, depthOffset, depthDesc);
RI::Texture storage = RI::createTexture(device, memory, storageOffset, storageDesc);
// Each texture comes with a view of the whole texture in its own format: color.view.
Two textures sharing memory (they need a discard before each first use):
// Two textures over the same bytes: use one, then the other, never both at once.
RI::Texture blurH = RI::createTexture(device, memory, scratchOffset, scratchDesc);
RI::Texture blurV = RI::createTexture(device, memory, scratchOffset, scratchDesc);
The same in native code¶
RI::Memory memory = RI::allocateMemory(device, RI::MemoryKind::eGPU, scratchOffset + scratchReq.size);
RI::Texture color = RI::createTexture(device, memory, 0, colorDesc);
RI::Texture depth = RI::createTexture(device, memory, depthOffset, depthDesc);
RI::Texture storage = RI::createTexture(device, memory, storageOffset, storageDesc);
// Each texture comes with a view of the whole texture in its own format: color.view.
// A 2D image placed in `memory` at `offset`. `viewFormats`: the formats views
// may use; more than one needs MUTABLE_FORMAT, and listing them exactly lets
// the driver keep compression.
VkImage createImage(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkImageCreateInfo info,
const std::vector<VkFormat> &viewFormats) {
const VkImageFormatListCreateInfo list{.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO,
.viewFormatCount = static_cast<uint32_t>(viewFormats.size()),
.pViewFormats = viewFormats.data()};
if (viewFormats.size() > 1) {
info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
info.pNext = &list;
}
VkImage image = VK_NULL_HANDLE;
check(vkCreateImage(device, &info, nullptr, &image), "vkCreateImage");
check(vkBindImageMemory(device, image, memory, offset), "vkBindImageMemory");
return image;
}
const VkImageCreateInfo colorInfo{
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.extent = {kWidth, kHeight, 1},
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, // the only choice for optimal tiling
};
VkImageCreateInfo depthInfo = colorInfo;
depthInfo.format = depthFormat;
depthInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
const std::vector<VkFormat> colorFormats = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB};
const VkMemoryAllocateInfo allocateInfo{
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = depthOffset + depthReq.memoryRequirements.size,
.memoryTypeIndex = findMemoryType(gpu, allowedTypes, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
};
VkDeviceMemory memory = VK_NULL_HANDLE;
check(vkAllocateMemory(device, &allocateInfo, nullptr, &memory), "vkAllocateMemory");
VkImage color = createImage(device, memory, 0, colorInfo, colorFormats);
VkImage depth = createImage(device, memory, depthOffset, depthInfo, {});
What to watch for: an image is created unbound and then bound to memory once. It starts in
the UNDEFINED layout, and its first use must transition it from there. Creating views in
other formats requires MUTABLE_FORMAT, and passing VkImageFormatListCreateInfo lets the
driver keep compression.
// A 2D texture description. Flags say what else than sampling it allows.
D3D12_RESOURCE_DESC1 textureDesc(UINT width, UINT height, DXGI_FORMAT format, D3D12_RESOURCE_FLAGS flags) {
return {.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
.Alignment = 0, // the default: 64 KiB (4 MiB for MSAA)
.Width = width,
.Height = height,
.DepthOrArraySize = 1,
.MipLevels = 1,
.Format = format,
.SampleDesc = {.Count = 1, .Quality = 0},
.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, // the driver's own (optimal) layout
.Flags = flags};
}
// GPU memory. ALLOW_ALL_BUFFERS_AND_TEXTURES needs resource heap tier 2.
const D3D12_HEAP_DESC heapDesc{.SizeInBytes = total.SizeInBytes,
.Properties = {.Type = D3D12_HEAP_TYPE_DEFAULT},
.Alignment = total.Alignment,
.Flags = D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES};
ComPtr<ID3D12Heap> memory;
check(device->CreateHeap(&heapDesc, IID_PPV_ARGS(&memory)), "CreateHeap");
// Castable formats: the other formats views may use (here sRGB). An exact
// list lets the driver keep compression.
const DXGI_FORMAT castable[] = {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
ComPtr<ID3D12Resource> color;
check(device->CreatePlacedResource2(memory.Get(), parts[0].Offset, &colorDesc, D3D12_BARRIER_LAYOUT_UNDEFINED,
nullptr, 1, castable, IID_PPV_ARGS(&color)),
"CreatePlacedResource2");
ComPtr<ID3D12Resource> depth;
check(device->CreatePlacedResource2(memory.Get(), parts[1].Offset, &depthDesc, D3D12_BARRIER_LAYOUT_UNDEFINED,
nullptr, 0, nullptr, IID_PPV_ARGS(&depth)),
"CreatePlacedResource2");
// A new placed texture is UNDEFINED, and render targets and depth textures
// must be initialized before use: a DISCARD barrier (or a clear or copy).
textureBarrier(cmd.Get(), color.Get(), D3D12_BARRIER_LAYOUT_UNDEFINED, D3D12_BARRIER_LAYOUT_RENDER_TARGET,
D3D12_BARRIER_SYNC_NONE, D3D12_BARRIER_ACCESS_NO_ACCESS, D3D12_BARRIER_SYNC_RENDER_TARGET,
D3D12_BARRIER_ACCESS_RENDER_TARGET, D3D12_TEXTURE_BARRIER_FLAG_DISCARD);
textureBarrier(cmd.Get(), depth.Get(), D3D12_BARRIER_LAYOUT_UNDEFINED, D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE,
D3D12_BARRIER_SYNC_NONE, D3D12_BARRIER_ACCESS_NO_ACCESS, D3D12_BARRIER_SYNC_DEPTH_STENCIL,
D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE, D3D12_TEXTURE_BARRIER_FLAG_DISCARD);
What to watch for: CreatePlacedResource2 takes the initial layout and the castable
formats (the other formats views may use). A placed render target or depth texture must
be initialized with a DISCARD barrier, a clear, or a copy before its first use. A depth
texture that shaders read is created TYPELESS, so its depth view and its shader view can
use different formats.
Rules and errors¶
offsetmust be a multiple of the alignment fromtextureRequirements, and the texture must fit inmemory; validation builds check both (programmer error).- Placing a texture in a memory kind that does not support it reports an unsupported error
(textures usually fit only
eGPUmemory; on D3D12,eGPUis the only kind allowed). - A texture placed over memory that another resource used must be discarded before its first use; validation builds report a programmer error otherwise.
- Destroy a texture only when the GPU no longer uses it (after its
timeline point), after the views you created of it, and
before freeing its memory.
destroyTexturealso destroystexture.view. Textureis a thin value: copies refer to the same texture. Using a destroyed texture is a programmer error in validation builds.- A cube texture must be square with 6 layers per cube, and a 3D texture must have one layer and
a color format. Only 2D textures may be multisampled, and only 3D textures may have a
depthabove 1. Validation builds report violations as a programmer error. - An imported cube image on Vulkan must have been created with
CUBE_COMPATIBLE, and an imported 3D render target must have been created with2D_ARRAY_COMPATIBLE.