RenderDevice.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. virtual float GetRefreshRate() { return 60.0f; }
  68. virtual bool WaitForVBlank() { return false; }
  69. };
  70. const int DRAWBUFFER_IDXBUFFER_SIZE = 8*1024;
  71. const int DRAWBUFFER_VTXBUFFER_SIZE = 64*1024;
  72. enum DepthFunc : int8
  73. {
  74. DepthFunc_Never,
  75. DepthFunc_Less,
  76. DepthFunc_LessEqual,
  77. DepthFunc_Equal,
  78. DepthFunc_Greater,
  79. DepthFunc_NotEqual,
  80. DepthFunc_GreaterEqual,
  81. DepthFunc_Always
  82. };
  83. enum VertexElementFormat : int8
  84. {
  85. VertexElementFormat_Single,
  86. VertexElementFormat_Vector2,
  87. VertexElementFormat_Vector3,
  88. VertexElementFormat_Vector4,
  89. VertexElementFormat_Color,
  90. VertexElementFormat_Byte4,
  91. VertexElementFormat_Short2,
  92. VertexElementFormat_Short4,
  93. VertexElementFormat_NormalizedShort2,
  94. VertexElementFormat_NormalizedShort4,
  95. VertexElementFormat_HalfVector2,
  96. VertexElementFormat_HalfVector4
  97. };
  98. enum VertexElementUsage : int8
  99. {
  100. VertexElementUsage_Position2D,
  101. VertexElementUsage_Position3D,
  102. VertexElementUsage_Color,
  103. VertexElementUsage_TextureCoordinate,
  104. VertexElementUsage_Normal,
  105. VertexElementUsage_Binormal,
  106. VertexElementUsage_Tangent,
  107. VertexElementUsage_BlendIndices,
  108. VertexElementUsage_BlendWeight,
  109. VertexElementUsage_Depth,
  110. VertexElementUsage_Fog,
  111. VertexElementUsage_PointSize,
  112. VertexElementUsage_Sample,
  113. VertexElementUsage_TessellateFactor
  114. };
  115. enum ConstantDataType : int8
  116. {
  117. ConstantDataType_Single,
  118. ConstantDataType_Vector2,
  119. ConstantDataType_Vector3,
  120. ConstantDataType_Vector4,
  121. ConstantDataType_Matrix
  122. };
  123. enum CullMode : int8
  124. {
  125. CullMode_None,
  126. CullMode_Front,
  127. CullMode_Back
  128. };
  129. enum Topology3D : int8
  130. {
  131. Topology3D_TriangleList,
  132. Topology3D_LineLine
  133. };
  134. enum TextureFlag : int8
  135. {
  136. TextureFlag_Additive = 1,
  137. TextureFlag_NoPremult = 2,
  138. TextureFlag_AllowRead = 4,
  139. TextureFlag_HasTransFollowing = 8
  140. };
  141. struct VertexDefData
  142. {
  143. VertexElementUsage mUsage;
  144. int mUsageIndex;
  145. VertexElementFormat mFormat;
  146. };
  147. class VertexDefinition
  148. {
  149. public:
  150. VertexDefData* mElementData;
  151. int mNumElements;
  152. public:
  153. VertexDefinition()
  154. {
  155. mElementData = NULL;
  156. mNumElements = 0;
  157. }
  158. VertexDefinition(VertexDefinition* src)
  159. {
  160. mElementData = new VertexDefData[src->mNumElements];
  161. mNumElements = src->mNumElements;
  162. memcpy(mElementData, src->mElementData, sizeof(VertexDefData) * mNumElements);
  163. }
  164. virtual ~VertexDefinition()
  165. {
  166. delete [] mElementData;
  167. }
  168. };
  169. class RenderState
  170. {
  171. public:
  172. Shader* mShader;
  173. bool mWriteDepthBuffer;
  174. DepthFunc mDepthFunc;
  175. bool mClipped;
  176. bool mTexWrap;
  177. bool mWireframe;
  178. RectF mClipRect;
  179. CullMode mCullMode;
  180. Topology3D mTopology;
  181. public:
  182. RenderState();
  183. virtual ~RenderState() {}
  184. virtual void SetShader(Shader* shader) { mShader = shader; }
  185. virtual void SetTexWrap(bool wrap) { mTexWrap = wrap; }
  186. virtual void SetWireframe(bool wireframe) { mWireframe = wireframe; }
  187. virtual void SetClipped(bool clipped) { mClipped = clipped; }
  188. virtual void SetClipRect(const RectF& rect) { mClipRect = rect; }
  189. virtual void SetWriteDepthBuffer(bool writeDepthBuffer) { mWriteDepthBuffer = writeDepthBuffer; }
  190. virtual void SetDepthFunc(DepthFunc depthFunc) { mDepthFunc = depthFunc; }
  191. virtual void SetTopology(Topology3D topology) { mTopology = topology; }
  192. };
  193. class PoolData
  194. {
  195. public:
  196. PoolData* mNext;
  197. };
  198. class MemoryPool : protected SLIList<PoolData*>
  199. {
  200. public:
  201. int mSize;
  202. public:
  203. MemoryPool(int size)
  204. {
  205. mSize = size;
  206. }
  207. ~MemoryPool()
  208. {
  209. auto cur = mHead;
  210. while (cur != NULL)
  211. {
  212. auto next = cur->mNext;
  213. delete [] cur;
  214. cur = next;
  215. }
  216. }
  217. void* AllocMemoryBlock()
  218. {
  219. if (IsEmpty())
  220. return new uint8[mSize];
  221. return (uint8*)PopFront();
  222. }
  223. void FreeMemoryBlock(void* block)
  224. {
  225. PoolData* poolData = (PoolData*)block;
  226. poolData->mNext = NULL;
  227. PushBack(poolData);
  228. }
  229. };
  230. enum ModelCreateFlags
  231. {
  232. ModelCreateFlags_None = 0,
  233. ModelCreateFlags_NoSetRenderState = 1
  234. };
  235. class RenderDevice
  236. {
  237. public:
  238. Array<DrawBatch*> mDrawBatchPool;
  239. BFApp* mApp;
  240. RenderWindow* mPhysRenderWindow;
  241. RenderState* mPhysRenderState;
  242. int mResizeCount;
  243. Array<RenderWindow*> mRenderWindowList;
  244. RenderTarget* mCurRenderTarget;
  245. DrawLayer* mCurDrawLayer;
  246. RenderState* mDefaultRenderState;
  247. RenderState* mCurRenderState;
  248. MemoryPool mPooledIndexBuffers;
  249. MemoryPool mPooledVertexBuffers;
  250. MemoryPool mPooledRenderCmdBuffers;
  251. public:
  252. virtual void PhysSetRenderState(RenderState* renderState) = 0;
  253. virtual void PhysSetRenderTarget(Texture* renderTarget) = 0;
  254. public:
  255. RenderDevice();
  256. virtual ~RenderDevice();
  257. virtual bool Init(BFApp* app) = 0;
  258. virtual void AddRenderWindow(RenderWindow* renderWindow);
  259. virtual void RemoveRenderWindow(RenderWindow* renderWindow);
  260. virtual RenderState* CreateRenderState(RenderState* srcRenderState);
  261. virtual ModelInstance* CreateModelInstance(ModelDef* modelDef, ModelCreateFlags flags) { return NULL; }
  262. virtual VertexDefinition* CreateVertexDefinition(VertexDefData* elementData, int numElements);
  263. virtual void FrameStart() = 0;
  264. virtual void FrameEnd();
  265. virtual Texture* LoadTexture(ImageData* imageData, int flags) = 0;
  266. virtual Texture* CreateDynTexture(int width, int height) = 0;
  267. virtual Texture* LoadTexture(const StringImpl& fileName, int flags);
  268. virtual Texture* CreateRenderTarget(int width, int height, bool destAlpha) = 0;
  269. virtual Shader* LoadShader(const StringImpl& fileName, VertexDefinition* vertexDefinition) = 0;
  270. virtual void SetRenderState(RenderState* renderState) = 0;
  271. };
  272. NS_BF_END;