learn-wgpu/beginner/tutorial8-depth/index.html
2021-03-11 18:11:00 +00:00

117 lines
33 KiB
HTML

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>The Depth Buffer | Learn Wgpu</title>
<meta name="description" content="">
<meta name="generator" content="VuePress 1.4.0">
<meta property="article:modified_time" content="2021-02-19T04:30:18.000Z"><meta property="og:site_name" content="Learn Wgpu"><meta property="og:title" content="The Depth Buffer"><meta property="og:type" content="website"><meta property="og:url" content="/beginner/tutorial8-depth/"><meta name="twitter:title" content="The Depth Buffer"><meta name="twitter:url" content="/beginner/tutorial8-depth/"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:label1" content="Written by"><meta name="twitter:data2" content="Benjamin R Hansen"><meta name="twitter:creator" content="https://twitter.com/sotrh760">
<link rel="preload" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css" as="style"><link rel="preload" href="/learn-wgpu/assets/js/app.525a5f73.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/2.da3fd46f.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/13.78eedf26.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/22.2132e362.js" as="script"><link rel="prefetch" href="/learn-wgpu/assets/js/10.de8cadfb.js"><link rel="prefetch" href="/learn-wgpu/assets/js/11.65c8dcc2.js"><link rel="prefetch" href="/learn-wgpu/assets/js/12.26aabcb1.js"><link rel="prefetch" href="/learn-wgpu/assets/js/14.f852c057.js"><link rel="prefetch" href="/learn-wgpu/assets/js/15.0fda50ec.js"><link rel="prefetch" href="/learn-wgpu/assets/js/16.f1c9d31a.js"><link rel="prefetch" href="/learn-wgpu/assets/js/17.f03fd0c9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/18.d3691f26.js"><link rel="prefetch" href="/learn-wgpu/assets/js/19.e9350ae9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/20.70e640cc.js"><link rel="prefetch" href="/learn-wgpu/assets/js/21.19d2890c.js"><link rel="prefetch" href="/learn-wgpu/assets/js/23.1a88f225.js"><link rel="prefetch" href="/learn-wgpu/assets/js/24.c0cbce8a.js"><link rel="prefetch" href="/learn-wgpu/assets/js/25.2c8435d6.js"><link rel="prefetch" href="/learn-wgpu/assets/js/26.5b0dad74.js"><link rel="prefetch" href="/learn-wgpu/assets/js/27.04f788d9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/28.a3ac06e2.js"><link rel="prefetch" href="/learn-wgpu/assets/js/29.281013fa.js"><link rel="prefetch" href="/learn-wgpu/assets/js/3.2a393c66.js"><link rel="prefetch" href="/learn-wgpu/assets/js/30.dc887be9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/4.6b1a77da.js"><link rel="prefetch" href="/learn-wgpu/assets/js/5.6c35427e.js"><link rel="prefetch" href="/learn-wgpu/assets/js/6.7f771450.js"><link rel="prefetch" href="/learn-wgpu/assets/js/7.88810c81.js"><link rel="prefetch" href="/learn-wgpu/assets/js/8.d5139107.js"><link rel="prefetch" href="/learn-wgpu/assets/js/9.ecb6e79c.js">
<link rel="stylesheet" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="inner"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/learn-wgpu/" class="home-link router-link-active"><!----> <span class="site-name">Learn Wgpu</span></a> <div class="links"><!----> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <div class="docs-layout"><aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/learn-wgpu/" class="sidebar-link">Introduction</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Beginner</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/beginner/tutorial1-window/" class="sidebar-link">Dependencies and the window</a></li><li><a href="/learn-wgpu/beginner/tutorial2-swapchain/" class="sidebar-link">The Swapchain</a></li><li><a href="/learn-wgpu/beginner/tutorial3-pipeline/" class="sidebar-link">The Pipeline</a></li><li><a href="/learn-wgpu/beginner/tutorial4-buffer/" class="sidebar-link">Buffers and Indices</a></li><li><a href="/learn-wgpu/beginner/tutorial5-textures/" class="sidebar-link">Textures and bind groups</a></li><li><a href="/learn-wgpu/beginner/tutorial6-uniforms/" class="sidebar-link">Uniform buffers and a 3d camera</a></li><li><a href="/learn-wgpu/beginner/tutorial7-instancing/" class="sidebar-link">Instancing</a></li><li><a href="/learn-wgpu/beginner/tutorial8-depth/" class="active sidebar-link">The Depth Buffer</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/learn-wgpu/beginner/tutorial8-depth/#sorting-from-back-to-front" class="sidebar-link">Sorting from back to front</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/beginner/tutorial8-depth/#a-pixels-depth" class="sidebar-link">A pixels depth</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/beginner/tutorial8-depth/#challenge" class="sidebar-link">Challenge</a></li></ul></li><li><a href="/learn-wgpu/beginner/tutorial9-models/" class="sidebar-link">Model Loading</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Intermediate</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/intermediate/tutorial10-lighting/" class="sidebar-link">Working with Lights</a></li><li><a href="/learn-wgpu/intermediate/tutorial11-normals/" class="sidebar-link">Normal Mapping</a></li><li><a href="/learn-wgpu/intermediate/tutorial12-camera/" class="sidebar-link">A Better Camera</a></li><li><a href="/learn-wgpu/intermediate/tutorial13-threading/" class="sidebar-link">Multi-threading with Wgpu and Rayon</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Showcase</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/learn-wgpu/news/" class="sidebar-link">News</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="the-depth-buffer"><a href="#the-depth-buffer" class="header-anchor">#</a> The Depth Buffer</h1> <p>Let's take a closer look at the last example.</p> <p><img src="/learn-wgpu/assets/img/forest_with_zoom.a43286ea.png" alt="forest_with_zoom.png"></p> <p>Models that should be in the back are getting rendered ahead of ones that should be in the front. This is caused by the draw order. By default, pixel data from a new object will replace old pixel data.</p> <p>There are two ways to solve this: sort the data from back to front, use what's known as a depth buffer.</p> <h2 id="sorting-from-back-to-front"><a href="#sorting-from-back-to-front" class="header-anchor">#</a> Sorting from back to front</h2> <p>This is the go to method for 2d rendering as it's pretty easier to know what's supposed to go in front of what. You can just use the z order. In 3d rendering it gets a little more tricky because the order of the objects changes based on the camera angle.</p> <p>A simple way of doing this is to sort all the objects by their distance to the cameras position. There are flaws with this method though as when a large object is behind a small object, parts of the large object that should be in front of the small object will be rendered behind. We'll also run into issues with objects that overlap <em>themselves</em>.</p> <p>If want to do this properly we need to have pixel level precision. That's where a <em>depth buffer</em> comes in.</p> <h2 id="a-pixels-depth"><a href="#a-pixels-depth" class="header-anchor">#</a> A pixels depth</h2> <p>A depth buffer is a black and white texture that stores the z-coordinate of rendered pixels. Wgpu can use this when drawing new pixels to determine whether to replace the data or keep it. This technique is called depth testing. This will fix our draw order problem without needing us to sort our objects!</p> <p>Let's make a function to create the depth texture in <code>texture.rs</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">impl</span> <span class="token class-name">Texture</span> <span class="token punctuation">{</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">DEPTH_FORMAT</span><span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureFormat</span> <span class="token operator">=</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureFormat</span><span class="token punctuation">::</span><span class="token class-name">Depth32Float</span><span class="token punctuation">;</span> <span class="token comment">// 1.</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">create_depth_texture</span><span class="token punctuation">(</span>device<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">Device</span><span class="token punctuation">,</span> sc_desc<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">SwapChainDescriptor</span><span class="token punctuation">,</span> label<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">str</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
<span class="token keyword">let</span> size <span class="token operator">=</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">Extent3d</span> <span class="token punctuation">{</span> <span class="token comment">// 2.</span>
width<span class="token punctuation">:</span> sc_desc<span class="token punctuation">.</span>width<span class="token punctuation">,</span>
height<span class="token punctuation">:</span> sc_desc<span class="token punctuation">.</span>height<span class="token punctuation">,</span>
depth<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> desc <span class="token operator">=</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureDescriptor</span> <span class="token punctuation">{</span>
label<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span>label<span class="token punctuation">)</span><span class="token punctuation">,</span>
size<span class="token punctuation">,</span>
mip_level_count<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
sample_count<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
dimension<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureDimension</span><span class="token punctuation">::</span><span class="token constant">D2</span><span class="token punctuation">,</span>
format<span class="token punctuation">:</span> <span class="token keyword">Self</span><span class="token punctuation">::</span><span class="token constant">DEPTH_FORMAT</span><span class="token punctuation">,</span>
usage<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureUsage</span><span class="token punctuation">::</span><span class="token constant">RENDER_ATTACHMENT</span> <span class="token comment">// 3.</span>
<span class="token operator">|</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureUsage</span><span class="token punctuation">::</span><span class="token constant">SAMPLED</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> texture <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_texture</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>desc<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> view <span class="token operator">=</span> texture<span class="token punctuation">.</span><span class="token function">create_view</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureViewDescriptor</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> sampler <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_sampler</span><span class="token punctuation">(</span>
<span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">SamplerDescriptor</span> <span class="token punctuation">{</span> <span class="token comment">// 4.</span>
address_mode_u<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">AddressMode</span><span class="token punctuation">::</span><span class="token class-name">ClampToEdge</span><span class="token punctuation">,</span>
address_mode_v<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">AddressMode</span><span class="token punctuation">::</span><span class="token class-name">ClampToEdge</span><span class="token punctuation">,</span>
address_mode_w<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">AddressMode</span><span class="token punctuation">::</span><span class="token class-name">ClampToEdge</span><span class="token punctuation">,</span>
mag_filter<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">FilterMode</span><span class="token punctuation">::</span><span class="token class-name">Linear</span><span class="token punctuation">,</span>
min_filter<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">FilterMode</span><span class="token punctuation">::</span><span class="token class-name">Linear</span><span class="token punctuation">,</span>
mipmap_filter<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">FilterMode</span><span class="token punctuation">::</span><span class="token class-name">Nearest</span><span class="token punctuation">,</span>
compare<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">CompareFunction</span><span class="token punctuation">::</span><span class="token class-name">LessEqual</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 5.</span>
lod_min_clamp<span class="token punctuation">:</span> <span class="token operator">-</span><span class="token number">100.0</span><span class="token punctuation">,</span>
lod_max_clamp<span class="token punctuation">:</span> <span class="token number">100.0</span><span class="token punctuation">,</span>
<span class="token punctuation">..</span><span class="token class-name">Default</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">Self</span> <span class="token punctuation">{</span> texture<span class="token punctuation">,</span> view<span class="token punctuation">,</span> sampler <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ol><li>We need the DEPTH_FORMAT for when we create the depth stage of the <code>render_pipeline</code> and creating the depth texture itself.</li> <li>Our depth texture needs to be the same size as our screen if we want things to render correctly. We can use our <code>sc_desc</code> to make sure that our depth texture is the same size as our swap chain images.</li> <li>Since we are rendering to this texture, we need to add the <code>OUTPUT_ATTACHMENT</code> flag to it.</li> <li>We technically don't <em>need</em> a sampler for a depth texture, but our <code>Texture</code> struct requires it, and we need one if we ever want to sample it.</li> <li>If we do decide to render our depth texture, we need to use <code>CompareFunction::LessEqual</code>. This is due to how the <code>samplerShadow</code> and <code>sampler2DShadow()</code> interacts with the <code>texture()</code> function in GLSL.</li></ol> <p>We create our <code>depth_texture</code> in <code>State::new()</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> depth_texture <span class="token operator">=</span> <span class="token namespace">texture<span class="token punctuation">::</span></span><span class="token class-name">Texture</span><span class="token punctuation">::</span><span class="token function">create_depth_texture</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>device<span class="token punctuation">,</span> <span class="token operator">&amp;</span>sc_desc<span class="token punctuation">,</span> <span class="token string">&quot;depth_texture&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>We need to modify our <code>render_pipeline</code> to allow depth testing.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> render_pipeline <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_render_pipeline</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPipelineDescriptor</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
depth_stencil<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">DepthStencilState</span> <span class="token punctuation">{</span>
format<span class="token punctuation">:</span> <span class="token namespace">texture<span class="token punctuation">::</span></span><span class="token class-name">Texture</span><span class="token punctuation">::</span><span class="token constant">DEPTH_FORMAT</span><span class="token punctuation">,</span>
depth_write_enabled<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
depth_compare<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">CompareFunction</span><span class="token punctuation">::</span><span class="token class-name">Less</span><span class="token punctuation">,</span> <span class="token comment">// 1.</span>
stencil<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">StencilState</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 2.</span>
bias<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">DepthBiasState</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token comment">// Setting this to true requires Features::DEPTH_CLAMPING</span>
clamp_depth<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol><li>The <code>depth_compare</code> function tells us when to discard a new pixel. Using <code>LESS</code> means pixels will be drawn front to back. Here are all the values you can use.</li></ol> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token attribute attr-name">#[repr(C)]</span>
<span class="token attribute attr-name">#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]</span>
<span class="token attribute attr-name">#[cfg_attr(feature = <span class="token string">&quot;serde&quot;</span>, derive(Serialize, Deserialize))]</span>
<span class="token keyword">pub</span> <span class="token keyword">enum</span> <span class="token type-definition class-name">CompareFunction</span> <span class="token punctuation">{</span>
<span class="token class-name">Undefined</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token class-name">Never</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token class-name">Less</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span>
<span class="token class-name">Equal</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span>
<span class="token class-name">LessEqual</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span>
<span class="token class-name">Greater</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">,</span>
<span class="token class-name">NotEqual</span> <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">,</span>
<span class="token class-name">GreaterEqual</span> <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">,</span>
<span class="token class-name">Always</span> <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li>There's another type of buffer called a stencil buffer. It's common practice to store the stencil buffer and depth buffer in the same texture. This fields control values for stencil testing. Since we aren't using a stencil buffer, we'll use default values. We'll cover stencil buffers <a href="../../todo">later</a>.</li></ol> <p>Don't forget to store the <code>depth_texture</code> in <code>State</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">Self</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
depth_texture<span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p>We need to remember to change the <code>resize()</code> method to create a new <code>depth_texture</code> and <code>depth_texture_view</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">resize</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> new_size<span class="token punctuation">:</span> <span class="token namespace">winit<span class="token punctuation">::</span>dpi<span class="token punctuation">::</span></span><span class="token class-name">PhysicalSize</span><span class="token operator">&lt;</span><span class="token keyword">u32</span><span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
<span class="token keyword">self</span><span class="token punctuation">.</span>depth_texture <span class="token operator">=</span> <span class="token namespace">texture<span class="token punctuation">::</span></span><span class="token class-name">Texture</span><span class="token punctuation">::</span><span class="token function">create_depth_texture</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">.</span>device<span class="token punctuation">,</span> <span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">.</span>sc_desc<span class="token punctuation">,</span> <span class="token string">&quot;depth_texture&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Make sure you update the <code>depth_texture</code> <em>after</em> you update <code>sc_desc</code>. If you don't, your program will crash as the <code>depth_texture</code> will be a different size than the <code>swap_chain</code> texture.</p> <p>The last change we need to make is in the <code>render()</code> function. We've created the <code>depth_texture</code>, but we're not currently using it. We use it by attaching it to the <code>depth_stencil_attachment</code> of a render pass.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> <span class="token keyword">mut</span> render_pass <span class="token operator">=</span> encoder<span class="token punctuation">.</span><span class="token function">begin_render_pass</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPassDescriptor</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
depth_stencil_attachment<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPassDepthStencilAttachmentDescriptor</span> <span class="token punctuation">{</span>
attachment<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">.</span>depth_texture<span class="token punctuation">.</span>view<span class="token punctuation">,</span>
depth_ops<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">Operations</span> <span class="token punctuation">{</span>
load<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">LoadOp</span><span class="token punctuation">::</span><span class="token class-name">Clear</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
store<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
stencil_ops<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>And that's all we have to do! No shader code needed! If you run the application, the depth issues will be fixed.</p> <p><img src="/learn-wgpu/assets/img/forest_fixed.a77f70f6.png" alt="forest_fixed.png"></p> <h2 id="challenge"><a href="#challenge" class="header-anchor">#</a> Challenge</h2> <p>Since the depth buffer is a texture, we can sample it in the shader. Because it's a depth texture, we'll have to use the <code>samplerShadow</code> uniform type and the <code>sampler2DShadow</code> function instead of <code>sampler</code>, and <code>sampler2D</code> respectively. Create a bind group for the depth texture (or reuse an existing one), and render it to the screen.</p> <div class="auto-github-link"><a href="https://github.com/sotrh/learn-wgpu/tree/master/code/beginner/tutorial8-depth/" target="_blank" rel="noopener noreferrer">Check out the code!</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2/19/2021, 4:30:18 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
<a href="/learn-wgpu/beginner/tutorial7-instancing/" class="prev">
Instancing
</a></span> <span class="next"><a href="/learn-wgpu/beginner/tutorial9-models/">
Model Loading
</a>
</span></p></div> </main></div></div><div class="global-ui"><!----></div></div>
<script src="/learn-wgpu/assets/js/app.525a5f73.js" defer></script><script src="/learn-wgpu/assets/js/2.da3fd46f.js" defer></script><script src="/learn-wgpu/assets/js/13.78eedf26.js" defer></script><script src="/learn-wgpu/assets/js/22.2132e362.js" defer></script>
</body>
</html>