/// <summary> /// Initializes a new instance of the <see cref="SpriteBatch" /> class. /// </summary> /// <param name="graphicsDevice"> The graphics device. </param> /// <param name="center"> (Optional) True to center the coordinate system in the viewport. </param> /// <param name="sortAlgorithm"> (Optional) The sort algorithm. </param> /// <exception cref="ArgumentException"> /// Thrown when one or more arguments have unsupported or /// illegal values. /// </exception> /// <exception cref="NullReferenceException"> Thrown when a value was unexpectedly null. </exception> public SpriteBatch(IGraphicsDevice graphicsDevice, bool center = false, SpriteSortAlgorithm sortAlgorithm = SpriteSortAlgorithm.MergeSort) { _device = graphicsDevice.Device; _context = graphicsDevice.DeviceContext; _center = center; _spriteSort = sortAlgorithm switch { SpriteSortAlgorithm.MergeSort => new SpriteMergeSort(), _ => throw new ArgumentException($"invalid sort algorithm ({sortAlgorithm})", nameof(sortAlgorithm)) }; _defaultBlendState = graphicsDevice.BlendStates.AlphaBlend; _defaultSamplerState = graphicsDevice.SamplerStates.LinearWrap; _defaultDepthStencilState = graphicsDevice.DepthStencilStates.None; _defaultRasterizerState = graphicsDevice.RasterizerStates.CullBackDepthClipOff; _defaultRasterizerScissorEnabledState = graphicsDevice.RasterizerStates.CullBackDepthClipOffScissorEnabled; _whiteTexture = graphicsDevice.Textures.White; _indexBuffer = IndexBuffer.Create(graphicsDevice, s_indices); Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{Shaders.POSITION_COLOR_TEXTURE}") ?? throw new NullReferenceException($"{assembly.GetName().Name}.{Shaders.POSITION_COLOR_TEXTURE}")) { Shader.Shader.Group group = (_shader = ShaderFileLoader.FromStream(graphicsDevice, stream) ?? throw new NullReferenceException(nameof(ShaderFileLoader.FromStream)))["DEFAULT"];
public void Map <T>(DeviceContext4 context4, T[] data, MapMode mapMode = MapMode.WriteDiscard, MapFlags mapFlags = MapFlags.None) where T : unmanaged { Map(context4, 0, data, 0, data.Length, mapMode, mapFlags); }
public unsafe void Map <T>(DeviceContext4 context4, int offset, T[] data, int dataOffset, int dataLength, MapMode mapMode = MapMode.WriteDiscard, MapFlags mapFlags = MapFlags.None) where T : unmanaged { DataBox box = context4.MapSubresource(_buffer, 0, mapMode, mapFlags); T * vpctPtr = (T *)box.DataPointer; for (int i = 0; i < dataLength; i++) { *(vpctPtr + offset + i) = data[i + dataOffset]; } context4.UnmapSubresource(_buffer, 0); }
/// <summary> /// Initializes a new instance of the <see cref="Canvas" /> class. /// </summary> /// <param name="graphicsDevice"> The graphics device. </param> /// <exception cref="NullReferenceException"> Thrown when a value was unexpectedly null. </exception> public Canvas(IGraphicsDevice graphicsDevice) { _context = graphicsDevice.DeviceContext; _defaultBlendState = graphicsDevice.BlendStates.AlphaBlend; _defaultSamplerState = graphicsDevice.SamplerStates.LinearWrap; _defaultDepthStencilState = graphicsDevice.DepthStencilStates.None; _defaultRasterizerState = graphicsDevice.RasterizerStates.CullBackDepthClipOff; _defaultRasterizerScissorEnabledState = graphicsDevice.RasterizerStates.CullBackDepthClipOffScissorEnabled; _indexBuffer = IndexBuffer.Create(graphicsDevice, s_indices); Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{Shaders.CANVAS}") ?? throw new NullReferenceException($"{assembly.GetName().Name}.{Shaders.CANVAS}")) { Shader.Shader.Group group = (_shader = ShaderFileLoader.FromStream(graphicsDevice, stream) ?? throw new NullReferenceException(nameof(ShaderFileLoader.FromStream)))["DEFAULT"];
/// <summary> /// Initializes the <see cref="GraphicsDevice" />. /// </summary> /// <param name="parameters"> [in,out] Options for controlling the operation. </param> public void Initialize(ref GameGraphicsParameters parameters) { if (IsInitialized) { return; } _vSync = parameters.UseVSync ? 1 : 0; using (Factory4 factory4 = new Factory4()) { _dxgiFactory = factory4.QueryInterface <Factory5>(); } ModeDescription modeDescription = new ModeDescription( parameters.Width, parameters.Height, parameters.Rational, parameters.Format); if (parameters.AdapterLuid != -1 && parameters.OutputIndex != -1) { using (Adapter adapter = _dxgiFactory.GetAdapterByLuid(parameters.AdapterLuid)) using (Output output = adapter.GetOutput(parameters.OutputIndex)) { _adapter4 = adapter.QueryInterface <Adapter4>(); _output6 = output.QueryInterface <Output6>(); _output6.GetClosestMatchingMode( null, modeDescription, out modeDescription); } } else { IEnumerable <Adapter> adapters = _dxgiFactory.Adapters1 .Where( a => (a.Description1.Flags & AdapterFlags.Software) != AdapterFlags.Software && a.GetOutputCount() > 0) .Select( a => (adapter: a, featureLevel: SharpDX.Direct3D11.Device.GetSupportedFeatureLevel(a))) .GroupBy(t => t.featureLevel) .OrderByDescending(t => t.Key) .First() .Select(k => k.adapter); _adapter4 = null; foreach (Adapter adapter in adapters) { using (adapter) { for (int o = 0; o < adapter.GetOutputCount(); o++) { using (Output output = adapter.GetOutput(o)) { output.GetClosestMatchingMode( null, modeDescription, out ModeDescription desc); if (_adapter4 == null || desc.RefreshRate.Numerator / desc.RefreshRate.Denominator > modeDescription.RefreshRate.Numerator / modeDescription.RefreshRate.Denominator) { modeDescription = desc; Interlocked.Exchange(ref _output6, output.QueryInterface <Output6>())?.Dispose(); Interlocked.Exchange(ref _adapter4, adapter.QueryInterface <Adapter4>())?.Dispose(); } } } } } } // ReSharper disable once VariableHidesOuterVariable Device CreateDevice(in GameGraphicsParameters parameters) { if (_adapter4 != null) { Console.WriteLine("------- GRAPHIC CARD INFORMATION -------"); Console.WriteLine($"Luid:\t\t\t{_adapter4.Description2.Luid}"); Console.WriteLine( $"Description:\t\t{_adapter4.Description2.Description.TrimEnd('\t', ' ', '\r', '\n', (char)0)}"); Console.WriteLine($"DeviceId:\t\t{_adapter4.Description2.DeviceId}"); Console.WriteLine($"VendorId:\t\t{_adapter4.Description2.VendorId}"); Console.WriteLine($"Revision:\t\t{_adapter4.Description2.Revision}"); Console.WriteLine($"SubsystemId:\t\t{_adapter4.Description2.SubsystemId}"); #if x64 Console.WriteLine(); Console.WriteLine($"SystemMemory:\t\t{_adapter4.Description2.DedicatedSystemMemory}"); Console.WriteLine($"VideoMemory:\t\t{_adapter4.Description2.DedicatedVideoMemory}"); Console.WriteLine($"SharedSystemMemory:\t{_adapter4.Description2.SharedSystemMemory}"); #endif Console.WriteLine(); Console.WriteLine($"Format:\t\t\t{modeDescription.Format}"); Console.WriteLine($"Width x Height:\t\t{modeDescription.Width} x {modeDescription.Height}"); Console.WriteLine( $"RefreshRate:\t\t{modeDescription.RefreshRate} ({modeDescription.RefreshRate.Numerator / modeDescription.RefreshRate.Denominator}HZ)"); Console.WriteLine($"Scaling:\t\t{modeDescription.Scaling}"); Console.WriteLine($"ScanlineOrdering:\t{modeDescription.ScanlineOrdering}"); Console.WriteLine(); Console.WriteLine($"DeviceName:\t\t{_output6.Description.DeviceName}"); Console.WriteLine( $"DesktopBounds:\t\t{_output6.Description.DesktopBounds.Left};{_output6.Description.DesktopBounds.Top};{_output6.Description.DesktopBounds.Right};{_output6.Description.DesktopBounds.Bottom}"); Console.WriteLine($"MonitorHandle:\t\t{_output6.Description.MonitorHandle}"); Console.WriteLine($"IsAttachedToDesktop:\t{_output6.Description.IsAttachedToDesktop}"); Console.WriteLine($"Rotation:\t\t{_output6.Description.Rotation}"); Console.WriteLine("----------------------------------------\n"); return(new Device(_adapter4, parameters.DeviceCreationFlags, s_featureLevels)); } return(new Device(parameters.DriverType, parameters.DeviceCreationFlags, s_featureLevels)); } using (Device defaultDevice = CreateDevice(in parameters)) { _d3DDevice5 = defaultDevice.QueryInterface <Device5>(); } _d3DDeviceContext = _d3DDevice5.ImmediateContext3.QueryInterface <DeviceContext4>(); _dxgiDevice4 = _d3DDevice5.QueryInterface <Device4>(); SampleDescription sampleDescription = new SampleDescription(1, 0); if (parameters.EnableMultiSampling && parameters.MultiSampleCount != MultiSampleCount.None) { sampleDescription.Count = (int)parameters.MultiSampleCount; sampleDescription.Quality = Math.Max( _d3DDevice5.CheckMultisampleQualityLevels( modeDescription.Format, sampleDescription.Count) - 1, 0); } SwapChainDescription swapChainDescription = new SwapChainDescription { BufferCount = parameters.BufferCount, ModeDescription = modeDescription, IsWindowed = true, OutputHandle = parameters.Handle, SampleDescription = sampleDescription, SwapEffect = parameters.SwapEffect, Usage = parameters.Usage, Flags = parameters.SwapChainFlags }; using (SwapChain swapChain = new SwapChain(_dxgiFactory, _d3DDevice5, swapChainDescription)) { _swapChain4 = swapChain.QueryInterface <SwapChain4>(); } _dxgiFactory.MakeWindowAssociation(parameters.Handle, parameters.WindowAssociationFlags); _swapChain4.ResizeTarget(ref modeDescription); SetFullscreenState(parameters.DisplayType == DisplayType.Fullscreen); _resizeParameters = new ResizeParameters { BufferCount = parameters.BufferCount, Width = parameters.Width, Height = parameters.Height, SwapChainFlags = parameters.SwapChainFlags }; Resize(_resizeParameters); _blendStates = new BlendStates(this); _depthStencilStates = new DepthStencilStates(this); _rasterizerStates = new RasterizerStates(this); _samplerStates = new SamplerStates(this); _textures = new Textures(this); IsInitialized = true; }