I've been looking everywhere and all I can find are tutorials on writing the shaders. None of them showed me how to incorporate them into my scene.

So essentially:

Given an hlsl shader, if I were to have a function called drawTexturedQuad() and I wanted the shader to be applied to the result, how exactly could I do this?


  • \$\begingroup\$ OpenGL dev here, but your method sound more like a FFP function that can't be combined with a shader. You have to draw the quad with texture yourself in the shader. \$\endgroup\$
    – JustSid
    Commented Jun 10, 2011 at 6:49
  • \$\begingroup\$ That's a funny question actually. And people making tutorials should be avare of this :). What version of DX are you using? \$\endgroup\$
    – Notabene
    Commented Jun 10, 2011 at 7:01

3 Answers 3


There are three steps:

  1. Load effect and set the technique
  2. Provide data to the effect
  3. Render

1) Load effect and set the technique

  // Declaration of your effect variable
  LPD3DXEFFECT  mDSEGeometryStage; 
  //With this method you load your effect file
  HR(**D3DXCreateEffectFromFile**( d3ddev, "./DeferredEffect_MaterialsStage.fx", 0, 0, D3DXSHADER_DEBUG, 0, &mDSEGeometryStage, &errors);) 
        // Some Error checking   
    if( errors )
        MessageBoxA(0, (char *)errors->GetBufferPointer(), 0, 0);
    // Retrieve a Technique from your effects handler
    D3DXHANDLE mhTech = mDSEGeometryStage->**GetTechniqueByName**("MaterialsTech");
            // Set the technique to be used 

2) Provide data to the effect:

    // Effect values handlers (String parameter is the name of a variable in your .fx file)
D3DXHANDLE mhView       = mDSEGeometryStage->GetParameterByName(0, "gView");            
D3DXHANDLE mhProjection = mDSEGeometryStage->GetParameterByName(0, "gProjection");      
D3DXHANDLE mhNearClip = mDSEGeometryStage->GetParameterByName(0, "gNearClip");
D3DXHANDLE mhFarClip = mDSEGeometryStage->GetParameterByName(0, "gFarClip");

    // Set effect values indicating the handler and the data as paramters
HR(mDSEGeometryStage->SetMatrix(mhProjection, &(camera->getProjectionMatrix())));
HR(mDSEGeometryStage->SetMatrix(mhView, &(camera->getViewMatrix())));       
HR(mDSEGeometryStage->SetFloat(mhFarClip, camera->getFarClip()));


3) Render: As said in a previous answer

ID3DXEffect provides Begin() and BeginPass() methods.

So, in the same rendering method you can start to render by calling the device BeginScene() method and inside call the Begin or BeginPass methods of your effect.


// Begin Scene  

    // Clear targets surface
        D3DCOLOR_ARGB(0, 0,0,0), 1.0f, 0);    

    // Begin Effect passes.
    UINT numPasses = 0;
    HR(mDSEGeometryStage->Begin(&numPasses, 0))
       // like:  drawTexturedQuad();    
    // End Effect passes
// End scene rendering
}// End of renderGeometryStage method

Hope it helps, but your question is not about a chunk of code that you can copy/paste, its more about "basic" concepts. So for this kind of things I really recommend you to follow some tutorial because you will understand all the underlying concepts better. Cheers!

  • \$\begingroup\$ Gotta upvote this- nice and comprehensive. \$\endgroup\$
    – DeadMG
    Commented Jun 10, 2011 at 14:22

ID3DXEffect provides Begin() and BeginPass() methods. All you have to do is call DrawQuad() appropriately between them and their equivalent End() and EndPass() methods.


Depends if you want to use OpenGL or Direct3D.

For D3D the SDK has all the gory details.

  • 4
    \$\begingroup\$ Hi welcome to Gamedev. Please read questions better. HLSL means directx, he also specified directx in tags. \$\endgroup\$
    – Notabene
    Commented Jun 10, 2011 at 7:50
  • \$\begingroup\$ AFAIK, you cannot use HLSL with OpenGL. It is DirectX only. \$\endgroup\$ Commented Jun 10, 2011 at 20:46
  • \$\begingroup\$ Fair enough. But reading the DX SDK would give him the answer. \$\endgroup\$ Commented Jun 11, 2011 at 7:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .