gfxCardProfile.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "console/script.h"
  27. #include "core/volume.h"
  28. // NOTE: The script class docs are in
  29. // Documentation\scriptDocs\docs\classGFXCardProfiler.txt
  30. void GFXCardProfiler::loadProfileScript(const char* aScriptName)
  31. {
  32. const String profilePath = Con::getVariable( "$pref::Video::profilePath" );
  33. String scriptName = !profilePath.isEmpty() ? profilePath.c_str() : "profile";
  34. scriptName += "/";
  35. scriptName += aScriptName;
  36. void *data = NULL;
  37. U32 dataSize = 0;
  38. Torque::FS::ReadFile( scriptName.c_str(), data, dataSize, true );
  39. if(data == NULL)
  40. {
  41. #ifdef TORQUE_DEBUG
  42. Con::warnf(" - No card profile %s exists", scriptName.c_str());
  43. #endif
  44. return;
  45. }
  46. const char *script = static_cast<const char *>(data);
  47. #ifdef TORQUE_DEBUG
  48. Con::printf(" - Loaded card profile %s", scriptName.c_str());
  49. #endif
  50. Con::evaluate(script, false, NULL);
  51. delete[] script;
  52. }
  53. void GFXCardProfiler::loadProfileScripts(const String& render, const String& vendor, const String& card, const String& version)
  54. {
  55. String script = render + "." TORQUE_SCRIPT_EXTENSION;
  56. loadProfileScript(script);
  57. script = render + "." + vendor + "." TORQUE_SCRIPT_EXTENSION;
  58. loadProfileScript(script);
  59. script = render + "." + vendor + "." + card + "." TORQUE_SCRIPT_EXTENSION;
  60. loadProfileScript(script);
  61. script = render + "." + vendor + "." + card + "." + version + "." TORQUE_SCRIPT_EXTENSION;
  62. loadProfileScript(script);
  63. }
  64. GFXCardProfiler::GFXCardProfiler() : mVideoMemory( 0 )
  65. {
  66. }
  67. GFXCardProfiler::~GFXCardProfiler()
  68. {
  69. mCapDictionary.clear();
  70. }
  71. String GFXCardProfiler::strippedString(const char *string)
  72. {
  73. String res = "";
  74. // And fill it with the stripped string...
  75. const char *a=string;
  76. while(*a)
  77. {
  78. if(isalnum(*a))
  79. {
  80. res += *a;
  81. }
  82. a++;
  83. }
  84. return res;
  85. }
  86. void GFXCardProfiler::init()
  87. {
  88. // Spew a bit...
  89. Con::printf("Initializing GFXCardProfiler (%s)", getRendererString().c_str());
  90. Con::printf(" o Chipset : '%s'", getChipString().c_str());
  91. Con::printf(" o Card : '%s'", getCardString().c_str());
  92. Con::printf(" o Version : '%s'", getVersionString().c_str());
  93. Con::printf(" o VRAM : %d MB", getVideoMemoryInMB());
  94. // Do card-specific setup...
  95. Con::printf(" - Scanning card capabilities...");
  96. setupCardCapabilities();
  97. // And finally, load stuff up...
  98. String render = strippedString(getRendererString());
  99. String chipset = strippedString(getChipString());
  100. String card = strippedString(getCardString());
  101. String version = strippedString(getVersionString());
  102. Con::printf(" - Loading card profiles...");
  103. loadProfileScripts(render, chipset, card, version);
  104. }
  105. U32 GFXCardProfiler::queryProfile(const String &cap)
  106. {
  107. U32 res;
  108. if( _queryCardCap( cap, res ) )
  109. return res;
  110. if(mCapDictionary.contains(cap))
  111. return mCapDictionary[cap];
  112. Con::errorf( "GFXCardProfiler (%s) - Unknown capability '%s'.", getRendererString().c_str(), cap.c_str() );
  113. return 0;
  114. }
  115. U32 GFXCardProfiler::queryProfile(const String &cap, U32 defaultValue)
  116. {
  117. PROFILE_SCOPE( GFXCardProfiler_queryProfile );
  118. U32 res;
  119. if( _queryCardCap( cap, res ) )
  120. return res;
  121. if( mCapDictionary.contains( cap ) )
  122. return mCapDictionary[cap];
  123. else
  124. return defaultValue;
  125. }
  126. void GFXCardProfiler::setCapability(const String &cap, U32 value)
  127. {
  128. // Check for dups.
  129. if( mCapDictionary.contains( cap ) )
  130. {
  131. Con::warnf( "GFXCardProfiler (%s) - Setting capability '%s' multiple times.", getRendererString().c_str(), cap.c_str() );
  132. mCapDictionary[cap] = value;
  133. return;
  134. }
  135. // Insert value as necessary.
  136. Con::printf( "GFXCardProfiler (%s) - Setting capability '%s' to %d.", getRendererString().c_str(), cap.c_str(), value );
  137. mCapDictionary.insert( cap, value );
  138. }
  139. bool GFXCardProfiler::checkFormat( const GFXFormat fmt, const GFXTextureProfile *profile, bool &inOutAutogenMips )
  140. {
  141. return _queryFormat( fmt, profile, inOutAutogenMips );
  142. }
  143. DECLARE_SCOPE( GFXCardProfilerAPI );
  144. IMPLEMENT_SCOPE( GFXCardProfilerAPI, GFXCardProfiler,, "");
  145. ConsoleDoc(
  146. "@class GFXCardProfilerAPI\n"
  147. "@brief This class is the interface between TorqueScript and GFXCardProfiler.\n\n"
  148. "You will not actually declare GFXCardProfilerAPI in TorqueScript. It exists solely "
  149. "to give access to the GFXCardProfiler's querying functions, such as GFXCardProfiler::getRenderer.\n\n"
  150. "@tsexample\n"
  151. "// Example of accessing GFXCardProfiler function from script\n"
  152. "// Notice you are not using the API version\n"
  153. "%videoMem = GFXCardProfiler::getVideoMemoryMB();\n"
  154. "@endtsexample\n\n"
  155. "@see GFXCardProfiler for more information\n\n"
  156. "@ingroup GFX\n"
  157. );
  158. DefineEngineStaticMethod( GFXCardProfilerAPI, getVersion, String, (),,
  159. "Returns the driver version string." )
  160. {
  161. return GFX->getCardProfiler()->getVersionString();
  162. }
  163. DefineEngineStaticMethod( GFXCardProfilerAPI, getCard, String, (),,
  164. "Returns the card name." )
  165. {
  166. return GFX->getCardProfiler()->getCardString();
  167. }
  168. DefineEngineStaticMethod( GFXCardProfilerAPI, getVendor, String, (),,
  169. "Returns the card vendor name." )
  170. {
  171. // TODO: Fix all of this vendor crap, it's not consistent
  172. return GFX->getCardProfiler()->getChipString();
  173. }
  174. DefineEngineStaticMethod( GFXCardProfilerAPI, getRenderer, String, (),,
  175. "Returns the renderer name. For example D3D11 or OpenGL." )
  176. {
  177. return GFX->getCardProfiler()->getRendererString();
  178. }
  179. DefineEngineStaticMethod( GFXCardProfilerAPI, getVideoMemoryMB, S32, (),,
  180. "Returns the amount of video memory in megabytes." )
  181. {
  182. return GFX->getCardProfiler()->getVideoMemoryInMB();
  183. }
  184. DefineEngineStaticMethod( GFXCardProfilerAPI, setCapability, void, ( const char *name, S32 value ),,
  185. "Used to set the value for a specific card capability.\n"
  186. "@param name The name of the capability being set.\n"
  187. "@param value The value to set for that capability." )
  188. {
  189. GFX->getCardProfiler()->setCapability( name, (U32)value );
  190. }
  191. DefineEngineStaticMethod( GFXCardProfilerAPI, queryProfile, S32, ( const char *name, S32 defaultValue ),,
  192. "Used to query the value of a specific card capability.\n"
  193. "@param name The name of the capability being queried.\n"
  194. "@param defaultValue The value to return if the capability is not defined." )
  195. {
  196. return (S32)GFX->getCardProfiler()->queryProfile( name, (U32)defaultValue );
  197. }
  198. DefineEngineStaticMethod( GFXCardProfilerAPI, getBestDepthFormat, String, (),,
  199. "Returns the card name." )
  200. {
  201. if (GFX->getCardProfiler()->queryProfile("GL::Workaround::intel_mac_depth", false))
  202. return "GFXFormatD16";
  203. else
  204. return "GFXFormatD24S8";
  205. }