734 rivejä
23 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "lolgl.h"
  15. #if defined _WIN32 && defined USE_D3D9
  16. # define FAR
  17. # define NEAR
  18. # include <d3d9.h>
  19. #elif defined _XBOX
  20. # include <xtl.h>
  21. # undef near /* Fuck Microsoft */
  22. # undef far /* Fuck Microsoft again */
  23. #endif
  24. using namespace std;
  25. namespace lol
  26. {
  27. //
  28. // The VertexBufferData class
  29. // --------------------------
  30. //
  31. class VertexBufferData
  32. {
  33. friend class VertexBuffer;
  34. friend class VertexDeclaration;
  35. size_t m_size;
  36. #if defined USE_D3D9
  37. IDirect3DDevice9 *m_dev;
  38. IDirect3DVertexBuffer9 *m_vbo;
  39. #elif defined _XBOX
  40. D3DDevice *m_dev;
  41. D3DVertexBuffer *m_vbo;
  42. #else
  43. GLuint m_vbo;
  44. uint8_t *m_memory;
  45. #endif
  46. };
  47. //
  48. // The VertexDeclarationData class
  49. // -------------------------------
  50. //
  51. class VertexDeclarationData
  52. {
  53. friend class VertexBuffer;
  54. friend class VertexDeclaration;
  55. #if defined USE_D3D9
  56. IDirect3DDevice9 *m_dev;
  57. IDirect3DVertexDeclaration9 *m_vdecl;
  58. #elif defined _XBOX
  59. D3DDevice *m_dev;
  60. D3DVertexDeclaration *m_vdecl;
  61. #else
  62. #endif
  63. };
  64. //
  65. // The VertexDeclaration class
  66. // ---------------------------
  67. //
  68. VertexStreamBase const VertexStreamBase::Empty;
  69. VertexDeclaration::VertexDeclaration(VertexStreamBase const &s1,
  70. VertexStreamBase const &s2,
  71. VertexStreamBase const &s3,
  72. VertexStreamBase const &s4,
  73. VertexStreamBase const &s5,
  74. VertexStreamBase const &s6,
  75. VertexStreamBase const &s7,
  76. VertexStreamBase const &s8,
  77. VertexStreamBase const &s9,
  78. VertexStreamBase const &s10,
  79. VertexStreamBase const &s11,
  80. VertexStreamBase const &s12)
  81. : m_count(0),
  82. m_data(new VertexDeclarationData())
  83. {
  84. if (&s1 != &VertexStreamBase::Empty) AddStream(s1);
  85. if (&s2 != &VertexStreamBase::Empty) AddStream(s2);
  86. if (&s3 != &VertexStreamBase::Empty) AddStream(s3);
  87. if (&s4 != &VertexStreamBase::Empty) AddStream(s4);
  88. if (&s5 != &VertexStreamBase::Empty) AddStream(s5);
  89. if (&s6 != &VertexStreamBase::Empty) AddStream(s6);
  90. if (&s7 != &VertexStreamBase::Empty) AddStream(s7);
  91. if (&s8 != &VertexStreamBase::Empty) AddStream(s8);
  92. if (&s9 != &VertexStreamBase::Empty) AddStream(s9);
  93. if (&s10 != &VertexStreamBase::Empty) AddStream(s10);
  94. if (&s11 != &VertexStreamBase::Empty) AddStream(s11);
  95. if (&s12 != &VertexStreamBase::Empty) AddStream(s12);
  96. Initialize();
  97. }
  98. VertexDeclaration::~VertexDeclaration()
  99. {
  100. #if defined _XBOX || defined USE_D3D9
  101. if (FAILED(m_data->m_vdecl->Release()))
  102. Abort();
  103. #else
  104. #endif
  105. delete m_data;
  106. }
  107. void VertexDeclaration::Bind()
  108. {
  109. #if defined _XBOX || defined USE_D3D9
  110. if (FAILED(m_data->m_dev->SetVertexDeclaration(m_data->m_vdecl)))
  111. Abort();
  112. #else
  113. /* FIXME: Nothing to do? */
  114. #endif
  115. }
  116. void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count)
  117. {
  118. if (count <= 0)
  119. return;
  120. #if defined _XBOX || defined USE_D3D9
  121. switch (type.ToScalar())
  122. {
  123. case MeshPrimitive::Triangles:
  124. if (FAILED(m_data->m_dev->DrawPrimitive(D3DPT_TRIANGLELIST,
  125. skip, count)))
  126. Abort();
  127. break;
  128. case MeshPrimitive::TriangleStrips:
  129. if (FAILED(m_data->m_dev->DrawPrimitive(D3DPT_TRIANGLESTRIP,
  130. skip, count)))
  131. Abort();
  132. break;
  133. case MeshPrimitive::TriangleFans:
  134. if (FAILED(m_data->m_dev->DrawPrimitive(D3DPT_TRIANGLEFAN,
  135. skip, count)))
  136. Abort();
  137. break;
  138. case MeshPrimitive::Points:
  139. if (FAILED(m_data->m_dev->DrawPrimitive(D3DPT_POINTLIST,
  140. skip, count)))
  141. Abort();
  142. break;
  143. case MeshPrimitive::Lines:
  144. if (FAILED(m_data->m_dev->DrawPrimitive(D3DPT_LINELIST,
  145. skip, count)))
  146. Abort();
  147. break;
  148. }
  149. #else
  150. /* FIXME: this has nothing to do here! */
  151. switch (type.ToScalar())
  152. {
  153. case MeshPrimitive::Triangles:
  154. glDrawArrays(GL_TRIANGLES, skip, count);
  155. break;
  156. case MeshPrimitive::TriangleStrips:
  157. glDrawArrays(GL_TRIANGLE_STRIP, skip, count);
  158. break;
  159. case MeshPrimitive::TriangleFans:
  160. glDrawArrays(GL_TRIANGLE_FAN, skip, count);
  161. break;
  162. case MeshPrimitive::Points:
  163. glDrawArrays(GL_POINTS, skip, count);
  164. break;
  165. case MeshPrimitive::Lines:
  166. glDrawArrays(GL_LINES, skip, count);
  167. break;
  168. }
  169. #endif
  170. }
  171. void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase,
  172. int vskip, int vcount,
  173. int skip, int count)
  174. {
  175. if (count <= 0)
  176. return;
  177. #if defined _XBOX || defined USE_D3D9
  178. switch (type.ToScalar())
  179. {
  180. case MeshPrimitive::Triangles:
  181. count = count / 3;
  182. if (FAILED(m_data->m_dev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
  183. vbase, vskip, vcount, skip, count)))
  184. Abort();
  185. break;
  186. case MeshPrimitive::TriangleStrips:
  187. count = count - 2;
  188. if (FAILED(m_data->m_dev->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,
  189. vbase, vskip, vcount, skip, count)))
  190. Abort();
  191. break;
  192. case MeshPrimitive::TriangleFans:
  193. count = count - 2;
  194. if (FAILED(m_data->m_dev->DrawIndexedPrimitive(D3DPT_TRIANGLEFAN,
  195. vbase, vskip, vcount, skip, count)))
  196. Abort();
  197. break;
  198. case MeshPrimitive::Points:
  199. if (FAILED(m_data->m_dev->DrawIndexedPrimitive(D3DPT_POINTLIST,
  200. vbase, vskip, vcount, skip, count)))
  201. Abort();
  202. break;
  203. case MeshPrimitive::Lines:
  204. if (FAILED(m_data->m_dev->DrawIndexedPrimitive(D3DPT_LINELIST,
  205. vbase, vskip, vcount, skip, count)))
  206. Abort();
  207. break;
  208. }
  209. #else
  210. /* FIXME: this has nothing to do here! */
  211. switch (type.ToScalar())
  212. {
  213. case MeshPrimitive::Triangles:
  214. /* FIXME: ignores most of the arguments! */
  215. UNUSED(vbase, vskip, vcount, skip);
  216. glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, 0);
  217. break;
  218. case MeshPrimitive::TriangleStrips:
  219. /* FIXME: ignores most of the arguments! */
  220. UNUSED(vbase, vskip, vcount, skip);
  221. glDrawElements(GL_TRIANGLE_STRIP, count, GL_UNSIGNED_SHORT, 0);
  222. break;
  223. case MeshPrimitive::TriangleFans:
  224. /* FIXME: ignores most of the arguments! */
  225. UNUSED(vbase, vskip, vcount, skip);
  226. glDrawElements(GL_TRIANGLE_FAN, count, GL_UNSIGNED_SHORT, 0);
  227. break;
  228. case MeshPrimitive::Points:
  229. /* FIXME: ignores most of the arguments! */
  230. UNUSED(vbase, vskip, vcount, skip);
  231. glDrawElements(GL_POINTS, count, GL_UNSIGNED_SHORT, 0);
  232. break;
  233. case MeshPrimitive::Lines:
  234. /* FIXME: ignores most of the arguments! */
  235. UNUSED(vbase, vskip, vcount, skip);
  236. glDrawElements(GL_LINES, count, GL_UNSIGNED_SHORT, 0);
  237. break;
  238. }
  239. #endif
  240. }
  241. void VertexDeclaration::Unbind()
  242. {
  243. #if defined _XBOX || defined USE_D3D9
  244. int stream = -1;
  245. for (int i = 0; i < m_count; i++)
  246. if (m_streams[i].index != stream)
  247. {
  248. stream = m_streams[i].index;
  249. if (FAILED(m_data->m_dev->SetStreamSource(stream, 0, 0, 0)))
  250. Abort();
  251. }
  252. /* "NULL is an invalid input to SetVertexDeclaration" (DX9 guide), so
  253. * we just don't touch the current vertex declaration. */
  254. #elif !defined __CELLOS_LV2__
  255. for (int i = 0; i < m_count; i++)
  256. {
  257. if (m_streams[i].reg >= 0)
  258. {
  259. for (int j = i + 1; j < m_count; j++)
  260. if (m_streams[j].reg == m_streams[i].reg)
  261. m_streams[j].reg = -1;
  262. glDisableVertexAttribArray(m_streams[i].reg);
  263. }
  264. }
  265. glBindBuffer(GL_ARRAY_BUFFER, 0);
  266. #else
  267. /* Or even: */
  268. glDisableClientState(GL_VERTEX_ARRAY);
  269. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  270. glDisableClientState(GL_NORMAL_ARRAY);
  271. glDisableClientState(GL_COLOR_ARRAY);
  272. #endif
  273. }
  274. void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1,
  275. ShaderAttrib attr2,
  276. ShaderAttrib attr3,
  277. ShaderAttrib attr4,
  278. ShaderAttrib attr5,
  279. ShaderAttrib attr6,
  280. ShaderAttrib attr7,
  281. ShaderAttrib attr8,
  282. ShaderAttrib attr9,
  283. ShaderAttrib attr10,
  284. ShaderAttrib attr11,
  285. ShaderAttrib attr12)
  286. {
  287. #if defined _XBOX || defined USE_D3D9
  288. // Don't bother in DirectX world, shader attributes are not used
  289. SetStream(vb, NULL);
  290. #else
  291. ShaderAttrib attribs[12] = { attr1, attr2, attr3, attr4, attr5, attr6,
  292. attr7, attr8, attr9, attr10, attr11, attr12 };
  293. SetStream(vb, attribs);
  294. #endif
  295. }
  296. void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attribs[])
  297. {
  298. if (!vb->m_data->m_size)
  299. return;
  300. #if defined _XBOX || defined USE_D3D9
  301. /* Only the first item is required to know which stream this
  302. * is about; the rest of the information is stored in the
  303. * vertex declaration already. */
  304. VertexUsage usage = VertexUsage((attr1.m_flags >> 16) & 0xffff);
  305. uint32_t index = attr1.m_flags & 0xffff;
  306. /* Find the stream number */
  307. uint32_t usage_index = 0;
  308. int stream = -1;
  309. for (int i = 0; i < m_count; i++)
  310. if (m_streams[i].usage == usage)
  311. if (usage_index++ == index)
  312. {
  313. stream = m_streams[i].index;
  314. break;
  315. }
  316. /* Compute this stream's stride */
  317. int stride = 0;
  318. for (int i = 0; i < m_count; i++)
  319. if (stream == m_streams[i].index)
  320. stride += m_streams[i].size;
  321. /* Now we know the stream index and the element stride */
  322. /* FIXME: precompute most of the crap above! */
  323. if (stream >= 0)
  324. {
  325. if (FAILED(m_data->m_dev->SetStreamSource(stream, vb->m_data->m_vbo,
  326. 0, stride)))
  327. Abort();
  328. }
  329. #else
  330. glBindBuffer(GL_ARRAY_BUFFER, vb->m_data->m_vbo);
  331. for (int n = 0; n < 12 && attribs[n].m_flags != (uint64_t)0 - 1; n++)
  332. {
  333. VertexUsage usage = VertexUsage((attribs[n].m_flags >> 16) & 0xffff);
  334. uint32_t index = attribs[n].m_flags & 0xffff;
  335. uint32_t reg = attribs[n].m_flags >> 32;
  336. # if !defined __CELLOS_LV2__
  337. if (reg != 0xffffffffu)
  338. glEnableVertexAttribArray((GLint)reg);
  339. # else
  340. switch (usage.ToScalar())
  341. {
  342. case VertexUsage::Position:
  343. glEnableClientState(GL_VERTEX_ARRAY);
  344. break;
  345. case VertexUsage::TexCoord:
  346. case VertexUsage::TexCoordExt:
  347. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  348. break;
  349. case VertexUsage::Normal:
  350. glEnableClientState(GL_NORMAL_ARRAY);
  351. break;
  352. case VertexUsage::Color:
  353. glEnableClientState(GL_COLOR_ARRAY);
  354. break;
  355. }
  356. # endif
  357. /* We need to parse the whole vertex declaration to retrieve
  358. * the information. It sucks. */
  359. int attr_index = 0;
  360. /* First, find the stream index */
  361. for (uint32_t usage_index = 0; attr_index < m_count; attr_index++)
  362. if (m_streams[attr_index].usage == usage)
  363. if (usage_index++ == index)
  364. break;
  365. if (attr_index == m_count)
  366. {
  367. Log::Error("stream #%d with usage %x not found in declaration\n",
  368. index, usage);
  369. attr_index = 0;
  370. }
  371. /* Now compute the stride and offset up to this stream index */
  372. int stride = 0, offset = 0;
  373. for (int i = 0; i < m_count; i++)
  374. if (m_streams[i].index == m_streams[attr_index].index)
  375. {
  376. /* Remember the register used for this stream */
  377. m_streams[i].reg = reg;
  378. stride += m_streams[i].size;
  379. if (i < attr_index)
  380. offset += m_streams[i].size;
  381. }
  382. /* Finally, we need to retrieve the type of the data */
  383. # if !defined GL_DOUBLE
  384. # define GL_DOUBLE 0
  385. # endif
  386. static struct { GLint size; GLenum type; } const tlut[] =
  387. {
  388. { 0, 0 },
  389. { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, /* half */
  390. { 1, GL_FLOAT }, { 2, GL_FLOAT }, { 3, GL_FLOAT },
  391. { 4, GL_FLOAT }, /* float */
  392. { 1, GL_DOUBLE }, { 2, GL_DOUBLE }, { 3, GL_DOUBLE },
  393. { 4, GL_DOUBLE }, /* double */
  394. { 1, GL_BYTE }, { 2, GL_BYTE }, { 3, GL_BYTE },
  395. { 4, GL_BYTE }, /* int8_t */
  396. { 1, GL_UNSIGNED_BYTE }, { 2, GL_UNSIGNED_BYTE },
  397. { 3, GL_UNSIGNED_BYTE }, { 4, GL_UNSIGNED_BYTE }, /* uint8_t */
  398. { 1, GL_SHORT }, { 2, GL_SHORT }, { 3, GL_SHORT },
  399. { 4, GL_SHORT }, /* int16_t */
  400. { 1, GL_UNSIGNED_SHORT }, { 2, GL_UNSIGNED_SHORT }, { 3,
  401. GL_UNSIGNED_SHORT }, { 4, GL_UNSIGNED_SHORT }, /* uint16_t */
  402. { 1, GL_INT }, { 2, GL_INT }, { 3, GL_INT },
  403. { 4, GL_INT }, /* int32_t */
  404. { 1, GL_UNSIGNED_INT }, { 2, GL_UNSIGNED_INT },
  405. { 3, GL_UNSIGNED_INT }, { 4, GL_UNSIGNED_INT }, /* uint32_t */
  406. };
  407. int type_index = m_streams[attr_index].stream_type;
  408. if (type_index < 0 || type_index >= (int)(sizeof(tlut) / sizeof(*tlut)))
  409. type_index = 0;
  410. # if !defined __CELLOS_LV2__
  411. if (reg != 0xffffffff)
  412. {
  413. if (tlut[type_index].type == GL_FLOAT
  414. || tlut[type_index].type == GL_DOUBLE
  415. || tlut[type_index].type == GL_BYTE
  416. || tlut[type_index].type == GL_UNSIGNED_BYTE
  417. # if defined USE_GLEW && !defined __APPLE__
  418. /* If this is not available, don't use it */
  419. || !glVertexAttribIPointer
  420. # endif
  421. || false)
  422. {
  423. /* Normalize unsigned bytes by default, because it's usually
  424. * some color information. */
  425. GLboolean normalize = (tlut[type_index].type == GL_UNSIGNED_BYTE)
  426. || (tlut[type_index].type == GL_BYTE);
  427. glVertexAttribPointer((GLint)reg, tlut[type_index].size,
  428. tlut[type_index].type, normalize,
  429. stride, (GLvoid const *)(uintptr_t)offset);
  430. }
  431. # if defined GL_VERSION_3_0
  432. else
  433. {
  434. glVertexAttribIPointer((GLint)reg, tlut[type_index].size,
  435. tlut[type_index].type,
  436. stride, (GLvoid const *)(uintptr_t)offset);
  437. }
  438. # endif
  439. }
  440. # else
  441. switch (usage)
  442. {
  443. case VertexUsage::Position:
  444. glVertexPointer(tlut[type_index].size, tlut[type_index].type,
  445. stride, (GLvoid const *)(uintptr_t)offset);
  446. break;
  447. case VertexUsage::TexCoord:
  448. case VertexUsage::TexCoordExt:
  449. glTexCoordPointer(tlut[type_index].size, tlut[type_index].type,
  450. stride, (GLvoid const *)(uintptr_t)offset);
  451. break;
  452. case VertexUsage::Normal:
  453. glNormalPointer(tlut[type_index].type,
  454. stride, (GLvoid const *)(uintptr_t)offset);
  455. break;
  456. case VertexUsage::Color:
  457. glColorPointer(tlut[type_index].size, tlut[type_index].type,
  458. stride, (GLvoid const *)(uintptr_t)offset);
  459. break;
  460. default:
  461. Log::Error("vertex usage %d is not supported yet\n", usage);
  462. break;
  463. }
  464. # endif
  465. }
  466. #endif
  467. }
  468. void VertexDeclaration::Initialize()
  469. {
  470. #if defined _XBOX || defined USE_D3D9
  471. static D3DVERTEXELEMENT9 const end_element[] = { D3DDECL_END() };
  472. static D3DDECLTYPE const X = D3DDECLTYPE_UNUSED;
  473. static D3DDECLTYPE const tlut[] =
  474. {
  475. D3DDECLTYPE_UNUSED,
  476. X, D3DDECLTYPE_FLOAT16_2, X, D3DDECLTYPE_FLOAT16_4, /* half */
  477. D3DDECLTYPE_FLOAT1, D3DDECLTYPE_FLOAT2, D3DDECLTYPE_FLOAT3,
  478. D3DDECLTYPE_FLOAT4, /* float */
  479. X, X, X, X, /* double */
  480. X, X, X, X, /* int8_t */
  481. X, X, X, D3DDECLTYPE_UBYTE4N, /* uint8_t */
  482. X, D3DDECLTYPE_SHORT2N, X, D3DDECLTYPE_SHORT4N, /* int16_t */
  483. X, D3DDECLTYPE_USHORT2N, X, D3DDECLTYPE_USHORT4N, /* uint16_t */
  484. X, X, X, X, /* int32_t */
  485. X, X, X, X, /* uint32_t */
  486. };
  487. static D3DDECLUSAGE const ulut[] =
  488. {
  489. D3DDECLUSAGE_POSITION,
  490. D3DDECLUSAGE_BLENDWEIGHT,
  491. D3DDECLUSAGE_BLENDINDICES,
  492. D3DDECLUSAGE_NORMAL,
  493. D3DDECLUSAGE_PSIZE,
  494. D3DDECLUSAGE_TEXCOORD,
  495. D3DDECLUSAGE_TANGENT,
  496. D3DDECLUSAGE_BINORMAL,
  497. D3DDECLUSAGE_TESSFACTOR,
  498. #if defined _XBOX
  499. D3DDECLUSAGE_TEXCOORD, /* FIXME: nonexistent */
  500. #else
  501. D3DDECLUSAGE_POSITIONT,
  502. #endif
  503. D3DDECLUSAGE_COLOR,
  504. D3DDECLUSAGE_FOG,
  505. D3DDECLUSAGE_DEPTH,
  506. D3DDECLUSAGE_SAMPLE,
  507. };
  508. D3DVERTEXELEMENT9 elements[12 + 1];
  509. for (int n = 0; n < m_count; n++)
  510. {
  511. elements[n].Stream = m_streams[n].index;
  512. elements[n].Offset = 0;
  513. for (int i = 0; i < n; i++)
  514. if (m_streams[i].index == m_streams[n].index)
  515. elements[n].Offset += m_streams[i].size;
  516. if (m_streams[n].stream_type >= 0
  517. && m_streams[n].stream_type < sizeof(tlut) / sizeof(*tlut))
  518. elements[n].Type = tlut[m_streams[n].stream_type];
  519. else
  520. elements[n].Type = D3DDECLTYPE_UNUSED;
  521. elements[n].Method = D3DDECLMETHOD_DEFAULT;
  522. if (m_streams[n].usage >= 0
  523. && m_streams[n].usage < sizeof(ulut) / sizeof(*ulut))
  524. elements[n].Usage = ulut[m_streams[n].usage];
  525. else
  526. elements[n].Usage = D3DDECLUSAGE_POSITION;
  527. elements[n].UsageIndex = 0;
  528. for (int i = 0; i < n; i++)
  529. if (elements[i].Stream == elements[n].Stream
  530. && elements[i].Usage == elements[n].Usage)
  531. elements[n].UsageIndex++;
  532. }
  533. elements[m_count] = end_element[0];
  534. # if defined USE_D3D9
  535. m_data->m_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
  536. # elif defined _XBOX
  537. m_data->m_dev = (D3DDevice *)g_renderer->GetDevice();
  538. # endif
  539. if (FAILED(m_data->m_dev->CreateVertexDeclaration(elements,
  540. &m_data->m_vdecl)))
  541. Abort();
  542. #else
  543. #endif
  544. }
  545. void VertexDeclaration::AddStream(VertexStreamBase const &s)
  546. {
  547. int index = m_count ? m_streams[m_count - 1].index + 1 : 0;
  548. for (int i = 0; s.m_streams[i].size; i++)
  549. {
  550. m_streams[m_count].stream_type = s.m_streams[i].stream_type;
  551. m_streams[m_count].usage = s.m_streams[i].usage;
  552. m_streams[m_count].size = s.m_streams[i].size;
  553. m_streams[m_count].index = index;
  554. m_streams[m_count].reg = -1;
  555. m_count++;
  556. }
  557. }
  558. int VertexDeclaration::GetStreamCount() const
  559. {
  560. return m_count ? m_streams[m_count - 1].index + 1 : 0;
  561. }
  562. VertexStreamBase VertexDeclaration::GetStream(int index) const
  563. {
  564. VertexStreamBase stream;
  565. int n = 0;
  566. int count = 0;
  567. for (int i = 0; i < m_count; ++i)
  568. {
  569. if (m_streams[i].index != index)
  570. continue;
  571. switch (m_streams[i].stream_type)
  572. {
  573. #define __T(T) \
  574. case VertexStreamBase::Type##T: stream.AddStream<T>(n++, m_streams[i].usage); break;
  575. __T(void)
  576. __T(half) __T(f16vec2) __T(f16vec3) __T(f16vec4)
  577. __T(float) __T(vec2) __T(vec3) __T(vec4)
  578. __T(double) __T(dvec2) __T(dvec3) __T(dvec4)
  579. __T(int8_t) __T(i8vec2) __T(i8vec3) __T(i8vec4)
  580. __T(uint8_t) __T(u8vec2) __T(u8vec3) __T(u8vec4)
  581. __T(int16_t) __T(i16vec2) __T(i16vec3) __T(i16vec4)
  582. __T(uint16_t) __T(u16vec2) __T(u16vec3) __T(u16vec4)
  583. __T(int32_t) __T(ivec2) __T(ivec3) __T(ivec4)
  584. __T(uint32_t) __T(uvec2) __T(uvec3) __T(uvec4)
  585. #undef __T
  586. }
  587. ++count;
  588. }
  589. while (count < 12)
  590. stream.AddStream<void>(count++, VertexUsage::Position);
  591. return stream;
  592. }
  593. //
  594. // The VertexBuffer class
  595. // ----------------------
  596. //
  597. VertexBuffer::VertexBuffer(size_t size)
  598. : m_data(new VertexBufferData)
  599. {
  600. m_data->m_size = size;
  601. if (!size)
  602. return;
  603. #if defined USE_D3D9 || defined _XBOX
  604. # if defined USE_D3D9
  605. m_data->m_dev = (IDirect3DDevice9 *)g_renderer->GetDevice();
  606. # elif defined _XBOX
  607. m_data->m_dev = (D3DDevice *)g_renderer->GetDevice();
  608. # endif
  609. if (FAILED(m_data->m_dev->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, nullptr,
  610. D3DPOOL_MANAGED, &m_data->m_vbo, nullptr)))
  611. Abort();
  612. #else
  613. glGenBuffers(1, &m_data->m_vbo);
  614. m_data->m_memory = new uint8_t[size];
  615. #endif
  616. }
  617. VertexBuffer::~VertexBuffer()
  618. {
  619. if (m_data->m_size)
  620. {
  621. #if defined USE_D3D9 || defined _XBOX
  622. if (FAILED(m_data->m_vbo->Release()))
  623. Abort();
  624. #else
  625. glDeleteBuffers(1, &m_data->m_vbo);
  626. delete[] m_data->m_memory;
  627. #endif
  628. }
  629. delete m_data;
  630. }
  631. size_t VertexBuffer::GetSize()
  632. {
  633. return m_data->m_size;
  634. }
  635. void *VertexBuffer::Lock(size_t offset, size_t size)
  636. {
  637. if (!m_data->m_size)
  638. return nullptr;
  639. #if defined USE_D3D9 || defined _XBOX
  640. void *ret;
  641. if (FAILED(m_data->m_vbo->Lock(offset, size, (void **)&ret, 0)))
  642. Abort();
  643. return ret;
  644. #else
  645. /* FIXME: is there a way to use "size"? */
  646. UNUSED(size);
  647. return m_data->m_memory + offset;
  648. #endif
  649. }
  650. void VertexBuffer::Unlock()
  651. {
  652. if (!m_data->m_size)
  653. return;
  654. #if defined USE_D3D9 || defined _XBOX
  655. if (FAILED(m_data->m_vbo->Unlock()))
  656. Abort();
  657. #else
  658. glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo);
  659. glBufferData(GL_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
  660. GL_STATIC_DRAW);
  661. glBindBuffer(GL_ARRAY_BUFFER, 0);
  662. #endif
  663. }
  664. } /* namespace lol */