private int m_NextIndex = 0;                                                                // next index for the m_InFlightRequests array

        // Add the specified component to the list of components with its delegates
        // and scripting API observer.  Components are required to register with
        // the context prior to their first surface bake request.  This method will
        // throw ArgumentException if this component already exists in the list
        // ArgumentNullException if any of the parameters are missing.
        public void RegisterComponent(SpatialMappingBase smComponent, SpatialMappingBase.SurfaceDataReadyCallback onDataReady, GetHighestPriorityCallback getHighestPri, SurfaceObserver observer)
        {
            if (smComponent == null)
            {
                throw new ArgumentNullException("smComponent");
            }
            if (onDataReady == null)
            {
                throw new ArgumentNullException("onDataReady");
            }
            if (getHighestPri == null)
            {
                throw new ArgumentNullException("getHighestPri");
            }
            if (observer == null)
            {
                throw new ArgumentNullException("observer");
            }
            SMComponentRecord findResult = m_Components.Find(record => record.m_Component == smComponent);

            if (findResult.m_Component != null)
            {
                throw new ArgumentException("RegisterComponent on a component already registered!");
            }
            SMComponentRecord rec;

            rec.m_Component       = smComponent;
            rec.m_OnDataReady     = onDataReady;
            rec.m_GetHighestPri   = getHighestPri;
            rec.m_SurfaceObserver = observer;
            m_Components.Add(rec);
        }
        // Components know what work there is to be done so ask the first one in the
        // list for an item of work then drop it to the end of the list so that a
        // really hungry component doesn't starve the rest of them out.
        private void RequestMeshPriorityFromComponents()
        {
            // fixme:  would like to do this twice if needed
            if (m_InFlightSurfaces < kIdealInFlightSurfaceCount)
            {
                for (int ii = 0; ii < m_Components.Count; ++ii)
                {
                    SMComponentRecord comp = m_Components[ii];
                    SurfaceData       nextRequest;
                    if (comp.m_GetHighestPri(out nextRequest))
                    {
                        if (-1 == m_NextIndex || !m_InFlightRequests[m_NextIndex].IsClear())
                        {
                            Debug.LogError(System.String.Format("SMContext:  next index {0} may not be clear!", m_NextIndex));
                        }
                        else
                        {
                            if (comp.m_SurfaceObserver.RequestMeshAsync(nextRequest, OnSurfaceDataReady))
                            {
                                //Debug.Log(string.Format("Attempting to Bake \"{0}\" : \"{1}\"", comp.m_Component.name, nextRequest.id.handle));
                                m_InFlightRequests[m_NextIndex].m_RequestData = nextRequest;
                                m_InFlightRequests[m_NextIndex].m_Requester   = comp;
                                m_InFlightSurfaces++;
                                m_NextIndex = m_NextIndex == 1 ? 0 : 1;

                                // drop this component to the end of the list
                                // so it can't starve others out.
                                m_Components.RemoveAt(ii);
                                m_Components.Add(comp);
                                break;
                            }
                            else
                            {
                                // if this fires it means that something's
                                // misconfigured, probably in the component.
                                Debug.LogError("SMContext:  unexpected failure requesting mesh bake!");
                            }
                        }
                        break;
                    }
                }
            }
        }