BsParamBlocks.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. void flushToGPU() { mBuffer->flushToGPU(); } \
  44. \
  45. private: \
  46. struct META_FirstEntry {}; \
  47. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_FirstEntry id) { } \
  48. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_FirstEntry id) { } \
  49. \
  50. typedef META_FirstEntry
  51. /**
  52. * Registers a new entry in a parameter block. Must be called in between BS_PARAM_BLOCK_BEGIN and BS_PARAM_BLOCK_END calls.
  53. */
  54. #define BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, NumElements) \
  55. META_Entry_##Name; \
  56. \
  57. struct META_NextEntry_##Name {}; \
  58. static void META_GetPrevEntries(Vector<GpuParamDataDesc>& params, META_NextEntry_##Name id) \
  59. { \
  60. META_GetPrevEntries(params, META_Entry_##Name()); \
  61. \
  62. params.push_back(GpuParamDataDesc()); \
  63. GpuParamDataDesc& newEntry = params.back(); \
  64. newEntry.name = #Name; \
  65. newEntry.type = (GpuParamDataType)TGpuDataParamInfo<Type>::TypeId; \
  66. newEntry.arraySize = NumElements; \
  67. } \
  68. \
  69. void META_InitPrevEntry(const SPtr<GpuParamsCore>& params, META_NextEntry_##Name id) \
  70. { \
  71. META_InitPrevEntry(params, META_Entry_##Name()); \
  72. params->getParam(#Name, Name); \
  73. } \
  74. \
  75. public: \
  76. TGpuDataParam<Type, true> Name; \
  77. \
  78. private: \
  79. typedef META_NextEntry_##Name
  80. /**
  81. * Registers a new entry in a parameter block. Must be called in between BS_PARAM_BLOCK_BEGIN and BS_PARAM_BLOCK_END calls.
  82. */
  83. #define BS_PARAM_BLOCK_ENTRY(Type, Name) BS_PARAM_BLOCK_ENTRY_ARRAY(Type, Name, 1)
  84. /** Ends parameter block definition. See BS_PARAM_BLOCK_BEGIN. */
  85. #define BS_PARAM_BLOCK_END \
  86. META_LastEntry; \
  87. \
  88. static Vector<GpuParamDataDesc> getEntries() \
  89. { \
  90. Vector<GpuParamDataDesc> entries; \
  91. META_GetPrevEntries(entries, META_LastEntry()); \
  92. return entries; \
  93. } \
  94. \
  95. void initEntries() \
  96. { \
  97. META_InitPrevEntry(mParams, META_LastEntry()); \
  98. } \
  99. \
  100. SPtr<GpuParamsCore> mParams; \
  101. SPtr<GpuParamBlockBufferCore> mBuffer; \
  102. GpuParamBlockDesc mBlockDesc; \
  103. };
  104. /** @} */
  105. }