sampler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef _SAMPLER_H_
  23. #define _SAMPLER_H_
  24. #include "platform/types.h"
  25. /// The sampling framework.
  26. ///
  27. /// Sampling allows per-frame snaphots of specific values to be logged. For
  28. /// each value that you want to have sampled, you define a sampling key and
  29. /// then simply call the sample function at an appropriate place. If you
  30. /// want to sample the same value multiple times within a single frame, simply
  31. /// register several keys for it.
  32. ///
  33. /// The easiest way to use this facility is with the SAMPLE macro.
  34. ///
  35. /// @code
  36. /// SAMPLE( "my/sample/value", my.sample->val );
  37. /// @endcode
  38. ///
  39. /// @section SamplerUsage Using the Sampler
  40. ///
  41. /// Before you use the sampler it is important that you let your game run for
  42. /// some frames and make sure that all relevant code paths have been touched (i.e.
  43. /// if you want to sample Atlas data, have an Atlas instance on screen). This
  44. /// will ensure that sampling keys are registered with the sampler.
  45. ///
  46. /// Then use the console to first enable the keys you are interested in. For
  47. /// example, to enable sampling for all Atlas keys:
  48. ///
  49. /// @code
  50. /// enableSamples( "atlas/*" );
  51. /// @endcode
  52. ///
  53. /// Finally, you have to start the actual sampling. This is achieved with the
  54. /// beginSampling console function that takes a string informing the backend
  55. /// where to store sample data and optionally a name of the specific logging backend
  56. /// to use. The default is the CSV backend. In most cases, the logging store
  57. /// will be a file name.
  58. ///
  59. /// @code
  60. /// beginSampling( "mysamples.csv" );
  61. /// @endcode
  62. ///
  63. /// To stop sampling, use:
  64. ///
  65. /// @code
  66. /// stopSampling();
  67. /// @endcode
  68. ///
  69. /// @section Sample Keys
  70. ///
  71. /// Sample key name should generally follow the pattern "path/to/group/samplename".
  72. /// This allows to very easily enable or disable specific sets of keys using
  73. /// wildcards.
  74. ///
  75. /// Note that sampling keys are case-insensitive.
  76. namespace Sampler
  77. {
  78. void init();
  79. void destroy();
  80. void beginFrame();
  81. void endFrame();
  82. void sample( U32 key, bool value );
  83. void sample( U32 key, S32 value );
  84. void sample( U32 key, F32 value );
  85. void sample( U32 key, const char* value );
  86. inline void sample( U32 key, U32 value )
  87. {
  88. sample( key, S32( value ) );
  89. }
  90. /// Register a new sample key.
  91. ///
  92. /// @note Note that all keys are disabled by default.
  93. U32 registerKey( const char* name );
  94. /// Enable sampling for all keys that match the given name
  95. /// pattern. Slashes are treated as separators.
  96. void enableKeys( const char* pattern, bool state = true );
  97. };
  98. #ifdef TORQUE_ENABLE_SAMPLING
  99. # define SAMPLE( name, value ) \
  100. { \
  101. static U32 key; \
  102. if( !key ) \
  103. key = Sampler::registerKey( name ); \
  104. Sampler::sample( key, value ); \
  105. }
  106. #else
  107. # define SAMPLE( name, value )
  108. #endif
  109. #define SAMPLE_VECTOR( name, value ) \
  110. { \
  111. SAMPLE( name "/x", value.x ); \
  112. SAMPLE( name "/y", value.y ); \
  113. SAMPLE( name "/z", value.z ); \
  114. }
  115. #define SAMPLE_MATRIX( name, value ) \
  116. { \
  117. SAMPLE( name "/a1", value[ value.idx( 0, 0 ) ] ); \
  118. SAMPLE( name "/a2", value[ value.idx( 1, 0 ) ] ); \
  119. SAMPLE( name "/a3", value[ value.idx( 2, 0 ) ] ); \
  120. SAMPLE( name "/a4", value[ value.idx( 3, 0 ) ] ); \
  121. SAMPLE( name "/b1", value[ value.idx( 0, 1 ) ] ); \
  122. SAMPLE( name "/b2", value[ value.idx( 1, 1 ) ] ); \
  123. SAMPLE( name "/b3", value[ value.idx( 2, 1 ) ] ); \
  124. SAMPLE( name "/b4", value[ value.idx( 3, 1 ) ] ); \
  125. SAMPLE( name "/c1", value[ value.idx( 0, 2 ) ] ); \
  126. SAMPLE( name "/c2", value[ value.idx( 1, 2 ) ] ); \
  127. SAMPLE( name "/c3", value[ value.idx( 2, 2 ) ] ); \
  128. SAMPLE( name "/c4", value[ value.idx( 3, 2 ) ] ); \
  129. SAMPLE( name "/d1", value[ value.idx( 0, 3 ) ] ); \
  130. SAMPLE( name "/d2", value[ value.idx( 1, 3 ) ] ); \
  131. SAMPLE( name "/d3", value[ value.idx( 2, 3 ) ] ); \
  132. SAMPLE( name "/d4", value[ value.idx( 3, 3 ) ] ); \
  133. }
  134. #endif // _SAMPLER_H_