Exemplo n.º 1
0
        protected bool DefaultBeginDraw()
        {
            if (IsDisposed)
            {
                return(false);
            }

            if (Device.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal)
            {
                RenderManager.ResetDeviceState(Device);
                return(true);
            }
            else if (!_Running)
            {
                return(false);
            }

            return(false);
        }
Exemplo n.º 2
0
 internal void Initialize(RenderCoordinator coordinator, RenderManager renderManager, int index)
 {
     Batches.ListPoolOrAllocator = _ListPool;
     Batches.Clear();
     Coordinator        = coordinator;
     this.RenderManager = renderManager;
     Index = index;
     State = (int)States.Initialized;
     Label = "Frame";
     ChangeRenderTargets = true;
     if (PrepareData == null)
     {
         PrepareData = new FramePrepareData();
     }
     else
     {
         PrepareData.Initialize();
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a render coordinator. A render manager and synchronous draw methods are automatically provided for you.
        /// </summary>
        /// <param name="deviceService"></param>
        public RenderCoordinator(
            IGraphicsDeviceService deviceService, Thread mainThread, ThreadGroup threadGroup,
            Func <bool> synchronousBeginDraw = null, Action synchronousEndDraw = null
            )
        {
            DeviceService      = deviceService;
            ThreadGroup        = threadGroup;
            Manager            = new RenderManager(deviceService.GraphicsDevice, mainThread, ThreadGroup);
            UseResourceLock    = Manager.UseResourceLock;
            CreateResourceLock = Manager.CreateResourceLock;

            _SyncBeginDraw = synchronousBeginDraw ?? DefaultBeginDraw;
            _SyncEndDraw   = synchronousEndDraw ?? DefaultEndDraw;

            DrawQueue = ThreadGroup.GetQueueForType <DrawTask>();

            UpdateGraphicsBackend(Device);
            RegisterForDeviceEvents();

            deviceService.DeviceCreated += DeviceService_DeviceCreated;
        }
Exemplo n.º 4
0
        public void Prepare(bool parallel)
        {
            if (Interlocked.Exchange(ref State, (int)States.Preparing) != (int)States.Initialized)
            {
                throw new InvalidOperationException("Frame was not in initialized state when prepare operation began ");
            }

            var numRemoved = BatchCombiner.CombineBatches(ref Batches, ref BatchesToRelease);

            // Batch combining shuffles the batches around to group by type. Once it's done,
            //  we need to do the final sort to preserve layer and material ordering.
            Batches.Sort(BatchComparer);

            if (!Monitor.TryEnter(PrepareLock, 5000))
            {
                throw new InvalidOperationException("Spent more than five seconds waiting for a previous prepare operation.");
            }

            try {
                if (parallel)
                {
                    RenderManager.ParallelPrepareBatches(this);
                }
                else
                {
                    RenderManager.SynchronousPrepareBatches(this);
                }
            } finally {
                Monitor.Exit(PrepareLock);
            }

            if (Interlocked.Exchange(ref State, (int)States.Prepared) != (int)States.Preparing)
            {
                throw new InvalidOperationException("Frame was not in preparing state when prepare operation completed");
            }
        }
Exemplo n.º 5
0
        private bool DoSynchronousDrawToRenderTarget(
            RenderTarget2D renderTarget,
            DefaultMaterialSet materials,
            Delegate drawBehavior, object userData,
            ref ViewTransform?viewTransform,
            string description
            )
        {
            var oldLazyState = materials.LazyViewTransformChanges;

            try {
                materials.LazyViewTransformChanges = false;
                materials.ApplyViewTransform(materials.ViewTransform, true);
                using (var frame = Manager.CreateFrame(this)) {
                    frame.ChangeRenderTargets = false;
                    frame.Label = description;
                    if (viewTransform.HasValue)
                    {
                        materials.PushViewTransform(in viewTransform);
                    }
                    else
                    {
                        materials.PushViewTransform(ViewTransform.CreateOrthographic(renderTarget.Width, renderTarget.Height));
                    }

                    try {
                        // FIXME: Should queued draws run here? They are probably meant to run in the next real frame

                        var singleBehavior = drawBehavior as Action <Frame>;
                        var multiBehavior  = drawBehavior as PendingDrawHandler;
                        if (singleBehavior != null)
                        {
                            singleBehavior(frame);
                        }
                        else if (multiBehavior != null)
                        {
                            multiBehavior(frame, materials, userData);
                        }
                        else
                        {
                            throw new ArgumentException("Draw behavior was not of a compatible type");
                        }

                        RunBeforePrepareHandlers();
                        PrepareNextFrame(frame, false);

                        Manager.DeviceManager.SetRenderTarget(renderTarget);
                        RenderManager.ResetDeviceState(Device);
                        Device.Clear(Color.Transparent);

                        RenderFrameToDraw(frame, false);
                        // We don't have to push/pop anymore because the stacks are cleared at the end of a frame.

                        return(true);
                    } finally {
                        materials.PopViewTransform();
                    }
                }
            } finally {
                materials.LazyViewTransformChanges = oldLazyState;
            }
        }
Exemplo n.º 6
0
 public DeviceManager(RenderManager renderManager, GraphicsDevice device)
 {
     RenderManager = renderManager;
     Device        = device;
     DeviceId      = ++NextDeviceId;
 }
Exemplo n.º 7
0
 public FramePool(RenderManager renderManager)
     : base(4)
 {
     RenderManager = renderManager;
 }