scene_line_segments.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "scene_line_segments.h"
  17. #include "scene.h"
  18. namespace embree
  19. {
  20. LineSegments::LineSegments (Scene* parent, RTCGeometryFlags flags, size_t numPrimitives, size_t numVertices, size_t numTimeSteps)
  21. : Geometry(parent,LINE_SEGMENTS,numPrimitives,numTimeSteps,flags)
  22. {
  23. segments.init(parent->device,numPrimitives,sizeof(int));
  24. vertices.resize(numTimeSteps);
  25. for (size_t i=0; i<numTimeSteps; i++) {
  26. vertices[i].init(parent->device,numVertices,sizeof(Vec3fa));
  27. }
  28. enabling();
  29. }
  30. void LineSegments::enabling()
  31. {
  32. if (numTimeSteps == 1) parent->world.numLineSegments += numPrimitives;
  33. else parent->worldMB.numLineSegments += numPrimitives;
  34. }
  35. void LineSegments::disabling()
  36. {
  37. if (numTimeSteps == 1) parent->world.numLineSegments -= numPrimitives;
  38. else parent->worldMB.numLineSegments -= numPrimitives;
  39. }
  40. void LineSegments::setMask (unsigned mask)
  41. {
  42. if (parent->isStatic() && parent->isBuild())
  43. throw_RTCError(RTC_INVALID_OPERATION,"static geometries cannot get modified");
  44. this->mask = mask;
  45. Geometry::update();
  46. }
  47. void LineSegments::setBuffer(RTCBufferType type, void* ptr, size_t offset, size_t stride, size_t size)
  48. {
  49. if (parent->isStatic() && parent->isBuild())
  50. throw_RTCError(RTC_INVALID_OPERATION,"static geometries cannot get modified");
  51. /* verify that all accesses are 4 bytes aligned */
  52. if (((size_t(ptr) + offset) & 0x3) || (stride & 0x3))
  53. throw_RTCError(RTC_INVALID_OPERATION,"data must be 4 bytes aligned");
  54. unsigned bid = type & 0xFFFF;
  55. if (type >= RTC_VERTEX_BUFFER0 && type < RTCBufferType(RTC_VERTEX_BUFFER0 + numTimeSteps))
  56. {
  57. size_t t = type - RTC_VERTEX_BUFFER0;
  58. vertices[t].set(ptr,offset,stride,size);
  59. vertices[t].checkPadding16();
  60. vertices0 = vertices[0];
  61. }
  62. else if (type >= RTC_USER_VERTEX_BUFFER0 && type < RTC_USER_VERTEX_BUFFER0+RTC_MAX_USER_VERTEX_BUFFERS)
  63. {
  64. if (bid >= userbuffers.size()) userbuffers.resize(bid+1);
  65. userbuffers[bid] = APIBuffer<char>(parent->device,numVertices(),stride);
  66. userbuffers[bid].set(ptr,offset,stride,size);
  67. userbuffers[bid].checkPadding16();
  68. }
  69. else if (type == RTC_INDEX_BUFFER)
  70. {
  71. if (size != (size_t)-1) disabling();
  72. segments.set(ptr,offset,stride,size);
  73. setNumPrimitives(size);
  74. if (size != (size_t)-1) enabling();
  75. }
  76. else
  77. throw_RTCError(RTC_INVALID_ARGUMENT,"unknown buffer type");
  78. }
  79. void* LineSegments::map(RTCBufferType type)
  80. {
  81. if (parent->isStatic() && parent->isBuild()) {
  82. throw_RTCError(RTC_INVALID_OPERATION,"static geometries cannot get modified");
  83. return nullptr;
  84. }
  85. if (type == RTC_INDEX_BUFFER) {
  86. return segments.map(parent->numMappedBuffers);
  87. }
  88. else if (type >= RTC_VERTEX_BUFFER0 && type < RTCBufferType(RTC_VERTEX_BUFFER0 + numTimeSteps)) {
  89. return vertices[type - RTC_VERTEX_BUFFER0].map(parent->numMappedBuffers);
  90. }
  91. else {
  92. throw_RTCError(RTC_INVALID_ARGUMENT,"unknown buffer type");
  93. return nullptr;
  94. }
  95. }
  96. void LineSegments::unmap(RTCBufferType type)
  97. {
  98. if (parent->isStatic() && parent->isBuild())
  99. throw_RTCError(RTC_INVALID_OPERATION,"static geometries cannot get modified");
  100. if (type == RTC_INDEX_BUFFER) {
  101. segments.unmap(parent->numMappedBuffers);
  102. }
  103. else if (type >= RTC_VERTEX_BUFFER0 && type < RTCBufferType(RTC_VERTEX_BUFFER0 + numTimeSteps)) {
  104. vertices[type - RTC_VERTEX_BUFFER0].unmap(parent->numMappedBuffers);
  105. vertices0 = vertices[0];
  106. }
  107. else {
  108. throw_RTCError(RTC_INVALID_ARGUMENT,"unknown buffer type");
  109. }
  110. }
  111. void LineSegments::immutable ()
  112. {
  113. const bool freeIndices = !parent->needLineIndices;
  114. const bool freeVertices = !parent->needLineVertices;
  115. if (freeIndices) segments.free();
  116. if (freeVertices )
  117. for (auto& buffer : vertices)
  118. buffer.free();
  119. }
  120. bool LineSegments::verify ()
  121. {
  122. /*! verify consistent size of vertex arrays */
  123. if (vertices.size() == 0) return false;
  124. for (const auto& buffer : vertices)
  125. if (buffer.size() != numVertices())
  126. return false;
  127. /*! verify segment indices */
  128. for (size_t i=0; i<numPrimitives; i++) {
  129. if (segments[i]+1 >= numVertices()) return false;
  130. }
  131. /*! verify vertices */
  132. for (const auto& buffer : vertices) {
  133. for (size_t i=0; i<buffer.size(); i++) {
  134. if (!isvalid(buffer[i].x)) return false;
  135. if (!isvalid(buffer[i].y)) return false;
  136. if (!isvalid(buffer[i].z)) return false;
  137. if (!isvalid(buffer[i].w)) return false;
  138. }
  139. }
  140. return true;
  141. }
  142. void LineSegments::interpolate(unsigned primID, float u, float v, RTCBufferType buffer, float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv, size_t numFloats)
  143. {
  144. /* test if interpolation is enabled */
  145. #if defined(DEBUG)
  146. if ((parent->aflags & RTC_INTERPOLATE) == 0)
  147. throw_RTCError(RTC_INVALID_OPERATION,"rtcInterpolate can only get called when RTC_INTERPOLATE is enabled for the scene");
  148. #endif
  149. /* calculate base pointer and stride */
  150. assert((buffer >= RTC_VERTEX_BUFFER0 && buffer < RTCBufferType(RTC_VERTEX_BUFFER0 + numTimeSteps)) ||
  151. (buffer >= RTC_USER_VERTEX_BUFFER0 && buffer <= RTC_USER_VERTEX_BUFFER1));
  152. const char* src = nullptr;
  153. size_t stride = 0;
  154. if (buffer >= RTC_USER_VERTEX_BUFFER0) {
  155. src = userbuffers[buffer&0xFFFF].getPtr();
  156. stride = userbuffers[buffer&0xFFFF].getStride();
  157. } else {
  158. src = vertices[buffer&0xFFFF].getPtr();
  159. stride = vertices[buffer&0xFFFF].getStride();
  160. }
  161. for (size_t i=0; i<numFloats; i+=VSIZEX)
  162. {
  163. const size_t ofs = i*sizeof(float);
  164. const size_t segment = segments[primID];
  165. const vboolx valid = vintx((int)i)+vintx(step) < vintx(numFloats);
  166. const vfloatx p0 = vfloatx::loadu(valid,(float*)&src[(segment+0)*stride+ofs]);
  167. const vfloatx p1 = vfloatx::loadu(valid,(float*)&src[(segment+1)*stride+ofs]);
  168. if (P ) vfloatx::storeu(valid,P+i,lerp(p0,p1,u));
  169. if (dPdu ) vfloatx::storeu(valid,dPdu+i,p1-p0);
  170. if (ddPdudu) vfloatx::storeu(valid,dPdu+i,vfloatx(zero));
  171. }
  172. }
  173. }