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-2013 Torus Knot Software Ltd 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ----------------------------------------------------------------------------- 27 */ 28 #ifndef __CgProgram_H__ 29 #define __CgProgram_H__ 30 31 #include "OgreCgPrerequisites.h" 32 #include "OgreHighLevelGpuProgram.h" 33 #include "OgreStringVector.h" 34 35 namespace Ogre { 36 /** Specialisation of HighLevelGpuProgram to provide support for nVidia's CG language. 37 @remarks 38 Cg can be used to compile common, high-level, C-like code down to assembler 39 language for both GL and Direct3D, for multiple graphics cards. You must 40 supply a list of profiles which your program must support using 41 setProfiles() before the program is loaded in order for this to work. The 42 program will then negotiate with the renderer to compile the appropriate program 43 for the API and graphics card capabilities. 44 */ 45 class CgProgram : public HighLevelGpuProgram 46 { 47 public: 48 /// Command object for setting entry point 49 class CmdEntryPoint : public ParamCommand 50 { 51 public: 52 String doGet(const void* target) const; 53 void doSet(void* target, const String& val); 54 }; 55 /// Command object for setting profiles 56 class CmdProfiles : public ParamCommand 57 { 58 public: 59 String doGet(const void* target) const; 60 void doSet(void* target, const String& val); 61 }; 62 /// Command object for setting compilation arguments 63 class CmdArgs : public ParamCommand 64 { 65 public: 66 String doGet(const void* target) const; 67 void doSet(void* target, const String& val); 68 }; 69 70 protected: 71 72 static CmdEntryPoint msCmdEntryPoint; 73 static CmdProfiles msCmdProfiles; 74 static CmdArgs msCmdArgs; 75 76 /// The CG context to use, passed in by factory 77 CGcontext mCgContext; 78 /** Internal load implementation, must be implemented by subclasses. 79 */ 80 void loadFromSource(void); 81 /** Internal method for creating an appropriate low-level program from this 82 high-level program, must be implemented by subclasses. */ 83 void createLowLevelImpl(void); 84 /// Internal unload implementation, must be implemented by subclasses 85 void unloadHighLevelImpl(void); 86 /// Populate the passed parameters with name->index map, must be overridden 87 void buildConstantDefinitions() const; 88 89 /// Load the high-level part in a thread-safe way, required for delegate functionality 90 void loadHighLevelSafe(); 91 92 /// Recurse down structures getting data on parameters 93 void recurseParams(CGparameter param, size_t contextArraySize = 1); 94 /// Turn a Cg type into a GpuConstantType and number of elements 95 void mapTypeAndElementSize(CGtype cgType, bool isRegisterCombiner, GpuConstantDefinition& def) const; 96 97 StringVector mProfiles; 98 String mEntryPoint; 99 String mSelectedProfile; 100 String mProgramString; 101 CGprofile mSelectedCgProfile; 102 String mCompileArgs; 103 // Unfortunately Cg uses char** for arguments - bleh 104 // This is a null-terminated list of char* (each null terminated) 105 char** mCgArguments; 106 107 GpuConstantDefinitionMap mParametersMap; 108 size_t mParametersMapSizeAsBuffer; 109 map<String,int>::type mSamplerRegisterMap; 110 CGenum mInputOp, mOutputOp; 111 112 /// Internal method which works out which profile to use for this program 113 void selectProfile(void); 114 /// Internal method which merges manual and automatic compile arguments 115 void buildArgs(void); 116 /// Releases memory for the horrible Cg char** 117 void freeCgArgs(void); 118 119 void getMicrocodeFromCache(void); 120 void compileMicrocode(void); 121 void addMicrocodeToCache(); 122 123 private: 124 HighLevelGpuProgramPtr mDelegate; 125 String getHighLevelLanguage() const; 126 String getHighLevelTarget() const; 127 void fixHighLevelOutput(String& hlSource); 128 129 130 public: 131 CgProgram(ResourceManager* creator, const String& name, ResourceHandle handle, 132 const String& group, bool isManual, ManualResourceLoader* loader, 133 CGcontext context); 134 ~CgProgram(); 135 136 /** Sets the entry point for this program ie the first method called. */ setEntryPoint(const String & entryPoint)137 void setEntryPoint(const String& entryPoint) { mEntryPoint = entryPoint; } 138 /** Gets the entry point defined for this program. */ getEntryPoint(void)139 const String& getEntryPoint(void) const { return mEntryPoint; } 140 /** Sets the Cg profiles which can be supported by the program. */ 141 void setProfiles(const StringVector& profiles); 142 /** Gets the Cg profiles which can be supported by the program. */ getProfiles(void)143 const StringVector& getProfiles(void) const { return mProfiles; } 144 /** Sets the compilation arguments for this program ie the first method called. */ setCompileArguments(const String & args)145 void setCompileArguments(const String& args) { mCompileArgs = args; } 146 /** Gets the entry point defined for this program. */ getCompileArguments(void)147 const String& getCompileArguments(void) const { return mCompileArgs; } 148 /// Overridden from GpuProgram 149 bool isSupported(void) const; 150 /// Overridden from GpuProgram 151 const String& getLanguage(void) const; 152 153 GpuProgramParametersSharedPtr createParameters(); 154 GpuProgram* _getBindingDelegate(); 155 156 bool isSkeletalAnimationIncluded(void) const; 157 bool isMorphAnimationIncluded(void) const; 158 bool isPoseAnimationIncluded(void) const; 159 bool isVertexTextureFetchRequired(void) const; 160 GpuProgramParametersSharedPtr getDefaultParameters(void); 161 bool hasDefaultParameters(void) const; 162 bool getPassSurfaceAndLightStates(void) const; 163 bool getPassFogStates(void) const; 164 bool getPassTransformStates(void) const; 165 bool hasCompileError(void) const; 166 void resetCompileError(void); 167 size_t getSize(void) const; 168 void touch(void); 169 170 /// Scan the file for #include and replace with source from the OGRE resources 171 static String resolveCgIncludes(const String& source, Resource* resourceBeingLoaded, const String& fileName); 172 }; 173 } 174 175 #endif 176