CmGpuParam.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "CmGpuParam.h"
  2. namespace CamelotFramework
  3. {
  4. /************************************************************************/
  5. /* STRUCT */
  6. /************************************************************************/
  7. GpuParamStruct::InternalData::InternalData(GpuParamDataDesc* paramDesc, GpuParamBlock** paramBlocks)
  8. :paramDesc(paramDesc), paramBlocks(paramBlocks), isDestroyed(false)
  9. { }
  10. GpuParamStruct::InternalData::InternalData()
  11. :paramDesc(nullptr), paramBlocks(nullptr), isDestroyed(true)
  12. { }
  13. GpuParamStruct::InternalData::~InternalData()
  14. { }
  15. GpuParamStruct::GpuParamStruct()
  16. :mData(cm_shared_ptr<InternalData>())
  17. { }
  18. GpuParamStruct::GpuParamStruct(GpuParamDataDesc* paramDesc, GpuParamBlock** paramBlocks)
  19. :mData(cm_shared_ptr<InternalData>(paramDesc, paramBlocks))
  20. { }
  21. void GpuParamStruct::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  22. {
  23. if(mData->isDestroyed)
  24. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  25. GpuParamDataDesc* paramDesc = mData->paramDesc;
  26. UINT32 elementSizeBytes = paramDesc->elementSize * sizeof(UINT32);
  27. #if CM_DEBUG_MODE
  28. if(sizeBytes > elementSizeBytes)
  29. {
  30. CM_EXCEPT(InvalidParametersException, "Provided element size larger than maximum element size. Maximum size: " +
  31. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  32. }
  33. if(arrayIdx >= paramDesc->arraySize)
  34. {
  35. CM_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  36. toString(paramDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  37. }
  38. #endif
  39. GpuParamBlock* paramBlock = mData->paramBlocks[paramDesc->paramBlockSlot];
  40. paramBlock->write((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  41. // Set unused bytes to 0
  42. if(sizeBytes < elementSizeBytes)
  43. {
  44. UINT32 diffSize = elementSizeBytes - sizeBytes;
  45. paramBlock->zeroOut((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32) + sizeBytes, diffSize);
  46. }
  47. }
  48. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  49. {
  50. if(mData->isDestroyed)
  51. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  52. GpuParamDataDesc* paramDesc = mData->paramDesc;
  53. UINT32 elementSizeBytes = paramDesc->elementSize * sizeof(UINT32);
  54. #if CM_DEBUG_MODE
  55. if(sizeBytes > elementSizeBytes)
  56. {
  57. CM_EXCEPT(InvalidParametersException, "Provided element size larger than maximum element size. Maximum size: " +
  58. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  59. }
  60. if(arrayIdx >= paramDesc->arraySize)
  61. {
  62. CM_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  63. toString(paramDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  64. }
  65. #endif
  66. GpuParamBlock* paramBlock = mData->paramBlocks[paramDesc->paramBlockSlot];
  67. paramBlock->read((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  68. }
  69. UINT32 GpuParamStruct::getElementSize() const
  70. {
  71. if(mData->isDestroyed)
  72. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  73. GpuParamDataDesc* paramDesc = mData->paramDesc;
  74. return paramDesc->elementSize * sizeof(UINT32);
  75. }
  76. void GpuParamStruct::destroy()
  77. {
  78. mData->isDestroyed = true;
  79. }
  80. /************************************************************************/
  81. /* TEXTURE */
  82. /************************************************************************/
  83. GpuParamTexture::InternalData::InternalData(GpuParamObjectDesc* paramDesc, HTexture* textures)
  84. :paramDesc(paramDesc), textures(textures), isDestroyed(false)
  85. { }
  86. GpuParamTexture::InternalData::InternalData()
  87. :paramDesc(nullptr), textures(nullptr), isDestroyed(true)
  88. { }
  89. GpuParamTexture::InternalData::~InternalData()
  90. { }
  91. GpuParamTexture::GpuParamTexture()
  92. :mData(cm_shared_ptr<InternalData>())
  93. { }
  94. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, HTexture* textures)
  95. :mData(cm_shared_ptr<InternalData>(paramDesc, textures))
  96. { }
  97. void GpuParamTexture::set(const HTexture& texture)
  98. {
  99. if(mData->isDestroyed)
  100. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  101. mData->textures[mData->paramDesc->slot] = texture;
  102. }
  103. HTexture GpuParamTexture::get()
  104. {
  105. if(mData->isDestroyed)
  106. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  107. return mData->textures[mData->paramDesc->slot];
  108. }
  109. void GpuParamTexture::destroy()
  110. {
  111. mData->isDestroyed = true;
  112. }
  113. /************************************************************************/
  114. /* SAMPLER STATE */
  115. /************************************************************************/
  116. GpuParamSampState::InternalData::InternalData(GpuParamObjectDesc* paramDesc, HSamplerState* samplerStates)
  117. :paramDesc(paramDesc), samplerStates(samplerStates), isDestroyed(false)
  118. { }
  119. GpuParamSampState::InternalData::InternalData()
  120. :paramDesc(nullptr), samplerStates(nullptr), isDestroyed(true)
  121. { }
  122. GpuParamSampState::InternalData::~InternalData()
  123. { }
  124. GpuParamSampState::GpuParamSampState()
  125. :mData(cm_shared_ptr<InternalData>())
  126. { }
  127. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, HSamplerState* samplerStates)
  128. :mData(cm_shared_ptr<InternalData>(paramDesc, samplerStates))
  129. { }
  130. void GpuParamSampState::set(const HSamplerState& samplerState)
  131. {
  132. if(mData->isDestroyed)
  133. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  134. mData->samplerStates[mData->paramDesc->slot] = samplerState;
  135. }
  136. HSamplerState GpuParamSampState::get()
  137. {
  138. if(mData->isDestroyed)
  139. CM_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  140. return mData->samplerStates[mData->paramDesc->slot];
  141. }
  142. void GpuParamSampState::destroy()
  143. {
  144. mData->isDestroyed = true;
  145. }
  146. }