// Copyright 2018-2024 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`

