Managed DirectX9 (Chapter 2)

xiaoxiao2021-03-06  50

Choose the correct Device

Translation: Clayman The Number of Possible Permutations WHEN CRETION A Device Is Quite Stagger. Today, there is a large number of different types of display cards, remember that the features supported by each graphics are almost impossible. You should ask Device and let it tell you the features you support. We will discuss next:

Enumerate all adapters in the system

Enumerate the format supported by each device

Determine the listed device function

Enumerate the adapter in the system

Most of the systems today support multi-display. Although this is not mainstream configuration, the multi-display is really useful, and it is increasingly popular. In the past, this is a high-end graphics card proprietary function. But now ATI, NVIDIA, and Matrox have supported multiple displays to share a multi-screen display technology for graphics cards.

Direct3D Device must specify to each adapter. Here, you can understand the "adapter" as a graphics card that links a specific display. For example, the graphics card of the ATI Radeon 9700 is just a physical adapter, but it has two display interfaces (DVI and VGA), so in Direct3D, it has two adapters. Maybe you don't know which one is choosing, or even how many devices is running in the system running, how do you detect them and choose the correct one?

In Direct3D, a static class called Manager can complete the above tasks: enumerate the information of the adapter and Device; get information that devices supported in the system.

The most important attribute of the Manager class is the list of adapters. This property will be used in many places. It has a "count" member stores the number of adapters in the system. Therefore, you can directly access the adapter (E.G. Manager.Adapters [0]) or enumerate all adapters in the system.

Test this feature with a simple program that will display the adapter in the system in a tree structure, and the display modes they support:

1. Create a new C # Windows Form project;

2. Add DirectX components;

3. Create a TreeView control and account for the full window: Set the Dock property to Fill

Ok, now add to each adapter, display the function of each display mode supported:

Public void loadinggraphics ()

