Thread: Pixel/Vertex shader framework design

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Pixel/Vertex shader framework design

    I'm creating a small framework of classes to manage pixel and vertex shaders outside of the Direct3D effects framework. Later I'll create classes that will do the same within the confines of the effects framework.

    My design issue is this:

    Should I create an object called CShader which encapsulates both a CPixelShader and CVertexShader object or should I just leave the two classes as they are?

    Not every vertex shader will have a pixel shader but most will. My idea was to place pointers in CShader and then create functions for pixel and vertex shaders which access the pointers.

    Then I was going to create a shader collection class which would manage a collection of CShader objects. The shaders would be compiled by calling CShaderMgr::CompileAll() which would then create the IDirect3DVertexShader9 and IDirect3DPixelShader9 interfaces in the CShader collection for later use. Setting a vertex or pixel shader would be as simple as calling CShader::GetPixelShader() or CShader::GetVertexShader() and passing those objects to IDirect3DDevice9::SetPixelShader() and IDirect3DDevice9::SetVertexShader().

    Any ideas as to how I should or shouldn't structure this?

    Current structure idea:

    CShader
    - CVertexShader
    - -CVertexShader.h
    - -CVertexShader.cpp

    - CPixelShader
    - - CPixelShader.h

    CShaderMgr
    - std::map<DWORD,CShader *> m_mapShaders;

    Sample code:
    Code:
    #pragma once
    //CVertexShader.h
    //Encapsulates a vertex shader
    #include <d3dx9.h>
    #include "dxutil.h"
    
    class CVertexShader
    {
      //Shared pointers
      IDirect3DDevice9            *m_spDevice;
           
      //Owned pointers
      IDirect3DVertexDeclaration9 *m_pVertexDec;
      IDirect3DVertexShader9      *m_pVSH;
      ID3DXConstantTable          *m_pConstantTable;
      ID3DXBuffer                 *m_pShaderCode;
      ID3DXBuffer                 *m_pErrorBuf;
      
      
      public:
        CVertexShader(IDirect3DDevice9 *pDevice):
                      m_spDevice(pDevice),m_pVertexDec(NULL),m_pVSH(NULL),m_pConstantTable(NULL),
                      m_pShaderCode(NULL),m_pErrorBuf(NULL) { }
        
        virtual ~CVertexShader()
        {
          SAFE_RELEASE(m_pVertexDec);
          SAFE_RELEASE(m_pVSH);
          SAFE_RELEASE(m_pConstantTable);
          SAFE_RELEASE(m_pShaderCode);
          SAFE_RELEASE(m_pErrorBuf);
        }
        
        //Vertex declaration functions
        HRESULT CreateVertexDeclaration(CONST D3DVERTEXELEMENT9 *pElements);
        IDirect3DVertexDeclaration9 *GetVertexDeclaration()
        {
          return m_pVertexDec;
        }
        
        
        HRESULT CompileFromFile(LPCSTR pSrcFile,
                                CONST D3DXMACRO *pDefines,
                                LPD3DXINCLUDE pIncludes,
                                LPCSTR pFunctionName,
                                LPCSTR pTarget,
                                DWORD  dwFlags);
        
        //Constant table access
        ID3DXConstantTable *GetConstantTable() {return m_pConstantTable;}
        
        //Shader code access
        ID3DXBuffer *GetShaderCode() {return m_pShaderCode;}
        
        //Shader error buffer access
        ID3DXBuffer *GetErrorBuf() {return m_pErrorBuf;}
        
        //Vertex shader object access
        IDirect3DVertexShader9 *m_pGetVertexShader() {return m_pVSH;}
    };
    Last edited by VirtualAce; 07-30-2006 at 03:47 AM.

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    I'm not familier with D3D shaders, but I don't see why you would need seperate classes for vertex and fragment shaders, unless of course, D3D9 treats them as such. I can really only go by what OpenGL does, which is that it compiles the source of the vertex shader and fragment shader into one program to be used later by your shader manager (although you could compile them seperatly into different programs, I guess).

    My shader wrapper consists merely of CGlsl, which contains all the functions to compile a shader program, turn on/off a shader, and pass arguements, and CGlslManager, which keeps an array of shaders, and a function to create a new shader, calling the CGlsl compile functions.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  2. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  3. Replies: 6
    Last Post: 11-12-2005, 11:57 AM