gfxCardProfile.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/gfxCardProfile.h"
  24. #include "console/console.h"
  25. #include "console/engineAPI.h"
  26. #include "core/volume.h"
  27. // NOTE: The script class docs are in
  28. // Documentation\scriptDocs\docs\classGFXCardProfiler.txt
  29. void GFXCardProfiler::loadProfileScript(const char* aScriptName)
  30. {
  31. const String profilePath = Con::getVariable( "$pref::Video::profilePath" );
  32. String scriptName = !profilePath.isEmpty() ? profilePath.c_str() : "profile";
  33. scriptName += "/";
  34. scriptName += aScriptName;
  35. void *data = NULL;
  36. U32 dataSize = 0;
  37. Torque::FS::ReadFile( scriptName.c_str(), data, dataSize, true );
  38. if(data == NULL)
  39. {
  40. Con::warnf(" - No card profile %s exists", scriptName.c_str());
  41. return;
  42. }
  43. const char *script = static_cast<const char *>(data);
  44. Con::printf(" - Loaded card profile %s", scriptName.c_str());
  45. Con::executef("eval", script);
  46. delete[] script;
  47. }
  48. void GFXCardProfiler::loadProfileScripts(const String& render, const String& vendor, const String& card, const String& version)
  49. {
  50. String script = render + ".cs";
  51. loadProfileScript(script);
  52. script = render + "." + vendor + ".cs";
  53. loadProfileScript(script);
  54. script = render + "." + vendor + "." + card + ".cs";
  55. loadProfileScript(script);
  56. script = render + "." + vendor + "." + card + "." + version + ".cs";
  57. loadProfileScript(script);
  58. }
  59. GFXCardProfiler::GFXCardProfiler() : mVideoMemory( 0 )
  60. {
  61. }
  62. GFXCardProfiler::~GFXCardProfiler()
  63. {
  64. mCapDictionary.clear();
  65. }
  66. String GFXCardProfiler::strippedString(const char *string)
  67. {
  68. String res = "";
  69. // And fill it with the stripped string...
  70. const char *a=string;
  71. while(*a)
  72. {
  73. if(isalnum(*a))
  74. {
  75. res += *a;
  76. }
  77. a++;
  78. }
  79. return res;
  80. }
  81. void GFXCardProfiler::init()
  82. {
  83. // Spew a bit...
  84. Con::printf("Initializing GFXCardProfiler (%s)", getRendererString().c_str());
  85. Con::printf(" o Chipset : '%s'", getChipString().c_str());
  86. Con::printf(" o Card : '%s'", getCardString().c_str());
  87. Con::printf(" o Version : '%s'", getVersionString().c_str());
  88. // Do card-specific setup...
  89. Con::printf(" - Scanning card capabilities...");
  90. setupCardCapabilities();
  91. // And finally, load stuff up...
  92. String render = strippedString(getRendererString());
  93. String chipset = strippedString(getChipString());
  94. String card = strippedString(getCardString());
  95. String version = strippedString(getVersionString());
  96. Con::printf(" - Loading card profiles...");
  97. loadProfileScripts(render, chipset, card, version);
  98. }
  99. U32 GFXCardProfiler::queryProfile(const String &cap)
  100. {
  101. U32 res;
  102. if( _queryCardCap( cap, res ) )
  103. return res;
  104. if(mCapDictionary.contains(cap))
  105. return mCapDictionary[cap];
  106. Con::errorf( "GFXCardProfiler (%s) - Unknown capability '%s'.", getRendererString().c_str(), cap.c_str() );
  107. return 0;
  108. }
  109. U32 GFXCardProfiler::queryProfile(const String &cap, U32 defaultValue)
  110. {
  111. PROFILE_SCOPE( GFXCardProfiler_queryProfile );
  112. U32 res;
  113. if( _queryCardCap( cap, res ) )
  114. return res;
  115. if( mCapDictionary.contains( cap ) )
  116. return mCapDictionary[cap];
  117. else
  118. return defaultValue;
  119. }
  120. void GFXCardProfiler::setCapability(const String &cap, U32 value)
  121. {
  122. // Check for dups.
  123. if( mCapDictionary.contains( cap ) )
  124. {
  125. Con::warnf( "GFXCardProfiler (%s) - Setting capability '%s' multiple times.", getRendererString().c_str(), cap.c_str() );
  126. mCapDictionary[cap] = value;
  127. return;
  128. }
  129. // Insert value as necessary.
  130. Con::printf( "GFXCardProfiler (%s) - Setting capability '%s' to %d.", getRendererString().c_str(), cap.c_str(), value );
  131. mCapDictionary.insert( cap, value );
  132. }
  133. bool GFXCardProfiler::checkFormat( const GFXFormat fmt, const GFXTextureProfile *profile, bool &inOutAutogenMips )
  134. {
  135. return _queryFormat( fmt, profile, inOutAutogenMips );
  136. }
  137. DECLARE_SCOPE( GFXCardProfilerAPI );
  138. IMPLEMENT_SCOPE( GFXCardProfilerAPI, GFXCardProfiler,, "");
  139. ConsoleDoc(
  140. "@class GFXCardProfilerAPI\n"
  141. "@brief This class is the interface between TorqueScript and GFXCardProfiler.\n\n"
  142. "You will not actually declare GFXCardProfilerAPI in TorqueScript. It exists solely "
  143. "to give access to the GFXCardProfiler's querying functions, such as GFXCardProfiler::getRenderer.\n\n"
  144. "@tsexample\n"
  145. "// Example of accessing GFXCardProfiler function from script\n"
  146. "// Notice you are not using the API version\n"
  147. "%videoMem = GFXCardProfiler::getVideoMemoryMB();\n"
  148. "@endtsexample\n\n"
  149. "@see GFXCardProfiler for more information\n\n"
  150. "@ingroup GFX\n"
  151. );
  152. DefineEngineStaticMethod( GFXCardProfilerAPI, getVersion, String, (),,
  153. "Returns the driver version string." )
  154. {
  155. return GFX->getCardProfiler()->getVersionString();
  156. }
  157. DefineEngineStaticMethod( GFXCardProfilerAPI, getCard, String, (),,
  158. "Returns the card name." )
  159. {
  160. return GFX->getCardProfiler()->getCardString();
  161. }
  162. DefineEngineStaticMethod( GFXCardProfilerAPI, getVendor, String, (),,
  163. "Returns the card vendor name." )
  164. {
  165. // TODO: Fix all of this vendor crap, it's not consistent
  166. return GFX->getCardProfiler()->getChipString();
  167. }
  168. DefineEngineStaticMethod( GFXCardProfilerAPI, getRenderer, String, (),,
  169. "Returns the renderer name. For example D3D9 or OpenGL." )
  170. {
  171. return GFX->getCardProfiler()->getRendererString();
  172. }
  173. DefineEngineStaticMethod( GFXCardProfilerAPI, getVideoMemoryMB, S32, (),,
  174. "Returns the amount of video memory in megabytes." )
  175. {
  176. return GFX->getCardProfiler()->getVideoMemoryInMB();
  177. }
  178. DefineEngineStaticMethod( GFXCardProfilerAPI, setCapability, void, ( const char *name, S32 value ),,
  179. "Used to set the value for a specific card capability.\n"
  180. "@param name The name of the capability being set.\n"
  181. "@param value The value to set for that capability." )
  182. {
  183. GFX->getCardProfiler()->setCapability( name, (U32)value );
  184. }
  185. DefineEngineStaticMethod( GFXCardProfilerAPI, queryProfile, S32, ( const char *name, S32 defaultValue ),,
  186. "Used to query the value of a specific card capability.\n"
  187. "@param name The name of the capability being queried.\n"
  188. "@param defaultValue The value to return if the capability is not defined." )
  189. {
  190. return (S32)GFX->getCardProfiler()->queryProfile( name, (U32)defaultValue );
  191. }