⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxmutenum.cs

📁 运用directX完成的坦克游戏雏形
💻 CS
📖 第 1 页 / 共 3 页
字号:
//--------------------------------------------------------------------------------------
// File: DXMUTEnum.cs
//
// Enumerates D3D adapters, devices, modes, etc.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
using System;
using System.Collections;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Microsoft.Samples.DirectX.UtilityToolkit
{
    /// <summary>
    /// Enumerates available Direct3D adapters, devices, modes, etc.  Singleton.
    /// </summary>
    public sealed class Enumeration
    {
        #region Creation (Not allowed)
        private Enumeration() {} // Do Not allow Creation
        /// <summary>
        /// Static constructor to create default lists
        /// </summary>
        static Enumeration()
        {
            // Create the default lists
            ResetPossibleDepthStencilFormats();
            ResetPossibleMultisampleTypeList();
            ResetPossiblePresentIntervalList();
        } 
        #endregion

        // Static Data
        private static bool isPostPixelShaderBlendingRequired = true;
        private static IDeviceCreation deviceCreationInterface = null;

        // Vertex processing
        private static bool isSoftwareVertexProcessing = true;
        private static bool isHardwareVertexProcessing = true;
        private static bool isPureHardwareVertexProcessing = true;
        private static bool isMixedVertexProcessing = false;

        // Limits
        private static uint minimumWidth = 0;
        private static uint maximumWidth = uint.MaxValue;
        private static uint minimumHeight = 0;
        private static uint maximumHeight = uint.MaxValue;
        private static uint minimumRefresh = 0;
        private static uint maximumRefresh = uint.MaxValue;
        private static uint multisampleQualityMax = 0xffff;

        // Lists
        private static ArrayList depthStencilPossibleList = new ArrayList();
        private static ArrayList multiSampleTypeList = new ArrayList();
        private static ArrayList presentIntervalList = new ArrayList();
        private static ArrayList adapterInformationList = new ArrayList();

        // Default arrays
        private static readonly Format[] allowedFormats = new Format[] {
                        Format.X8R8G8B8, 
                        Format.X1R5G5B5,
                        Format.R5G6B5,
                        Format.A2R10G10B10 };
        private static readonly Format[] backbufferFormatsArray = new Format[] {
                        Format.A8R8G8B8,
                        Format.X8R8G8B8, 
                        Format.A1R5G5B5,
                        Format.X1R5G5B5,
                        Format.R5G6B5,
                        Format.A2R10G10B10 };
        private static readonly DeviceType[] deviceTypeArray = new DeviceType[] {
                        DeviceType.Hardware,
                        DeviceType.Software,
                        DeviceType.Reference };


        // Implementation

        /// <summary>
        /// Enumerates available D3D adapters, devices, modes, etc
        /// </summary>
        public static void Enumerate(IDeviceCreation acceptableCallback)
        {
            DisplayModeSorter sorter = new DisplayModeSorter();
            try
            {
                // Store the callback
                deviceCreationInterface = acceptableCallback;

                // Clear the adapter information stored currently
                adapterInformationList.Clear();
                ArrayList adapterFormatList = new ArrayList();

                // Look through every adapter on the system
                foreach(AdapterInformation ai in Manager.Adapters)
                {
                    EnumAdapterInformation adapterInfo = new EnumAdapterInformation();
                    // Store some information
                    adapterInfo.AdapterOrdinal = (uint)ai.Adapter; // Ordinal
                    adapterInfo.AdapterInformation = ai.Information; // Information

                    // Get list of all display modes on this adapter.  
                    // Also build a temporary list of all display adapter formats.
                    adapterFormatList.Clear();

                    // Now check to see which formats are supported
                    for(int i = 0; i < allowedFormats.Length; i++)
                    {
                        // Check each of the supported display modes for this format
                        foreach(DisplayMode dm in ai.SupportedDisplayModes[allowedFormats[i]])
                        {
                            if ( (dm.Width < minimumWidth) ||
                                (dm.Height < minimumHeight) ||
                                (dm.Width > maximumWidth) ||
                                (dm.Height > maximumHeight) ||
                                (dm.RefreshRate < minimumRefresh) ||
                                (dm.RefreshRate > maximumRefresh) )
                            {
                                continue; // This format isn't valid
                            }

                            // Add this to the list
                            adapterInfo.displayModeList.Add(dm);

                            // Add this to the format list if it doesn't already exist
                            if (!adapterFormatList.Contains(dm.Format))
                            {
                                adapterFormatList.Add(dm.Format);
                            }
                        }
                    }

                    // Get the adapter display mode
                    DisplayMode currentAdapterMode = ai.CurrentDisplayMode;
                    // Check to see if this format is in the list
                    if (!adapterFormatList.Contains(currentAdapterMode.Format))
                    {
                        adapterFormatList.Add(currentAdapterMode.Format);
                    }

                    // Sort the display mode list
                    adapterInfo.displayModeList.Sort(sorter);

                    // Get information for each device with this adapter
                    EnumerateDevices(adapterInfo, adapterFormatList);

                    // If there was at least one device on the adapter, and it's compatible
                    // add it to the list
                    if (adapterInfo.deviceInfoList.Count > 0)
                    {
                        adapterInformationList.Add(adapterInfo);
                    }
                }

                // See if all of the descriptions are unique
                bool isUnique = true;
                for(int i = 0; i < adapterInformationList.Count; i++)
                {
                    for (int j = i+1; j < adapterInformationList.Count; j++)
                    {
                        EnumAdapterInformation eai1 = adapterInformationList[i] as EnumAdapterInformation;
                        EnumAdapterInformation eai2 = adapterInformationList[j] as EnumAdapterInformation;

                        if (string.Compare(eai1.AdapterInformation.Description,
                            eai2.AdapterInformation.Description, true) == 0)
                        {
                            isUnique = false;
                            break;
                        }
                    }
                    if (!isUnique)
                        break;
                }

                // Now fill the unique description
                for(int i = 0; i < adapterInformationList.Count; i++)
                {
                    EnumAdapterInformation eai1 = adapterInformationList[i] as EnumAdapterInformation;

                    eai1.UniqueDescription = eai1.AdapterInformation.Description;
                    // If the descriptions aren't unique, append the adapter ordinal
                    if (!isUnique)
                        eai1.UniqueDescription += string.Format(" (#{0})", eai1.AdapterOrdinal);
                }
            }
            catch (TypeLoadException)
            {
                // Couldn't load the manager class, no Direct is available.
                throw new NoDirect3DException();
            }
        }

        /// <summary>
        /// Enumerates D3D devices for a particular adapter.
        /// </summary>
        private static void EnumerateDevices(EnumAdapterInformation adapterInfo, ArrayList adapterFormatList)
        {
            // Ignore any exceptions while looking for these device types
            DirectXException.IgnoreExceptions();
            // Enumerate each Direct3D device type
            for(uint i = 0; i < deviceTypeArray.Length; i++)
            {
                // Create a new device information object
                EnumDeviceInformation deviceInfo = new EnumDeviceInformation();

                // Store the type
                deviceInfo.DeviceType = deviceTypeArray[i];

                // Try to get the capabilities
                deviceInfo.Caps = Manager.GetDeviceCaps((int)adapterInfo.AdapterOrdinal, deviceInfo.DeviceType);

                // Get information about each device combination on this device
                EnumerateDeviceCombos( adapterInfo, deviceInfo, adapterFormatList);

                // Do we have any device combinations?
                if (deviceInfo.deviceSettingsList.Count > 0)
                {
                    // Yes, add it
                    adapterInfo.deviceInfoList.Add(deviceInfo);
                }
            }
            // Turn exception handling back on
            DirectXException.EnableExceptions();
        }

        /// <summary>
        /// Enumerates device combinations for a particular device.
        /// </summary>
        private static void EnumerateDeviceCombos(EnumAdapterInformation adapterInfo, EnumDeviceInformation deviceInfo, 
            ArrayList adapterFormatList)
        {
            // Find out which adapter formats are supported by this device
            foreach(Format adapterFormat in adapterFormatList)
            {
                for(int i = 0; i < backbufferFormatsArray.Length; i++)
                {
                    // Go through each windowed mode
                    for (int windowedIndex = 0; windowedIndex < 2; windowedIndex++)
                    {
                        bool isWindowedIndex = (windowedIndex == 1);
                        if ((!isWindowedIndex) && (adapterInfo.displayModeList.Count == 0))
                            continue; // Nothing here

                        if (!Manager.CheckDeviceType((int)adapterInfo.AdapterOrdinal, deviceInfo.DeviceType,
                            adapterFormat, backbufferFormatsArray[i], isWindowedIndex))
                            continue; // Unsupported

                        // Do we require post pixel shader blending?
                        if (isPostPixelShaderBlendingRequired)
                        {
                            // If the backbuffer format doesn't support Usage.QueryPostPixelShaderBlending
                            // then alpha test, pixel fog, render-target blending, color write enable, and dithering
                            // are not supported.
                            if (!Manager.CheckDeviceFormat((int)adapterInfo.AdapterOrdinal, deviceInfo.DeviceType,
                                    adapterFormat, Usage.QueryPostPixelShaderBlending, 
                                    ResourceType.Textures, backbufferFormatsArray[i]))
                                continue; // Unsupported
                        }

                        // If an application callback function has been provided, make sure this device
                        // is acceptable to the app.
                        if (deviceCreationInterface != null)
                        {
                            if (!deviceCreationInterface.IsDeviceAcceptable(deviceInfo.Caps, 
                                adapterFormat, backbufferFormatsArray[i],isWindowedIndex))
                                continue; // Application doesn't like this device
                        }

                        // At this point, we have an adapter/device/adapterformat/backbufferformat/iswindowed
                        // DeviceCombo that is supported by the system and acceptable to the app. We still 
                        // need to find one or more suitable depth/stencil buffer format,
                        // multisample type, and present interval.

                        EnumDeviceSettingsCombo deviceCombo = new EnumDeviceSettingsCombo();

                        // Store the information
                        deviceCombo.AdapterOrdinal = adapterInfo.AdapterOrdinal;
                        deviceCombo.DeviceType = deviceInfo.DeviceType;
                        deviceCombo.AdapterFormat = adapterFormat;
                        deviceCombo.BackBufferFormat = backbufferFormatsArray[i];
                        deviceCombo.IsWindowed = isWindowedIndex;

                        // Build the depth stencil format and multisample type list
                        BuildDepthStencilFormatList(deviceCombo);
                        BuildMultiSampleTypeList(deviceCombo);
                        if (deviceCombo.multiSampleTypeList.Count == 0)
                        {
                            // Nothing to do
                            continue;
                        }
                        // Build the conflict and present lists

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -