简体   繁体   English

在 computeShader 代码运行后将数据存储在 GraphicsBuffer 中总是 (0,0,0)

[英]Storing data inside GraphicsBuffer after computeShader code runs always are (0,0,0)

Environment of work: Unity3D 2021.2.7.f1 Direct3D11工作环境:Unity3D 2021.2.7.f1 Direct3D11

I try to make Laplacian Smoothing working on GPU For this case I set up Compute Shader among others VertexBuffer(input Graphics Buffer) and outVertexBuffer( output Grpahics Buffer), unfortunatelly I have a weird problem with storing data into GraphicsBuffer (storing vertices Vector3) which i use as "output" of compute shader.我尝试让 Laplacian Smoothing 在 GPU 上工作 对于这种情况,我在其他 VertexBuffer(输入图形缓冲区)和 outVertexBuffer(output Grpahics 缓冲区)中设置了 Compute Shader,不幸的是,我在将数据存储到 GraphicsBuffer(存储顶点 Vector3)时遇到了一个奇怪的问题我用作计算着色器的“输出”。

Assigning of ComputeShader component: ComputeShader 组件的分配:

public void RunSmoothingShader(int kernel,int verticesNo)
{
    ComputeBuffer connectionsBuffer = new 
ComputeBuffer(_builder._triangleBudget*6,sizeof(int));

    ComputeBuffer offsetsBuffer = new ComputeBuffer(_builder._triangleBudget, sizeof(int));
    ComputeBuffer _counterBuffer = new ComputeBuffer(1, 4, ComputeBufferType.Counter);
    connectionsBuffer.SetData(VertexConnection.connectionsTable);
    offsetsBuffer.SetData(VertexConnection.offsetsTable);
    smoothingShader.SetBuffer(0,"ConnectionsBuffer",connectionsBuffer);
    smoothingShader.SetBuffer(0,"OffsetsBuffer",offsetsBuffer);
    smoothingShader.SetBuffer(0, "Counter", _counterBuffer);
    smoothingShader.SetBuffer(0,"OutVertexBuffer",outVertexBuffer);
    smoothingShader.SetInt("MaxVerticesNumber", verticesNo);
    smoothingShader.SetBuffer(0,"VertexBuffer", _builder._vertexBuffer);
    
 
    smoothingShader.Dispatch(kernel,64,64,64);
}

Initialization of outVertexBuffer: (stride is equal 12 because type of float3) outVertexBuffer 的初始化:(步长等于 12,因为类型为 float3)

 outVertexBuffer =
            new GraphicsBuffer(GraphicsBuffer.Target.Vertex, 5000000, 12);

In LaplacianSmoothing ComputeShader I want to recalculate positions of vertices and store them into outVertexBuffer using Store3 function. Below is code for LaplacianSmoothing in ComputeShader (HLSL style):在 LaplacianSmoothing ComputeShader 中,我想重新计算顶点的位置并使用 Store3 function 将它们存储到outVertexBuffer中。下面是 ComputeShader 中 LaplacianSmoothing 的代码(HLSL 样式):

#pragma kernel LaplacianSmooth

RWTexture2D<float4> Result;
RWByteAddressBuffer VertexBuffer;
RWStructuredBuffer<uint> Counter; // used only for counting
RWByteAddressBuffer ConnectionsBuffer;
RWByteAddressBuffer OffsetsBuffer;
RWByteAddressBuffer OutVertexBuffer;
int MaxVerticesNumber;

#define SIZEOF_UINT 4
#define SIZEOF_FLOAT3 12

