CmGpuParam.cpp 5.6 KB

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