Skip to content

GraphicsBuffer | Readback Data Function? #206

@sebastian-wolter-itk

Description

@sebastian-wolter-itk

Currently there seems to be no readback function for reading GraphicsBuffer back.
I've tried mapping the buffer but this leads

Image

Is there some other way to do that?

CS Code:

using Prowl.Runtime;
using Prowl.Runtime.Rendering;
using System;
using Veldrid;
public class NewScript : MonoBehaviour
{
	public AssetRef<ComputeShader> shader;
	GraphicsBuffer buffer;

    public override void Start()
	{
		Debug.Log("Start");
	}

	public override void Update()
	{
		Debug.Log("Update");

		ComputeDescriptor descriptor = new ComputeDescriptor(shader);
		descriptor.SetInt("numElements", 128);
		descriptor.SetBuffer("buffer", buffer);
		ComputeDispatcher.Dispatch(descriptor, 0, 128, 1, 1);
        Span<float> data = new float[128];
        var mappedResource = Graphics.Device.Map(buffer.Buffer, Veldrid.MapMode.ReadWrite);
        if ((data.Length * sizeof(float)) < mappedResource.SizeInBytes)
            throw new ArgumentException("Insufficient space to store the requested pixel data", nameof(data));

        unsafe
        {
            fixed (void* ptr = data)
            {
                Buffer.MemoryCopy((void*)mappedResource.Data, ptr, data.Length * sizeof(float), mappedResource.SizeInBytes);
            }
        }

        Debug.Log(data[0]);
        Debug.Log(data[1]);

        Graphics.Device.Unmap(buffer.Buffer);
    }

    public override void OnEnable()
    {
		buffer = new GraphicsBuffer(1024 * 1024, sizeof(float), true);
    }

    public override void OnDisable()
    {
		buffer.Dispose();

    }
}

Compute:

#pragma kernel main

RWStructuredBuffer<float> buffer;
int numElements;

[numthreads(1, 1, 1)]
void main( uint3 id : SV_DispatchThreadID )
{
    if(id.x >= numElements) return;
    buffer[id.x] = (float)id.x;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions