gfxCardProfile.cpp 7.9 KB

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