CmCgProgram.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __CgProgram_H__
  25. #define __CgProgram_H__
  26. #include "CmPrerequisites.h"
  27. #include <Cg/cg.h>
  28. #include "CmHighLevelGpuProgram.h"
  29. namespace CamelotEngine {
  30. /** Specialisation of HighLevelGpuProgram to provide support for nVidia's CG language.
  31. @remarks
  32. Cg can be used to compile common, high-level, C-like code down to assembler
  33. language for both GL and Direct3D, for multiple graphics cards. You must
  34. supply a list of profiles which your program must support using
  35. setProfiles() before the program is loaded in order for this to work. The
  36. program will then negotiate with the renderer to compile the appropriate program
  37. for the API and graphics card capabilities.
  38. */
  39. class CM_EXPORT CgProgram : public HighLevelGpuProgram
  40. {
  41. public:
  42. ~CgProgram();
  43. /** Gets the entry point defined for this program. */
  44. const String& getEntryPoint(void) const { return mEntryPoint; }
  45. /** Sets the compilation arguments for this program ie the first method called. */
  46. void setCompileArguments(const String& args) { mCompileArgs = args; }
  47. /** Gets the entry point defined for this program. */
  48. const String& getCompileArguments(void) const { return mCompileArgs; }
  49. /// Overridden from GpuProgram
  50. bool isSupported(void) const;
  51. /// Overridden from GpuProgram
  52. const String& getLanguage(void) const;
  53. /// scan the file for #include and replace with source from the OGRE resources
  54. static String resolveCgIncludes(const String& source, Resource* resourceBeingLoaded, const String& fileName);
  55. protected:
  56. friend class CgProgramFactory;
  57. /// The CG context to use, passed in by factory
  58. CGcontext mCgContext;
  59. /// Program handle
  60. CGprogram mCgProgram;
  61. CgProgram(CGcontext context, const String& source, const String& entryPoint, const String& language,
  62. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  63. /** Internal load implementation, must be implemented by subclasses.
  64. */
  65. void loadFromSource(void);
  66. /** Internal method for creating an appropriate low-level program from this
  67. high-level program, must be implemented by subclasses. */
  68. void createLowLevelImpl(void);
  69. /// Internal unload implementation, must be implemented by subclasses
  70. void unloadHighLevelImpl(void);
  71. /// Populate the passed parameters with name->index map, must be overridden
  72. void buildConstantDefinitions() const;
  73. /// Recurse down structures getting data on parameters
  74. void recurseParams(CGparameter param, UINT32 contextArraySize = 1) const;
  75. /// Turn a Cg type into a GpuConstantType and number of elements
  76. void mapTypeAndElementSize(CGtype cgType, bool isRegisterCombiner, GpuConstantDefinition& def) const;
  77. String mSelectedProfile;
  78. CGprofile mSelectedCgProfile;
  79. String mCompileArgs;
  80. // Unfortunately Cg uses char** for arguments - bleh
  81. // This is a null-terminated list of char* (each null terminated)
  82. char** mCgArguments;
  83. /// Internal method which works out which profile to use for this program
  84. void selectProfile(void);
  85. /// Internal method which merges manual and automatic compile arguments
  86. void buildArgs(void);
  87. /// Releases memory for the horrible Cg char**
  88. void freeCgArgs(void);
  89. /************************************************************************/
  90. /* SERIALIZATION */
  91. /************************************************************************/
  92. public:
  93. friend class CgProgramRTTI;
  94. static RTTITypeBase* getRTTIStatic();
  95. virtual RTTITypeBase* getRTTI() const;
  96. };
  97. /** Utility function, checks Cg for errors, throw exception if detected.
  98. @param ogreMethod Ogre method name, as Class::method
  99. @param errorTextPrefix The text to prefix the Cg error text with
  100. */
  101. void checkForCgError(const String& ogreMethod, const String& errorTextPrefix, CGcontext context);
  102. }
  103. #endif