OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreShaderProgramProcessor.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 -----------------------------------------------------------------------------
26 */
27 #ifndef _ShaderProgramProcessor_
28 #define _ShaderProgramProcessor_
29 
31 #include "OgreShaderFunctionAtom.h"
32 #include "OgreGpuProgram.h"
33 
34 namespace Ogre {
35 namespace RTShader {
36 
49 {
50 
51 // Interface.
52 public:
53 
57 
59  virtual ~ProgramProcessor();
60 
62  virtual const String& getTargetLanguage() const = 0;
63 
69  virtual bool preCreateGpuPrograms(ProgramSet* programSet) = 0;
70 
75  virtual bool postCreateGpuPrograms(ProgramSet* programSet) = 0;
76 
77 // Protected types.
78 protected:
79 
80  //-----------------------------------------------------------------------------
81  // Class that holds merge parameter information.
83  {
84  // Interface.
85  public:
88 
89 
91  void clear();
92 
94  void addSourceParameter(ParameterPtr srcParam, int mask);
95 
97  size_t getSourceParameterCount() const { return mSrcParameterCount; }
98 
100  ParameterPtr getSourceParameter(unsigned int index) { return mSrcParameter[index]; }
101 
103  int getSourceParameterMask(unsigned int index) const { return mSrcParameterMask[index]; }
104 
106  int getDestinationParameterMask(unsigned int index) const { return mDstParameterMask[index]; }
107 
109  int getUsedFloatCount();
110 
112  ParameterPtr getDestinationParameter(int usage, int index);
113 
114  protected:
115 
117  void createDestinationParameter(int usage, int index);
118 
119 
120  protected:
121  // Destination merged parameter.
123  // Source parameters - 4 source at max 1,1,1,1 -> 4.
124  ParameterPtr mSrcParameter[4];
125  // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
126  int mSrcParameterMask[4];
127  // Destination parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
128  int mDstParameterMask[4];
129  // The actual source parameters count.
131  // The number of used floats.
133  };
135 
136 
137  //-----------------------------------------------------------------------------
138  // A struct that defines merge parameters combination.
140  {
141  // The count of each source type. I.E (1 FLOAT1, 0 FLOAT2, 1 FLOAT3, 0 FLOAT4).
142  size_t srcParameterTypeCount[4];
143  // Source parameters mask. OPM_ALL means all fields used, otherwise it is split source parameter.
144  int srcParameterMask[4];
145 
147  int float1Count, int float1Mask,
148  int float2Count, int float2Mask,
149  int float3Count, int float3Mask,
150  int float4Count, int float4Mask)
151  {
152  srcParameterTypeCount[0] = float1Count;
153  srcParameterTypeCount[1] = float2Count;
154  srcParameterTypeCount[2] = float3Count;
155  srcParameterTypeCount[3] = float4Count;
156  srcParameterMask[0] = float1Mask;
157  srcParameterMask[1] = float2Mask;
158  srcParameterMask[2] = float3Mask;
159  srcParameterMask[3] = float4Mask;
160 
161  }
162  };
164 
165  //-----------------------------------------------------------------------------
169 
170 protected:
171 
173  void buildMergeCombinations();
174 
180  virtual bool compactVsOutputs(Function* vsMain, Function* fsMain);
181 
187  void countVsTexcoordOutputs(Function* vsMain, int& outTexCoordSlots, int& outTexCoordFloats);
188 
193  void buildTexcoordTable(const ShaderParameterList& paramList, ShaderParameterList outParamsTable[4]);
194 
195 
200  void mergeParameters(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
201 
202 
207  void mergeParametersByPredefinedCombinations(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams);
208 
214  bool mergeParametersByCombination(const MergeCombination& combination, ShaderParameterList paramsTable[4],
215  MergeParameter* mergedParameter);
216 
222  void mergeParametersReminders(ShaderParameterList paramsTable[4], MergeParameterList& mergedParams, ShaderParameterList& splitParams);
223 
224 
226  void generateLocalSplitParameters(Function* func, GpuProgramType progType, MergeParameterList& mergedParams, ShaderParameterList& splitParams, LocalParameterMap& localParamsMap);
227 
230  void rebuildParameterList(Function* func, int paramsUsage, MergeParameterList& mergedParams);
231 
233  void rebuildFunctionInvocations(FunctionAtomInstanceList& funcAtomList, MergeParameterList& mergedParams, LocalParameterMap& localParamsMap);
234 
236  void buildParameterReferenceMap(FunctionAtomInstanceList& funcAtomList, ParameterOperandMap& paramsRefMap);
237 
239  void replaceParametersReferences(MergeParameterList& mergedParams, ParameterOperandMap& paramsRefMap);
240 
242  void replaceSplitParametersReferences(LocalParameterMap& localParamsMap, ParameterOperandMap& paramsRefMap);
243 
245  static int getParameterFloatCount(GpuConstantType type);
246 
248  static int getParameterMaskByType(GpuConstantType type);
249 
251  static int getParameterMaskByFloatCount(int floatCount);
252 
254  void bindAutoParameters(Program* pCpuProgram, GpuProgramPtr pGpuProgram);
255 
256 protected:
257  // Merging combinations defs.
259  // Maximum texcoord slots.
261  // Maximum texcoord floats count.
263  map<Function *, String *>::type mFunctionMap; // Map between function signatures and source code
264 
265 };
266 
267 
271 }
272 }
273 
274 #endif
275 
map< Parameter *, ParameterPtr >::type LocalParameterMap
std::vector< T, A > type
MergeCombination(int float1Count, int float1Mask, int float2Count, int float2Mask, int float3Count, int float3Mask, int float4Count, int float4Mask)
map< Function *, String * >::type mFunctionMap
GpuProgramType
Enumerates the types of programs which can run on the GPU.
A class that provides extra processing services on CPU based programs.
#define _OgreRTSSExport
int getDestinationParameterMask(unsigned int index) const
Return destination parameter mask by index.
int getSourceParameterMask(unsigned int index) const
Return source parameter mask by index.
vector< ParameterPtr >::type ShaderParameterList
ParameterPtr getSourceParameter(unsigned int index)
Return source parameter by index.
size_t getSourceParameterCount() const
Return the source parameter count.
vector< MergeParameter >::type MergeParameterList
GpuConstantType
Enumeration of the types of constant we may encounter in programs.
A class that represents a shader based program function.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
std::map< K, V, P, A > type
vector< FunctionAtom * >::type FunctionAtomInstanceList
vector< MergeCombination >::type MergeCombinationList
A class that represents a shader based program.
_StringBase String
Definition: OgreCommon.h:53
Container class for shader based programs.
map< Parameter *, OperandPtrVector >::type ParameterOperandMap