Effects in DirectX 11

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

13:18
Sat
15
May 2010

Effects in DirectX 11

I no longer believe Microsoft did a good job complicating new DirectX so much. Effects framework - the API that supported loading and using effect files that grouped HLSL code and render states into passes and techniques - is no longer intrinsic part of D3DX. Instead they provided source code for this library so you have to compile it by yourself!

To do this: Enter your DX SDK subdirectory "Samples\C++\Effects11", open a "Effects11_*.sln" solution file appropriate for your Visual C++ version and compile the project in both Debug and Release configuration.

Then to use effects API in your project you have to include this header: "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Inc\D3dx11effect.h" and link with this lib: "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Debug\D3DX11EffectsD.lib (Debug) or "YOUR_DX_SDK_PATH\Samples\C++\Effects11\Release\D3DX11Effects.lib" (Release), as well as with "d3dcompiler.lib" (in both configurations).

Here is example of how to load an effect from file. You need to first compile a source code from file or memory into a blob binary effect and then create real effect object from this blob.

// Compile effect from HLSL file into binary Blob in memory
ID3D10Blob *effectBlob = 0, *errorsBlob = 0;
HRESULT hr = D3DX11CompileFromFile(
  "Effect1.fx", 0, 0, 0, "fx_5_0", 0, 0, 0, &effectBlob, &errorsBlob, 0);
assert(SUCCEEDED(hr) && effectBlob);
if (errorsBlob) errorsBlob->Release();
// Create D3DX11 effect from compiled binary memory block
ID3DX11Effect *g_Effect;
hr = D3DX11CreateEffectFromMemory(
  effectBlob->GetBufferPointer(), effectBlob->GetBufferSize(), 0, g_Dev, &g_Effect);
assert(SUCCEEDED(hr));
effectBlob->Release();

The effect itself is not enough. You need to retrieve object that represents "pass" to use it. So you get a technique from the effect (by index or by name) and then the pass from the technique.

ID3DX11EffectTechnique *g_EffectTechnique; // No need to be Release()-d.
g_EffectTechnique = g_Effect->GetTechniqueByIndex(0);
assert(g_EffectTechnique && g_EffectTechnique->IsValid());

ID3DX11EffectPass *g_EffectPass; // No need to be Release()-d.
g_EffectPass = g_EffectTechnique->GetPassByIndex(0);
assert(g_EffectPass && g_EffectPass->IsValid());

Now when you have this object, you can apply settings from this pass to the device context during rendering:

g_EffectPass->Apply(0, g_Ctx);
g_Ctx->Draw(3, 0);

But still one problem reamins. In DirectX 11 you need to pass a pointer to the bytecode with compiled shader when creating input layout - a step that you probably cannot omit. Fortunately there is a way to access this pointer stored inside loaded effect. You just need to pass through two descriptors, just like this:

D3DX11_PASS_SHADER_DESC effectVsDesc;
g_EffectPass->GetVertexShaderDesc(&effectVsDesc);
D3DX11_EFFECT_SHADER_DESC effectVsDesc2;
effectVsDesc.pShaderVariable->GetShaderDesc(effectVsDesc.ShaderIndex, &effectVsDesc2);
const void *vsCodePtr = effectVsDesc2.pBytecode;
unsigned vsCodeLen = effectVsDesc2.BytecodeLength;

ID3D11InputLayout *g_InputLayout;
D3D11_INPUT_ELEMENT_DESC inputDesc[] = { /* ... */ };
hr = g_Dev->CreateInputLayout(
  inputDesc, _countof(inputDesc), vsCodePtr, vsCodeLen, &g_InputLayout);

Luckily it looks like the effect framework doesn't add much functionality over pure HLSL shader supported by D3D11 itself, so you don't have to use it. Defining these techniques and passess is not so important after all...

Comments (6) | Tags: directx rendering | Author: Adam Sawicki | Share

Comments

