csplugincommon/rendermanager/shadersetup.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csplugincommon/rendermanager/shadersetup.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2007-2008 by Marten Svanfeldt
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_CSPLUGINCOMMON_RENDERMANAGER_SHADERSETUP_H__
    00020 #define __CS_CSPLUGINCOMMON_RENDERMANAGER_SHADERSETUP_H__
    00021
    00026 #include "csutil/compositefunctor.h"
    00027
    00028 #include "csplugincommon/rendermanager/rendertree.h"
    00029 #include "csplugincommon/rendermanager/operations.h"
    00030 #include "csplugincommon/rendermanager/svsetup.h"
    00031
    00032 namespace CS
    00033 {
    00034 namespace RenderManager
    00035 {
    00036
    00037   typedef csArray<size_t> TicketArrayType;
    00038   typedef csArray<iShader*> ShaderArrayType;
    00039
    00047   template<typename RenderTree, typename LayerConfigType>
    00048   class ShaderSetup
    00049   {
    00050   public:
    00051     ShaderSetup (ShaderArrayType& shaderArray, const LayerConfigType& layerConfig)
    00052       : shaderArray (shaderArray), layerConfig (layerConfig)
    00053     {
    00054     }
    00055
    00056     void operator() (typename RenderTree::MeshNode* node)
    00057     {
    00058       // Get the shader
    00059       const size_t totalMeshes = node->GetOwner().totalRenderMeshes;
    00060
    00061       for (size_t i = 0; i < node->meshes.GetSize (); ++i)
    00062       {
    00063         typename RenderTree::MeshNode::SingleMesh& mesh = node->meshes[i];
    00064
    00065         csRenderMesh* rm = mesh.renderMesh;
    00066
    00067         for (size_t layer = 0; layer < layerConfig.GetLayerCount (); ++layer)
    00068         {
    00069           size_t layerOffset = layer*totalMeshes;
    00070
    00071           iShader* shader = 0;
    00072           if (rm->material)
    00073           {
    00074             size_t layerShaderNum;
    00075             const csStringID* layerShaders = layerConfig.GetShaderTypes (layer, layerShaderNum);
    00076
    00077             shader = rm->material->GetMaterial ()->GetFirstShader (layerShaders,
    00078               layerShaderNum);
    00079           }
    00080           shaderArray[mesh.contextLocalId+layerOffset] = shader ? shader : layerConfig.GetDefaultShader (layer);
    00081         }
    00082       }
    00083     }
    00084
    00085   private:
    00086     ShaderArrayType& shaderArray;
    00087     const LayerConfigType& layerConfig;
    00088   };
    00089
    00090   template<typename RenderTree, typename LayerConfigType>
    00091   struct OperationTraits<ShaderSetup<RenderTree, LayerConfigType> >
    00092   {
    00093     typedef OperationUnorderedParallel Ordering;
    00094   };
    00095
    00096
    00097
    00105   template<typename RenderTree, typename LayerConfigType>
    00106   class TicketSetup
    00107   {
    00108   public:
    00109
    00110     TicketSetup (SVArrayHolder& svArrays, csShaderVariableStack& varStack,
    00111       const ShaderArrayType& shaderArray, TicketArrayType& tickets,
    00112       const LayerConfigType& layerConfig)
    00113       : svArrays (svArrays), varStack (varStack),
    00114       shaderArray (shaderArray), ticketArray (tickets),
    00115       layerConfig (layerConfig)
    00116     {
    00117     }
    00118
    00119
    00120     void operator() (typename RenderTree::MeshNode* node)
    00121     {
    00122       const size_t totalMeshes = node->GetOwner().totalRenderMeshes;
    00123
    00124       for (size_t i = 0; i < node->meshes.GetSize (); ++i)
    00125       {
    00126         typename RenderTree::MeshNode::SingleMesh& mesh = node->meshes[i];
    00127
    00128         for (size_t layer = 0; layer < layerConfig.GetLayerCount (); ++layer)
    00129         {
    00130           size_t layerOffset = layer*totalMeshes;
    00131
    00132           // Setup the shader array
    00133           svArrays.SetupSVStack (varStack, layer, mesh.contextLocalId);
    00134
    00135           // Get the ticket
    00136           iShader* shader = shaderArray[mesh.contextLocalId+layerOffset];
    00137           ticketArray[mesh.contextLocalId+layerOffset] = shader ? shader->GetTicket (*mesh.renderMesh, varStack) : ~0;
    00138         }
    00139       }
    00140     }
    00141
    00142   private:
    00143     SVArrayHolder& svArrays;
    00144     csShaderVariableStack& varStack;
    00145     const ShaderArrayType& shaderArray;
    00146     TicketArrayType& ticketArray;
    00147     const LayerConfigType& layerConfig;
    00148   };
    00149
    00150   template<typename RenderTree, typename LayerConfigType>
    00151   struct OperationTraits<TicketSetup<RenderTree, LayerConfigType> >
    00152   {
    00153     typedef OperationUnordered Ordering;
    00154   };
    00155
    00156   // Some helper for below
    00157   template<typename RenderTree, typename LayerConfigType>
    00158   struct StandardSATSetupTypes
    00159   {
    00160     typedef ShaderSetup<RenderTree, LayerConfigType> ShaderSetupType;
    00161     typedef ShaderSVSetup<RenderTree, LayerConfigType> ShaderSVSetupType;
    00162     typedef TicketSetup<RenderTree, LayerConfigType> TicketSetupType;
    00163
    00164     typedef CS::Meta::CompositeFunctorType2<TicketSetupType,
    00165       ShaderSVSetupType>CombinedFunctorType;
    00166   };
    00167
    00171   template<typename ContextNodeType, typename LayerConfigType>
    00172   void SetupStandardShader (ContextNodeType& context,
    00173     iShaderManager* shaderManager,
    00174     const LayerConfigType& layerConfig)
    00175   {
    00176     context.shaderArray.SetSize (context.totalRenderMeshes*layerConfig.GetLayerCount ());
    00177
    00178     // Shader setup
    00179     typedef typename ContextNodeType::TreeType Tree;
    00180     typedef StandardSATSetupTypes<Tree, LayerConfigType> TypeHelper;
    00181
    00182     typename TypeHelper::ShaderSetupType
    00183       shaderSetup (context.shaderArray, layerConfig);
    00184
    00185     ForEachMeshNode (context, shaderSetup);
    00186   }
    00187
    00192   template<typename ContextNodeType, typename LayerConfigType>
    00193   void SetupStandardTicket (ContextNodeType& context,
    00194     iShaderManager* shaderManager,
    00195     const LayerConfigType& layerConfig)
    00196   {
    00197     context.ticketArray.SetSize (context.totalRenderMeshes*layerConfig.GetLayerCount ());
    00198
    00199     typedef typename ContextNodeType::TreeType Tree;
    00200     typedef StandardSATSetupTypes<Tree, LayerConfigType> TypeHelper;
    00201
    00202     /* Ticket and shader SV setup
    00203      * Note that SVs have to be set up *after* the tickets - otherwise SVs
    00204      * from fallbacks won't work */
    00205     typename TypeHelper::TicketSetupType
    00206       ticketSetup (context.svArrays, shaderManager->GetShaderVariableStack (),
    00207         context.shaderArray, context.ticketArray, layerConfig);
    00208
    00209     typename TypeHelper::ShaderSVSetupType
    00210       shaderSVSetup (context.svArrays, context.shaderArray,
    00211       context.ticketArray, layerConfig);
    00212
    00213     // Do the two operations sequentially after each other
    00214     typename TypeHelper::CombinedFunctorType
    00215       combFunctor (CS::Meta::CompositeFunctor (ticketSetup, shaderSVSetup));
    00216
    00217     ForEachMeshNode (context, combFunctor);
    00218   }
    00219
    00220   typedef csDirtyAccessArray<csStringID> ShaderVariableNameArray;
    00221 }
    00222 }
    00223
    00224 #endif