RenderDevice.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #pragma once
  2. #include "Common.h"
  3. #include "RenderTarget.h"
  4. #include "util/Rect.h"
  5. #include "util/SLIList.h"
  6. NS_BF_BEGIN;
  7. class Texture;
  8. class Shader;
  9. class ShaderPass;
  10. class BFApp;
  11. class DefaultVertex3D
  12. {
  13. public:
  14. float x;
  15. float y;
  16. float z;
  17. float u;
  18. float v;
  19. uint32 color;
  20. public:
  21. DefaultVertex3D()
  22. {
  23. }
  24. DefaultVertex3D(float _x, float _y, float _z, float _u, float _v, uint32 _color)
  25. {
  26. x = _x;
  27. y = _y;
  28. z = _z;
  29. color = _color;
  30. u = _u;
  31. v = _v;
  32. }
  33. void Set(float _x, float _y, float _z, float _u, float _v, uint32 _color)
  34. {
  35. x = _x;
  36. y = _y;
  37. z = _z;
  38. color = _color;
  39. u = _u;
  40. v = _v;
  41. }
  42. };
  43. class RenderWindow;
  44. class DrawBatch;
  45. class DrawLayer;
  46. class BFWindow;
  47. class ImageData;
  48. class DrawLayer;
  49. class ModelInstance;
  50. class FBXReader;
  51. class RenderCmd;
  52. class ModelDef;
  53. class RenderDevice;
  54. class RenderWindow : public RenderTarget
  55. {
  56. public:
  57. RenderDevice* mRenderDevice;
  58. BFWindow* mWindow;
  59. Array<DrawLayer*> mDrawLayerList;
  60. DrawLayer* mCurDrawLayer;
  61. public:
  62. RenderWindow();
  63. virtual ~RenderWindow();
  64. virtual void SetAsTarget() = 0;
  65. virtual void Resized() = 0;
  66. virtual void Present() = 0;
  67. };
  68. const int DRAWBUFFER_IDXBUFFER_SIZE = 8*1024;
  69. const int DRAWBUFFER_VTXBUFFER_SIZE = 64*1024;
  70. enum DepthFunc : int8
  71. {
  72. DepthFunc_Never,
  73. DepthFunc_Less,
  74. DepthFunc_LessEqual,
  75. DepthFunc_Equal,
  76. DepthFunc_Greater,
  77. DepthFunc_NotEqual,
  78. DepthFunc_GreaterEqual,
  79. DepthFunc_Always
  80. };
  81. enum VertexElementFormat : int8
  82. {
  83. VertexElementFormat_Single,
  84. VertexElementFormat_Vector2,
  85. VertexElementFormat_Vector3,
  86. VertexElementFormat_Vector4,
  87. VertexElementFormat_Color,
  88. VertexElementFormat_Byte4,
  89. VertexElementFormat_Short2,
  90. VertexElementFormat_Short4,
  91. VertexElementFormat_NormalizedShort2,
  92. VertexElementFormat_NormalizedShort4,
  93. VertexElementFormat_HalfVector2,
  94. VertexElementFormat_HalfVector4
  95. };
  96. enum VertexElementUsage : int8
  97. {
  98. VertexElementUsage_Position2D,
  99. VertexElementUsage_Position3D,
  100. VertexElementUsage_Color,
  101. VertexElementUsage_TextureCoordinate,
  102. VertexElementUsage_Normal,
  103. VertexElementUsage_Binormal,
  104. VertexElementUsage_Tangent,
  105. VertexElementUsage_BlendIndices,
  106. VertexElementUsage_BlendWeight,
  107. VertexElementUsage_Depth,
  108. VertexElementUsage_Fog,
  109. VertexElementUsage_PointSize,
  110. VertexElementUsage_Sample,
  111. VertexElementUsage_TessellateFactor
  112. };
  113. enum ConstantDataType : int8
  114. {
  115. ConstantDataType_Single,
  116. ConstantDataType_Vector2,
  117. ConstantDataType_Vector3,
  118. ConstantDataType_Vector4,
  119. ConstantDataType_Matrix
  120. };
  121. enum CullMode : int8
  122. {
  123. CullMode_None,
  124. CullMode_Front,
  125. CullMode_Back
  126. };
  127. enum Topology3D : int8
  128. {
  129. Topology3D_TriangleList,
  130. Topology3D_LineLine
  131. };
  132. enum TextureFlag : int8
  133. {
  134. TextureFlag_Additive = 1,
  135. TextureFlag_NoPremult = 2,
  136. TextureFlag_AllowRead = 4,
  137. };
  138. struct VertexDefData
  139. {
  140. VertexElementUsage mUsage;
  141. int mUsageIndex;
  142. VertexElementFormat mFormat;
  143. };
  144. class VertexDefinition
  145. {
  146. public:
  147. VertexDefData* mElementData;
  148. int mNumElements;
  149. public:
  150. VertexDefinition()
  151. {
  152. mElementData = NULL;
  153. mNumElements = 0;
  154. }
  155. virtual ~VertexDefinition()
  156. {
  157. delete [] mElementData;
  158. }
  159. };
  160. class RenderState
  161. {
  162. public:
  163. Shader* mShader;
  164. bool mWriteDepthBuffer;
  165. DepthFunc mDepthFunc;
  166. bool mClipped;
  167. bool mTexWrap;
  168. bool mWireframe;
  169. Rect mClipRect;
  170. CullMode mCullMode;
  171. Topology3D mTopology;
  172. public:
  173. RenderState();
  174. virtual ~RenderState() {}
  175. virtual void SetShader(Shader* shader) { mShader = shader; }
  176. virtual void SetTexWrap(bool wrap) { mTexWrap = wrap; }
  177. virtual void SetWireframe(bool wireframe) { mWireframe = wireframe; }
  178. virtual void SetClipped(bool clipped) { mClipped = clipped; }
  179. virtual void SetClipRect(const Rect& rect) { mClipRect = rect; }
  180. virtual void SetWriteDepthBuffer(bool writeDepthBuffer) { mWriteDepthBuffer = writeDepthBuffer; }
  181. virtual void SetDepthFunc(DepthFunc depthFunc) { mDepthFunc = depthFunc; }
  182. virtual void SetTopology(Topology3D topology) { mTopology = topology; }
  183. };
  184. class PoolData
  185. {
  186. public:
  187. PoolData* mNext;
  188. };
  189. class MemoryPool : protected SLIList<PoolData*>
  190. {
  191. public:
  192. int mSize;
  193. public:
  194. MemoryPool(int size)
  195. {
  196. mSize = size;
  197. }
  198. ~MemoryPool()
  199. {
  200. auto cur = mHead;
  201. while (cur != NULL)
  202. {
  203. auto next = cur->mNext;
  204. delete [] cur;
  205. cur = next;
  206. }
  207. }
  208. void* AllocMemoryBlock()
  209. {
  210. if (IsEmpty())
  211. return new uint8[mSize];
  212. return (uint8*)PopFront();
  213. }
  214. void FreeMemoryBlock(void* block)
  215. {
  216. PoolData* poolData = (PoolData*)block;
  217. poolData->mNext = NULL;
  218. PushBack(poolData);
  219. }
  220. };
  221. enum ModelCreateFlags
  222. {
  223. ModelCreateFlags_None = 0,
  224. ModelCreateFlags_NoSetRenderState = 1
  225. };
  226. class RenderDevice
  227. {
  228. public:
  229. Array<DrawBatch*> mDrawBatchPool;
  230. RenderWindow* mPhysRenderWindow;
  231. RenderState* mPhysRenderState;
  232. int mResizeCount;
  233. Array<RenderWindow*> mRenderWindowList;
  234. RenderTarget* mCurRenderTarget;
  235. DrawLayer* mCurDrawLayer;
  236. RenderState* mDefaultRenderState;
  237. RenderState* mCurRenderState;
  238. MemoryPool mPooledIndexBuffers;
  239. MemoryPool mPooledVertexBuffers;
  240. MemoryPool mPooledRenderCmdBuffers;
  241. public:
  242. virtual void PhysSetRenderState(RenderState* renderState) = 0;
  243. virtual void PhysSetRenderTarget(Texture* renderTarget) = 0;
  244. public:
  245. RenderDevice();
  246. virtual ~RenderDevice();
  247. virtual bool Init(BFApp* app) = 0;
  248. virtual void AddRenderWindow(RenderWindow* renderWindow);
  249. virtual void RemoveRenderWindow(RenderWindow* renderWindow);
  250. virtual RenderState* CreateRenderState(RenderState* srcRenderState);
  251. virtual ModelInstance* CreateModelInstance(ModelDef* modelDef, ModelCreateFlags flags) { return NULL; }
  252. virtual VertexDefinition* CreateVertexDefinition(VertexDefData* elementData, int numElements);
  253. virtual void FrameStart() = 0;
  254. virtual void FrameEnd();
  255. virtual Texture* LoadTexture(ImageData* imageData, int flags) = 0;
  256. virtual Texture* CreateDynTexture(int width, int height) = 0;
  257. virtual Texture* LoadTexture(const StringImpl& fileName, int flags);
  258. virtual Texture* CreateRenderTarget(int width, int height, bool destAlpha) = 0;
  259. virtual Shader* LoadShader(const StringImpl& fileName, VertexDefinition* vertexDefinition) = 0;
  260. virtual void SetRenderState(RenderState* renderState) = 0;
  261. };
  262. NS_BF_END;