VertexFormat.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "RendererTypes.h"
  25. namespace crown
  26. {
  27. struct VertexFormatInfo
  28. {
  29. bool has_attrib(ShaderAttrib::Enum attrib) const
  30. {
  31. return sizes[attrib] != 0;
  32. }
  33. /// Returns the number of components per @a attrib
  34. size_t num_components(ShaderAttrib::Enum attrib) const
  35. {
  36. return (size_t) sizes[attrib];
  37. }
  38. /// Returns the byte offset between consecutive vertex @a attrib
  39. size_t attrib_stride(ShaderAttrib::Enum /*attrib*/) const
  40. {
  41. size_t stride = 0;
  42. for (uint8_t i = 0; i < ShaderAttrib::COUNT; i++)
  43. {
  44. stride += sizes[i];
  45. }
  46. return stride * sizeof(float);
  47. }
  48. /// Returns the byte offset of the first @a attrib in the format
  49. size_t attrib_offset(ShaderAttrib::Enum attrib) const
  50. {
  51. size_t offset = 0;
  52. for (uint8_t i = 0; i < attrib; i++)
  53. {
  54. offset += sizes[i];
  55. }
  56. return offset * sizeof(float);
  57. }
  58. public:
  59. uint8_t sizes[ShaderAttrib::COUNT];
  60. };
  61. // VertexFormat to VertexFormatInfo
  62. const VertexFormatInfo VERTEX_FORMAT_INFO[VertexFormat::COUNT] =
  63. {
  64. { 2, 0, 0, 0, 0, 0, 0 },
  65. { 2, 3, 0, 0, 0, 0, 0 },
  66. { 2, 0, 4, 0, 0, 0, 0 },
  67. { 2, 0, 0, 2, 0, 0, 0 },
  68. { 2, 3, 4, 0, 0, 0, 0 },
  69. { 2, 3, 4, 2, 0, 0, 0 },
  70. { 3, 0, 0, 0, 0, 0, 0 },
  71. { 3, 3, 0, 0, 0, 0, 0 },
  72. { 3, 0, 4, 0, 0, 0, 0 },
  73. { 3, 0, 0, 2, 0, 0, 0 },
  74. { 3, 3, 4, 0, 0, 0, 0 },
  75. { 3, 3, 0, 2, 0, 0, 0 },
  76. { 3, 3, 4, 2, 0, 0, 0 }
  77. };
  78. class Vertex
  79. {
  80. public:
  81. /// Returns the bytes occupied by @a format
  82. static size_t bytes_per_vertex(VertexFormat::Enum format)
  83. {
  84. const VertexFormatInfo& info = VERTEX_FORMAT_INFO[format];
  85. size_t size = 0;
  86. for (uint8_t i = 0; i < ShaderAttrib::COUNT; i++)
  87. {
  88. size += info.sizes[i];
  89. }
  90. // Components are always float
  91. return size * sizeof(float);
  92. }
  93. /// Returns the bits occupied by @a format
  94. static size_t bits_per_vertex(VertexFormat::Enum format)
  95. {
  96. return bytes_per_vertex(format) * 8;
  97. }
  98. static const VertexFormatInfo& info(VertexFormat::Enum format)
  99. {
  100. return VERTEX_FORMAT_INFO[format];
  101. }
  102. private:
  103. // Disable construction
  104. Vertex();
  105. };
  106. } // namespace crown