BsParamBlocks.h 5.3 KB

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