a
2013-05-07 05:42:49
Nike is so famous that almost everyone know it all over the world.<a href="http://www.niketnbwfr.eu/">nike pas cher</a>.No matter developed countries or developing countries,Nike has presence stores into and also arrow paying online.
aa
2013-05-07 05:43:14
Nike is so famous that almost everyone know it all over the world.[url=http://www.niketnbwfr.eu/]nike pas cher[/url].No matter developed countries or developing countries,Nike has presence stores into and also arrow paying online.
Chuck Walbourn
2013-07-16 05:52:02
Note the latest version of Effects 11 is available from here <http://go.microsoft.com/fwlink/p/?LinkId=271568>
Yo
2014-05-06 00:58:12
Y'all gone make me lose my mind. Up in here, up in here.
polo
2014-07-27 10:48:50
http://www.superbagsmarket.com/ Super Bags Market
http://www.nike-jordanshoes.com/ Nike Jordan Shoes
http://www.thebeatsbydre.net/ Beats By Dre Outlet
http://www.michaelusakors.com/ Michael Kors Outlet Online
http://www.polo-tshirts.com/ Ralph Polo Outlet Online
http://www.northsclearance.com/ North Clearace Outlet
http://www.polo-ralphlaurenonline.com/ Ralph Polo Outlet
http://www.guccishoesuk-2014.net/ Gucci Shoes Outlet
http://www.michaelkorsstates.com/ Michael Kors Outlet
http://www.polooutlet-store.com/ Polo Outlet Store
http://www.ralph-laurenhome.com/ Ralph Lauren UK
http://www.marcjacobsonsale.com/ Marc Jacobs On Sale
http://www.mcmworldwides.com/ MCM Outlet
http://www.hermesoutletonline.net/ Hermes Outlet Online
http://www.salongchamppairs.com/ Sac Longchamp Pairs
http://www.canada-gooser.com/ Canada Goose Outlet
http://www.burbagssale2014.com/ Burberry Outlet
http://www.michaelkors.so/ Michael Kors Outlet
http://www.oakley-sunglassfactory.com/ Oakley Sunglaases Factory
http://www.north-faceoutlet.us.com/ North Face Outlet Online
http://www.moncler-clearance.com/ Moncler Clearance
http://www.woolrich-clearance.com/ Woolrich Clearance
http://www.barbour-jacketsoutlet.com/ Barbour Jackets Outlet Online
http://www.moncler-jacketsoutletonline.com/ Moncler Jackets Outlet Online
http://www.airmaxshoesfactory.com/ Air Max Shoes
http://www.monsterbeatsbydres.com/ Monster Beats Outlet
http://www.louis-vuittonblackfriday.com/ Louis Vuitton Outlet
http://www.lv-guccishoesfactory.com/ Louis Vuitton Factory
http://www.mcmoutlet-jp.com/ MCM &#24215;&#38138;
http://www.cheapdiscountoutlet.com/ Discount Clothes Sale
http://www.michael-korsfactoryonline.com/ Michael Kors Factory Online
http://www.coach-factoryoutlets.net/ Coach Factory Outlet
http://www.coach-blackfriday.net/ Coach Black Friday
http://www.coach-storeoutletonline.com/ Coach Store Outlet Online
http://www.coach-factoryoutletonlines.com/ Coach Factory Outlet Online
http://www.coachcoachoutlet.net/ Coach Outlet
http://www.coach-factory.net/ Coach Factory
http://www.coach-pursesoutlet.net/ Coach Purses Outlet Online
http://www.coach-outletonline.net/ Coach Outlet USA
http://www.mksfactoryoutlet.com/ Michael Kors Factory Outlet
http://www.zxcoachoutlet.com/ Coach Outlet Online USA
http://www.mischristmas.com/ Coach Factory Outlet Online
http://www.misblackfriday.com/ Black Friday
http://www.newoutletonlinemall.com/ Coach Purses Outlet Online
http://www.clickmichaelkors.com/ Michael Kors USA
http://www.cmichaelkorsoutlet.com/ Michael Kors Outlet USA
http://www.ralphlaurenepolo.com/ Ralph Lauren Polo
Chanel Handbags
2014-08-19 10:13:24
http://www.coachoutletstoreinuk.com/ Coach Outlet Online
http://www.coachfactoryoutletanus.com/ Coach Outlet
http://www.coachoutletonlineunsius.com/ Coach Factory Outlet
http://www.coachoutletonlinetur.com/ Coach Outlet Online 
http://www.coachfactoryoutletnbsa.com/ Coach Factory Outlet
http://www.coachfactoryoutletuisa.com/ Coach Outlet Store Online http://www.coachoutletstorenie.com/ Coach Outlet Store Online
http://www.coachfactorystoreuin.com/ Coach Factory Outlet
http://www.coachfactoryoutletreba.com/ Coach Outlet 
http://www.coachfactoryoutletonlinesius.com/ Coach Handbags Outlet  http://www.coachoutletonlinetnse.com/ Coach Outlet http://www.coachoutletnoia.com/ Coach Outlet
http://www.chaneloutletinaus.com/ Chanel Outlet Online
http://www.louisvuittonoutletina.com/ Louis Vuitton
http://www.louisvuittonoutletine.com/ Louis Vuitton Outlet
http://www.guccibeltsoutletbcus.com/ Gucci Belts
http://www.guccibelststco.com/ Gucci Belt
http://www.coachoutletstoreinuk.com/ Coach Outlet Online
http://www.coachfactoryoutletanus.com/ Coach Outlet
http://www.coachoutletonlineunsius.com/ Coach Factory Outlet http://www.coachoutletonlinetur.com/ Coach Outlet Online
http://www.coachfactoryoutletnbsa.com/ Coach Outlet Store
http://www.coachfactoryoutletuisa.com/ Coach Outlet Store Online
http://www.coachoutletstorenie.com/ Coach Outlet
http://www.coachfactorystoreuin.com/ Coach Factory Online
http://www.coachfactoryoutletreba.com/ Coach Factory Outlet http://www.coachfactoryoutletonlinesius.com/ Coach Factory Outlet
http://www.coachoutletonlinetnse.com/ Coach Outlet http://www.coachoutletnoia.com/ Coach Outlet Store Online
http://www.chaneloutletinaus.com/ Chanel Outlet Online
http://www.louisvuittonoutletina.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletine.com/ Louis Vuitton Handbags
http://www.guccibeltsoutletbcus.com/ Gucci Belt
http://www.guccibelststco.com/ Gucci Belts
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet Online
http://www.oakleysunglassescnc.name/ Oakley Sunglasses Outlet
http://www.louisvuittonoutletmoba.com/ Louis Vuitton Outlet Online
http://www.oakleyssunglassesmobo.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesmaba.com/ Cheap Oakley Sunglasses
http://www.oakleysunglassinsc.com/ Oakley Sunglasses
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet
http://www.oakleysunglassescnc.name/ Oakley Sunglasses Outlet
http://www.louisvuittonoutletmoba.com/ Louis Vuitton Outlet
http://www.oakleyssunglassesmobo.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesmaba.com/ Oakley Sunglasses
http://www.oakleysunglassinsc.com/ Oakley Sunglasses

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2014 Adam Sawicki
Copyright © 2004-2014 Adam Sawicki