Skip to content

GetData() corrupts data when an element size is not a multiple of 16 bytes #2

@k4G17eYWI

Description

@k4G17eYWI

Prerequisites

  • I have verified this issue is present in the develop branch
  • I have searched open and closed issues to ensure it has not already been reported.

MonoGame Version

MonoGame.Framework.Compute.DesktopGL 3.8.3

Which MonoGame platform are you using?

MonoGame Cross-Platform Desktop Application (mgdesktopgl)

Operating System

Windows

Description

I have a struct of 3 floats. It takes three bytes. When I generate elements {111,222,333} in a shader and get it back through GetData() data becomes corrupted. Looks like it tries to extend stride to 16 bytes. If I add another float to my structure it works well (I think because of a stride becomes a multiple of 16 bytes)

image

Steps to Reproduce

shader code:

RWStructuredBuffer<particle> buf;

struct particle2
{
    float3 position;
    // float w;
};

[numthreads(1,1,1)]
void EmitParticles(uint3 id : SV_DispatchThreadID)
{
    particle2 p;
    p.position = float3(111,222,333);  
    buf[id.x] = p;
}

c# code:

public struct Particle2
{
    public float X;
    public float Y;
    public float Z;
    // public float W;
}

_shader.DispatchIndirect(0, 0, _argsBuffer);
var data = new Particle2[100];
_testBuffer.GetData(data);

If I uncomment a third component in both shader and c# it works as it should.

Minimal Example Repo

No response

Expected Behavior

GetData() should not corrupt data. If it is impossible to operate structs which stride is not a multiple of 16 an exception should be thrown.

Resulting Behavior

I have a struct of 3 floats. It takes three bytes. When I generate elements {111,222,333} in a shader and get it back through GetData() data becomes corrupted. Looks like it tries to extend stride to 16 bytes. If I add another float to my structure it works well (I think because of a stride becomes a multiple of 16 bytes)

Files

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions