Пример #1
0
 static private void InitDebugOutput()
 {
     if (VRage.MyCompilationSymbols.DX11Debug && VRage.MyCompilationSymbols.DX11DebugOutput)
     {
         DebugInfoQueue = Device.QueryInterface <InfoQueue>();
         DebugInfoQueue.SetBreakOnSeverity(MessageSeverity.Corruption, true);
         DebugInfoQueue.SetBreakOnSeverity(MessageSeverity.Error, true);
         DebugInfoQueue.MessageCountLimit = 4096;
         DebugInfoQueue.ClearStorageFilter();
     }
 }
Пример #2
0
 static private void InitDebugOutput()
 {
     if (VRage.MyCompilationSymbols.DX11Debug && VRage.MyCompilationSymbols.DX11DebugOutput)
     {
         DebugInfoQueue = Device.QueryInterface <InfoQueue>();
         DebugInfoQueue.SetBreakOnSeverity(MessageSeverity.Corruption, true);
         DebugInfoQueue.SetBreakOnSeverity(MessageSeverity.Error, true);
         DebugInfoQueue.MessageCountLimit = 4096;
         DebugInfoQueue.ClearStorageFilter();
         if (!VRage.MyCompilationSymbols.DX11DebugOutputEnableInfo)
         {
             InfoQueueFilter filter = new InfoQueueFilter();
             filter.DenyList               = new InfoQueueFilterDescription();
             filter.DenyList.Severities    = new MessageSeverity[1];
             filter.DenyList.Severities[0] = MessageSeverity.Information;
             DebugInfoQueue.AddStorageFilterEntries(filter);
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="breakOnWarning">Generate an error on warning</param>
        public SharpDebugger(SharpDevice device, bool breakOnWarning)
        {
            _device = device;
            //init the debug device
            Debug = new DeviceDebug(device.Device);
            //init the queue interface
            Queue = Debug.QueryInterface <InfoQueue>();

            if (breakOnWarning)
            {
                Queue.SetBreakOnSeverity(MessageSeverity.Warning, true);
            }
        }
Пример #4
0
        public Dx11RenderingDevice()
        {
            logger.Info("Dx11 rendering device creating.");
#if DEBUG
            const DeviceCreationFlags DebugFlags = DeviceCreationFlags.Debug;
#else
            const DeviceCreationFlags DebugFlags = DeviceCreationFlags.None;
#endif
            try
            {
                Factory = new Factory();
                if (Factory.Adapters.Count() == 0)
                {
                    MessageBox.Show("Your system have no video adapters. Try to install video adapter.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw new Exception("There are no valid video adapters in system.");
                }

                var adapter = Factory.GetAdapter(0);
                if (adapter == null)
                {
                    MessageBox.Show("DirectX wasn't able to acquire video adapter. Try to restart your system.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw new Exception("DirectX wasn't able to acquire video adapter.");
                }

                if (adapter.Outputs == null || adapter.Outputs.Count() == 0)
                {
                    MessageBox.Show("There are no video displays connected to your system. Try to connect a display.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw new Exception("No connected displays found.");
                }

                Device = new Device(adapter, DebugFlags | DeviceCreationFlags.SingleThreaded, FeatureLevel.Level_10_0);
            }
            catch (Exception exc)
            {
                switch ((uint)exc.HResult)
                {
                case 0x887A0004:
                    MessageBox.Show("Your DirectX version, videocard or drivers are out of date.\nDirectX 11 installation and videocard with DirectX 10 support is required.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case 0x887A002D:
                    MessageBox.Show("Warning: provided build is a debug build.\nPlease install DirectX SDK or request release build from QA team.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case 0x887A0005:
                case 0x887A0020:
                    MessageBox.Show("There was a serious video system error while initializing Direct3D device.\nTry to restart your system.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                default:
                    MessageBox.Show("Unknown error while creating Direct3D device!\nShutting down now.", "DirectX error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }

                throw new Exception("Can't create Direct3D 11 device! Exception: " + exc);
            }

#if DEBUG
            using (InfoQueue DeviceInfoQueue = Device.QueryInterface <InfoQueue>())
            {
                DeviceInfoQueue.SetBreakOnSeverity(MessageSeverity.Warning, true);
            }
#endif

            try
            {
                Context = Device.ImmediateContext;

                /*TestShader = new Dx11PipelineState(this, "TestShader", new InputElement[]
                 * {
                 * new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                 * new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 0, 1, InputClassification.PerVertexData, 0)
                 * });*/
                TextShader = new Dx11PipelineState(this, "TextShader", new InputElement[]
                {
                    new InputElement("POSITION", 0, Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0),
                    new InputElement("UVW", 0, Format.R32G32_UInt, 0, 1, InputClassification.PerVertexData, 0)
                });
                SpriteShader = new Dx11PipelineState(this, "SpriteShader", new InputElement[]
                {
                    new InputElement("POSITION", 0, Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0),
                    new InputElement("UVW", 0, Format.R32G32_UInt, 0, 1, InputClassification.PerVertexData, 0)
                });
                RoomShader = new Dx11PipelineState(this, "RoomShader", new InputElement[]
                {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                    new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 0, 1, InputClassification.PerVertexData, 0),
                    new InputElement("OVERLAY", 0, Format.R8G8B8A8_UNorm, 0, 2, InputClassification.PerVertexData, 0),
                    new InputElement("UVWANDBLENDMODE", 0, Format.R32G32_UInt, 0, 3, InputClassification.PerVertexData, 0),
                    new InputElement("EDITORUVANDSECTORTEXTURE", 0, Format.R32_UInt, 0, 4, InputClassification.PerVertexData, 0)
                });
                RasterizerBackCulling = new RasterizerState(Device, new RasterizerStateDescription
                {
                    CullMode = CullMode.Back,
                    FillMode = FillMode.Solid,
                });
                SamplerDefault = new SamplerState(Device, new SamplerStateDescription
                {
                    AddressU          = TextureAddressMode.Mirror,
                    AddressV          = TextureAddressMode.Mirror,
                    AddressW          = TextureAddressMode.Wrap,
                    Filter            = Filter.Anisotropic,
                    MaximumAnisotropy = 4,
                });
                SamplerRoundToNearest = new SamplerState(Device, new SamplerStateDescription
                {
                    AddressU          = TextureAddressMode.Wrap,
                    AddressV          = TextureAddressMode.Wrap,
                    AddressW          = TextureAddressMode.Wrap,
                    Filter            = Filter.MinMagMipPoint,
                    MaximumAnisotropy = 4,
                });
                {
                    DepthStencilStateDescription desc = DepthStencilStateDescription.Default();
                    desc.DepthComparison  = Comparison.LessEqual;
                    desc.DepthWriteMask   = DepthWriteMask.All;
                    desc.IsDepthEnabled   = true;
                    desc.IsStencilEnabled = false;
                    DepthStencilDefault   = new DepthStencilState(Device, desc);
                }
                {
                    DepthStencilStateDescription desc = DepthStencilStateDescription.Default();
                    desc.DepthComparison  = Comparison.Always;
                    desc.DepthWriteMask   = DepthWriteMask.Zero;
                    desc.IsDepthEnabled   = false;
                    desc.IsStencilEnabled = false;
                    DepthStencilNoZBuffer = new DepthStencilState(Device, desc);
                }
                BlendingDisabled = new BlendState(Device, BlendStateDescription.Default());
                {
                    BlendStateDescription desc = BlendStateDescription.Default();
                    desc.RenderTarget[0].IsBlendEnabled        = true;
                    desc.RenderTarget[0].SourceBlend           = desc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
                    desc.RenderTarget[0].DestinationBlend      = desc.RenderTarget[0].DestinationAlphaBlend = BlendOption.InverseSourceAlpha;
                    desc.RenderTarget[0].BlendOperation        = desc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
                    desc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                    BlendingPremultipliedAlpha = new BlendState(Device, desc);
                }
            }
            catch (Exception exc)
            {
                throw new Exception("Can't assign needed Direct3D parameters! Exception: " + exc);
            }

            // Sector textures
            bool       support16BitTexture = Device.CheckFormatSupport(Format.B5G5R5A1_UNorm).HasFlag(FormatSupport.Texture2D); // For some reason not all DirectX devices support 16 bit textures.
            string[]   sectorTextureNames  = Enum.GetNames(typeof(SectorTexture)).Skip(1).ToArray();
            GCHandle[] handles             = new GCHandle[sectorTextureNames.Length];
            try
            {
                DataBox[] dataBoxes = new DataBox[sectorTextureNames.Length];
                for (int i = 0; i < sectorTextureNames.Length; ++i)
                {
                    string name = nameof(TombLib) + "." + nameof(Rendering) + ".SectorTextures." + sectorTextureNames[i] + ".png";
                    using (Stream stream = ThisAssembly.GetManifestResourceStream(name))
                    {
                        ImageC image = ImageC.FromStream(stream);
                        if ((image.Width != SectorTextureSize) || (image.Height != SectorTextureSize))
                        {
                            throw new ArgumentOutOfRangeException("The embedded resource '" + name + "' is not of a valid size.");
                        }

                        if (support16BitTexture)
                        { // Compress image data into B5G5R5A1 format to save a bit of GPU memory. (3 MB saved with currently 23 images)
                            ushort[] sectorTextureData = new ushort[SectorTextureSize * SectorTextureSize];
                            for (int j = 0; j < (SectorTextureSize * SectorTextureSize); ++j)
                            {
                                ColorC Color = image.Get(j);
                                sectorTextureData[j] = (ushort)(
                                    ((Color.B >> 3) << 0) |
                                    ((Color.G >> 3) << 5) |
                                    ((Color.R >> 3) << 10) |
                                    ((Color.A >> 7) << 15));
                            }
                            handles[i]   = GCHandle.Alloc(sectorTextureData, GCHandleType.Pinned);
                            dataBoxes[i] = new DataBox(handles[i].AddrOfPinnedObject(), sizeof(ushort) * SectorTextureSize, 0);
                        }
                        else
                        {
                            handles[i]   = GCHandle.Alloc(image.ToByteArray(), GCHandleType.Pinned);
                            dataBoxes[i] = new DataBox(handles[i].AddrOfPinnedObject(), sizeof(uint) * SectorTextureSize, 0);
                        }
                    }
                }

                SectorTextureArray = new Texture2D(Device, new Texture2DDescription
                {
                    Width             = SectorTextureSize,
                    Height            = SectorTextureSize,
                    MipLevels         = 1,
                    ArraySize         = sectorTextureNames.Length,
                    Format            = support16BitTexture ? Format.B5G5R5A1_UNorm : Format.B8G8R8A8_UNorm,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage             = ResourceUsage.Immutable,
                    BindFlags         = BindFlags.ShaderResource,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    OptionFlags       = ResourceOptionFlags.None
                }, dataBoxes);
            }
            finally
            {
                foreach (GCHandle handle in handles)
                {
                    handle.Free();
                }
            }
            SectorTextureArrayView = new ShaderResourceView(Device, SectorTextureArray);

            // Set omni present state
            ResetState();

            logger.Info("Dx11 rendering device created.");
        }