RI::createView, RI::destroyView¶
What it does¶
A view (RI::TextureView) selects part of a texture—a mip range, a layer or depth-slice
range, and an aspect—interpreted in a single format.
Operations on textures take a view rather than a texture directly, including
rendering attachments, descriptors,
clears, and discards.
Every texture already comes with texture.view, which covers the whole texture in its own
format. When you need a subset of mips, layers, or aspects, or a different format, call
RI::createView(device, texture, desc) once to create a view, and
RI::destroyView(device, view) to destroy it when you are done. RI keeps no hidden cache of
views and never creates them implicitly.
All fields in TextureViewDesc are optional (a default-initialized TextureViewDesc selects
the whole texture in its own format):
format:Format::eUndefined(default) uses the texture's own format. Any other format must be the texture's format or one listed in itsTextureDesc::viewFormats.baseMip,mipCount: the mip range starting atbaseMip(default0).kAllMips(default) selects every mip frombaseMipto the last mip of the texture.baseLayer,layerCount: the array layer range starting atbaseLayer(default0).kAllLayers(default) selects every layer frombaseLayerto the end.- For a 3D texture,
baseLayerandlayerCountselect depth slices of the view's first mip (baseMip). Only render targets use this slice range (shader views always access the whole volume), and a view that selects a subset of depth slices must cover only one mip (mipCount = 1). - For a cube texture, a view whose layer range starts on a multiple of 6 and covers a non-zero multiple of 6 layers (whole cubes) reads in shaders as a cube or cube array; any other layer range reads as a 2D array (shapes).
- For a 3D texture,
aspect: which part of a texture the view covers.
Aspect |
Covers |
|---|---|
eDefault |
Color for color formats; depth and stencil for depth-stencil formats (depth only if the format has no stencil) |
eColor |
Color (color formats only) |
eDepth |
Depth only (depth formats only) |
eStencil |
Stencil only (formats with stencil only) |
The returned RI::TextureView exposes format, width, height, and samples. width
and height are the dimensions of the view's first mip (baseMip), clamped to at least 1—for
example, a view starting at mip 2 of a 1024×1024 texture has width = 256 and height = 256.
Signature¶
// <RI/RI.hpp>, namespace RI
TextureView createView(Device &device, const Texture &texture, const TextureViewDesc &desc);
From the header: The view's format is the texture's or one of its desc.viewFormats; any other is an error in every build (P74).
// <RI/RI.hpp>, namespace RI
struct TextureView {
Format format = Format::eUndefined;
uint32 width = 0; // of the view's first mip
uint32 height = 0;
uint32 samples = 1;
// internal: backend-private state (not part of the API)
};
From the header: Part of a texture (P73), created explicitly with createView; every texture also has a ready-made whole-texture view.
| Field | Type | Default | Description |
|---|---|---|---|
format |
Format |
Format::eUndefined |
|
width |
uint32 |
0 |
of the view's first mip |
height |
uint32 |
0 |
|
samples |
uint32 |
1 |
// <RI/RI.hpp>, namespace RI
struct TextureViewDesc {
Format format = Format::eUndefined; // eUndefined: the texture's format
uint32 baseMip = 0;
uint32 mipCount = kAllMips;
uint32 baseLayer = 0; // 3D: first depth slice
uint32 layerCount = kAllLayers; // 3D: depth slices of the view's first mip
Aspect aspect = Aspect::eDefault;
std::string_view name; // empty: unnamed (P106)
};
From the header: What a view is in HLSL follows from the texture and the range (P102): a cube texture's view covering whole cubes reads as TextureCube or TextureCubeArray, and is written and rendered to as a 2D array of faces; other views of it are 2D arrays. A 3D texture's view reads and writes the whole volume (Texture3D, RWTexture3D) of its mips; its slice range (baseLayer, layerCount, one mip) only matters as a render target.
| Field | Type | Default | Description |
|---|---|---|---|
format |
Format |
Format::eUndefined |
eUndefined: the texture's format |
baseMip |
uint32 |
0 |
|
mipCount |
uint32 |
kAllMips |
|
baseLayer |
uint32 |
0 |
3D: first depth slice |
layerCount |
uint32 |
kAllLayers |
3D: depth slices of the view's first mip |
aspect |
Aspect |
Aspect::eDefault |
|
name |
std::string_view |
empty: unnamed (P106) |
// <RI/RI.hpp>, namespace RI
enum class Aspect : uint8 {
eDefault, // color for color formats, depth (and stencil) for depth formats
eColor,
eDepth,
eStencil,
};
| Value | Meaning |
|---|---|
eDefault |
color for color formats, depth (and stencil) for depth formats |
eColor |
|
eDepth |
|
eStencil |
Example¶
// The same texels read as sRGB (listed in viewFormats).
RI::TextureView colorSrgb = RI::createView(device, color, {.format = RI::Format::eRGBA8Srgb});
// Shaders read depth or stencil, never both: a depth-only view.
RI::TextureView depthOnly = RI::createView(device, depth, {.aspect = RI::Aspect::eDepth});
// One mip, e.g. to write mip 1 while sampling mip 0.
RI::TextureView storageMip1 = RI::createView(device, storage, {.baseMip = 1, .mipCount = 1});
// storageMip1.width == 256: a view reports the size of its first mip.
The same in native code¶
// The same texels read as sRGB (listed in viewFormats).
RI::TextureView colorSrgb = RI::createView(device, color, {.format = RI::Format::eRGBA8Srgb});
// Shaders read depth or stencil, never both: a depth-only view.
RI::TextureView depthOnly = RI::createView(device, depth, {.aspect = RI::Aspect::eDepth});
// One mip, e.g. to write mip 1 while sampling mip 0.
RI::TextureView storageMip1 = RI::createView(device, storage, {.baseMip = 1, .mipCount = 1});
// storageMip1.width == 256: a view reports the size of its first mip.
// A view: a format, an aspect, a mip range and a layer range of the image.
VkImageView createView(VkDevice device, VkImage image, VkFormat format, VkImageAspectFlags aspect, uint32_t baseMip,
uint32_t mipCount) {
const VkImageViewCreateInfo info{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = format,
.subresourceRange = {.aspectMask = aspect,
.baseMipLevel = baseMip,
.levelCount = mipCount,
.baseArrayLayer = 0,
.layerCount = 1},
};
VkImageView view = VK_NULL_HANDLE;
check(vkCreateImageView(device, &info, nullptr, &view), "vkCreateImageView");
return view;
}
// An image has no view of its own: create every view you use.
VkImageView colorView = createView(device, color, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT, 0, 1);
VkImageView colorSrgb = createView(device, color, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, 0, 1);
VkImageView depthView =
createView(device, depth, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, 0, 1);
// Shaders read one aspect: a depth-only view.
VkImageView depthOnly = createView(device, depth, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1);
What to watch for: Unlike RI textures, a VkImage has no built-in default view; you must
create a VkImageView for every use. Creating a view in a different format requires
VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT on the image (and preferably
VkImageFormatListCreateInfo so drivers can keep compression). If the view's format does
not support a usage the image has (for example, sRGB formats cannot be used as storage
images), you must drop that usage with VkImageViewUsageCreateInfo—RI handles this
automatically.
// Attachments are named by CPU descriptors in non-shader-visible heaps:
// one heap of render-target views (RTV), one of depth-stencil views (DSV).
const D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV, .NumDescriptors = 8};
const D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV, .NumDescriptors = 8};
ComPtr<ID3D12DescriptorHeap> rtvHeap;
ComPtr<ID3D12DescriptorHeap> dsvHeap;
check(device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap)), "CreateDescriptorHeap");
check(device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&dsvHeap)), "CreateDescriptorHeap");
const D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtvHeap->GetCPUDescriptorHandleForHeapStart();
const D3D12_CPU_DESCRIPTOR_HANDLE dsv = dsvHeap->GetCPUDescriptorHandleForHeapStart();
const D3D12_CPU_DESCRIPTOR_HANDLE dsvReadOnly{
dsv.ptr + device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV)};
const D3D12_RENDER_TARGET_VIEW_DESC rtvDesc{.Format = DXGI_FORMAT_R8G8B8A8_UNORM,
.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D,
.Texture2D = {.MipSlice = 0}};
device->CreateRenderTargetView(color.Get(), &rtvDesc, rtv);
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc{.Format = DXGI_FORMAT_D32_FLOAT,
.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D,
.Flags = D3D12_DSV_FLAG_NONE,
.Texture2D = {.MipSlice = 0}};
device->CreateDepthStencilView(depth.Get(), &dsvDesc, dsv);
// A second, read-only DSV: depth tested, not written, while shaders sample it.
dsvDesc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH;
device->CreateDepthStencilView(depth.Get(), &dsvDesc, dsvReadOnly);
// Shader views go in the shader-visible heap (see snippets/d3d12/compute.cpp):
// the color texture read as sRGB, the depth texture read as R32_FLOAT.
const D3D12_DESCRIPTOR_HEAP_DESC viewHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
.NumDescriptors = 16,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
ComPtr<ID3D12DescriptorHeap> viewHeap;
check(device->CreateDescriptorHeap(&viewHeapDesc, IID_PPV_ARGS(&viewHeap)), "CreateDescriptorHeap");
const UINT viewSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
const D3D12_CPU_DESCRIPTOR_HANDLE slot0 = viewHeap->GetCPUDescriptorHandleForHeapStart();
const D3D12_SHADER_RESOURCE_VIEW_DESC colorSrv{.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, // castable
.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D,
.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
.Texture2D = {.MostDetailedMip = 0, .MipLevels = 1}};
device->CreateShaderResourceView(color.Get(), &colorSrv, slot0);
const D3D12_SHADER_RESOURCE_VIEW_DESC depthSrv{.Format = DXGI_FORMAT_R32_FLOAT,
.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D,
.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
.Texture2D = {.MostDetailedMip = 0, .MipLevels = 1}};
device->CreateShaderResourceView(depth.Get(), &depthSrv, D3D12_CPU_DESCRIPTOR_HANDLE{slot0.ptr + viewSize});
What to watch for: D3D12 has no separate view objects, only descriptors: render-target
views (RTVs) and depth-stencil views (DSVs) live in CPU-only descriptor heaps, while shader
resource views (SRVs) and unordered access views (UAVs) live in shader-visible heaps. When
you call createView, RI allocates and creates the RTV or DSV descriptors allowed by the
texture's usage (two DSVs for depth-stencil textures: one writable and one read-only).
Shader descriptors (SRVs and UAVs) are written later by
writeDescriptors.
Rules and errors¶
- A format that is neither the texture's format nor listed in its
viewFormatsis a programmer error in every build:createView: format ... is not the texture's format or one of its viewFormats (P74). - In validation builds, a view format must have the same size per texel as the texture's
format (programmer error). On Vulkan, validation builds also check that
mipCountandlayerCountare non-zero and that the mip and layer ranges lie within the texture (programmer error). - For a 3D texture, a view that selects a subset of depth slices must cover one mip
(
mipCount = 1; programmer error in validation builds). - Requesting an aspect the format does not have (such as
Aspect::eDepthon a color texture) is a programmer error (reported on Vulkan). - Shaders cannot read a view that includes both depth and stencil: create a view with
Aspect::eDepthorAspect::eStencilinstead (writeDescriptorsreports a programmer error on Vulkan if both are present). - A rendering attachment view must cover a single mip (
mipCount = 1). - Destroy every view you created with
RI::destroyViewbefore destroying its texture, and only after the GPU has finished using it. Do not passtexture.viewtoRI::destroyView;RI::destroyTexturedestroys it automatically.