[numthreads(8,8,8)]
void LaplacianSmooth (uint3 id : SV_DispatchThreadID)
{
    uint currentIndex = 512*512*id.z + 512*id.y+id.x;
    if (currentIndex < MaxVerticesNumber)
    {
        float3 currentVertex = asfloat(VertexBuffer.Load3(2*currentIndex*SIZEOF_FLOAT3));
        float3 sumNeighVertex = 0;
        uint neighborAdrr=0;
        int currentOffset = OffsetsBuffer.Load(currentIndex*SIZEOF_UINT);
        int nextOffset = OffsetsBuffer.Load((currentIndex+1)*SIZEOF_UINT);
        int connectionCount = nextOffset-currentOffset;
        for (int i=0; i<connectionCount;i++)
        
        {
            neighborAdrr = ConnectionsBuffer.Load((currentOffset+i)*SIZEOF_UINT);// Adress of neighbor;
        
            sumNeighVertex += asfloat(VertexBuffer.Load3(neighborAdrr*2*SIZEOF_FLOAT3));
        }

        sumNeighVertex = sumNeighVertex / connectionCount;

        float3 newVert = sumNeighVertex+currentVertex;
        OutVertexBuffer.Store3(currentIndex*SIZEOF_FLOAT3,asuint(currentVertex));
    }
}

In next step I want to get data from outVertexBuffer and assing them into vertexArray GraphicsBuffer.GetData(Vector3[] array) function.在下一步中,我想从 outVertexBuffer 获取数据并将它们分配到 vertexArray GraphicsBuffer.GetData(Vector3[] array) function。

public void OnSmoothMeshLaplacian()
{

PrepareSmoothing();
var mesh = GetComponent<MeshFilter>().mesh;
Mesh testMesh = Instantiate(mesh);
GameObject.Find("Laplacian").GetComponent<MeshFilter>().sharedMesh = testMesh;
//testMesh = LaplacianFilter(testMesh, 5,1f);

Vector3[] vertexArray;
vertexArray = new Vector3[2*testMesh.vertices.Length];
var network = VertexConnection.BuildNetwork(testMesh.triangles);
RunSmoothingShader(smoothingShader.FindKernel("LaplacianSmooth"), testMesh.vertexCount);
outVertexBuffer.GetData(vertexArray);
testMesh.SetVertexBufferData(vertexArray, 0, 0, vertAndPosList.Length);
testMesh.RecalculateBounds();
Debug.Log("Mesh:" + mesh.bounds.size);
Debug.Log("Test Mesh:" + testMesh.bounds.size);
Debug.Log($"Shrinkage: {(mesh.bounds.size - testMesh.bounds.size).magnitude / mesh.bounds.size.magnitude * 100f}%");
    }

I set up the breakpoint after getData function and checked the data, all of them are (0,0,0).我在getData function之后设置断点,查看数据,都是(0,0,0)。 I use RenderDoc for checking how buffers works, googled documentations for all function used in shader and main program, but nothing explain this lack of assigning data.我使用 RenderDoc 检查缓冲区的工作方式,用谷歌搜索了着色器和主程序中使用的所有 function 的文档,但没有任何解释缺少分配数据。 I want to add that when I use inputBuffer also as output then I get data (logicaly incorrect, but still not (0,0,0).我想补充一点,当我将 inputBuffer 也用作 output 时,我会得到数据(逻辑上不正确,但仍然不是 (0,0,0)。

Try this:尝试这个:

outVertexBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, 5000000, 12);

Notice the changed Target.请注意更改后的目标。 Graphics buffer needs at least one of the Compute target flags to be able to be bound to a compute kernel. Target.Vertex is not such a flag.图形缓冲区需要至少一个计算目标标志才能绑定到计算 kernel。Target.Vertex 不是这样的标志。 You could still add it but in your case it's not needed.您仍然可以添加它,但在您的情况下不需要。

In fact outVertexBuffer could just be a ComputeBuffer .事实上outVertexBuffer可能只是一个ComputeBuffer No need to use GraphicsBuffer since you just copy it to the mesh on the CPU side.无需使用GraphicsBuffer ,因为您只需将其复制到 CPU 端的网格。

EDIT---编辑 - -

Another thing: SetVertexBufferData() requires you to configure all vertex attributes first and the data you pass should basically be raw vertex data: positions, normals, uvs (if any) and so on.另一件事: SetVertexBufferData()要求您首先配置所有顶点属性,并且您传递的数据基本上应该是原始顶点数据:位置、法线、uvs(如果有)等。

If you only want to set vertices perhaps it would be easier to just use Mesh.SetVertices() .如果您只想设置顶点,也许使用Mesh.SetVertices()会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM