# Can I turn off depthStencil for some object

**URL:** <https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065>\
**Category:** Metal by Tutorials\
**Created:** [August 9, 2020, 2:24pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065 "2020-08-09T14:24:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 9, 2020, 2:24pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/1 "2020-08-09T14:24:06Z")

</div>

Hello, I’m moving forward to the part that add depthStencil.  
for purpose of comparison, I put 3 object in the scene to render  
 ![image](https://files.chunter.kodeco.com/original/3X/d/e/de425613ea3899382df2b215d48acc99dfc6b357.png)

1. the triangle from ch04 when add the concept of matrix
2. the mushroom I create from blender and import using the new method introduced in ch05
3. I also create a little helper object that indicate the origin of the scene and the length of unit 1  
I capsulated each into a single class and render them one by one like below

```auto
        ch04Triangle.render(renderEncoder: renderEncoder)
        ch05MeshWithNormal.render(renderEncoder: renderEncoder, uniforms: &uniforms)
        center.render(renderEncoder: renderEncoder, uniforms: &uniforms)

```

the question is the when I add depthStencil , my ch04 triangle disappeared and the center no longer sit in front of the other object  
 ![image](https://files.chunter.kodeco.com/original/3X/3/1/31960e14260b58a5ccfaa4c0ed5a8fd3bf4eeeab.png)  
Is there any way that I can turn off depthStencil for some object(e.g. my Center object)

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 10, 2020, 12:42am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/2 "2020-08-10T00:42:07Z")

</div>

I’m going to use the example of the pipeline state instead of depth stencil state for the moment.

In chapter 5, you use a different pipeline state for each model. This will eventually change to a different pipeline state per submesh, depending on the type of surface material. Sometimes you need a different shader depending on the materials and you need a different pipeline state for each different shader.

This might not be the best way of doing it, but it’s difficult in a book to refactor everything all the time.

I would prefer a centralised shader controller in a real app and also ensure that my models conformed to a specific paradigm of textures etc, which I don’t enforce in this app. Later in the GPU chapter 15, we show you how to create a central heap of textures, which is more real life than holding the texture per submesh.

Going back to depth stencil states, you could create a singleton controller that holds all the depth stencil states you need for the app and pass a parameter to the model that holds the depth stencil state for that particular model. In Renderer, you’d make sure that the depth stencil state was loaded for the model, just as you currently load the pipeline state.

Or, if you have a special debug model, you could treat it as I have treated DebugLights in the Utility group, and render the model separately after all the other models have been rendered, with its own depth stencil state.

---

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 10, 2020, 1:33am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/3 "2020-08-10T01:33:44Z")

</div>

Hi, thank you for advice.  
yes, I have give each object their own shader and own pipelinestate. This works fine.  
but for depthStencilState it seems did not work. The depthStencilState seems so high level in the renderEncoder. Once set there is no way to cancel it . But Inspired by the web , after 1 day of hard work I finally succeed by creating another MTLRenderPassDescriptor and set its colorAttachments[0].loadAction to .load

```auto
        let pass2Descriptor = MTLRenderPassDescriptor()
        pass2Descriptor.colorAttachments[0].texture = drawable.texture
        pass2Descriptor.colorAttachments[0].loadAction = .load
        let noDepthEncoder = commandBuffer.makeRenderCommandEncoder( descriptor: pass2Descriptor)!
        center.render(renderEncoder: noDepthEncoder, uniforms: &uniforms)
        
        noDepthEncoder.endEncoding()

```

![image](https://files.chunter.kodeco.com/original/3X/d/c/dcaf7518f5dcca66310b81a450ca92fa6ef9fd9b.png)

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 10, 2020, 2:39am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/4 "2020-08-10T02:39:40Z")

</div>

I’m glad you got it working, but I’m surprised you needed a new render pass.

I tested it out with my debug sunlight line. These go through the tree and the train and disappear, but if I build a second depth stencil state:

```auto
 static func buildDepthStencilState2() -> MTLDepthStencilState? {
    let descriptor = MTLDepthStencilDescriptor()
    descriptor.depthCompareFunction = .always
    descriptor.isDepthWriteEnabled = false
    return Renderer.device.makeDepthStencilState(descriptor: descriptor)
  }

```

and in `debugLights(renderEncoder:lightType:)`, before calling `drawDirectionalLight(etc:)`, I added:

```auto
renderEncoder.setDepthStencilState(Self.buildDepthStencilState2())

```

![Screen Shot 2020-08-10 at 12.38.39 pm](https://files.chunter.kodeco.com/original/3X/7/5/7575b48a7c2e53622f37487eaaf13361f6683d72.png)

The lines now render on top of the model.

(Btw - it’s really bad practice to build a depth stencil state on the fly like this - they should be set up at the start of the app before the draw loop)

---

<div class="post-metadata">

**Author:** ![growthlyzet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/growthlyzet/32/81494_2.png) [@growthlyzet](https://forums.kodeco.com/u/growthlyzet)\
**Post date:** [August 10, 2020, 3:42pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/5 "2020-08-10T15:42:35Z")

</div>

I also facing this type of problem.

---

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 10, 2020, 8:48pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/6 "2020-08-10T20:48:48Z")

</div>

really ? I will test DepthStencilState again.  
So do you mean the render pass solution is better than depthStencil for this problem?

---

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 10, 2020, 9:29pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/7 "2020-08-10T21:29:23Z")

</div>

still not succeed, I add the code  
step1  
 ![image](https://files.chunter.kodeco.com/original/3X/2/a/2a2010ac6ec04ae808cb50c1e0a1140e1219b866.png)  
step2  
 ![image](https://files.chunter.kodeco.com/original/3X/8/f/8f547b7031a77df1dd79ae11b697f467434a4ddc.png)  
step3  
 ![image](https://files.chunter.kodeco.com/original/3X/f/3/f3d99ea9f66cc6ffed2eb795d6b93f822ee833bb.png)  
why my sunlight flickering all the time. I did not see any animation code  
for clear, I keep only one line . It still go inside the train and disappear  
 ![image](https://files.chunter.kodeco.com/original/3X/e/8/e87e36f604babecfb6d10fe49f134edabb26eb31.png)

---

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 10, 2020, 9:33pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/8 "2020-08-10T21:33:01Z")

</div>

Ok ,I success  
the clause “descriptor.depthCompareFunction = .always” finally did it . No matter the descriptor.isDepthWriteEnabled = true or false.

Thanks ,always.  
So the left question is what cause the sunlight lines flickering all the time?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 10, 2020, 11:02pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/9 "2020-08-10T23:02:55Z")

</div>

Well done!

The sunlight lines flicker because sometimes the lines are too thin to be drawn, so they flicker in and out. This is because of the shader code that makes the debug points into circles and remove half the fragment.

```auto
fragment float4 fragment_light(float2 point [[point_coord]],
                               constant float3 &color [[buffer(1)]]) {
// float d = distance(point, float2(0.5, 0.5));
// if (d > 0.5) {
// discard_fragment();
// }
  return float4(color ,1);
}

```

This should make your lines thicker. If you want thick lines on lines, and the circle on points, you can set a boolean and send it as another parameter to the shader using, eg, buffer(2) and test it there. However, as you asked before, the line isn’t going to be any thicker than that unless you draw quads along it.

Changing states, such as pipeline state and depth stencil state, on the GPU is very fast. Faster than the render pass solution.

My previous comment about best practice referred to my creating the depth stencil state during the draw call. Your creation of it up front is the way to go.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 10, 2020, 11:03pm UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/10 "2020-08-10T23:03:21Z")

</div>

@growthlyzet - Hi! Do relaxslow’s answers help you?

---

<div class="post-metadata">

**Author:** ![relaxslow](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/relaxslow/32/103881_2.png) [@relaxslow](https://forums.kodeco.com/u/relaxslow)\
**Post date:** [August 11, 2020, 1:00am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/11 "2020-08-11T01:00:54Z")

</div>

perfect, now i have enough knowledge to build my own helper object

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 11, 2020, 1:12am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/12 "2020-08-11T01:12:41Z")

</div>

Also, as you probably realised, if you want to use a different vertex or fragment shader on your helper object, then you can use a different pipeline state.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 6, 2021, 7:10am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/13 "2021-04-06T07:10:14Z")

</div>

@relaxslow Thank you for sharing your solution - much appreciated!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 6, 2021, 7:10am UTC](https://forums.kodeco.com/t/can-i-turn-off-depthstencil-for-some-object/117065/14 "2021-04-06T07:10:32Z")

</div>

@growthlyzet Do you stil have issues with this?
