tessellation.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. namespace embree
  5. {
  6. /* adjust discret tessellation level for feature-adaptive pre-subdivision */
  7. __forceinline float adjustTessellationLevel(float l, const size_t sublevel)
  8. {
  9. for (size_t i=0; i<sublevel; i++) l *= 0.5f;
  10. float r = ceilf(l);
  11. for (size_t i=0; i<sublevel; i++) r *= 2.0f;
  12. return r;
  13. }
  14. __forceinline int stitch(const int x, const int fine, const int coarse) {
  15. return (2*x+1)*coarse/(2*fine);
  16. }
  17. __forceinline void stitchGridEdges(const unsigned int low_rate,
  18. const unsigned int high_rate,
  19. const unsigned int x0,
  20. const unsigned int x1,
  21. float * __restrict__ const uv_array,
  22. const unsigned int uv_array_step)
  23. {
  24. #if 1
  25. const float inv_low_rate = rcp((float)(low_rate-1));
  26. for (unsigned x=x0; x<=x1; x++) {
  27. uv_array[(x-x0)*uv_array_step] = float(stitch(x,high_rate-1,low_rate-1))*inv_low_rate;
  28. }
  29. if (unlikely(x1 == high_rate-1))
  30. uv_array[(x1-x0)*uv_array_step] = 1.0f;
  31. #else
  32. assert(low_rate < high_rate);
  33. assert(high_rate >= 2);
  34. const float inv_low_rate = rcp((float)(low_rate-1));
  35. const unsigned int dy = low_rate - 1;
  36. const unsigned int dx = high_rate - 1;
  37. int p = 2*dy-dx;
  38. unsigned int offset = 0;
  39. unsigned int y = 0;
  40. float value = 0.0f;
  41. for(unsigned int x=0;x<high_rate-1; x++) // '<=' would be correct but we will leave the 1.0f at the end
  42. {
  43. uv_array[offset] = value;
  44. offset += uv_array_step;
  45. if (unlikely(p > 0))
  46. {
  47. y++;
  48. value = (float)y * inv_low_rate;
  49. p -= 2*dx;
  50. }
  51. p += 2*dy;
  52. }
  53. #endif
  54. }
  55. __forceinline void stitchUVGrid(const float edge_levels[4],
  56. const unsigned int swidth,
  57. const unsigned int sheight,
  58. const unsigned int x0,
  59. const unsigned int y0,
  60. const unsigned int grid_u_res,
  61. const unsigned int grid_v_res,
  62. float * __restrict__ const u_array,
  63. float * __restrict__ const v_array)
  64. {
  65. const unsigned int x1 = x0+grid_u_res-1;
  66. const unsigned int y1 = y0+grid_v_res-1;
  67. const unsigned int int_edge_points0 = (unsigned int)edge_levels[0] + 1;
  68. const unsigned int int_edge_points1 = (unsigned int)edge_levels[1] + 1;
  69. const unsigned int int_edge_points2 = (unsigned int)edge_levels[2] + 1;
  70. const unsigned int int_edge_points3 = (unsigned int)edge_levels[3] + 1;
  71. if (unlikely(y0 == 0 && int_edge_points0 < swidth))
  72. stitchGridEdges(int_edge_points0,swidth,x0,x1,u_array,1);
  73. if (unlikely(y1 == sheight-1 && int_edge_points2 < swidth))
  74. stitchGridEdges(int_edge_points2,swidth,x0,x1,&u_array[(grid_v_res-1)*grid_u_res],1);
  75. if (unlikely(x0 == 0 && int_edge_points1 < sheight))
  76. stitchGridEdges(int_edge_points1,sheight,y0,y1,&v_array[grid_u_res-1],grid_u_res);
  77. if (unlikely(x1 == swidth-1 && int_edge_points3 < sheight))
  78. stitchGridEdges(int_edge_points3,sheight,y0,y1,v_array,grid_u_res);
  79. }
  80. __forceinline void gridUVTessellator(const float edge_levels[4],
  81. const unsigned int swidth,
  82. const unsigned int sheight,
  83. const unsigned int x0,
  84. const unsigned int y0,
  85. const unsigned int grid_u_res,
  86. const unsigned int grid_v_res,
  87. float * __restrict__ const u_array,
  88. float * __restrict__ const v_array)
  89. {
  90. assert( grid_u_res >= 1);
  91. assert( grid_v_res >= 1);
  92. assert( edge_levels[0] >= 1.0f );
  93. assert( edge_levels[1] >= 1.0f );
  94. assert( edge_levels[2] >= 1.0f );
  95. assert( edge_levels[3] >= 1.0f );
  96. #if defined(__AVX__)
  97. const vint8 grid_u_segments = vint8(swidth)-1;
  98. const vint8 grid_v_segments = vint8(sheight)-1;
  99. const vfloat8 inv_grid_u_segments = rcp(vfloat8(grid_u_segments));
  100. const vfloat8 inv_grid_v_segments = rcp(vfloat8(grid_v_segments));
  101. unsigned int index = 0;
  102. vint8 v_i( zero );
  103. for (unsigned int y=0;y<grid_v_res;y++,index+=grid_u_res,v_i += 1)
  104. {
  105. vint8 u_i ( step );
  106. const vbool8 m_v = v_i < grid_v_segments;
  107. for (unsigned int x=0;x<grid_u_res;x+=8, u_i += 8)
  108. {
  109. const vbool8 m_u = u_i < grid_u_segments;
  110. const vfloat8 u = select(m_u, vfloat8(x0+u_i) * inv_grid_u_segments, 1.0f);
  111. const vfloat8 v = select(m_v, vfloat8(y0+v_i) * inv_grid_v_segments, 1.0f);
  112. vfloat8::storeu(&u_array[index + x],u);
  113. vfloat8::storeu(&v_array[index + x],v);
  114. }
  115. }
  116. #else
  117. const vint4 grid_u_segments = vint4(swidth)-1;
  118. const vint4 grid_v_segments = vint4(sheight)-1;
  119. const vfloat4 inv_grid_u_segments = rcp(vfloat4(grid_u_segments));
  120. const vfloat4 inv_grid_v_segments = rcp(vfloat4(grid_v_segments));
  121. unsigned int index = 0;
  122. vint4 v_i( zero );
  123. for (unsigned int y=0;y<grid_v_res;y++,index+=grid_u_res,v_i += 1)
  124. {
  125. vint4 u_i ( step );
  126. const vbool4 m_v = v_i < grid_v_segments;
  127. for (unsigned int x=0;x<grid_u_res;x+=4, u_i += 4)
  128. {
  129. const vbool4 m_u = u_i < grid_u_segments;
  130. const vfloat4 u = select(m_u, vfloat4(x0+u_i) * inv_grid_u_segments, 1.0f);
  131. const vfloat4 v = select(m_v, vfloat4(y0+v_i) * inv_grid_v_segments, 1.0f);
  132. vfloat4::storeu(&u_array[index + x],u);
  133. vfloat4::storeu(&v_array[index + x],v);
  134. }
  135. }
  136. #endif
  137. }
  138. }