Aqoole_Hateenaの技術日記

vulkan+raytraceで色々描いてます

Depth Imageの初期値設定時の注意点

初期値(clear value)より奥のfragmentは描画されないので注意が必要

Depth Value

depthの値はVkPipelineDepthStencilStateCreateInfoのminDepthBoundsからmaxDepthBoundsの値に制限される。
Vulkan® 1.3.205 - A Specification (with all registered Vulkan extensions)

(参考)Depth Valueの計算式(OpenGL)

LearnOpenGL - Depth testing

F_{depth}=\frac{z - near}{far - near}
の式によって、z座標の値を[near, far]の範囲内に制限する。

Clear Value

VkRenderPassBeginInfoにVkClearValueで設定する。
VkClearDepthStencilValue(3)

depth is the clear value for the depth aspect of the depth/stencil attachment. It is a floating-point value which is automatically converted to the attachment’s format.

初期値の意味

OpenGLの数式を例にして、zにnearとfarを入れるとわかる通り、

0.0f <-> 1.0f
手前 <->

の関係になっている。例えばdepthを0.0fで初期化してしまうと、すべてのfragmentが手前で初期化されてしまうので、どの図形も見えなくなる。
必ず初期値は奥になるようにしなければ、何も見えない。


以下は縦軸と横軸のdepthが異なるひし形を2枚描いた図である。
上図が図形のすべてが描画されている状態。
下図は0.5fで初期化した場合で、depthの値が0.5fより大きいfragmentは描かれないことがわかる。

f:id:Aqoole_Hateena:20220206203710p:plain
四角形2枚(clear value = 1.01f)
f:id:Aqoole_Hateena:20220206203800p:plain
四角形2枚(clear value = 0.5f)