scene_triangle_mesh.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "scene_triangle_mesh.h"
  4. #include "scene.h"
  5. namespace embree
  6. {
  7. #if defined(EMBREE_LOWEST_ISA)
  8. TriangleMesh::TriangleMesh (Device* device)
  9. : Geometry(device,GTY_TRIANGLE_MESH,0,1)
  10. {
  11. vertices.resize(numTimeSteps);
  12. }
  13. void TriangleMesh::setMask (unsigned mask)
  14. {
  15. this->mask = mask;
  16. Geometry::update();
  17. }
  18. void TriangleMesh::setNumTimeSteps (unsigned int numTimeSteps)
  19. {
  20. vertices.resize(numTimeSteps);
  21. Geometry::setNumTimeSteps(numTimeSteps);
  22. }
  23. void TriangleMesh::setVertexAttributeCount (unsigned int N)
  24. {
  25. vertexAttribs.resize(N);
  26. Geometry::update();
  27. }
  28. void TriangleMesh::setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num)
  29. {
  30. /* verify that all accesses are 4 bytes aligned */
  31. if (((size_t(buffer->getPtr()) + offset) & 0x3) || (stride & 0x3))
  32. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "data must be 4 bytes aligned");
  33. if (type == RTC_BUFFER_TYPE_VERTEX)
  34. {
  35. if (format != RTC_FORMAT_FLOAT3)
  36. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "invalid vertex buffer format");
  37. /* if buffer is larger than 16GB the premultiplied index optimization does not work */
  38. if (stride*num > 16ll*1024ll*1024ll*1024ll)
  39. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "vertex buffer can be at most 16GB large");
  40. if (slot >= vertices.size())
  41. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid vertex buffer slot");
  42. vertices[slot].set(buffer, offset, stride, num, format);
  43. vertices[slot].checkPadding16();
  44. vertices0 = vertices[0];
  45. }
  46. else if (type == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
  47. {
  48. if (format < RTC_FORMAT_FLOAT || format > RTC_FORMAT_FLOAT16)
  49. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "invalid vertex attribute buffer format");
  50. if (slot >= vertexAttribs.size())
  51. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "invalid vertex attribute buffer slot");
  52. vertexAttribs[slot].set(buffer, offset, stride, num, format);
  53. vertexAttribs[slot].checkPadding16();
  54. }
  55. else if (type == RTC_BUFFER_TYPE_INDEX)
  56. {
  57. if (slot != 0)
  58. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  59. if (format != RTC_FORMAT_UINT3)
  60. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "invalid index buffer format");
  61. triangles.set(buffer, offset, stride, num, format);
  62. setNumPrimitives(num);
  63. }
  64. else
  65. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "unknown buffer type");
  66. }
  67. void* TriangleMesh::getBuffer(RTCBufferType type, unsigned int slot)
  68. {
  69. if (type == RTC_BUFFER_TYPE_INDEX)
  70. {
  71. if (slot != 0)
  72. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  73. return triangles.getPtr();
  74. }
  75. else if (type == RTC_BUFFER_TYPE_VERTEX)
  76. {
  77. if (slot >= vertices.size())
  78. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  79. return vertices[slot].getPtr();
  80. }
  81. else if (type == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
  82. {
  83. if (slot >= vertexAttribs.size())
  84. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  85. return vertexAttribs[slot].getPtr();
  86. }
  87. else
  88. {
  89. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "unknown buffer type");
  90. return nullptr;
  91. }
  92. }
  93. void TriangleMesh::updateBuffer(RTCBufferType type, unsigned int slot)
  94. {
  95. if (type == RTC_BUFFER_TYPE_INDEX)
  96. {
  97. if (slot != 0)
  98. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  99. triangles.setModified();
  100. }
  101. else if (type == RTC_BUFFER_TYPE_VERTEX)
  102. {
  103. if (slot >= vertices.size())
  104. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  105. vertices[slot].setModified();
  106. }
  107. else if (type == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
  108. {
  109. if (slot >= vertexAttribs.size())
  110. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer slot");
  111. vertexAttribs[slot].setModified();
  112. }
  113. else
  114. {
  115. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "unknown buffer type");
  116. }
  117. Geometry::update();
  118. }
  119. void TriangleMesh::commit()
  120. {
  121. /* verify that stride of all time steps are identical */
  122. for (unsigned int t=0; t<numTimeSteps; t++)
  123. if (vertices[t].getStride() != vertices[0].getStride())
  124. throw_RTCError(RTC_ERROR_INVALID_OPERATION,"stride of vertex buffers have to be identical for each time step");
  125. Geometry::commit();
  126. }
  127. void TriangleMesh::addElementsToCount (GeometryCounts & counts) const
  128. {
  129. if (numTimeSteps == 1) counts.numTriangles += numPrimitives;
  130. else counts.numMBTriangles += numPrimitives;
  131. }
  132. bool TriangleMesh::verify()
  133. {
  134. /*! verify size of vertex arrays */
  135. if (vertices.size() == 0) return false;
  136. for (const auto& buffer : vertices)
  137. if (buffer.size() != numVertices())
  138. return false;
  139. /*! verify size of user vertex arrays */
  140. for (const auto& buffer : vertexAttribs)
  141. if (buffer.size() != numVertices())
  142. return false;
  143. /*! verify triangle indices */
  144. for (size_t i=0; i<size(); i++) {
  145. if (triangles[i].v[0] >= numVertices()) return false;
  146. if (triangles[i].v[1] >= numVertices()) return false;
  147. if (triangles[i].v[2] >= numVertices()) return false;
  148. }
  149. /*! verify vertices */
  150. for (const auto& buffer : vertices)
  151. for (size_t i=0; i<buffer.size(); i++)
  152. if (!isvalid(buffer[i]))
  153. return false;
  154. return true;
  155. }
  156. void TriangleMesh::interpolate(const RTCInterpolateArguments* const args) {
  157. interpolate_impl<4>(args);
  158. }
  159. #endif
  160. namespace isa
  161. {
  162. TriangleMesh* createTriangleMesh(Device* device) {
  163. return new TriangleMeshISA(device);
  164. }
  165. }
  166. }