Aqoole_Hateenaの技術日記

vulkan+raytraceで色々描いてます

Rasterization Stageの指定

Rasterization Stageでの処理

rasterization stageではrasterizationとmultisamplingの処理が行われる。
それぞれVkPipelineRasterizationStateCreateInfoとVkPipelineMultisampleStateCreateInfoにパラメータを記述する。

VkPipelineRasterizationStateCreateInfo

// Provided by VK_VERSION_1_0
typedef struct VkPipelineRasterizationStateCreateInfo {
    VkStructureType                            sType;
    const void*                                pNext;
    VkPipelineRasterizationStateCreateFlags    flags;
    VkBool32                                   depthClampEnable;
    VkBool32                                   rasterizerDiscardEnable;
    VkPolygonMode                              polygonMode;
    VkCullModeFlags                            cullMode;
    VkFrontFace                                frontFace;
    VkBool32                                   depthBiasEnable;
    float                                      depthBiasConstantFactor;
    float                                      depthBiasClamp;
    float                                      depthBiasSlopeFactor;
    float                                      lineWidth;
} VkPipelineRasterizationStateCreateInfo;

  • depthClampEnable controls whether to clamp the fragment’s depth values as described in Depth Test. If the pipeline is not created with VkPipelineRasterizationDepthClipStateCreateInfoEXT present then enabling depth clamp will also disable clipping primitives to the z planes of the frustrum as described in Primitive Clipping. Otherwise depth clipping is controlled by the state set in VkPipelineRasterizationDepthClipStateCreateInfoEXT.
  • rasterizerDiscardEnable controls whether primitives are discarded immediately before the rasterization stage.
  • polygonMode is the triangle rendering mode. See VkPolygonMode.
  • cullMode is the triangle facing direction used for primitive culling. See VkCullModeFlagBits.
  • frontFace is a VkFrontFace value specifying the front-facing triangle orientation to be used for culling.
  • depthBiasEnable controls whether to bias fragment depth values.

VkPipelineRasterizationStateCreateInfo(3)

depthClampEnable

depth testとは、あるdepth valueがpositionに対応するdepth bufferの値と比較し、よりfrontであれば表示し、behindであれば表示しない、というもの。depth test自体はrenderingでは一般的な用語なので、OpenGLでの説明を引用。

When depth testing is enabled, OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the fragment is rendered and the depth buffer is updated with the new depth value. If the depth test fails, the fragment is discarded.
LearnOpenGL - Depth testing

depthClampEnableはdepthの値をdepth testの値にclampするかどうかを決める。

cullMode

破棄する三角形の向きを決める。
cullは「淘汰、間引き」の意味。
cullingの意味・使い方・読み方 | Weblio英和辞書

VkCullModeFlagBits

VkCullModeFlagBits - Bitmask controlling triangle culling

// Provided by VK_VERSION_1_0
typedef enum VkCullModeFlagBits {
    VK_CULL_MODE_NONE = 0,
    VK_CULL_MODE_FRONT_BIT = 0x00000001,
    VK_CULL_MODE_BACK_BIT = 0x00000002,
    VK_CULL_MODE_FRONT_AND_BACK = 0x00000003,
} VkCullModeFlagBits;

  • VK_CULL_MODE_NONE specifies that no triangles are discarded
  • VK_CULL_MODE_FRONT_BIT specifies that front-facing triangles are discarded
  • VK_CULL_MODE_BACK_BIT specifies that back-facing triangles are discarded
  • VK_CULL_MODE_FRONT_AND_BACK specifies that all triangles are discarded.

Following culling, fragments are produced for any triangles which have not been discarded.
VkCullModeFlagBits(3)

frontFace

どの回り方(反時計回りor時計回り)が表面かを指定する。

// Provided by VK_VERSION_1_0
typedef enum VkFrontFace {
    VK_FRONT_FACE_COUNTER_CLOCKWISE = 0,
    VK_FRONT_FACE_CLOCKWISE = 1,
} VkFrontFace;

VkFrontFace(3)

depthBiasEnable

depth biasとは

The depth values of all fragments generated by the rasterization of a polygon can be biased (offset) by a single depth bias value o that is computed for that polygon.
Vulkan® 1.2.203 - A Specification (with all registered Vulkan extensions)

すべてのdepth valueはひとつのdepth bias valueでbias可能とある。
biasは「偏り」の意味。
biasの意味・使い方・読み方 | Weblio英和辞書
これだけだと意味が分からないのでもう少し調べてみると、Nvidiaのdeveloper zoneにはこのようにある。

Specify the constant offset used to compute a depth offset for polygons. See the units parameter description on the OpenGL glPolygonOffset manual page for details. See the D3DRS_DEPTHBIAS render state description for DirectX 9.
DepthBias

depth offsetを計算するための定数のoffsetとある。
詳細を知りたければOpenGL glPolygonOffset manual pageを見ろとあるので、見てみた。

