summaryrefslogtreecommitdiff
path: root/appendices/VK_EXT_fragment_density_map.adoc
blob: ec18fdd8eeb75188ebe842e534cd0e57a540b8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2018-2023 The Khronos Group Inc.
//
// SPDX-License-Identifier: CC-BY-4.0

include::{generated}/meta/{refprefix}VK_EXT_fragment_density_map.adoc[]

=== Other Extension Metadata

*Last Modified Date*::
    2021-09-30
*Interactions and External Dependencies*::
  - This extension provides API support for
    {GLSLregistry}/ext/GLSL_EXT_fragment_invocation_density.txt[`GL_EXT_fragment_invocation_density`]
*Contributors*::
  - Matthew Netsch, Qualcomm Technologies, Inc.
  - Robert VanReenen, Qualcomm Technologies, Inc.
  - Jonathan Wicks, Qualcomm Technologies, Inc.
  - Tate Hornbeck, Qualcomm Technologies, Inc.
  - Sam Holmes, Qualcomm Technologies, Inc.
  - Jeff Leger, Qualcomm Technologies, Inc.
  - Jan-Harald Fredriksen, ARM
  - Jeff Bolz, NVIDIA
  - Pat Brown, NVIDIA
  - Daniel Rakos, AMD
  - Piers Daniell, NVIDIA

=== Description

This extension allows an application to specify areas of the render target
where the fragment shader may be invoked fewer times.
These fragments are broadcasted out to multiple pixels to cover the render
target.

The primary use of this extension is to reduce workloads in areas where
lower quality may not be perceived such as the distorted edges of a lens or
the periphery of a user's gaze.

include::{generated}/interfaces/VK_EXT_fragment_density_map.adoc[]

=== New or Modified Built-In Variables

  * <<interfaces-builtin-variables-fraginvocationcount,code:FragInvocationCountEXT>>
  * <<interfaces-builtin-variables-fragsize,code:FragSizeEXT>>

=== New SPIR-V Capabilities

  * <<spirvenv-capabilities-table-FragmentDensityEXT,
    code:FragmentDensityEXT>>

ifdef::isrefpage[]
=== Examples

==== Fragment Density Map

An image can be bound as a fragment density map attachment to a render pass.
This image contains normalized (x, y) float component fragment density
values for regions of the framebuffer that will be used in rasterization for
every subpass.
A float component ranges from (0.0, 1.0] where 1.0 means full density along
that axis.
Implementations <<fragmentdensitymapops,use these values as hints>> to
optimize rendering in areas of low density.
Subpass color and depth attachments can be created as subsampled, which can
help to further optimize rendering in areas of low density.

The density map image can be modified by the application until calling
fname:vkCmdBeginRenderPass for the render pass that uses the image.
If ename:VK_IMAGE_VIEW_CREATE_FRAGMENT_DENSITY_MAP_DYNAMIC_BIT_EXT is used,
then the application can modify the image until the device reads it during
ename:VK_PIPELINE_STAGE_FRAGMENT_DENSITY_PROCESS_BIT_EXT.

[source,c++]
----
// Create fragment density map
VkImageCreateInfo imageCreateInfo =
{
   .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
   .pNext = nullptr,
   .flags = 0,
   .imageType = VK_IMAGE_TYPE_2D,    // Must be 2D
   .format = VK_FORMAT_R8G8_UNORM,   // Must have VK_FORMAT_FEATURE_FRAGMENT_DENSITY_MAP_BIT_EXT
   .extend = {64, 64, 1},
   .mipLevels = 1,
   .arrayLayers = 2,                 // 1 for each multiview view
   .samples = VK_SAMPLE_COUNT_1_BIT, // Must be 1x MSAA
   .tiling = tiling,
   .usage = VK_IMAGE_USAGE_FRAGMENT_DENSITY_MAP_BIT_EXT,
   // ...
};

vkCreateImage(device, &imageCreateInfo, nullptr, &fdmImage);

VkImageViewCreateInfo viewCreateInfo =
{
   .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
   .pNext = nullptr,
   .flags = 0,                      // VkImageViewCreateFlags
   .image = fdmImage,
   .viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
   .format = VK_FORMAT_R8G8_UNORM,
   .components = { 0 },             // VK_COMPONENT_SWIZZLE_IDENTITY
   .subresourceRange = {
        .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
        .baseMipLevel = 0,
        .levelCount = 1,
        .baseArrayLayer = 0,
        .layerCount = 2,
   }
};

vkCreateImageView(device, &viewCreateInfo, nullptr, &fdmImageView);

// Add fdmImage to render pass

VkAttachmentReference fragmentDensityMapAttachmentReference =
{
   fdmAttachmentIdx,
   VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT,
};

VkRenderPassFragmentDensityMapCreateInfoEXT fdmAttachmentCreateInfo =
{
   VK_STRUCTURE_TYPE_RENDER_PASS_FRAGMENT_DENSITY_MAP_CREATE_INFO_EXT,
   // ...
   fragmentDensityMapAttachmentReference,
};

VkRenderPassCreateInfo2 renderPassCreateInfo =
{
   VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2,
   &fdmAttachmentCreateInfo,
   // ...
};

vkCreateRenderPass2(device, &renderPassCreateInfo, nullptr, &renderPass);

// Add fdmImage to framebuffer
// Color attachments can be created with VK_IMAGE_CREATE_SUBSAMPLED_BIT_EXT
// All attachments must be created with VK_IMAGE_CREATE_FRAGMENT_DENSITY_MAP_OFFSETS_BIT_EXT
VkFramebufferCreateInfo framebufferCreateInfo =
{
   .sType = VK_STRUCTURE_TYPE_FRAME_BUFFER_CREATE_INFO,
   // ...
   .renderPass = renderPass,
   // ...
   .pAttachments = pAttachments, // Includes fdmImageView at fdmAttachmentIdx
   .width = 1024,
   .height = 1024,
   .layers = 1
};

vkCreateFramebuffer(device, &framebufferCreateInfo, nullptr, &framebuffer);

// Start recording render pass in command buffer

VkRenderPassBeginInfo renderPassBeginInfo =
{
   .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
   // ...
   .renderPass = renderPass,
   .framebuffer = framebuffer,
   // ...
};

// Can no longer modify the fdmImage's contents after this call
vkCmdBeginRenderPass2(commandBuffer, &renderPassBeginInfo, pSubpassBeginInfo);
----
endif::isrefpage[]

=== Version History

  * Revision 1, 2018-09-25 (Matthew Netsch)
  ** Initial version
  * Revision 2, 2021-09-30 (Jon Leech)
  ** Add interaction with `apiext:VK_KHR_format_feature_flags2` to `vk.xml`