Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPool"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 /// <param name="logger">The logger.</param>
 public ShaderPool(Device device, IConstantBufferPool cbPool, LogWrapper logger)
     : base(false)
 {
     ConstantBufferPool = cbPool;
     this.device        = device;
     this.logger        = logger;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPoolManager"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 /// <param name="logger"></param>
 public ShaderPoolManager(Device device, IConstantBufferPool cbPool, LogWrapper logger)
 {
     shaderPools[Constants.VertexIdx]   = Collect(new ShaderPool(device, cbPool, logger));
     shaderPools[Constants.DomainIdx]   = Collect(new ShaderPool(device, cbPool, logger));
     shaderPools[Constants.HullIdx]     = Collect(new ShaderPool(device, cbPool, logger));
     shaderPools[Constants.GeometryIdx] = Collect(new ShaderPool(device, cbPool, logger));
     shaderPools[Constants.PixelIdx]    = Collect(new ShaderPool(device, cbPool, logger));
     shaderPools[Constants.ComputeIdx]  = Collect(new ShaderPool(device, cbPool, logger));
     layoutPool = Collect(new LayoutPool(device, logger));
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPoolManager"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 public ShaderPoolManager(Device device, IConstantBufferPool cbPool)
 {
     shaderPools[Constants.VertexIdx]   = new ShaderPool(device, cbPool);
     shaderPools[Constants.DomainIdx]   = new ShaderPool(device, cbPool);
     shaderPools[Constants.HullIdx]     = new ShaderPool(device, cbPool);
     shaderPools[Constants.GeometryIdx] = new ShaderPool(device, cbPool);
     shaderPools[Constants.PixelIdx]    = new ShaderPool(device, cbPool);
     shaderPools[Constants.ComputeIdx]  = new ShaderPool(device, cbPool);
     layoutPool = new LayoutPool(device);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPoolManager"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 public ShaderPoolManager(Device device, IConstantBufferPool cbPool)
 {
     shaderPools[ShaderStage.Vertex.ToIndex()]   = Collect(new ShaderPool(device, cbPool));
     shaderPools[ShaderStage.Domain.ToIndex()]   = Collect(new ShaderPool(device, cbPool));
     shaderPools[ShaderStage.Hull.ToIndex()]     = Collect(new ShaderPool(device, cbPool));
     shaderPools[ShaderStage.Geometry.ToIndex()] = Collect(new ShaderPool(device, cbPool));
     shaderPools[ShaderStage.Pixel.ToIndex()]    = Collect(new ShaderPool(device, cbPool));
     shaderPools[ShaderStage.Compute.ToIndex()]  = Collect(new ShaderPool(device, cbPool));
     layoutPool = Collect(new LayoutPool(device));
 }
Exemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 public Light3DSceneShared(IConstantBufferPool pool)
 {
     buffer = pool.Register(DefaultBufferNames.LightCB, LightsBufferModel.SizeInBytes);
 }
Exemplo n.º 6
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;
        }
            /// <summary>
            /// Create Shader.
            /// <para>All constant buffers for all shaders are created here./></para>
            /// </summary>
            /// <param name="device"></param>
            /// <param name="pool"></param>
            /// <param name="logger"></param>
            /// <returns></returns>
            public ShaderBase CreateShader(Device device, IConstantBufferPool pool, LogWrapper logger)
            {
                if (ByteCode == null)
                {
                    return(null);
                }
                ShaderReflector = ShaderReflector ?? new ShaderReflector();
                ShaderReflector.Parse(ByteCode, ShaderType);
                Level = ShaderReflector.FeatureLevel;
                if (Level > device.FeatureLevel)
                {
                    logger?.Log(LogLevel.Warning, $"Shader {this.Name} requires FeatureLevel {Level}. Current device only supports FeatureLevel {device.FeatureLevel} and below.");
                    return(null);
                    //throw new Exception($"Shader {this.Name} requires FeatureLevel {Level}. Current device only supports FeatureLevel {device.FeatureLevel} and below.");
                }
                this.ConstantBufferMappings = ShaderReflector.ConstantBufferMappings.Values.ToArray();
                this.TextureMappings        = ShaderReflector.TextureMappings.Values.ToArray();
                this.UAVMappings            = ShaderReflector.UAVMappings.Values.ToArray();
                this.SamplerMappings        = ShaderReflector.SamplerMappings.Values.ToArray();

                ShaderBase shader = null;

                switch (ShaderType)
                {
                case ShaderStage.Vertex:
                    shader = new VertexShader(device, Name, ByteCode);
                    break;

                case ShaderStage.Pixel:
                    shader = new PixelShader(device, Name, ByteCode);
                    break;

                case ShaderStage.Compute:
                    shader = new ComputeShader(device, Name, ByteCode);
                    break;

                case ShaderStage.Domain:
                    shader = new DomainShader(device, Name, ByteCode);
                    break;

                case ShaderStage.Hull:
                    shader = new HullShader(device, Name, ByteCode);
                    break;

                case ShaderStage.Geometry:
                    if (IsGSStreamOut)
                    {
                        shader = new GeometryShader(device, Name, ByteCode, GSSOElement, GSSOStrides, GSSORasterized);
                    }
                    else
                    {
                        shader = new GeometryShader(device, Name, ByteCode);
                    }
                    break;

                default:
                    break;
                }
                if (ConstantBufferMappings != null)
                {
                    foreach (var mapping in ConstantBufferMappings)
                    {
                        shader.ConstantBufferMapping.AddMapping(mapping.Description.Name, mapping.Slot, pool.Register(mapping.Description));
                    }
                }
                if (TextureMappings != null)
                {
                    foreach (var mapping in TextureMappings)
                    {
                        shader.ShaderResourceViewMapping.AddMapping(mapping.Description.Name, mapping.Slot, mapping);
                    }
                }
                if (UAVMappings != null)
                {
                    foreach (var mapping in UAVMappings)
                    {
                        shader.UnorderedAccessViewMapping.AddMapping(mapping.Description.Name, mapping.Slot, mapping);
                    }
                }
                if (SamplerMappings != null)
                {
                    foreach (var mapping in SamplerMappings)
                    {
                        shader.SamplerMapping.AddMapping(mapping.Name, mapping.Slot, mapping);
                    }
                }
                return(shader);
            }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPool"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 /// <param name="logger">The logger.</param>
 public ShaderPool(Device device, IConstantBufferPool cbPool, LogWrapper logger)
     : base(device, logger)
 {
     ConstantBufferPool = cbPool;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShaderPool"/> class.
 /// </summary>
 /// <param name="device">The device.</param>
 /// <param name="cbPool">The cb pool.</param>
 public ShaderPool(Device device, IConstantBufferPool cbPool)
     : base(false)
 {
     ConstantBufferPool = cbPool;
     this.device        = device;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Create Shader.
        /// <para>All constant buffers for all shaders are created here./></para>
        /// </summary>
        /// <param name="device"></param>
        /// <param name="pool"></param>
        /// <returns></returns>
        public ShaderBase CreateShader(Device device, IConstantBufferPool pool)
        {
            if (ByteCode == null)
            {
                return(null);
            }
            if (shaderReflector != null)
            {
                shaderReflector.Parse(ByteCode, ShaderType);
                Level = shaderReflector.FeatureLevel;
                if (Level > device.FeatureLevel)
                {
                    throw new Exception($"Shader {this.Name} requires FeatureLevel {Level}. Current device only supports FeatureLevel {device.FeatureLevel} and below.");
                }
                this.ConstantBufferMappings = shaderReflector.ConstantBufferMappings.Values.ToArray();
                this.TextureMappings        = shaderReflector.TextureMappings.Values.ToArray();
                this.UAVMappings            = shaderReflector.UAVMappings.Values.ToArray();
                this.SamplerMappings        = shaderReflector.SamplerMappings.Values.ToArray();
            }
            ShaderBase shader = null;

            switch (ShaderType)
            {
            case ShaderStage.Vertex:
                shader = new VertexShader(device, Name, ByteCode);
                break;

            case ShaderStage.Pixel:
                shader = new PixelShader(device, Name, ByteCode);
                break;

            case ShaderStage.Compute:
                shader = new ComputeShader(device, Name, ByteCode);
                break;

            case ShaderStage.Domain:
                shader = new DomainShader(device, Name, ByteCode);
                break;

            case ShaderStage.Hull:
                shader = new HullShader(device, Name, ByteCode);
                break;

            case ShaderStage.Geometry:
                shader = new GeometryShader(device, Name, ByteCode);
                break;

            default:
                shader = NullShader.GetNullShader(ShaderType);
                break;
            }
            if (ConstantBufferMappings != null)
            {
                foreach (var mapping in ConstantBufferMappings)
                {
                    shader.ConstantBufferMapping.AddMapping(mapping.Description.Name, mapping.Slot, pool.Register(mapping.Description));
                }
            }
            if (TextureMappings != null)
            {
                foreach (var mapping in TextureMappings)
                {
                    shader.ShaderResourceViewMapping.AddMapping(mapping.Description.Name, mapping.Slot, mapping);
                }
            }
            if (UAVMappings != null)
            {
                foreach (var mapping in UAVMappings)
                {
                    shader.UnorderedAccessViewMapping.AddMapping(mapping.Description.Name, mapping.Slot, mapping);
                }
            }
            if (SamplerMappings != null)
            {
                foreach (var mapping in SamplerMappings)
                {
                    shader.SamplerMapping.AddMapping(mapping.Name, mapping.Slot, mapping);
                }
            }
            return(shader);
        }
Exemplo n.º 11
0
 public ShaderPool(Device device, IConstantBufferPool cbPool)
     : base(device)
 {
     ConstantBufferPool = cbPool;
 }
Exemplo n.º 12
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;
        }