Пример #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="items"></param>
 /// <param name="pool"></param>
 /// <param name="context"></param>
 /// <param name="parameter"></param>
 /// <param name="filterType"></param>
 /// <param name="outputCommands"></param>
 /// <returns></returns>
 public bool ScheduleAndRun(List <RenderCore> items, IDeviceContextPool pool,
                            IRenderContext context, RenderParameter parameter, RenderType filterType, List <KeyValuePair <int, CommandList> > outputCommands)
 {
     outputCommands.Clear();
     if (items.Count > schedulerParams.MinimumDrawCalls)
     {
         var partitionParams = Partitioner.Create(0, items.Count, items.Count / schedulerParams.MaxNumberOfTasks + 1);
         Parallel.ForEach(partitionParams, (range, state) =>
         {
             var deferred = pool.Get();
             SetRenderTargets(deferred, ref parameter);
             for (int i = range.Item1; i < range.Item2; ++i)
             {
                 if (items[i].RenderType == filterType)
                 {
                     items[i].Render(context, deferred);
                 }
             }
             var command = deferred.DeviceContext.FinishCommandList(true);
             pool.Put(deferred);
             lock (outputCommands)
             {
                 outputCommands.Add(new KeyValuePair <int, CommandList>(range.Item1, command));
             }
         });
         return(true);
     }
     else
     {
         return(false);
     }
 }
 protected override bool OnAttach(IRenderTechnique technique)
 {
     DefaultShaderPass = technique[DefaultShaderPassName];
     contextPool = technique.EffectsManager.DeviceContextPool;
     textureSampler = Collect(technique.EffectsManager.StateManager.Register(SamplerDescription));
     CreateCubeMapResources();
     return true;
 }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="items"></param>
        /// <param name="pool"></param>
        /// <param name="context"></param>
        /// <param name="parameter"></param>
        /// <param name="outputCommands"></param>
        /// <param name="numRendered"></param>
        /// <returns></returns>
        public bool ScheduleAndRun(List <SceneNode> items, IDeviceContextPool pool,
                                   RenderContext context, RenderParameter parameter, List <KeyValuePair <int, CommandList> > outputCommands, out int numRendered)
        {
            outputCommands.Clear();
            int totalCount = 0;

            numRendered = 0;
            Exception exception = null;

            if (items.Count > schedulerParams.MinimumDrawCalls)
            {
                var frustum         = context.BoundingFrustum;
                var partitionParams = Partitioner.Create(0, items.Count, items.Count / schedulerParams.MaxNumberOfTasks + 1);
                Parallel.ForEach(partitionParams, (range, state) =>
                {
                    try
                    {
                        int counter  = 0;
                        var deferred = pool.Get();
                        SetRenderTargets(deferred, ref parameter);
                        for (int i = range.Item1; i < range.Item2; ++i)
                        {
                            if (context.EnableBoundingFrustum && !items[i].TestViewFrustum(ref frustum))
                            {
                                continue;
                            }
                            items[i].RenderCore.Render(context, deferred);
                            ++counter;
                        }
                        var command = deferred.FinishCommandList(true);
                        pool.Put(deferred);
                        lock (outputCommands)
                        {
                            outputCommands.Add(new KeyValuePair <int, CommandList>(range.Item1, command));
                            totalCount += counter;
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                });
                numRendered = totalCount;
                if (exception != null)
                {
                    throw exception;
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
 protected override void OnDetach()
 {
     contextPool = null;
     cubeMap = null;
     cubeDSV = null;
     textureDesc.Width = textureDesc.Height = dsvTextureDesc.Width = dsvTextureDesc.Height = 0;
     for (int i = 0; i < 6; ++i)
     {
         cubeRTVs[i] = null;
         cubeDSVs[i] = null;
     }
     base.OnDetach();
 }
Пример #5
0
 protected override void OnDetach()
 {
     RemoveAndDispose(ref textureSampler);
     RemoveAndDispose(ref cubeMap);
     RemoveAndDispose(ref cubeDSV);
     textureDesc.Width = textureDesc.Height = dsvTextureDesc.Width = dsvTextureDesc.Height = 0;
     for (var i = 0; i < 6; ++i)
     {
         RemoveAndDispose(ref cubeRTVs[i]);
         RemoveAndDispose(ref cubeDSVs[i]);
     }
     contextPool = null;
 }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DX11RenderBufferProxyBase"/> class.
 /// </summary>
 /// <param name="deviceResource">The device resources.</param>
 /// <param name="useDepthStencilBuffer"></param>
 public DX11RenderBufferProxyBase(IDeviceResources deviceResource, bool useDepthStencilBuffer = true)
 {
     this.DeviceResources       = deviceResource;
     deviceContextPool          = new DeviceContextPool(Device);
     this.UseDepthStencilBuffer = useDepthStencilBuffer;
 }
Пример #7
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize(int adapterIndex)
        {
            Log(LogLevel.Information, $"Adapter Index = {adapterIndex}");
            var adapter = GetAdapter(ref adapterIndex);

            AdapterIndex = adapterIndex;
#if DX11
            if (adapter != null)
            {
                if (adapter.Description.VendorId == 0x1414 && adapter.Description.DeviceId == 0x8c)
                {
                    DriverType = DriverType.Warp;
                    device     = new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0);
                }
                else
                {
                    DriverType = DriverType.Hardware;
#if DEBUGMEMORY
                    device = new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
#else
                    device = new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport);
#if DX11_1
                    device1 = device.QueryInterface <global::SharpDX.Direct3D11.Device1>();
#endif
#endif
                    // DeviceCreationFlags.Debug should not be used in productive mode!
                    // See: http://sharpdx.org/forum/4-general/1774-how-to-debug-a-sharpdxexception
                    // See: http://stackoverflow.com/questions/19810462/launching-sharpdx-directx-app-with-devicecreationflags-debug
                }
            }
#else
            device = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_1);
#endif
            Log(LogLevel.Information, $"Direct3D device initilized. DriverType: {DriverType}; FeatureLevel: {device.FeatureLevel}");

            #region Initial Internal Pools
            Log(LogLevel.Information, "Initializing resource pools");
            RemoveAndDispose(ref constantBufferPool);
            constantBufferPool = Collect(new ConstantBufferPool(Device, Logger));

            RemoveAndDispose(ref shaderPoolManager);
            shaderPoolManager = Collect(new ShaderPoolManager(Device, constantBufferPool, Logger));

            RemoveAndDispose(ref statePoolManager);
            statePoolManager = Collect(new StatePoolManager(Device));

            RemoveAndDispose(ref geometryBufferManager);
            geometryBufferManager = Collect(new GeometryBufferManager(this));

            RemoveAndDispose(ref materialTextureManager);
            materialTextureManager = Collect(new TextureResourceManager(Device));

            RemoveAndDispose(ref materialVariableManager);
            materialVariableManager = Collect(new MaterialVariablePool(this));

            RemoveAndDispose(ref deviceContextPool);
            deviceContextPool = Collect(new DeviceContextPool(Device));
            #endregion
            Log(LogLevel.Information, "Initializing Direct2D resources");
            factory2D          = Collect(new global::SharpDX.Direct2D1.Factory1(global::SharpDX.Direct2D1.FactoryType.MultiThreaded));
            wicImgFactory      = Collect(new global::SharpDX.WIC.ImagingFactory());
            directWriteFactory = Collect(new global::SharpDX.DirectWrite.Factory(global::SharpDX.DirectWrite.FactoryType.Shared));
            using (var dxgiDevice2 = device.QueryInterface <global::SharpDX.DXGI.Device>())
            {
                device2D        = Collect(new global::SharpDX.Direct2D1.Device(factory2D, dxgiDevice2));
                deviceContext2D = Collect(new global::SharpDX.Direct2D1.DeviceContext(device2D, global::SharpDX.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations));
            }
            Initialized = true;
        }
Пример #8
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize(int adapterIndex)
        {
            logger.LogInformation("Adapter Index = {}", adapterIndex);
            var adapter = GetAdapter(ref adapterIndex);

            AdapterIndex = adapterIndex;
            if (AdapterIndex < 0 || adapter == null)
            {
                throw new PlatformNotSupportedException("Graphic adapter does not meet minimum requirement, must support DirectX 10 or above.");
            }
#if DX11
            if (adapter != null)
            {
                DriverType = EnableSoftwareRendering ? DriverType.Warp : DriverType.Hardware;
                if (adapter.Description.VendorId == 0x1414 && adapter.Description.DeviceId == 0x8c)
                {
                    DriverType = DriverType.Warp;
                    device     = EnableSoftwareRendering ?
                                 new global::SharpDX.Direct3D11.Device(DriverType, DeviceCreationFlags.BgraSupport)
                        : new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport);
                }
                else
                {
                    if (DriverType == DriverType.Warp)
                    {
#if DEBUGMEMORY
                        device = new global::SharpDX.Direct3D11.Device(DriverType, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
#else
                        device = new global::SharpDX.Direct3D11.Device(DriverType, DeviceCreationFlags.BgraSupport);
#endif
                    }
                    else
                    {
#if DEBUGMEMORY
                        device = new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
#else
                        device = new global::SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport);
#endif
                        // DeviceCreationFlags.Debug should not be used in productive mode!
                        // See: http://sharpdx.org/forum/4-general/1774-how-to-debug-a-sharpdxexception
                        // See: http://stackoverflow.com/questions/19810462/launching-sharpdx-directx-app-with-devicecreationflags-debug
                    }
                }

#if DX11_1
                device1 = device.QueryInterface <global::SharpDX.Direct3D11.Device1>();
#endif
            }
#else
            device = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_1);
#endif
            logger.LogInformation("Direct3D device initilized. DriverType: {}; FeatureLevel: {}", DriverType, device.FeatureLevel);

            #region Initial Internal Pools
            logger.LogInformation("Initializing resource pools");
            RemoveAndDispose(ref constantBufferPool);
            constantBufferPool = new ConstantBufferPool(Device);

            RemoveAndDispose(ref shaderPoolManager);
            shaderPoolManager = new ShaderPoolManager(Device, constantBufferPool);

            RemoveAndDispose(ref statePoolManager);
            statePoolManager = new StatePoolManager(Device);

            RemoveAndDispose(ref geometryBufferManager);
            geometryBufferManager = new GeometryBufferManager(this);

            RemoveAndDispose(ref materialTextureManager);
            materialTextureManager = new TextureResourceManager(Device);

            RemoveAndDispose(ref materialVariableManager);
            materialVariableManager = new MaterialVariablePool(this);

            RemoveAndDispose(ref deviceContextPool);
            deviceContextPool = new DeviceContextPool(Device);

            RemoveAndDispose(ref structArrayPool);
            structArrayPool = new StructArrayPool();
            #endregion
            logger.LogInformation("Initializing Direct2D resources");
            factory2D          = new global::SharpDX.Direct2D1.Factory1(global::SharpDX.Direct2D1.FactoryType.MultiThreaded);
            wicImgFactory      = new global::SharpDX.WIC.ImagingFactory();
            directWriteFactory = new global::SharpDX.DirectWrite.Factory(global::SharpDX.DirectWrite.FactoryType.Shared);
            using (var dxgiDevice2 = device.QueryInterface <global::SharpDX.DXGI.Device>())
            {
                device2D        = new global::SharpDX.Direct2D1.Device(factory2D, dxgiDevice2);
                deviceContext2D = new global::SharpDX.Direct2D1.DeviceContext(device2D,
                                                                              global::SharpDX.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations);
            }
            Initialized = true;
        }
 protected override void OnDispose(bool disposeManagedResources)
 {
     commandList.Clear();
     deferredContextPool = null;
     base.OnDispose(disposeManagedResources);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DeferredContextRenderer"/> class.
 /// </summary>
 /// <param name="deviceResources">The deviceResources.</param>
 /// <param name="scheduler"></param>
 public DeferredContextRenderer(IDevice3DResources deviceResources, IRenderTaskScheduler scheduler) : base(deviceResources)
 {
     deferredContextPool = deviceResources.DeviceContextPool;
     this.scheduler      = scheduler;
 }
Пример #11
0
        private void RenderOthers(List <RenderCore> list, RenderType filter, IRenderContext context, IDeviceContextPool deviceContextPool,
                                  ref RenderParameter parameter,
                                  CommandList[] commandsArray, int idx)
        {
            var deviceContext = deviceContextPool.Get();

            SetRenderTargets(deviceContext, ref parameter);
            bool hasValue = false;

            for (int i = 0; i < list.Count; ++i)
            {
                if (list[i].RenderType == filter)
                {
                    list[i].Render(context, deviceContext);
                    hasValue = true;
                }
            }
            if (hasValue)
            {
                commandsArray[idx] = deviceContext.DeviceContext.FinishCommandList(true);
            }
            else
            {
                commandsArray[idx] = null;
            }
            deviceContextPool.Put(deviceContext);
        }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeferredContextRenderer"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="scheduler"></param>
 public DeferredContextRenderer(Device device, IRenderTaskScheduler scheduler) : base(device)
 {
     deferredContextPool = Collect(new DeviceContextPool(device));
     this.scheduler      = scheduler;
 }