BsParamBlocks.h 5.5 KB

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