--- id: wiki-2026-0508-depth-pre-pass title: Depth Pre-Pass category: 10_Wiki/Topics status: verified canonical_id: self aliases: [Z-prepass, depth prepass, early-Z, Z-only pass] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [graphics, rendering, gpu, optimization, depth-buffer, overdraw] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: HLSL / GLSL / Shader framework: Unreal / Unity / Custom --- # Depth Pre-Pass ## 매 한 줄 > **"매 main shading 전 의 depth-only render"**. 매 overdraw 의 reduce — 매 expensive pixel shader 의 hidden surface 의 skip. 매 modern GPU 의 early-Z + Hi-Z + 매 deferred / forward+. 매 cost: 매 vertex 2x. ## 매 핵심 ### 매 motivation - **Overdraw**: 매 same pixel 의 shade 여러 번. - **Expensive shader**: 매 PBR + IBL + many light → 매 pixel cost ↑. - **Solution**: 매 depth 만 의 first → 매 main pass 의 occluded fragment 의 reject. ### 매 mechanism 1. **Pass 1**: 매 depth-only (vertex + null pixel shader). 2. **Pass 2**: 매 full shading + EQUAL depth test. 3. 매 GPU 의 early-Z 의 pixel shader 전 의 reject. ### 매 trade-off - **Win**: 매 expensive pixel shader 의 occluded fragment 의 skip. - **Loss**: 매 vertex stage 2x. - **Net**: 매 shader complexity 의 high 의 win. ### 매 modern variant - **Hi-Z**: 매 hierarchical depth 의 GPU 의 cull tile. - **Forward+**: 매 light culling + Z-prepass. - **Deferred**: 매 G-buffer 의 prepass-like. - **Visibility buffer**: 매 modern alternative (Unreal 5 Nanite). ### 매 응용 1. **Open world**: 매 dense vegetation overdraw. 2. **Particle**: 매 alpha sort cost. 3. **PBR-heavy**: 매 expensive shader. 4. **VR**: 매 fill rate critical. 5. **Mobile**: 매 tile-based 의 different approach (defer to TBDR). ## 💻 패턴 ### Z-prepass (Unreal-style HLSL) ```hlsl // Pass 1 — depth only struct VSInput { float3 pos : POSITION; }; float4 VS_DepthOnly(VSInput v) : SV_POSITION { return mul(float4(v.pos, 1.0), MVP); } // 매 no pixel shader (or null) // Pass 2 — full shading with EQUAL test DepthStencilState : DepthFunc = EQUAL; DepthWrite = OFF; float4 PS_Main(VSOutput v) : SV_TARGET { return PBR_Shade(v); // 매 expensive } ``` ### OpenGL setup ```cpp // Pass 1 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); DrawScene(depth_only_program); // Pass 2 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_FALSE); glDepthFunc(GL_EQUAL); DrawScene(main_program); ``` ### Vulkan render pass ```cpp VkAttachmentDescription depthAttach = { .format = VK_FORMAT_D32_SFLOAT, .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, .storeOp = VK_ATTACHMENT_STORE_OP_STORE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, }; // Subpass 1: depth only // Subpass 2: shading w/ EQUAL test, read-only depth ``` ### Cost-benefit decision ```cpp bool should_use_depth_prepass(SceneStats stats) { // 매 high overdraw + expensive shader → 매 win float avg_overdraw = stats.pixels_shaded / stats.unique_pixels; float shader_cost = stats.shader_alu_count; return avg_overdraw > 2.0 && shader_cost > 100; } ``` ### Hi-Z chain (compute) ```hlsl // 매 each level 의 max of 4 source pixels [numthreads(8, 8, 1)] void GenerateHiZ(uint3 tid : SV_DispatchThreadID) { float4 d; d.x = SrcDepth[tid.xy * 2 + uint2(0, 0)]; d.y = SrcDepth[tid.xy * 2 + uint2(1, 0)]; d.z = SrcDepth[tid.xy * 2 + uint2(0, 1)]; d.w = SrcDepth[tid.xy * 2 + uint2(1, 1)]; DstDepth[tid.xy] = max(max(d.x, d.y), max(d.z, d.w)); } ``` ### Occlusion query (validation) ```cpp GLuint q; glGenQueries(1, &q); glBeginQuery(GL_SAMPLES_PASSED, q); DrawObject(obj); glEndQuery(GL_SAMPLES_PASSED); GLuint samples; glGetQueryObjectuiv(q, GL_QUERY_RESULT, &samples); // 매 samples == 0 → fully occluded ``` ### Early-Z disqualification ```hlsl // 매 pixel shader 의 discard / depth-write → 매 early-Z 의 disabled float4 PS_Bad(VSOut v) : SV_TARGET { if (alpha < 0.5) discard; // ❌ early-Z 의 break return shade(v); } // 매 ✅ early-Z friendly: 매 alpha test 의 discard 의 separate pass [earlydepthstencil] float4 PS_Good(VSOut v) : SV_TARGET { return shade(v); } ``` ### TBDR mobile (no prepass needed) ``` // 매 mobile (Adreno, Mali, Apple) 의 tile-based deferred // 매 already 의 hidden surface 의 reject before pixel shader // 매 explicit Z-prepass 의 redundant ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | Desktop + heavy PBR | Z-prepass | | Mobile (TBDR) | NO (HW already does it) | | Forward+ | Z-prepass + light cull | | Deferred | G-buffer pass = prepass | | Particle / transparent | After opaque (no prepass) | | Modern Unreal 5 | Visibility buffer / Nanite | **기본값**: 매 desktop + 매 heavy shader → Z-prepass. 매 mobile → skip. 매 modern engine → visibility buffer. ## 🔗 Graph - 부모: [[Rendering-Pipeline]] - 변형: [[Depth Pre-Pass|Early-Z]] · [[Hi-Z]] - 응용: [[Deferred-Rendering]] - Adjacent: [[Overdraw]] · [[Compute-Shader]] ## 🤖 LLM 활용 **언제**: 매 PBR-heavy desktop. 매 dense scene. 매 fill-rate-bound. **언제 X**: 매 mobile TBDR. 매 vertex-bound. 매 simple shader. ## ❌ 안티패턴 - **Always prepass**: 매 vertex-bound 의 lose. - **Discard in main**: 매 early-Z 의 break. - **Mobile prepass**: 매 redundant + cost. - **No EQUAL test in main**: 매 prepass 의 useless. - **Animated geometry mismatch**: 매 prepass 와 main 의 different vertex. ## 🧪 검증 / 중복 - Verified (Real-Time Rendering 4ed, Unreal docs, Vulkan spec). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-04-20 | Auto-reinforced | | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — Z-prepass + 매 HLSL / GL / Vulkan / Hi-Z code |