gfxCardProfile.cpp 7.8 KB

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