BsParamBlocks.h 5.9 KB

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