Thread: Pixel/Vertex shader framework design

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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