# Performance: clearing a texture quickly and texture rendering for simulation

**URL:** <https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486>\
**Category:** Questions\
**Created:** [November 22, 2021, 8:20pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486 "2021-11-22T20:20:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 22, 2021, 8:20pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/1 "2021-11-22T20:20:30Z")

</div>

Hi fellow Babyloner, this is a three part question on performance, but you get triple fake internet points! 🙂

1. What is the fastest way to clear a texture on every frame?
2. Can I bind a texture to a `thinInstanceBuffer` without getting it back to the CPU?
3. What’s a simple way to do a compute shader without WebGPU? 🙂 Do I need to do this a RTT with a separate pass, or could I somehow do it as part of the main render with some kludge? Any reference PGs?

For context, I’m trying a boids GPU implementation that uses some smart shortcuts to get a `O(n + k)` simulation on the GPU, nothing fancy or new: the idea is to store boid data (position, velocity) on float32 textures that are updated by a shader. I’ll need a sort of index texture that has to be zero-ed on every frame, and the texture with the positions will be used by thin instances to update the position/orientations. But this is going for \> 1k meshes, so I’m trying to squeeze some basic bottlenecks.

For the texture clearing, the standard approaches seem to be `clearRect`, which apparently is CPU bound and vary a lot based on the platform and browser, and a GL call such as `glCopyImageSubData` or `glClearTexImage`, which seems to use `ARB_clear_texture` if available, but I couldn’t find how common that extension is on webgl. I don’t really care when the clearing is done, before or after rendering. I just want to do it as asynchronously and quickly as possible.

For the thin instance buffer, since I’m not updating a `Float32Array` on the GPU, I wonder if I can share the same buffer as the texture and get zero-copy. It looks like `RawTexture.CreateRGBATexture()` does this, but I’m not sure if the changed texture is sent back to the CPU.

---

<div class="post-metadata">

**Author:** ![DarraghBurke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darraghburke/32/10621_2.png) [@DarraghBurke](https://forum.babylonjs.com/u/DarraghBurke)\
**Post date:** [November 22, 2021, 8:26pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/2 "2021-11-22T20:26:27Z")

</div>

Maybe @sebavan can bring his incredible WebGL knowledge to this thread 🙂

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 22, 2021, 9:37pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/3 "2021-11-22T21:37:19Z")

</div>

I believe that for texture clearing there is a function `scene.cleanCachedTextureBuffer()`  
Example - [https://playground.babylonjs.com/#AS54U8#10](https://playground.babylonjs.com/#AS54U8#10)

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 22, 2021, 9:52pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/4 "2021-11-22T21:52:19Z")

</div>

> [@labris](#):
>
> I believe that for texture clearing there is a function `scene.cleanCachedTextureBuffer()`

Thanks, but I don’t think this clears the texture (i,e, paint it with 0.0). It seems to set the buffers of all textures in the scene to null, which is something quite different.

Apparently there’s a [OpenGL call not mirrored in WebGL](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glClearTexSubImage.xhtml). Perhaps `gl.copyTexSubImage2D` would work, but it’s still unclear if it’s a good way to do it.

---

<div class="post-metadata">

**Author:** ![brunobg](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brunobg/32/5523_2.png) [@brunobg](https://forum.babylonjs.com/u/brunobg)\
**Post date:** [November 25, 2021, 4:44pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/5 "2021-11-25T16:44:46Z")

</div>

@sebavan can I bother you once again to see if you have any input about this one?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 25, 2021, 8:11pm UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/6 "2021-11-25T20:11:57Z")

</div>

w0000t, I completely missed this thread…

About clear a pretty fast one is to use the gl.clear command from a render target 🙂 you could use a scissorRect to only clear a certain area.

As there are no compute shaders on WebGL you can not share renderBuffer and GpuBuffer.

You should look into WebGPU for those 🙂 as you could leverage everything available there. And I think @Evgeni_Popov already did a simulation for boyids

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 26, 2021, 9:54am UTC](https://forum.babylonjs.com/t/performance-clearing-a-texture-quickly-and-texture-rendering-for-simulation/25486/7 "2021-11-26T09:54:28Z")

</div>

The boid compute shader sample is in [https://doc.babylonjs.com/advanced\_topics/webGPU/computeShader](https://doc.babylonjs.com/advanced_topics/webGPU/computeShader) but it’s a 2D one and obviously only working in WebGPU.

> [@brunobg](#):
>
> - What is the fastest way to clear a texture on every frame?

To clear a render target texture use `engine.clear(...)`.

> [@brunobg](#):
>
> - Can I bind a texture to a `thinInstanceBuffer` without getting it back to the CPU?

Textures can’t be bound to `thinInstanceBuffer`, only arrays of numbers (`Float32Array`, etc).

Textures must be passed as inputs to shaders. In WebGPU, storage textures can be filled by a compute shader and passed to the vertex/fragment shaders (but you must write your shaders with WGSL). See [Writing shaders for WebGPU in WGSL | Babylon.js Documentation](https://doc.babylonjs.com/advanced_topics/webGPU/webGPUWGSL)

> [@brunobg](#):
>
> What’s a simple way to do a compute shader without WebGPU?

Compute shaders are not supported in WebGL. You can do a RTT pass and fill your texture by the fragment shader.
