[1] | 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-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __HighLevelGpuProgram_H__ |
---|
| 30 | #define __HighLevelGpuProgram_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | #include "OgreGpuProgram.h" |
---|
| 34 | |
---|
| 35 | namespace Ogre { |
---|
| 36 | |
---|
| 37 | /** Abstract base class representing a high-level program (a vertex or |
---|
| 38 | fragment program). |
---|
| 39 | @remarks |
---|
| 40 | High-level programs are vertex and fragment programs written in a high-level |
---|
| 41 | language such as Cg or HLSL, and as such do not require you to write assembler code |
---|
| 42 | like GpuProgram does. However, the high-level program does eventually |
---|
| 43 | get converted (compiled) into assembler and then eventually microcode which is |
---|
| 44 | what runs on the GPU. As well as the convenience, some high-level languages like Cg allow |
---|
| 45 | you to write a program which will operate under both Direct3D and OpenGL, something |
---|
| 46 | which you cannot do with just GpuProgram (which requires you to write 2 programs and |
---|
| 47 | use each in a Technique to provide cross-API compatibility). Ogre will be creating |
---|
| 48 | a GpuProgram for you based on the high-level program, which is compiled specifically |
---|
| 49 | for the API being used at the time, but this process is transparent. |
---|
| 50 | @par |
---|
| 51 | You cannot create high-level programs direct - use HighLevelGpuProgramManager instead. |
---|
| 52 | Plugins can register new implementations of HighLevelGpuProgramFactory in order to add |
---|
| 53 | support for new languages without requiring changes to the core Ogre API. To allow |
---|
| 54 | custom parameters to be set, this class extends StringInterface - the application |
---|
| 55 | can query on the available custom parameters and get/set them without having to |
---|
| 56 | link specifically with it. |
---|
| 57 | */ |
---|
| 58 | class _OgreExport HighLevelGpuProgram : public GpuProgram |
---|
| 59 | { |
---|
| 60 | protected: |
---|
| 61 | /// Whether the high-level program (and it's parameter defs) is loaded |
---|
| 62 | bool mHighLevelLoaded; |
---|
| 63 | /// The underlying assembler program |
---|
| 64 | GpuProgramPtr mAssemblerProgram; |
---|
| 65 | /// Have we built the name->index parameter map yet? |
---|
| 66 | mutable bool mConstantDefsBuilt; |
---|
| 67 | /// Parameter name -> ConstantDefinition map, shared instance used by all parameter objects |
---|
| 68 | mutable GpuNamedConstants mConstantDefs; |
---|
| 69 | |
---|
| 70 | /// Internal load high-level portion if not loaded |
---|
| 71 | virtual void loadHighLevel(void); |
---|
| 72 | /// Internal unload high-level portion if loaded |
---|
| 73 | virtual void unloadHighLevel(void); |
---|
| 74 | /** Internal load implementation, loads just the high-level portion, enough to |
---|
| 75 | get parameters. |
---|
| 76 | */ |
---|
| 77 | virtual void loadHighLevelImpl(void); |
---|
| 78 | /** Internal method for creating an appropriate low-level program from this |
---|
| 79 | high-level program, must be implemented by subclasses. */ |
---|
| 80 | virtual void createLowLevelImpl(void) = 0; |
---|
| 81 | /// Internal unload implementation, must be implemented by subclasses |
---|
| 82 | virtual void unloadHighLevelImpl(void) = 0; |
---|
| 83 | /// Populate the passed parameters with name->index map |
---|
| 84 | virtual void populateParameterNames(GpuProgramParametersSharedPtr params); |
---|
| 85 | /** Build the constant definition map, must be overridden. |
---|
| 86 | @note The implementation must fill in the mConstantDefs field at a minimum, |
---|
| 87 | and if the program requires that parameters are bound using logical |
---|
| 88 | parameter indexes then the mFloatLogicalToPhysical and mIntLogicalToPhysical |
---|
| 89 | maps must also be populated. |
---|
| 90 | */ |
---|
| 91 | virtual void buildConstantDefinitions() const = 0; |
---|
| 92 | |
---|
| 93 | /** @copydoc Resource::loadImpl */ |
---|
| 94 | void loadImpl(); |
---|
| 95 | /** @copydoc Resource::unloadImpl */ |
---|
| 96 | void unloadImpl(); |
---|
| 97 | public: |
---|
| 98 | /** Constructor, should be used only by factory classes. */ |
---|
| 99 | HighLevelGpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle, |
---|
| 100 | const String& group, bool isManual = false, ManualResourceLoader* loader = 0); |
---|
| 101 | ~HighLevelGpuProgram(); |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | /** Creates a new parameters object compatible with this program definition. |
---|
| 105 | @remarks |
---|
| 106 | Unlike low-level assembly programs, parameters objects are specific to the |
---|
| 107 | program and therefore must be created from it rather than by the |
---|
| 108 | HighLevelGpuProgramManager. This method creates a new instance of a parameters |
---|
| 109 | object containing the definition of the parameters this program understands. |
---|
| 110 | */ |
---|
| 111 | GpuProgramParametersSharedPtr createParameters(void); |
---|
| 112 | /** @copydoc GpuProgram::getBindingDelegate */ |
---|
| 113 | GpuProgram* _getBindingDelegate(void) { return mAssemblerProgram.getPointer(); } |
---|
| 114 | |
---|
| 115 | /** Get the full list of GpuConstantDefinition instances. |
---|
| 116 | @note |
---|
| 117 | Only available if this parameters object has named parameters. |
---|
| 118 | */ |
---|
| 119 | const GpuNamedConstants& getConstantDefinitions() const; |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | }; |
---|
| 125 | |
---|
| 126 | /** Specialisation of SharedPtr to allow SharedPtr to be assigned to HighLevelGpuProgramPtr |
---|
| 127 | @note Has to be a subclass since we need operator=. |
---|
| 128 | We could templatise this instead of repeating per Resource subclass, |
---|
| 129 | except to do so requires a form VC6 does not support i.e. |
---|
| 130 | ResourceSubclassPtr<T> : public SharedPtr<T> |
---|
| 131 | */ |
---|
| 132 | class _OgreExport HighLevelGpuProgramPtr : public SharedPtr<HighLevelGpuProgram> |
---|
| 133 | { |
---|
| 134 | public: |
---|
| 135 | HighLevelGpuProgramPtr() : SharedPtr<HighLevelGpuProgram>() {} |
---|
| 136 | explicit HighLevelGpuProgramPtr(HighLevelGpuProgram* rep) : SharedPtr<HighLevelGpuProgram>(rep) {} |
---|
| 137 | HighLevelGpuProgramPtr(const HighLevelGpuProgramPtr& r) : SharedPtr<HighLevelGpuProgram>(r) {} |
---|
| 138 | HighLevelGpuProgramPtr(const ResourcePtr& r) : SharedPtr<HighLevelGpuProgram>() |
---|
| 139 | { |
---|
| 140 | // lock & copy other mutex pointer |
---|
| 141 | OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) |
---|
| 142 | { |
---|
| 143 | OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) |
---|
| 144 | OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) |
---|
| 145 | pRep = static_cast<HighLevelGpuProgram*>(r.getPointer()); |
---|
| 146 | pUseCount = r.useCountPointer(); |
---|
| 147 | if (pUseCount) |
---|
| 148 | { |
---|
| 149 | ++(*pUseCount); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /// Operator used to convert a ResourcePtr to a HighLevelGpuProgramPtr |
---|
| 155 | HighLevelGpuProgramPtr& operator=(const ResourcePtr& r) |
---|
| 156 | { |
---|
| 157 | if (pRep == static_cast<HighLevelGpuProgram*>(r.getPointer())) |
---|
| 158 | return *this; |
---|
| 159 | release(); |
---|
| 160 | // lock & copy other mutex pointer |
---|
| 161 | OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) |
---|
| 162 | { |
---|
| 163 | OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) |
---|
| 164 | OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) |
---|
| 165 | pRep = static_cast<HighLevelGpuProgram*>(r.getPointer()); |
---|
| 166 | pUseCount = r.useCountPointer(); |
---|
| 167 | if (pUseCount) |
---|
| 168 | { |
---|
| 169 | ++(*pUseCount); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | // RHS must be a null pointer |
---|
| 175 | assert(r.isNull() && "RHS must be null if it has no mutex!"); |
---|
| 176 | setNull(); |
---|
| 177 | } |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | /// Operator used to convert a GpuProgramPtr to a HighLevelGpuProgramPtr |
---|
| 181 | HighLevelGpuProgramPtr& operator=(const GpuProgramPtr& r); |
---|
| 182 | }; |
---|
| 183 | |
---|
| 184 | } |
---|
| 185 | #endif |
---|