BsParamBlocks.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuParamDesc.h"
  4. #include "BsGpuParams.h"
  5. #include "BsRenderAPI.h"
  6. #include "BsGpuParamBlockBuffer.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * Macros used for manually constructing parameter block buffers used by GPU programs.
  11. */
  12. #define BS_PARAM_BLOCK_BEGIN(Name) \
  13. struct Name \
  14. { \
  15. Name() \
  16. { \
  17. Vector<GpuParamDataDesc> params = getEntries(); \
  18. RenderAPICore& rapi = RenderAPICore::instance(); \
  19. \
  20. mBlockDesc = rapi.generateParamBlockDesc(#Name, params); \
  21. \
  22. SPtr<GpuParamDesc> paramsDesc = bs_shared_ptr_new<GpuParamDesc>(); \
  23. paramsDesc->paramBlocks[#Name] = mBlockDesc; \
  24. for (auto& param : params) \
  25. paramsDesc->params[param.name] = param; \
  26. \
  27. mParams = GpuParamsCore::create(paramsDesc, rapi.getGpuProgramHasColumnMajorMatrices()); \
  28. \
  29. mBuffer = GpuParamBlockBufferCore::create(mBlockDesc.blockSize * sizeof(UINT32)); \
  30. mParams->setParamBlockBuffer(#Name, mBuffer); \
  31. initEntries(); \
  32. } \
  33. \
  34. const SPtr<GpuParamBlockBufferCore>& getBuffer() const { return mBuffer; } \
  35. const GpuParamBlockDesc& getDesc() const { return mBlockDesc; } \
  36. \
  37. private: \
  38. struct META_FirstEntry {}; \
  39. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_FirstEntry id) { } \
  40. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_FirstEntry id) { } \
  41. \
  42. typedef META_FirstEntry
  43. #define BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, NumElements) \
  44. META_Entry_##Name; \
  45. \
  46. struct META_NextEntry_##Name {}; \
  47. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_NextEntry_##Name id) \
  48. { \
  49. META_GetPrevEntries(params, META_Entry_##Name##()); \
  50. \
  51. params.push_back(GpuParamDataDesc()); \
  52. GpuParamDataDesc& newEntry = params.back(); \
  53. newEntry.name = #Name; \
  54. newEntry.type = (GpuParamDataType)TGpuDataParamInfo<Type>::TypeId; \
  55. newEntry.arraySize = NumElements; \
  56. } \
  57. \
  58. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_NextEntry_##Name id) \
  59. { \
  60. META_InitPrevEntry(params, META_Entry_##Name##()); \
  61. params->getParam(#Name, Name); \
  62. } \
  63. \
  64. public: \
  65. TGpuDataParam<Type, true> Name; \
  66. \
  67. private: \
  68. typedef META_NextEntry_##Name
  69. #define BS_PARAM_BLOCK_ENTRY(Type, Name) BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, 1)
  70. #define BS_PARAM_BLOCK_END \
  71. META_LastEntry; \
  72. \
  73. static Vector<GpuParamDataDesc> getEntries() \
  74. { \
  75. Vector<GpuParamDataDesc> entries; \
  76. META_GetPrevEntries(entries, META_LastEntry()); \
  77. return entries; \
  78. } \
  79. \
  80. void initEntries() \
  81. { \
  82. META_InitPrevEntry(mParams, META_LastEntry()); \
  83. } \
  84. \
  85. SPtr<GpuParamsCore> mParams; \
  86. SPtr<GpuParamBlockBufferCore> mBuffer; \
  87. GpuParamBlockDesc mBlockDesc; \
  88. };
  89. }