Writing to RWTexture2DArray mipmaps in compute shader

Currently attempting to dive into compute shaders and trying to google some of this stuff is kicking my butt. :face_with_spiral_eyes:

Pretty sure I’ve got the basics down but I hit a roadblock attempting to write to the mipmaps for a RWTexture2DArray I created. My current relevant code excerpts:

RWTexture2DArray<float4> outputTexture : register(u0);

...

[numthreads(32, 32, 1)]
void Convolve(uint3 ThreadID : SV_DispatchThreadID)
{
    ...

    outputTexture[ThreadID] = float4(1.0, 1.0, 1.0, 1.0);
}

It only seems to be able to write to mip 0 and outputTexture doesn’t accept a float4 to specify a mip level.

I feel like this has to be a thing (this page certainly would seem to imply such a thing is possible: Subresources (Direct3D 12 Graphics) - Win32 apps | Microsoft Learn) but I’m not exactly sure what to do with that information, especially in the context of Unity.

From what I gather, the subresource exists and I could sample it with a sampler, but how do I go about writing to it? Is there some Unity-specific thing I’m missing here? Unity’s compute shader documentation is, uh… lacking, to put it nicely. :roll_eyes:

For some background context: the whole reason I’m using RWTexture2DArray explicitly is that there appears to be no way to write to a TextureCube so I’m doing the compute logic in a 6-deep RWTexture2DArray instead, then copying back to a cubemap RenderTexture afterwards. If there’s a better way to do that, I’m all ears. :wink:

Is the destination mip level something that needs to be specified on the C# side? I haven’t found any Unity documentation suggesting so which leads me to believe this needs to be done in the compute shader itself.

Bump.

You specify the mip level in C# ComputeShader.SetTexture (Unity - Scripting API: ComputeShader.SetTexture).

2 Likes

Ah ha! Thank you, that’s exactly what I was missing.