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-2012 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
    00058     ProgramProcessor();
    00059
    00061     virtual ~ProgramProcessor();
    00062
    00064     virtual const String& getTargetLanguage() const = 0;
    00065
    00071     virtual bool preCreateGpuPrograms(ProgramSet* programSet) = 0;
    00072
    00077     virtual bool postCreateGpuPrograms(ProgramSet* programSet) = 0;
    00078
    00079 // Protected types.
    00080 protected:
    00081
    00082     //-----------------------------------------------------------------------------
    00083     // Class that holds merge parameter information.
    00084     class _OgreRTSSExport MergeParameter
    00085     {
    00086     // Interface.
    00087     public:
    00089         MergeParameter();
    00090
    00091
    00093         void clear();
    00094
    00096         void addSourceParameter(ParameterPtr srcParam, int mask);
    00097
    00099         size_t getSourceParameterCount() const { return mSrcParameterCount; }
    00100
    00102         ParameterPtr getSourceParameter(unsigned int index) { return mSrcParameter[index]; }
    00103
    00105         int getSourceParameterMask(unsigned int index) const { return mSrcParameterMask[index]; }
    00106
    00108         int getDestinationParameterMask(unsigned int index) const { return mDstParameterMask[index]; }
    00109
    00111         int getUsedFloatCount();
    00112
    00114         ParameterPtr getDestinationParameter(int usage, int index);
    00115
    00116     protected:
    00117
    00119         void createDestinationParameter(int usage, int index);
    00120
    00121
    00122     protected:
    00123         // Destination merged parameter.
    00124         ParameterPtr mDstParameter;
    00125         // Source parameters - 4 source at max 1,1,1,1 -> 4.
    00126         ParameterPtr mSrcParameter[4];
    00127         // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
    00128         int mSrcParameterMask[4];
    00129         // Destination parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
    00130         int mDstParameterMask[4];
    00131         // The actual source parameters count.
    00132         size_t mSrcParameterCount;
    00133         // The number of used floats.
    00134         size_t mUsedFloatCount;
    00135     };
    00136     typedef vector<MergeParameter>::type    MergeParameterList;
    00137
    00138
    00139     //-----------------------------------------------------------------------------
    00140     // A struct that defines merge parameters combination.
    00141     struct _OgreRTSSExport MergeCombination
    00142     {
    00143         // The count of each source type. I.E (1 FLOAT1, 0 FLOAT2, 1 FLOAT3, 0 FLOAT4).
    00144         size_t srcParamterTypeCount[4];
    00145         // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
    00146         int srcParameterMask[4];
    00147
    00148         MergeCombination(
    00149             int float1Count, int float1Mask,
    00150             int float2Count, int float2Mask,
    00151             int float3Count, int float3Mask,
    00152             int float4Count, int float4Mask)
    00153         {
    00154             srcParamterTypeCount[0] = float1Count;
    00155             srcParamterTypeCount[1] = float2Count;
    00156             srcParamterTypeCount[2] = float3Count;
    00157             srcParamterTypeCount[3] = float4Count;
    00158             srcParameterMask[0]     = float1Mask;
    00159             srcParameterMask[1]     = float2Mask;
    00160             srcParameterMask[2]     = float3Mask;
    00161             srcParameterMask[3]     = float4Mask;
    00162
    00163         }
    00164     };
    00165     typedef vector<MergeCombination>::type  MergeCombinationList;
    00166
    00167     //-----------------------------------------------------------------------------
    00168     typedef vector<Operand*>::type                      OperandPtrVector;
    00169     typedef map<Parameter*, OperandPtrVector>::type     ParameterOperandMap;
    00170     typedef map<Parameter*, ParameterPtr>::type         LocalParameterMap;
    00171
    00172 protected:
    00173
    00175     void buildMergeCombinations();
    00176
    00182     virtual bool compactVsOutputs(Function* vsMain, Function* fsMain);
    00183
    00189     void countVsTexcoordOutputs(Function* vsMain, int& outTexCoordSlots, int& outTexCoordFloats);
    00190
    00195     void buildTexcoordTable(const ShaderParameterList& paramList, ShaderParameterList outParamsTable[4]);
    00196
    00197
    00202     void mergeParameters(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
    00203
    00204
    00209     void mergeParametersByPredefinedCombinations(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams);
    00210
    00216     bool mergeParametersByCombination(const MergeCombination& combination, ShaderParameterList paramsTable[4],
    00217                                                                  MergeParameter* mergedParameter);
    00218
    00224     void mergeParametersReminders(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
    00225
    00226
    00228     void generateLocalSplitParameters(Function* func, GpuProgramType progType, MergeParameterList& mergedParams, ShaderParameterList& splitParams, LocalParameterMap& localParamsMap);
    00229
    00232     void rebuildParameterList(Function* func, int paramsUsage, MergeParameterList& mergedParams);
    00233
    00235     void rebuildFunctionInvocations(FunctionAtomInstanceList& funcAtomList, MergeParameterList& mergedParams, LocalParameterMap& localParamsMap);
    00236
    00238     void buildParameterReferenceMap(FunctionAtomInstanceList& funcAtomList, ParameterOperandMap& paramsRefMap);
    00239
    00241     void replaceParametersReferences(MergeParameterList& mergedParams, ParameterOperandMap& paramsRefMap);
    00242
    00244     void replaceSplitParametersReferences(LocalParameterMap& localParamsMap, ParameterOperandMap& paramsRefMap);
    00245
    00247     static int getParameterFloatCount(GpuConstantType type);
    00248
    00250     static int getParameterMaskByType(GpuConstantType type);
    00251
    00253     static int getParameterMaskByFloatCount(int floatCount);
    00254
    00256     void bindAutoParameters(Program* pCpuProgram, GpuProgramPtr pGpuProgram);
    00257
    00258 protected:
    00259     // Merging combinations defs.
    00260     MergeCombinationList mParamMergeCombinations;
    00261     // Maximum texcoord slots.
    00262     int mMaxTexCoordSlots;
    00263     // Maximum texcoord floats count.
    00264     int mMaxTexCoordFloats;
    00265     std::map<Function *, String *>  mFunctionMap;           // Map between function signatures and source code
    00266
    00267 };
    00268
    00269
    00273 }
    00274 }
    00275
    00276 #endif
    00277

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:24