Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Disposing of computeBuffer causes result to screw up.

Discussion in 'Scripting' started by rafaeltab, Oct 13, 2019.

  1. rafaeltab

    rafaeltab

    Joined:
    May 22, 2017
    Posts:
    6
    Hello everybody!

    I am trying to procedurally generate a cave system using the marching cubes algorithm and perlin noise.
    I have implemented it in 2 ways: using GPU and using CPU. The CPU version is working perfectly fine, no errors nothing. However the GPU version has some problems. Of which the title is the main one that I am trying to solve right now, I will mention the other ones as well so I don't have to make a bazillion threads.

    So on to the actual question.
    I send data to a Compute Shader using Compute Buffers and also get some back also using Compute Buffers.
    If I don't dispose of the buffers I get a bucket load of warnings every time I generate (very often since I am using a Chunk system, but that is not relevant to the question) so I tried to do so BUT THEN all hell broke loose. I can actually release the buffer that sends the data to the Compute Shader with only one slight problem that I get the following errors (when putting the mesh in a mesh collider) sometimes which goes along with the chunk not to being generated at all:

    [Physics.PhysX] cleaning the mesh failed
    UnityEngine.MeshCollider:set_sharedMesh(Mesh)
    Assets.Scripts.Chunk:Display(GameObject) (at Assets/Scripts/WorldGeneration/Chunk.cs:116)
    <AllChunks>d__27:MoveNext() (at Assets/Scripts/WorldGeneration/MeshGenerator.cs:243)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    MeshGenerator:Generate(Boolean) (at Assets/Scripts/WorldGeneration/MeshGenerator.cs:190)
    MeshGenerator:Start() (at Assets/Scripts/WorldGeneration/MeshGenerator.cs:63)

    [Physics.PhysX] cleaning the mesh failed

    .When I release the buffer that receives the triangles however, I can't even understand whats going on anymore. When I release it before the input one it generates each chunk, but inside of every other chunk. So I've got a bunch of chunks all containing the mesh for every chunk so it is just a big bunch of random tunnels jambled together.

    When I release the buffer after the input one it generates one and the same chunk every time..

    I think all this is happening because of the time of when I release them, now I release them at the end of each individual generation. However I might need to get rid of them at the end of the frame or some other time, When? and can I get there with a startCoroutine. (Without adding code to the MonoBehaviour but just calling the startCoroutine on a MonoBehaviour instance).

    Anyone that could make sense of anything that is happening, I applaud you and thank you.

    Anyone that works at unity, please add more tutorials or docs on how to use these properly. Furthermore, Thanks for the awesomeness of the engine.

    Thanks to everyone who attempts to answer the question.

    P.S. If you need a specific piece of code I can definitely supply that. However, I feel that it is unnecessary to supply everything right now since it is probably fixable without the code.
     
    CloudyVR likes this.
  2. Breaking_Reality

    Breaking_Reality

    Joined:
    Feb 23, 2017
    Posts:
    6
    Sorry to revive an old post but I have the same issue! Bump
     
    CloudyVR likes this.
  3. Crysaac

    Crysaac

    Joined:
    Nov 22, 2018
    Posts:
    2
    So i did not quite get the problem you are having, but i had a lot of problems with releasing an AppendBuffer too. For me the data from previous calculations still stayed in the buffer causing it to still have the data of old chunks until the buffer count was higher than my given max and the game broke. I finally found a fix today. I did the following:

    trianglesBuffer.Release();
    trianglesBuffer = new ComputeBuffer(maxTriangleCount, sizeof(float) * 3 * 3, ComputeBufferType.Append);
    trianglesBuffer.SetCounterValue(0);

    Reseting the Countervalue after the new init fixed everything for me. I had not known that releasing the buffer does not reset that value. ^^' I hope this helps somebody that comes across this post.
     
  4. Breaking_Reality

    Breaking_Reality

    Joined:
    Feb 23, 2017
    Posts:
    6
    I'm actually still working on my project and never fixed the issue. I hope this fixes it haha thanks!