简体   繁体   中英

DirectX 11 Effects

I'm trying to learn DirectX 11 and am writing a small application using Visual Studio 2013 for Windows 7. I'm following the book "Introduction to 3D Game Programming with DirectX 11" and so far everything's gone okay. I've just come up to the part of the book where it shows the BoxApp example and I'm just working through the code trying to implement something similar in my app just to get a better understanding of whats going on.

I've come across an issue with the following deprecated library functions used in the example code:

D3DX11CompileFromFile(....);
D3DX11CreateEffectFromMemory(...);

Firstly just to see if I understand why these are no longer available, my understanding as to why these functions no longer exist is that the D3DX header files and libraries have been deprecated for Windows 8. Originally they were helper libraries provided in the DirectX SDK for Dx9, and then provided by the Os for Dx10 before being deprecated for Dx11.1(2)?

Now my main question is how would I go about compiling shaders and effects without the deprecated D3DX headers? I'm just a bit confused about the correct way to go about this.

To me it seems there are two options:

  1. Download the helper libraries Effects11 which'll give similar previous functionality.
  2. Use the D3DCompiler library and compile effects manually.

Like I said I'm not quite sure which the best option would be (or even if there's a better alternative option(s)). My understanding is Effects11 was introduced more for support of older Dx10 that needed to be ported to Dx11. As I've not got any Dx10 code to port I'm not too fussed about that support, I'm not too fussed about Metro app support in Windows 8 (I think thats the right term for them?) either but if the D3DCompiler path would mean support for metro apps as well then I guess that'd be desirable.

Anyway, any advice or insight would be appreciated. Thanks in advance.

Well, it might help to go over the history of the Direct3DX Utility Library (D3DX). It was originally shipped as part of the DirectX 7 SDK as a static library that you linked directly into your application. It covered many of the same features that it does now, though notably it didn't support effects because shaders weren't introduced until Direct3D 8.

This all worked pretty well until one day Microsoft decided that static libraries were a big security hole. It was probably because the discovery of some nasty bugs in GDI+ that left a number of applications that used GDI+ vulnerable to exploits related to malformed images. Even though GDI+ was shipped as a DLL, each application that used it would install its own copy in its own installation directory. This meant that Microsoft couldn't simply patch the GDI+ DLL using Windows update, each application that used it had to create their own patch and distribute it to their users.

So Microsoft decided shipping binary code to developers to include in with their applications was a mistake. Any binary code Microsoft provided had to be updatable through Windows Update. That meant no more shared libraries and new restrictions on how DLLs can be redistributed and installed. This is why you're required to use the DirectX runtime installer when redistributing the D3DX DLLs. (It's also why Visual Studio had all the complicated requirements involving manifests with their redistributable DLLs.)

The problem with this though is that it led to Microsoft having to create a new version of the D3DX DLL every time they changed it, which was almost every time they released a new version of the SDK. Each new DLL wasn't backwards compatible with the old DLL, so it got a new name. If you look in your system32 directory you can probably find a couple of dozen different D3DX DLLs.

(Apparently only one security vulnerability in D3DX was ever disclosed, CVE-2006-4183 . As near as I can tell this was only fixed in subsequent releases the SDK, there was no patch issued to fix the bug in older versions of D3DX DLL.)

Eventually Microsoft decided to stop releasing new versions of the DirectX SDK and make it a part of the Windows SDK. Everything except D3DX that is, and so it was pretty much dead as far as Microsoft was concerned. No maintenance, no improvements and no replacement.

Then one day Microsoft decided to essentially open source the D3DX library, because I don't know. So we're pretty much back where we started, with applications linking the library directly into their executables. Except now it's open source, not a binary, so any security related bugs aren't Microsoft's problem.

So you basically you have four options:

  • use the D3DX DLL from last release of the DirectX SDK (June 2010)
  • use the open source Effect11 library
  • write your own code
  • use some sort of third party middleware

The first option means you'll probably need to bundle the DirectX redist with your installer. Also getting it to work newer versions of Visual Studio is a fairly complicated procedure. The second option means that you're ultimately responsible for getting it to work. The same is true with third option and its potentially a lot more work. The fourth option may not exist. So I think as a new Direct3D programmer today it's probably best to go with the open source libraries.

Another option is to use an already established game engine, like Unreal or Unity, but I'm assuming you've already discounted this possibility.

The latest version of the "Effects 11" runtime is on CodePlex .

There are two main issues with using Effects 11 at this point:

(1) The fx_5_0 HLSL profile that is required to build a shader (or really shaders plus metadata) that is required to use "Effects 11" is deprecated. It currently works "as is" in the latest public D3DCompiler (#47), although a few of the newer "DirectX 11.1" and "DirectX 11.2" era constructs that work in the other 5_0 profiles fails to compile with "fx_5_0". Officially it's deprecated and is expected to be removed in a future release of the HLSL compiler.

(2) "Effects 11" requires that you have the D3DCompile API available at runtime. This was an issue for Windows Store apps on Windows 8.0 and Windows Phone 8.0 because you could only use D3DCompile for development. With Windows Store apps for Windows 8.1 and Windows Phone 8.1, D3DCompile #47 is available for Store deployed apps. That means in theory you could use "Effects 11" for this purpose, but it's discouraged.

The "Effects 11" CodePlex discussion area actually addresses a number of your questions:

Is Effects 11 deprecated?

How do I avoid using fx_5_0?

The "Effects 11" runtime is still quite useful as a tool for learning HLSL generally, and I've used it to publish a number of old DirectX SDK samples and tutorials for the educational value.

As to if you should invest in learning it or using it long-term, that's totally up to you.

For beginners who just want to code with Direct3D 11, you can use the DirectX Tool Kit 's BasicEffect which has a bunch of 'stock' shaders and deals with all the constants and other issues without having to use "Effects 11" at all. You can also try using the Visual Studio Shader Designer (DGSL) system (also supported by DirectX Tool Kit) if you want to play with some of the things you can do here, although that's really much more of a 'toy' solution but can be useful. In any case, you can then build on these experiences to learn to write your own "effects" system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM