U5.3.5 dynamic mesh generation "OculusWaitForGPU"

Hi,

I’m currently using Unity V5.3.5. In my game, I’m changing meshes on a trigger pull of my controller. At that moment, several meshes with max 1200 vertices and ~6500 triangles are altered (no change in the number of triangles or vertices). The mesh change is performed via worker threads which results in arrays containing the result. Those arrays are then assigned to a newly generated mesh (see code snippet).

        private void ApplyPatchMesh(ref TerrainPatch p)
        {
            MeshFilter meshFilter = p.m_PatchObject.GetComponent<MeshFilter>();
            if (meshFilter == null)
                meshFilter = p.m_PatchObject.AddComponent(typeof(MeshFilter)) as MeshFilter;

            if (p.m_Renderer == null)
            {
                p.m_Renderer = p.m_PatchObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
                p.m_Renderer.material.shader = Shader.Find("Sidema/TerrainGPUNoHeight");
            }

            if (p.m_Collider == null)
                p.m_Collider = p.m_PatchObject.AddComponent(typeof(MeshCollider)) as MeshCollider;

            if (p.m_PatchObject.GetComponent<Rigidbody>() == null)
            {
                Rigidbody rb = p.m_PatchObject.AddComponent<Rigidbody>();
                rb.isKinematic = true;
            }

            Mesh m = new Mesh();
            m.name = p.m_PatchObject.name;

            p.vertices = p.m_patchJob.vertices;
            p.uvs = p.m_patchJob.uvs;
            p.triangles = p.m_patchJob.triangles;
            p.tangents = p.m_patchJob.tangents;

            m.vertices = p.vertices;
            m.uv = p.uvs;
            m.triangles = p.triangles;
            m.tangents = p.tangents;
            m.RecalculateNormals();
            p.m_Collider.sharedMesh = null;
            p.m_Collider.sharedMesh = m;


            meshFilter.sharedMesh = m;
            m.RecalculateBounds();

            ApplySubTextures(p);
        }

I’ve optimized the code and the deep profiler shows that my code never uses more than 0.76 ms.

But what I see is that OculusWaitForGPU takes up to 50-60 ms in those frames when I change the meshes.

Unfortunately, I only can suppose that it’s the mesh push to the GFX card which causes the “hiccup”, but 50-60ms seems to be highly unreasonable for max. 4-8 6500 triangle patches.

The high OculusWaitForGPU peak lasts for 2 frames.

Has anyone an idea how to improve this? What is OculusWaitForGPU exactly doing at this point when I push a new mesh to the GFX card?

Hi Metron!

It’s doing what it says! The CPU is waiting for the GPU to finish up it’s calculations before submitting the next frame. If you attach a GPU profiler you might be able to dig a little deeper into the problem to see what’s choking it up.

Here is a talk about VR rendering that explains why OculusWaitForGPU (and other mechanisms like it) exist right now: http://www.gdcvault.com/play/1021771/Advanced-VR

The “Timing: Scheduling, Prediction, VSync, GPU Bubbles” section is a decent place to start.

I hope this helps!

-Will

Thanks. I’d imagine that it does what it says. I was just wondering why pushing the meshes had such an impact.

I don’t have any GPU bubbles building up unless I push the meshes to the card. I’ll check via the GPU profiler though to be sure.