DrawLayer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include "Common.h"
  3. #include "util/SLIList.h"
  4. #include "fbx/FBXReader.h"
  5. #include "gfx/RenderCmd.h"
  6. #include "gfx/RenderDevice.h"
  7. NS_BF_BEGIN;
  8. class RenderWindow;
  9. class Texture;
  10. class Shader;
  11. class ShaderPass;
  12. class BFApp;
  13. class Vertex3D;
  14. class DrawLayer;
  15. class RenderState;
  16. #define MAX_TEXTURES 4
  17. class DrawBatch : public RenderCmd
  18. {
  19. public:
  20. DrawLayer* mDrawLayer;
  21. int mId;
  22. bool mIsVertexBufferHead;
  23. bool mIsIndexBufferHead;
  24. void* mVertices;
  25. int mVtxSize;
  26. int mVtxIdx;
  27. int mAllocatedVertices;
  28. uint16* mIndices;
  29. int mIdxIdx;
  30. int mAllocatedIndices;
  31. Texture* mCurTextures[MAX_TEXTURES];
  32. public:
  33. DrawBatch();
  34. virtual ~DrawBatch();
  35. virtual void Free() override;
  36. void Clear();
  37. DrawBatch* AllocateChainedBatch(int minVtxCount, int minIdxCount);
  38. virtual void* AllocTris(int vtxCount);
  39. virtual void* AllocStrip(int vtxCount);
  40. virtual void AllocIndexed(int vtxCount, int idxCount, void** verticesOut, uint16** indicesOut, uint16* idxOfsOut);
  41. };
  42. #define DRAWBUFFER_CMDBUFFER_SIZE 64*1024
  43. class DrawLayer
  44. {
  45. public:
  46. SLIList<RenderCmd*> mRenderCmdList;
  47. DrawBatch* mCurDrawBatch;
  48. RenderWindow* mRenderWindow;
  49. RenderDevice* mRenderDevice;
  50. void* mIdxBuffer;
  51. void* mVtxBuffer;
  52. void* mRenderCmdBuffer;
  53. int mIdxByteIdx;
  54. int mVtxByteIdx;
  55. int mRenderCmdByteIdx;
  56. Texture* mCurTextures[MAX_TEXTURES];
  57. public:
  58. template <typename T>
  59. T* AllocRenderCmd(int extraBytes = 0)
  60. {
  61. if (mRenderCmdByteIdx + sizeof(T) + extraBytes >= DRAWBUFFER_CMDBUFFER_SIZE)
  62. {
  63. mRenderCmdBuffer = mRenderDevice->mPooledRenderCmdBuffers.AllocMemoryBlock();
  64. T* cmd = new(mRenderCmdBuffer) T();
  65. cmd->mIsPoolHead = true;
  66. mRenderCmdByteIdx = sizeof(T) + extraBytes;
  67. return cmd;
  68. }
  69. T* cmd = new((uint8*)mRenderCmdBuffer + mRenderCmdByteIdx) T();
  70. cmd->mIsPoolHead = false;
  71. mRenderCmdByteIdx += sizeof(T) + extraBytes;
  72. return cmd;
  73. }
  74. public:
  75. void CloseDrawBatch();
  76. virtual DrawBatch* CreateDrawBatch() = 0;
  77. virtual DrawBatch* AllocateBatch(int minVtxCount, int minIdxCount);
  78. void QueueRenderCmd(RenderCmd* renderCmd);
  79. virtual RenderCmd* CreateSetTextureCmd(int textureIdx, Texture* texture) = 0;
  80. virtual void SetShaderConstantData(int usageIdx, int slotIdx, void* constData, int size) = 0;
  81. virtual void SetShaderConstantDataTyped(int usageIdx, int slotIdx, void* constData, int size, int* typeData, int typeCount);
  82. public:
  83. DrawLayer();
  84. virtual ~DrawLayer();
  85. virtual void Draw();
  86. virtual void Flush();
  87. virtual void Clear();
  88. virtual void* AllocTris(int vtxCount);
  89. virtual void* AllocStrip(int vtxCount);
  90. virtual void AllocIndexed(int vtxCount, int idxCount, void** verticesOut, uint16** indicesOut, uint16* idxOfsOut);
  91. virtual void SetTexture(int texIdx, Texture* texture);
  92. };
  93. NS_BF_END;