OgreShaderProgramProcessor.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 Permission is hereby granted, free of charge, to any person obtaining a copy
00009 of this software and associated documentation files (the "Software"), to deal
00010 in the Software without restriction, including without limitation the rights
00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 copies of the Software, and to permit persons to whom the Software is
00013 furnished to do so, subject to the following conditions:
00014 
00015 The above copyright notice and this permission notice shall be included in
00016 all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 THE SOFTWARE.
00025 -----------------------------------------------------------------------------
00026 */
00027 #ifndef _ShaderProgramProcessor_
00028 #define _ShaderProgramProcessor_
00029 
00030 #include "OgreShaderPrerequisites.h"
00031 #include "OgreShaderParameter.h"
00032 #include "OgreShaderFunctionAtom.h"
00033 
00034 
00035 namespace Ogre {
00036 namespace RTShader {
00037 
00049 class _OgreRTSSExport ProgramProcessor : public RTShaderSystemAlloc
00050 {
00051 
00052 // Interface.
00053 public: 
00054 
00057     ProgramProcessor();
00058 
00060     virtual ~ProgramProcessor();
00061 
00063     virtual const String& getTargetLanguage() const = 0;
00064     
00070     virtual bool preCreateGpuPrograms(ProgramSet* programSet) = 0;
00071 
00076     virtual bool postCreateGpuPrograms(ProgramSet* programSet) = 0;
00077  
00078 // Protected types.
00079 protected:
00080     
00081     //-----------------------------------------------------------------------------
00082     // Class that holds merge parameter information.
00083     class _OgreRTSSExport MergeParameter 
00084     {
00085     // Interface.
00086     public:
00088         MergeParameter();
00089 
00090 
00092         void clear();
00093         
00095         void addSourceParameter(ParameterPtr srcParam, int mask);
00096 
00098         size_t getSourceParameterCount() const { return mSrcParameterCount; }
00099 
00101         ParameterPtr getSourceParameter(unsigned int index) { return mSrcParameter[index]; }
00102 
00104         int getSourceParameterMask(unsigned int index) const { return mSrcParameterMask[index]; }
00105 
00107         int getDestinationParameterMask(unsigned int index) const { return mDstParameterMask[index]; }
00108 
00110         int getUsedFloatCount();
00111         
00113         ParameterPtr getDestinationParameter(int usage, int index);
00114 
00115     protected:
00116     
00118         void createDestinationParameter(int usage, int index);
00119 
00120 
00121     protected:
00122         // Destination merged parameter.
00123         ParameterPtr mDstParameter;
00124         // Source parameters - 4 source at max 1,1,1,1 -> 4.
00125         ParameterPtr mSrcParameter[4];
00126         // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
00127         int mSrcParameterMask[4];
00128         // Destination parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
00129         int mDstParameterMask[4];
00130         // The actual source parameters count.
00131         size_t mSrcParameterCount;
00132         // The number of used floats.
00133         int mUsedFloatCount;
00134     };
00135     typedef vector<MergeParameter>::type    MergeParameterList;
00136 
00137     
00138     //-----------------------------------------------------------------------------
00139     // A struct that defines merge parameters combination.
00140     struct _OgreRTSSExport MergeCombination
00141     {       
00142         // The count of each source type. I.E (1 FLOAT1, 0 FLOAT2, 1 FLOAT3, 0 FLOAT4).
00143         size_t srcParameterTypeCount[4];
00144         // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
00145         int srcParameterMask[4];
00146 
00147         MergeCombination(
00148             int float1Count, int float1Mask,
00149             int float2Count, int float2Mask,
00150             int float3Count, int float3Mask,
00151             int float4Count, int float4Mask)
00152         {
00153             srcParameterTypeCount[0] = float1Count;
00154             srcParameterTypeCount[1] = float2Count;
00155             srcParameterTypeCount[2] = float3Count;
00156             srcParameterTypeCount[3] = float4Count;
00157             srcParameterMask[0]     = float1Mask;
00158             srcParameterMask[1]     = float2Mask;
00159             srcParameterMask[2]     = float3Mask;
00160             srcParameterMask[3]     = float4Mask;
00161 
00162         }
00163     };
00164     typedef vector<MergeCombination>::type  MergeCombinationList;
00165 
00166     //-----------------------------------------------------------------------------
00167     typedef vector<Operand*>::type                      OperandPtrVector;
00168     typedef map<Parameter*, OperandPtrVector>::type     ParameterOperandMap;
00169     typedef map<Parameter*, ParameterPtr>::type         LocalParameterMap;
00170 
00171 protected:
00172 
00174     void buildMergeCombinations();
00175 
00181     virtual bool compactVsOutputs(Function* vsMain, Function* fsMain);
00182 
00188     void countVsTexcoordOutputs(Function* vsMain, int& outTexCoordSlots, int& outTexCoordFloats);
00189 
00194     void buildTexcoordTable(const ShaderParameterList& paramList, ShaderParameterList outParamsTable[4]);
00195 
00196 
00201     void mergeParameters(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
00202 
00203 
00208     void mergeParametersByPredefinedCombinations(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams);
00209 
00215     bool mergeParametersByCombination(const MergeCombination& combination, ShaderParameterList paramsTable[4], 
00216                                                                  MergeParameter* mergedParameter);
00217 
00223     void mergeParametersReminders(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
00224 
00225 
00227     void generateLocalSplitParameters(Function* func, GpuProgramType progType, MergeParameterList& mergedParams, ShaderParameterList& splitParams, LocalParameterMap& localParamsMap);
00228     
00231     void rebuildParameterList(Function* func, int paramsUsage, MergeParameterList& mergedParams);
00232 
00234     void rebuildFunctionInvocations(FunctionAtomInstanceList& funcAtomList, MergeParameterList& mergedParams, LocalParameterMap& localParamsMap);
00235 
00237     void buildParameterReferenceMap(FunctionAtomInstanceList& funcAtomList, ParameterOperandMap& paramsRefMap);
00238 
00240     void replaceParametersReferences(MergeParameterList& mergedParams, ParameterOperandMap& paramsRefMap);
00241 
00243     void replaceSplitParametersReferences(LocalParameterMap& localParamsMap, ParameterOperandMap& paramsRefMap);
00244 
00246     static int getParameterFloatCount(GpuConstantType type);        
00247 
00249     static int getParameterMaskByType(GpuConstantType type);
00250     
00252     static int getParameterMaskByFloatCount(int floatCount);
00253     
00255     void bindAutoParameters(Program* pCpuProgram, GpuProgramPtr pGpuProgram);
00256 
00257 protected:
00258     // Merging combinations defs.
00259     MergeCombinationList mParamMergeCombinations;
00260     // Maximum texcoord slots.
00261     int mMaxTexCoordSlots;
00262     // Maximum texcoord floats count.
00263     int mMaxTexCoordFloats;
00264     map<Function *, String *>::type  mFunctionMap;           // Map between function signatures and source code
00265 
00266 };
00267 
00268 
00272 }
00273 }
00274 
00275 #endif
00276 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:47