{

Foreach (AdapterInformation Ai In Manager.Adapters)

{

Treenode root = new treenode (ai.information.description);

Treenode Driverinfo = New Treenode (Striver Information: {0} - {1} ", ai.information.drivername, ai.information.driververs);

Root.Node.Add (DriverInfo);

treeNode displayMode - new TreeNodeJ (string.Format ( "Vurrent Display Mode: {0} × {1} × {2}", ai.CurrentDisplayMode.Width, ai.CurrentDisplayMOde.Height, ai.CurrentDisplayuMode.Format));

Foreach (DisplayMode DM in ai.supportedDisplayModes) {

Treenode SupportedNode = New Treenode (String.Format ("Supported: {0} × {1} × {2}", DM.WIDTH, DM.HEIGHT, DM.FORMAT);

DisplayMode.Nodes.Add (SupportedNode);

}

Root.Nodes.Add (DisplayMode);

TreeView1.Node.Add (root);

}

}

Although the code looks little, it is actually very simple. You can see what we have made first. First, we enumerate the adapter in the system. The Foreach iterator of the C # makes this process easy. For each adapter, this loop is only once and the AdapterInformation structure is populated with a given adapter. Observe the AdapterInformation structure, have the following members

Public struct AdapterInformation

{

Int adapter;

DisplayMode CurrentdIndDisplayMode;

AdapterDetails Information;

AdapterDetails getWhqlinformation ();

DisplayModeEnumerator SupportedDisplayModes;

}

Here Adapter member refers to the number of adapters when creating Device. The number of orders is a 0-based index, and the number of orders is equal to the number of adapters in the system. Both two returned AdapterDetails structures use the same method to return the same result. For Information members, Windows Hardware Quality Labs (WHQL) does not return detail, but GetWhqlinformation can. Getting this information takes some costs and events, so we divide it into two parts.

The AdapterDetails structure saves a lot of information for the adapter, including the description of the adapter itself, and the driver information. Although this is not necessarily used, the application can make judgments for hardware types in turn.

The remaining two members returned to the DisplayMode structure. These structures contain a large number of display modes, including the displayed height and width, refresh rate, and usage format. CurrentDisplayMode Returns the current display mode, but SupportDisplayModes returns a list of patterns supported by the adapter.

SO, we use the description of the device as the root node of the Tree View from the Information property. Then a child node representing the name of the driver and the version number. A child node showing the current display mode is also added, and all supported display modes are listed under this subtitle.

Operating a program, you can see a list of all support patterns. These modes can be used as the correct backup buffer format when populating the Present Parameter structure. Every enumerated mode has a string displayed in a fixed mode (E.G X8R

8G

8b8), alphabet and numbers alternate. The letter represents the type of data, the number indicates the number of digits of this type of data. Low is a common letter:

A - Alpha B - Blue X --- Unused L ---- Luminance R ---- Red P ---- Palette G --- Green

(Although there are many formats, there are only a few of the correct use of backup buffers and display mode. Models available for backup buffers include: A2R

10g

10b10, A1R

5G

5B5, A8R

8G

8b8, x1r

5G

5B5, X8R

8G

8b8, r

5G

5b5; Display Formats Can Be The Same As The Back Buffer Formats, with the Exception of Those That Contain An Alpha Component. The Only Format That Can Be Used for a Display Weith Alpha I A2R

10g

10b10, And Even THAT I'S ONLY IN FULL-Screen Mode.)

The number of digits accounted for each type is the total size of this format. Such as X8R

8G

8b8, 32-bit format, red, green, blue each 8 digits, and 8 bits are not used.

So far, we have obtained the number of adapters to create, the backup buffer format to be supported, then other parameters for the Device constructor? Fortunately, the Manager class has everything we need.

Judgment which device is available

There are many ways to manage if the Manager class can be used to detect if your adapter supports a specific function. For example, you need to detect whether the adapter supports a special format, but does not want to enumerate all possible adapters and formats, then you can use the Manager class to solve this problem. Use the following method:

Pubic Static System.Boolean CheckDeviceType (int Adapter, DeviceType CheckType, Format DisplayFormat, Format BackbufferFormat, Bool Windowed, int result)

This method can quickly detect whether DEVICE supports the format you will use. The first parameter is the adapter order you want to detect; the second is the Device type to be detected, but most of this value is set to DeviceType.fardware. Then specify the backup buffer type and display formats that will be used, and whether full screen is required. The last parameter is optional, if used, he will return an integer for this method (ie the HRESULT in COM). If this is an effective device, the method returns True, otherwise false. This method is useful when you know the format to use in advance.

(It should be noted that in window mode, the format of the backup buffer does not have to match the display format, as long as your hardware supports the appropriate color conversion. No matter whether your hardware supports this feature, the checkdeviceType method returns appropriate As a result, you should use the Manager class CheckDeviceFormatConVersion method to determine whether this conversion is supported. You can also use format.unknown in window mode. No such conversion in full screen mode.)

Test the function of Device (Capabilities)

We call each Device to fully use hardware, or "Capability", or "CAP". Direct3D has a CAPS structure that can list each possible Capabilities supported by Device. Once you have created Device, you can use Device's CAPS properties to detect the features he supported, but if you want to know the features supported by Device, what should I do? Naturally, there is also a method to complete this task.

Now, a little bit of code is added to obtain Capabilities for each adapter in the system. We will no longer use Tree View to display these Capabilities, which may contain hundreds of Capabilities. The best way is to use a Text Box. Back to the design mode of Windows Form, change the Tree View's Dock property to "Left", change the width to half; into the Text Box control, set the Dock property to "Fill", Multiline is set to true, scrollbars set to "Both". Now you may want to add a hook (hook) for the program so that after you select an adapter, the data in the TextBox will be updated. Add the following code using the AfTerselect event of Tree View:

Private void TreeView_1AFTERSELECT (Object Sender, System.Windows.Forms E)

{

IF (e.node.parent == null)

{

TextBox1.text = E.Node.Text "Capabilities: / R / N / R / N" Manager.GetDeviceCaps (E.Node.indes, DeviceType.hardware) .tostring (). Replace ("/ n", " / r / n ");

}

}

As you can see, it is quite simple. Run a look at the results.

转载请注明原文地址:https://www.9cbs.com/read-51252.html

New Post(0)