BsParamBlocks.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsGpuParamDesc.h"
  6. #include "BsGpuParams.h"
  7. #include "BsRenderAPI.h"
  8. #include "BsGpuParamBlockBuffer.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Renderer-Internal
  12. * @{
  13. */
  14. /**
  15. * Starts a new custom parameter block. Custom parameter blocks allow you to create C++ structures that map directly
  16. * to GPU program buffers (for example uniform buffer in OpenGL or constant buffer in DX). Must be followed by
  17. * BS_PARAM_BLOCK_END.
  18. */
  19. #define BS_PARAM_BLOCK_BEGIN(Name) \
  20. struct Name \
  21. { \
  22. Name() \
  23. { \
  24. Vector<GpuParamDataDesc> params = getEntries(); \
  25. RenderAPICore& rapi = RenderAPICore::instance(); \
  26. \
  27. mBlockDesc = rapi.generateParamBlockDesc(#Name, params); \
  28. \
  29. SPtr<GpuParamDesc> paramsDesc = bs_shared_ptr_new<GpuParamDesc>(); \
  30. paramsDesc->paramBlocks[#Name] = mBlockDesc; \
  31. for (auto& param : params) \
  32. paramsDesc->params[param.name] = param; \
  33. \
  34. mParams = GpuParamsCore::create(paramsDesc, rapi.getAPIInfo().getGpuProgramHasColumnMajorMatrices()); \
  35. \
  36. mBuffer = GpuParamBlockBufferCore::create(mBlockDesc.blockSize * sizeof(UINT32)); \
  37. mParams->setParamBlockBuffer(#Name, mBuffer); \
  38. initEntries(); \
  39. } \
  40. \
  41. const SPtr<GpuParamBlockBufferCore>& getBuffer() const { return mBuffer; } \
  42. const GpuParamBlockDesc& getDesc() const { return mBlockDesc; } \
  43. \
  44. private: \
  45. struct META_FirstEntry {}; \
  46. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_FirstEntry id) { } \
  47. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_FirstEntry id) { } \
  48. \
  49. typedef META_FirstEntry
  50. /**
  51. * Registers a new entry in a parameter block. Must be called in between BS_PARAM_BLOCK_BEGIN and BS_PARAM_BLOCK_END calls.
  52. */
  53. #define BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, NumElements) \
  54. META_Entry_##Name; \
  55. \
  56. struct META_NextEntry_##Name {}; \
  57. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_NextEntry_##Name id) \
  58. { \
  59. META_GetPrevEntries(params, META_Entry_##Name##()); \
  60. \
  61. params.push_back(GpuParamDataDesc()); \
  62. GpuParamDataDesc& newEntry = params.back(); \
  63. newEntry.name = #Name; \
  64. newEntry.type = (GpuParamDataType)TGpuDataParamInfo<Type>::TypeId; \
  65. newEntry.arraySize = NumElements; \
  66. } \
  67. \
  68. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_NextEntry_##Name id) \
  69. { \
  70. META_InitPrevEntry(params, META_Entry_##Name##()); \
  71. params->getParam(#Name, Name); \
  72. } \
  73. \
  74. public: \
  75. TGpuDataParam<Type, true> Name; \
  76. \
  77. private: \
  78. typedef META_NextEntry_##Name
  79. /**
  80. * Registers a new entry in a parameter block. Must be called in between BS_PARAM_BLOCK_BEGIN and BS_PARAM_BLOCK_END calls.
  81. */
  82. #define BS_PARAM_BLOCK_ENTRY(Type, Name) BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, 1)
  83. /** Ends parameter block definition. See BS_PARAM_BLOCK_BEGIN. */
  84. #define BS_PARAM_BLOCK_END \
  85. META_LastEntry; \
  86. \
  87. static Vector<GpuParamDataDesc> getEntries() \
  88. { \
  89. Vector<GpuParamDataDesc> entries; \
  90. META_GetPrevEntries(entries, META_LastEntry()); \
  91. return entries; \
  92. } \
  93. \
  94. void initEntries() \
  95. { \
  96. META_InitPrevEntry(mParams, META_LastEntry()); \
  97. } \
  98. \
  99. SPtr<GpuParamsCore> mParams; \
  100. SPtr<GpuParamBlockBufferCore> mBuffer; \
  101. GpuParamBlockDesc mBlockDesc; \
  102. };
  103. /** @} */
  104. }