compushady icon indicating copy to clipboard operation
compushady copied to clipboard

Working with float4s

Open SpicyMelonYT opened this issue 1 year ago • 1 comments

Hi again.

Currently I am attempting to use the "R16G16B16A16_FLOAT" format. I want to work with float4s instead of the uint4s in the demo. In my hlsl file I made the appropriate corrections for using float4s.

However, when reading the buffer back to the cpu, im not getting what I would expect. When I have a 1x1 texture i get back a bytes object of length 8. When its a 2x2 texture i get a 272 length bytes object. And when I use an 8x8 texture I get a bytes object of length 1856. All of which confuse me as I thought that I should be getting back a length equal to the width x height x depth. Where width and height are from the texture size and depth is the format (16 x 4 = 64 for the format R16G16B16A16_FLOAT).

Can you help me understand thee result

HLSL

RWTexture2D<float4> texture : register(u0);
[numthreads(8, 8, 1)]
void main(int3 id : SV_DispatchThreadID)
{
    float4 color = float4(1.0, 2.0, 3.0, 4.0);
    texture[id.xy] = color;
}

And I have made sure that the return buffer is using HEAP_READBACK in the code

SpicyMelonYT avatar Oct 07 '23 08:10 SpicyMelonYT

Hi, first of all R16G16B16A16_FLOAT is a 8 bytes thing (half precision floats). Probably you want to use R32G32B32A32_FLOAT given your expectations and the shader code using floats (and not half)

Secondary, whenever you deal with texture memory you should use the row_pitch property of the texture. It reports the alignement value per-row.

In your specific case the row_pitch is 256 bytes. The phormula is (row_pitch * (height - 1)) + width * size_of_pixel

1x1: (256 * (1-1)) + 1 * 8 = 8 bytes 
2x2 = (256 * (2-1)) + 2 * 8 = 256 + 16 = 272
8x8 = (256 * (8-1)) + 8 * 8 = 1792 + 64 = 1856

rdeioris avatar Oct 07 '23 17:10 rdeioris