How to use RWTexture3D<int>

Hello,

I create a texture3d like so:

  Data = new Texture3D(
                GraphicsDevice,
                VolumeSize,
                VolumeSize,
                VolumeSize,
                false, 
                SurfaceFormat.Color, 
                ShaderAccess.ReadWrite);

As visible, I use color as surface format (meaning in total 32 bit per data).
But when I now try to use RWTexture3D<int> assign in a compute shader numbers between, let’s say, 1 and 10, and try to access them in another compute-shader, I don’t retrieve the number I assigned (in fact, I don’t even know exactly what I am getting, it could be anything).

But when using RWTexture3D<float4>, assigning to it data like float4(7 / 256.0f, 0, 0, 0) I can retrieve the exact data I assigned to in another shader by using following piece of code:

int getData(int3 position)
{
    int4 data = texture3D[position] * 255; // I have to multiply by 255, because the numbers are scaled between 0 and 1.
     
    int r = data.r;
    int g = data.g;
    int b = data.b;
    int a = data.a;
    
    return r | (g << 8) | (b << 16) | (a << 24);
}

Of course, I want to reduce the amount of bitwise operations as much as I can, and the function is not needed at all actually, because RWTexture allows arbitrary types.

But why doesn’t int work?

There’s a separate surface format for integer textures. It’s currently not part of MonoGame. I added uint textures to my personal MG fork for DX and GL with those two commits: commit1, commit2.

Maybe I’ll add this to the compute fork at some point, as it’s quite useful for compute shaders. You can do atomic operations on int surfaces for example.

1 Like

Okay no problem, I will use single as surface format then, it works just fine’.

An additional question: Will it ever happen that RWTextures can be accessed from pixel shaders? If so, monogame would be a very very serious framework, espacially with regards to raytracing/global illumination.

Would we be able to read/write from rwtextures in a pixel shader, we could voxelize meshes on the fly while the model is rasterized.

How hard is that to implement to access rwtextures from pixel shaders? I know you can (or should I say could) only write to rendertargets from a pixelshader, but can’t we just “trick” it into thinking rwtextures are rendertargets? Maybe by assigning rwtextures to registers of rendertargets?

Best regards, and awesome fork. If the monogame team would only use your fork in the next release… it would imo definetely boost this project.

_

Thanks @3r0rXx.

It’s definitely possible with DX 11 and OpenGL 4. I need to look into it to find out how much work that is exactly. Maybe I’ll do that soon.

2 Likes