tsShapeAlloc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 _TSSHAPEALLOC_H_
  23. #define _TSSHAPEALLOC_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _MMATH_H_
  28. #include "math/mMath.h"
  29. #endif
  30. /// Alloc structure used in the reading/writing of shapes.
  31. ///
  32. /// In read mode we assemble contents of 32-bit, 16-bit, and 8-bit buffers
  33. /// into a single destination buffer.
  34. ///
  35. /// In write mode we dissemble a stream of memory (which may be scattered in physical memory)
  36. /// into 32-bit, 16-bit, 8-bit, Point3F, and Point2F buffers using function calls similar
  37. /// to the read calls.
  38. ///
  39. /// Read usage:
  40. /// 1. call "setRead" with each incoming memory buffers and clear=true.
  41. /// 2. run through set of operations for allocating and transfering memory to target buffer
  42. /// these are the operations under "DECLARE_ALLOC" that call readOnly in the .cc file.
  43. /// 3. call "doAlloc" to create buffer exactly as large as needed.
  44. /// 4. repeat step 1 & 2 to do the actual transfer of memory, except with clear=false
  45. /// (note: first time through nothing was copied to the shape, we only kept track
  46. /// of the size of the transfer).
  47. /// 5. call getBuffer to get the target (destination buffer)
  48. ///
  49. /// write usage:
  50. /// 1. call "setWrite" (no parameters).
  51. /// 2. run through set of operations for allocating and transfering memory to internal buffers
  52. /// these are the operations under "DECLARE_ALLOC" that call writeOnly in the .cc file.
  53. /// 3. call getBuffer32 and getBufferSize32 to get 32-bit buffer and size. Similarly for
  54. /// 16-bit, 8-bit (getBuffer16, getBuffer8).
  55. ///
  56. /// TSShape::assesmbleShape and TSShape::dissembleShape can be used as examples
  57. class TSShapeAlloc
  58. {
  59. S32 mMode; ///< read or write
  60. /// reading and writing (when reading these are the input; when writing these are the output)
  61. S32 * mMemBuffer32;
  62. S16 * mMemBuffer16;
  63. S8 * mMemBuffer8;
  64. /// for writing only...
  65. S32 mSize32;
  66. S32 mSize16;
  67. S32 mSize8;
  68. S32 mFullSize32;
  69. S32 mFullSize16;
  70. S32 mFullSize8;
  71. /// reading and writing...
  72. S32 mMemGuard32;
  73. S16 mMemGuard16;
  74. S8 mMemGuard8;
  75. /// reading
  76. S32 mSaveGuard32;
  77. S16 mSaveGuard16;
  78. S8 mSaveGuard8;
  79. /// reading only...this is the output
  80. S8 * mDest;
  81. S32 mSize;
  82. S32 mMult; ///< mult incoming sizes by this (when 0, then mDest doesn't grow --> skip mode)
  83. public:
  84. enum { ReadMode = 0, WriteMode = 1, PageSize = 1024 }; ///< PageSize must be multiple of 4 so that we can always
  85. ///< "over-read" up to next dword
  86. void setRead(S32 * buff32, S16 * buff16, S8 * buff8, bool clear);
  87. void setWrite();
  88. // reading only...
  89. void doAlloc();
  90. void align32(); ///< align on dword boundary
  91. S8 * getBuffer() { return mDest; }
  92. S32 getSize() { return mSize; }
  93. void setSkipMode(bool skip) { mMult = skip ? 0 : 1; }
  94. /// @name Reading Operations:
  95. ///
  96. /// get(): reads one or more entries of type from input buffer (doesn't affect output buffer)
  97. ///
  98. /// copyToShape(): copies entries of type from input buffer to output buffer
  99. ///
  100. /// allocShape(): creates room for entries of type in output buffer (no effect on input buffer)
  101. ///
  102. /// getPointer(): gets pointer to next entries of type in input buffer (no effect on input buffer)
  103. ///
  104. /// @note all operations advance current "position" of input and output buffers
  105. /// writing operations:
  106. ///
  107. /// set(): adds one entry to appropriate buffer
  108. ///
  109. /// copyToBuffer(): adds count entries to approrpiate buffer
  110. ///
  111. /// getBuffer(): returns associated buffer (i.e., getBuffer32 gets 32bit buffer)
  112. ///
  113. /// getBufferSize(): returns size of associated buffer
  114. ///
  115. /// @{
  116. #define DECLARE_ALLOC(suffix,type) \
  117. type get##suffix(); \
  118. void get##suffix(type*,S32); \
  119. type * copyToShape##suffix(S32,bool returnSomething=false); \
  120. type * getPointer##suffix(S32); \
  121. type * allocShape##suffix(S32); \
  122. bool checkGuard##suffix(); \
  123. type getPrevGuard##suffix(); \
  124. type getSaveGuard##suffix(); \
  125. type * getBuffer##suffix(); \
  126. S32 getBufferSize##suffix(); \
  127. void setGuard##suffix(); \
  128. type * extend##suffix(S32); \
  129. type set##suffix(type); \
  130. void copyToBuffer##suffix(type*,S32);
  131. DECLARE_ALLOC(32,S32)
  132. DECLARE_ALLOC(16,S16)
  133. DECLARE_ALLOC(8,S8)
  134. /// @}
  135. void checkGuard();
  136. void setGuard();
  137. };
  138. #endif // _H_TS_SHAPE_ALLOC_