Vertex Index Buffer Ex.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /******************************************************************************/
  2. #if EE_PRIVATE
  3. enum VTX_COMPRESS_FLAG
  4. {
  5. VTX_COMPRESS_NRM =1<<0, // compress "Vec normal " to "VecB4 normal "
  6. VTX_COMPRESS_TAN_BIN=1<<1, // compress "Vec tangent, binormal" to "VecB4 tan_bin"
  7. VTX_COMPRESS_TEX =1<<2, // compress "Vec2 tex " to "VecH2 tex "
  8. VTX_COMPRESS_TEX_8 =1<<3, // compress "Vec2 tex " to "VecB4 tex "
  9. VTX_COMPRESS_NRM_TAN_BIN=VTX_COMPRESS_NRM|VTX_COMPRESS_TAN_BIN,
  10. };
  11. Vec UByte4ToNrm (C VecB4 &v ); // ( 0..255) -> (-1..1 )
  12. void UByte4ToTan (C VecB4 &v, Vec *tan, Vec *bin, C Vec *nrm); // ( 0..255) -> (-1..1 )
  13. VecB4 NrmToUByte4(C Vec &v ); // (-1..1 ) -> ( 0..255)
  14. VecB4 TanToUByte4(C Vec &v ); // (-1..1 ) -> ( 0..255)
  15. VecB4 TBNToUByte4( C Vec *tan, C Vec *bin, C Vec *nrm); // (-1..1 ) -> ( 0..255)
  16. Vec SByte4ToNrm (C VecB4 &v ); // (-128..127) -> (- 1..1 )
  17. void SByte4ToTan (C VecB4 &v, Vec *tan, Vec *bin, C Vec *nrm); // (-128..127) -> (- 1..1 )
  18. VecB4 NrmToSByte4(C Vec &v ); // (- 1..1 ) -> (-128..127)
  19. VecB4 TanToSByte4(C Vec &v ); // (- 1..1 ) -> (-128..127)
  20. VecB4 TBNToSByte4( C Vec *tan, C Vec *bin, C Vec *nrm); // (- 1..1 ) -> (-128..127)
  21. /******************************************************************************/
  22. struct VtxFormatKey
  23. {
  24. UInt flag , // MESH_BASE_FLAG
  25. compress; // VTX_COMPRESS_FLAG
  26. VtxFormatKey() {}
  27. VtxFormatKey(UInt flag, UInt compress) {T.flag=flag; T.compress=compress;}
  28. };
  29. #if DX9
  30. Bool SetVtxFormatFromVtxDecl(IDirect3DVertexDeclaration9 *vf, D3DVERTEXELEMENT9 (&ve)[MAX_FVF_DECL_SIZE]);
  31. #elif GL
  32. enum GL_VTX_SEMANTIC : Byte // !! must be in sync with all "ATTR*" mentions in the engine !!
  33. {
  34. GL_VTX_POS , // 0
  35. GL_VTX_NRM , // 1
  36. GL_VTX_TAN , // 2
  37. GL_VTX_TEX0 , // 3
  38. GL_VTX_TEX1 , // 4, used for lightmaps
  39. GL_VTX_BONE , // 5
  40. GL_VTX_WEIGHT , // 6
  41. GL_VTX_COLOR , // 7
  42. // Galaxy Tab 2 (PowerVR SGX 540) has only 8 vertex attributes
  43. GL_VTX_MATERIAL, // 8
  44. GL_VTX_HLP , // 9
  45. // Galaxy Tab 3/4 have only 10 vertex attributes
  46. GL_VTX_SIZE , // 10
  47. GL_VTX_TEX2 , // 11
  48. GL_VTX_NUM , // 12
  49. };
  50. GL_VTX_SEMANTIC VtxSemanticToIndex(Int semantic);
  51. struct VtxFormatGL
  52. {
  53. struct Elm
  54. {
  55. GL_VTX_SEMANTIC semantic;
  56. Byte component_num;
  57. Bool component_normalize;
  58. GLenum component_type;
  59. UInt offset;
  60. void set(GL_VTX_SEMANTIC semantic, Byte component_num, GLenum component_type, Bool component_normalize, UInt offset) {T.semantic=semantic; T.component_num=component_num; T.component_type=component_type; T.component_normalize=component_normalize; T.offset=offset;}
  61. };
  62. Mems<Elm> elms;
  63. UInt vtx_size, vao;
  64. void del ();
  65. Bool create(C MemPtrN<Elm, 32> &elms);
  66. void disable ()C;
  67. void enableSet()C;
  68. void bind(C VtxBuf &vb);
  69. ~VtxFormatGL() {del();}
  70. VtxFormatGL() {vtx_size=vao=0;}
  71. };
  72. #endif
  73. #endif
  74. struct VtxFormat // Vertex Format
  75. {
  76. VtxFormat& del();
  77. #if EE_PRIVATE
  78. #if DX9
  79. Bool create(D3DVERTEXELEMENT9 ve[]);
  80. #elif DX11
  81. Bool create(D3D11_INPUT_ELEMENT_DESC ve[], Int elms);
  82. #elif GL
  83. Bool create(C MemPtrN<VtxFormatGL::Elm, 32> &elms);
  84. #endif
  85. Bool create(UInt flag, UInt compress); // 'flag'=MESH_BASE_FLAG, 'compress'=VTX_COMPRESS_FLAG
  86. #if GL
  87. void bind(C VtxBuf &vb);
  88. #else
  89. INLINE void bind(C VtxBuf &vb) {}
  90. #endif
  91. #endif
  92. ~VtxFormat() {del();}
  93. VtxFormat() {vf=null;}
  94. #if !EE_PRIVATE
  95. private:
  96. #endif
  97. #if EE_PRIVATE
  98. GPU_API(IDirect3DVertexDeclaration9, ID3D11InputLayout, VtxFormatGL) *vf;
  99. #else
  100. Ptr vf;
  101. #endif
  102. NO_COPY_CONSTRUCTOR(VtxFormat);
  103. };
  104. /******************************************************************************/
  105. struct VtxBuf // Vertex Buffer
  106. {
  107. // manage
  108. VtxBuf& del();
  109. // get
  110. Bool is ()C {return _buf!=NULL ;} // if created
  111. Int vtxs ()C {return _vtx_num ;} // get number of vertexes
  112. Int vtxSize ()C {return _vtx_size;} // get size of a single vertex
  113. UInt memUsage()C {return _vtx_num*_vtx_size;} // get memory usage of the vertex buffer (in bytes)
  114. // operations
  115. C Byte* lockedData()C {return _data;}
  116. Byte* lock (LOCK_MODE lock=LOCK_READ_WRITE);
  117. C Byte* lockRead ()C;
  118. void unlock () ;
  119. void unlock ()C;
  120. #if EE_PRIVATE
  121. Byte* lockDynamic(); // !! this is to be called only for VI buffers !!
  122. void unlockDynamic(); // !! this is to be called only for VI buffers !!
  123. Bool setFrom(CPtr data, Int size); // false on fail
  124. // manage
  125. Bool createRaw(Int memory_size , Bool dynamic=false, CPtr data=null); // false on fail
  126. Bool createNum(Int vtx_size, Int vtx_num , Bool dynamic=false, CPtr data=null); // false on fail
  127. Bool create (Int vtx_num , UInt flag, UInt compress, Bool dynamic=false ); // 'flag'=MESH_BASE_FLAG, 'compress'=VTX_COMPRESS_FLAG, false on fail
  128. Bool create (C VtxBuf &src , Int dynamic=-1 ); // create from 'src' , false on fail
  129. void freeOpenGLESData(); // this method is used only under OpenGL ES (on other platforms it is ignored), the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file)
  130. // draw
  131. #if DX9
  132. void set(Int stream=0)C {D3D->SetStreamSource(stream, _buf, 0, _vtx_size);}
  133. #elif DX11
  134. void set(Int stream=0)C {UInt stride=_vtx_size, offset=0; D3DC->IASetVertexBuffers(stream, 1, &_buf, &stride, &offset);}
  135. #elif GL
  136. void set(Int stream=0)C {glBindBuffer(GL_ARRAY_BUFFER, _buf);}
  137. #endif
  138. // io
  139. Bool save(File &f)C;
  140. Bool load(File &f) ;
  141. #endif
  142. ~VtxBuf() {del ( );}
  143. VtxBuf() {Zero(T);}
  144. #if !EE_PRIVATE
  145. private:
  146. #endif
  147. Bool _dynamic;
  148. LOCK_MODE _lock_mode;
  149. Int _vtx_size, _vtx_num, _lock_count;
  150. Byte *_data;
  151. #if EE_PRIVATE
  152. GPU_API(IDirect3DVertexBuffer9 *_buf, ID3D11Buffer *_buf, union{UInt _buf; Ptr buf_ptr;});
  153. #else
  154. Ptr _buf;
  155. #endif
  156. NO_COPY_CONSTRUCTOR(VtxBuf);
  157. };
  158. /******************************************************************************/
  159. struct IndBuf // Index Buffer
  160. {
  161. // manage
  162. IndBuf& del();
  163. // get
  164. Bool is ()C {return _ind_num>0;}
  165. Bool bit16 ()C {return _bit16 ;} // if indices are 16-bit (false for 32-bit)
  166. UInt memUsage()C {return _ind_num*(_bit16 ? 2 : 4);} // get memory usage of the index buffer (in bytes)
  167. // operations
  168. CPtr lockedData()C {return _data;}
  169. Ptr lock (LOCK_MODE lock=LOCK_READ_WRITE);
  170. CPtr lockRead ()C;
  171. void unlock () ;
  172. void unlock ()C;
  173. #if EE_PRIVATE
  174. Bool setFrom(CPtr data); // false on fail
  175. Bool create(Int indexes, Bool bit16, Bool dynamic=false, CPtr data=null); // false on fail
  176. Bool create(C IndBuf &src , Int dynamic=-1 ); // create from 'src', false on fail
  177. IndBuf& setTri (Int i, Int v0, Int v1, Int v2);
  178. IndBuf& setQuad(Int i, Int v0, Int v1, Int v2, Int v3);
  179. void freeOpenGLESData(); // this method is used only under OpenGL ES (on other platforms it is ignored), the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file)
  180. // draw
  181. #if DX9
  182. void set()C {D3D->SetIndices(_buf);}
  183. #elif DX11
  184. void set()C {D3DC->IASetIndexBuffer(_buf, _bit16 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);}
  185. #elif GL
  186. void set()C {glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buf);}
  187. #endif
  188. // io
  189. Bool save(File &f)C;
  190. Bool load(File &f) ;
  191. #endif
  192. ~IndBuf() {del ( );}
  193. IndBuf() {Zero(T);}
  194. #if !EE_PRIVATE
  195. private:
  196. #endif
  197. Bool _dynamic, _bit16;
  198. LOCK_MODE _lock_mode;
  199. Int _ind_num, _lock_count;
  200. Byte *_data;
  201. #if EE_PRIVATE
  202. GPU_API(IDirect3DIndexBuffer9 *_buf, ID3D11Buffer *_buf, union{UInt _buf; Ptr buf_ptr;});
  203. #else
  204. Ptr _buf;
  205. #endif
  206. NO_COPY_CONSTRUCTOR(IndBuf);
  207. };
  208. /******************************************************************************/
  209. #if EE_PRIVATE
  210. void InitVtxInd();
  211. void ShutVtxInd();
  212. #if GL
  213. INLINE void SetDefaultVAO() {if(D.notShaderModelGLES2())glBindVertexArray(VAO);}
  214. #else
  215. INLINE void SetDefaultVAO() {}
  216. #endif
  217. void BindIndexBuffer(UInt buf);
  218. extern ThreadSafeMap<VtxFormatKey, VtxFormat> VtxFormats;
  219. extern IndBuf IndBuf16384Quads, IndBufBorder, IndBufPanel, IndBufPanelEx, IndBufRectBorder, IndBufRectShaded;
  220. #endif
  221. /******************************************************************************/