Loading and using an HLSL shader?

jmasterx
  • Loading and using an HLSL shader? jmasterx

    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?

    Thanks

  • Depends if you want to use OpenGL or Direct3D.

    For D3D the SDK has all the gory details.

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

  • 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; 
      ...
      initEffects()
      {
      //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);
            errors->Release();
        }       
        // Retrieve a Technique from your effects handler
        D3DXHANDLE mhTech = mDSEGeometryStage->**GetTechniqueByName**("MaterialsTech");
                // Set the technique to be used 
        HR(mDSEGeometryStage->SetTechnique(mhTech));
      }
    

    2) Provide data to the effect:

    renderGeometryStage()
    {
        // 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  
    HR(d3ddev->BeginScene());
    {
                    ...     
    
        // Clear targets surface
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET |D3DCLEAR_ZBUFFER, 
            D3DCOLOR_ARGB(0, 0,0,0), 1.0f, 0);    
    
        // Begin Effect passes.
        UINT numPasses = 0;
        HR(mDSEGeometryStage->Begin(&numPasses, 0))
        {
           // PLACE HERE YOUR RENDERING STUFF 
           // like:  drawTexturedQuad();    
        }
        // End Effect passes
        HR(mDSEGeometryStage->End());
    }   
    // End scene rendering
    HR(d3ddev->EndScene());    
    }// 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!

Tags
c++ directx shaders hlsl
Related questions and answers
  • shader stage of D3D11, setting them all at once and just never touching them again? My shaders would reference these samplers in HLSL if they need them (for instance some shaders would reference...In my Direct3D 11 application, I am using several sampler states to retrieve texture data. Some of them are used in all pixel shaders, some of them are only used in very specific ones. The question... to reference these samplers in my c++ code after creation and setting, I could just release my own external references after they are set and they will not get destroyed, as D3D is keeping internal

  • I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why. If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates? // a struct for the vertex shader return value struct VSOut { float4 Col

  • I'm rendering to a depth map in order to use it as a shader resource view, but when I sample the depth map in my shader, the red component has a value of 1 while all other channels have a value of 0. The Texture2D I use to create the DepthStencilView is bound with the D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE flags, the DepthStencilView has the DXGI_FORMAT_D32_FLOAT format...; #if defined(DEBUG) || defined(_DEBUG) deviceFlags |= D3D11_CREATE_DEVICE_DEBUG | D3D11_RLDO_DETAIL; #endif This is how I create my depth map. I've omitted error checking for the sake of brevity

  • I've got an effect which is a fairly simple two-pass deal- but it involves rendering to a texture in the first pass. Is it possible to change the render target from within an effect? I would definitely appreciate the reduction in code duplication from C++ by being able to do both passes within HLSL.

  • I am unable to wrap my mind around designing a good way to manage resources in my game. For instance, a vertex buffer in DirectX or OpenGL would lead me to believe I should have one central buffer in which I would adapt to my game. But how would I efficiently manage this? If I had 2 game objects which used the same model but with different properties such as color or position, I would I assume reading from the same section of memory for geometry and having them each passing there own properties to the vertex/pixel shader. But what boggles me is having them read from the same section

  • that make really hard to work with when coding some functions that use them. I was thinking of making ie. SimpleMesh and HierarchyMesh objects, which will also require that the renderer can deal with different types of objects in the same scene. I was also thinking about making a MeshNode class and then make a Mesh object that contains them, but then I have some conflict on where to store some data... shader in every node. Other option I was thinking was making some helper functions to deal with the simpler cases, which would set some default parameters and would ask only the most basic ones

  • I am setting an HLSL effect variable in the following way in a number of places. extern ID3D10EffectVectorVariable* pColour; pColour = pEffect->GetVariableByName("Colour")->AsVector(); pColour->SetFloatVector(temporaryLines[i].colour); In one of the places it is set in a loop, each line in the vector temporaryLines has a D3DXCOLOR variable associated with it. The most annoying thing... = temporaryLines.size(); for(int i = 0; i < allLines; i++) { pColour->SetFloatVector(temporaryLines[i].colour); // in the line loop too? // combine the matrices and render D3DXMATRIX

  • I don't get how coord (a UV vec2 in my mind) is equal to the dot product (a scalar value)? Same problem I had before with "g". What do I set the plane to be? In my opengl c++ 3.0 code, I set... Language 1.0 Several sites have mentioned that this should be "easy" to do in a GLSL vert shader. But I just can not get it to work. My hunch is that I'm not setting the planes up correctly, or I'm missing...I also posted this on the main StackExchange, but this seems like a better place, but for give me for the double post if it shows up twice. I have been trying for several hours to implement a GLSL

  • Alright, I'm having a hard time getting a bool packed and aligned into a hlsl constant buffer and I'm not sure why. Here is the buffer in hlsl cbuffer MaterialBuffer : register(b1) { float3 materialDiffuseAlbedo; float materialSpecularExponent; float3 materialSpecularAlbedo; bool isTextured; }; And here it is in c++ struct GeometryBufferPass_MaterialBuffer { XMFLOAT3 diffuse; float specularExponent; XMFLOAT3 specular; bool isTextured; }; I've tried moving the bool and padding the struct in all kinds of ways with no luck. What is the correct way to do this?

Data information