glPolygonOffset
void glPolygonOffset(GLfloat factor,
 	GLfloat units);

glPolygonOffset is useful for rendering hidden-line images, for applying decals to surfaces, and for rendering solids with highlighted edges.
Description
When GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT is enabled, each fragment's depth value will be offset after it is interpolated from the depth values of the appropriate vertices. The value of the offset is factor × DZ + r × units , where DZ is a measurement of the change in depth relative to the screen area of the polygon, and r is the smallest value that is guaranteed to produce a resolvable offset for a given implementation. The offset is added before the depth test is performed and before the value is written into the depth buffer.
glPolygonOffset - OpenGL 4 Reference Pages

「depth bias(glPolygonOffset)は隠れたラインやimagesをsurface上に転写するときに使える」とある。壁の奥にオブジェクトがある場合に、そのedge部分だけを壁の表面に描きたい場合などに、depthにoffsetを与えて補正することにより描けるよう。

VkPipelineMultisampleStateCreateInfo

// Provided by VK_VERSION_1_0
typedef struct VkPipelineMultisampleStateCreateInfo {
    VkStructureType                          sType;
    const void*                              pNext;
    VkPipelineMultisampleStateCreateFlags    flags;
    VkSampleCountFlagBits                    rasterizationSamples;
    VkBool32                                 sampleShadingEnable;
    float                                    minSampleShading;
    const VkSampleMask*                      pSampleMask;
    VkBool32                                 alphaToCoverageEnable;
    VkBool32                                 alphaToOneEnable;
} VkPipelineMultisampleStateCreateInfo;

  • rasterizationSamples is a VkSampleCountFlagBits value specifying the number of samples used in rasterization.
  • sampleShadingEnable can be used to enable Sample Shading.
  • minSampleShading specifies a minimum fraction of sample shading if sampleShadingEnable is set to VK_TRUE.
  • pSampleMask is a pointer to an array of VkSampleMask values used in the sample mask test.
  • alphaToCoverageEnable controls whether a temporary coverage value is generated based on the alpha component of the fragment’s first color output as specified in the Multisample Coverage section.
  • alphaToOneEnable controls whether the alpha component of the fragment’s first color output is replaced with one as described in Multisample Coverage.

rasterizationSamplesではsampleのカウントを指定する。
VkPhysicalDeviceLimitsのstandardSampleLocationsがVK_TRUEの場合、下tableのようなsample locationsになる。

f:id:Aqoole_Hateena:20220116123241p:plain
Table36 Standard sample locations

出典:Vulkan® 1.2.203 - A Specification (with all registered Vulkan extensions)

sampleShadingEnableはsample rate shadingの機能のon/offを設定する。
sample rate shadingとは、fragment shaderがpixelごとではなく、sampleごとに動作することである。
multisamplingにより図形のedgeはよりきれいに表現されるが、samplingした結果をfragment shaderに送るため、multisamplingはfragment shaderが高空間周波数の結果を出力した場合に対しては無力である。空間周波数とは、空間内の位置全体で周期的な構造の特性である。
Spatial frequency - Wikipedia
sampleShadingEnableをonにするとfragment shaderがsample rateで動くため、このような場合にも対応できる。

minSampleShadingはsampleShadingEnableが有効な場合のパラメータで、0.0f - 1.0fの値を入力する。
minSampleShadingはfragment shaderの動作頻度を管理する。
値が1.0の場合、pixel内のすべてのsampleにおいてfragment shaderが実行され、その結果を別のsampleにも送る。
値が0.0から1.0の間の場合、fragment shaderを実行するsample数の割合はdevice依存で計算される。

pSampleMaskは32bitのbit maskである。sample indexとbit位置が対応しており、bitが1であれば、fragment shaderが実行される。

If sample shading is enabled, fragment shaders will only see values of 1 for samples being shaded - other bits will be 0.
Vulkan® 1.2.203 - A Specification (with all registered Vulkan extensions)

alphaToCoverageEnableは対応する位置のfragment shaderの出力のalpha値に基づいて、一時的なcoverage maskを決めるかどうかを定める。
coverage maskとはmultisamplingにおいて、実際に有効となるfragment shaderをもつsampleのindexに対応したbit maskである。

If alphaToCoverageEnable is enabled, a temporary coverage mask is generated where each bit is determined by the fragment’s alpha value, which is ANDed with the fragment coverage mask.
Vulkan® 1.2.203 - A Specification (with all registered Vulkan extensions)

alphaToOneEnableはalpha値を1.0fなどの最大値に書き換えるかどうかを決める。

Finally, if alphaToOneEnable is enabled, each alpha value is replaced by the maximum representable alpha value for fixed-point color attachments, or by 1.0 for floating-point attachments. Otherwise, the alpha values are not changed.
Vulkan® 1.2.203 - A Specification (with all registered Vulkan extensions)

サポートされているsample count取得方法

aqoole-hateena.hatenablog.com

更新履歴

2022/1/17 サポートされているsample count取得方法を追記