How to use a shader to write to a mipmap (partial mipmap update)


I am using a ComputeShader to write data to a RenderTexture. I need to update MipMaps after writing to the texture, as proper (updated) MipMaps are required to visualize the texture using a normal material / graphics shader.

The challenge is. The texture is very large (16k x 16k). The modification to it are small and frequent. So I need to do the job within the existing GPU texture buffers using a compute shader. Preferably I try to limit the Mipmap update to the effected region.

So my questions are:

  • How to update a MipMap from a ComputeShader?

  • Is there a way to directly access/write to a specific texture mipmap from a Shader? (i.e. using level, UV to index)

Many Thanks


I don’t think you can pick a mipmap level in the shader. What you can do is assign a specific mipmap level to the shader variable on the C# side:

computeShader.SetTexture(kernel, name, renderTexture, mipmapLevel);

If you want to access all mipmaps in the shader, you need one RWTexture2D variable per mipmap.

Thanks a lot. Yes that seems to be a promising option.

Just tricky on how to handle different texture sizes, mipmap levels. As I need to assign individual texture variables in the shader.

Follow on:

  • Would it be possible to use a NativePtr to access the texture and just expect the MipMaps to be found at some index ranges behind w x h?

  • Is the shader executed in sequence with respect to dimensions? Let’s say, I want to use id.xy as 2D coordinates in the picture and id.z as index for the mipmap levels. Can I assume that it first iterates over id.xy before incrementing id.z to go to the next mipmap. Or do I need to invoke a separate shader call for each mipmap?