gfxCardProfile.cpp 7.9 KB

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