genericConstBuffer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/genericConstBuffer.h"
  24. #include "platform/profiler.h"
  25. #include "core/stream/stream.h"
  26. GenericConstBufferLayout::GenericConstBufferLayout()
  27. {
  28. VECTOR_SET_ASSOCIATION( mParams );
  29. mBufferSize = 0;
  30. mCurrentIndex = 0;
  31. mTimesCleared = 0;
  32. }
  33. void GenericConstBufferLayout::addParameter(const String& name, const GFXShaderConstType constType, const U32 offset, const U32 size, const U32 arraySize, const U32 alignValue)
  34. {
  35. #ifdef TORQUE_DEBUG
  36. // Make sure we don't have overlapping parameters
  37. S32 start = offset;
  38. S32 end = offset + size;
  39. for (Params::iterator i = mParams.begin(); i != mParams.end(); i++)
  40. {
  41. const ParamDesc& dp = *i;
  42. S32 pstart = dp.offset;
  43. S32 pend = pstart + dp.size;
  44. pstart -= start;
  45. pend -= end;
  46. // This is like a minkowski sum for two line segments, if the newly formed line contains
  47. // the origin, then they intersect
  48. bool intersect = ((pstart >= 0 && 0 >= pend) || ((pend >= 0 && 0 >= pstart)));
  49. AssertFatal(!intersect, "Overlapping shader parameter!");
  50. }
  51. #endif
  52. ParamDesc desc;
  53. desc.name = name;
  54. desc.constType = constType;
  55. desc.offset = offset;
  56. desc.size = size;
  57. desc.arraySize = arraySize;
  58. desc.alignValue = alignValue;
  59. desc.index = mCurrentIndex++;
  60. mParams.push_back(desc);
  61. mBufferSize = getMax(desc.offset + desc.size, mBufferSize);
  62. AssertFatal(mBufferSize, "Empty constant buffer!");
  63. }
  64. bool GenericConstBufferLayout::set(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer)
  65. {
  66. PROFILE_SCOPE(GenericConstBufferLayout_set);
  67. // Shader compilers like to optimize float4x4 uniforms into float3x3s.
  68. // So long as the real paramater is a matrix of-some-type and the data
  69. // passed in is a MatrixF ( which is will be ), we DO NOT have a
  70. // mismatched const type.
  71. AssertFatal( pd.constType == constType ||
  72. (
  73. ( pd.constType == GFXSCT_Float2x2 ||
  74. pd.constType == GFXSCT_Float3x3 ||
  75. pd.constType == GFXSCT_Float4x4 ) &&
  76. ( constType == GFXSCT_Float2x2 ||
  77. constType == GFXSCT_Float3x3 ||
  78. constType == GFXSCT_Float4x4 )
  79. ), "Mismatched const type!" );
  80. // This "cute" bit of code allows us to support 2x3 and 3x3 matrices in shader constants but use our MatrixF class. Yes, a hack. -BTR
  81. switch (pd.constType)
  82. {
  83. case GFXSCT_Float2x2 :
  84. case GFXSCT_Float3x3 :
  85. case GFXSCT_Float4x4 :
  86. return setMatrix(pd, constType, size, data, basePointer);
  87. break;
  88. default :
  89. break;
  90. }
  91. AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
  92. // Ok, we only set data if it's different than the data we already have, this maybe more expensive than just setting the data, but
  93. // we'll have to do some timings to see. For example, the lighting shader constants rarely change, but we can't assume that at the
  94. // renderInstMgr level, but we can check down here. -BTR
  95. if (dMemcmp(basePointer+pd.offset, data, size) != 0)
  96. {
  97. dMemcpy(basePointer+pd.offset, data, size);
  98. return true;
  99. }
  100. return false;
  101. }
  102. bool GenericConstBufferLayout::setMatrix(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer)
  103. {
  104. PROFILE_SCOPE(GenericConstBufferLayout_setMatrix);
  105. // We're generic, so just copy the full MatrixF in
  106. AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
  107. // Matrices are an annoying case because of the alignment issues. There are alignment issues in the matrix itself, and then potential inter matrices alignment issues.
  108. // So GL and DX will need to derive their own GenericConstBufferLayout classes and override this method to deal with that stuff. For GenericConstBuffer, copy the whole
  109. // 4x4 matrix regardless of the target case.
  110. if (dMemcmp(basePointer+pd.offset, data, size) != 0)
  111. {
  112. dMemcpy(basePointer+pd.offset, data, size);
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool GenericConstBufferLayout::getDesc(const String& name, ParamDesc& param) const
  118. {
  119. for (U32 i = 0; i < mParams.size(); i++)
  120. {
  121. if (mParams[i].name.equal(name))
  122. {
  123. param = mParams[i];
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. bool GenericConstBufferLayout::getDesc(const U32 index, ParamDesc& param) const
  130. {
  131. if ( index < mParams.size() )
  132. {
  133. param = mParams[index];
  134. return true;
  135. }
  136. return false;
  137. }
  138. bool GenericConstBufferLayout::write(Stream* s)
  139. {
  140. // Write out the size of the ParamDesc structure as a sanity check.
  141. if (!s->write((U32) sizeof(ParamDesc)))
  142. return false;
  143. // Next, write out the number of elements we've got.
  144. if (!s->write(mParams.size()))
  145. return false;
  146. for (U32 i = 0; i < mParams.size(); i++)
  147. {
  148. s->write(mParams[i].name);
  149. if (!s->write(mParams[i].offset))
  150. return false;
  151. if (!s->write(mParams[i].size))
  152. return false;
  153. U32 t = (U32) mParams[i].constType;
  154. if (!s->write(t))
  155. return false;
  156. if (!s->write(mParams[i].arraySize))
  157. return false;
  158. if (!s->write(mParams[i].alignValue))
  159. return false;
  160. if (!s->write(mParams[i].index))
  161. return false;
  162. }
  163. return true;
  164. }
  165. /// Load this layout from a stream
  166. bool GenericConstBufferLayout::read(Stream* s)
  167. {
  168. U32 structSize;
  169. if (!s->read(&structSize))
  170. return false;
  171. if (structSize != sizeof(ParamDesc))
  172. {
  173. AssertFatal(false, "Invalid shader layout structure size!");
  174. return false;
  175. }
  176. U32 numParams;
  177. if (!s->read(&numParams))
  178. return false;
  179. mParams.setSize(numParams);
  180. mBufferSize = 0;
  181. mCurrentIndex = 0;
  182. for (U32 i = 0; i < mParams.size(); i++)
  183. {
  184. s->read(&mParams[i].name);
  185. if (!s->read(&mParams[i].offset))
  186. return false;
  187. if (!s->read(&mParams[i].size))
  188. return false;
  189. U32 t;
  190. if (!s->read(&t))
  191. return false;
  192. mParams[i].constType = (GFXShaderConstType) t;
  193. if (!s->read(&mParams[i].arraySize))
  194. return false;
  195. if (!s->read(&mParams[i].alignValue))
  196. return false;
  197. if (!s->read(&mParams[i].index))
  198. return false;
  199. mBufferSize = getMax(mParams[i].offset + mParams[i].size, mBufferSize);
  200. mCurrentIndex = getMax(mParams[i].index, mCurrentIndex);
  201. }
  202. mCurrentIndex++;
  203. return true;
  204. }
  205. void GenericConstBufferLayout::clear()
  206. {
  207. mParams.clear();
  208. mBufferSize = 0;
  209. mCurrentIndex = 0;
  210. mTimesCleared++;
  211. }
  212. GenericConstBuffer::GenericConstBuffer(GenericConstBufferLayout* layout)
  213. : mBuffer( NULL ),
  214. mLayout( layout ),
  215. mDirtyStart( U32_MAX ),
  216. mDirtyEnd( 0 )
  217. {
  218. if ( layout && layout->getBufferSize() > 0 )
  219. {
  220. mBuffer = new U8[mLayout->getBufferSize()];
  221. // Always set a default value, that way our isEqual checks
  222. // will work in release as well.
  223. dMemset( mBuffer, 0xFFFF, mLayout->getBufferSize() );
  224. #ifdef TORQUE_DEBUG
  225. // Clear the debug assignment tracking.
  226. mWasAssigned.setSize( layout->getParameterCount() );
  227. dMemset( mWasAssigned.address(), 0, mWasAssigned.memSize() );
  228. #endif
  229. }
  230. }
  231. GenericConstBuffer::~GenericConstBuffer()
  232. {
  233. delete [] mBuffer;
  234. }
  235. #ifdef TORQUE_DEBUG
  236. void GenericConstBuffer::assertUnassignedConstants( const char *shaderName )
  237. {
  238. for ( U32 i=0; i < mWasAssigned.size(); i++ )
  239. {
  240. if ( mWasAssigned[i] )
  241. continue;
  242. GenericConstBufferLayout::ParamDesc pd;
  243. mLayout->getDesc( i, pd );
  244. // Assert on the unassigned constant.
  245. AssertFatal( false, avar( "The '%s' shader constant in shader '%s' was unassigned!",
  246. pd.name.c_str(), shaderName ) );
  247. }
  248. }
  249. #endif