gridrange.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/default.h"
  5. namespace embree
  6. {
  7. struct __aligned(16) GridRange
  8. {
  9. unsigned int u_start;
  10. unsigned int u_end;
  11. unsigned int v_start;
  12. unsigned int v_end;
  13. __forceinline GridRange() {}
  14. __forceinline GridRange(unsigned int u_start, unsigned int u_end, unsigned int v_start, unsigned int v_end)
  15. : u_start(u_start), u_end(u_end), v_start(v_start), v_end(v_end) {}
  16. __forceinline unsigned int width() const {
  17. return u_end-u_start+1;
  18. }
  19. __forceinline unsigned int height() const {
  20. return v_end-v_start+1;
  21. }
  22. __forceinline bool hasLeafSize() const
  23. {
  24. const unsigned int u_size = u_end-u_start+1;
  25. const unsigned int v_size = v_end-v_start+1;
  26. assert(u_size >= 1);
  27. assert(v_size >= 1);
  28. return u_size <= 3 && v_size <= 3;
  29. }
  30. static __forceinline unsigned int split(unsigned int start,unsigned int end)
  31. {
  32. const unsigned int center = (start+end)/2;
  33. assert (center > start);
  34. assert (center < end);
  35. return center;
  36. }
  37. __forceinline void split(GridRange& r0, GridRange& r1) const
  38. {
  39. assert( hasLeafSize() == false );
  40. const unsigned int u_size = u_end-u_start+1;
  41. const unsigned int v_size = v_end-v_start+1;
  42. r0 = *this;
  43. r1 = *this;
  44. if (u_size >= v_size)
  45. {
  46. const unsigned int u_mid = split(u_start,u_end);
  47. r0.u_end = u_mid;
  48. r1.u_start = u_mid;
  49. }
  50. else
  51. {
  52. const unsigned int v_mid = split(v_start,v_end);
  53. r0.v_end = v_mid;
  54. r1.v_start = v_mid;
  55. }
  56. }
  57. __forceinline unsigned int splitIntoSubRanges(GridRange r[4]) const
  58. {
  59. assert( !hasLeafSize() );
  60. unsigned int children = 0;
  61. GridRange first,second;
  62. split(first,second);
  63. if (first.hasLeafSize()) {
  64. r[0] = first;
  65. children++;
  66. }
  67. else {
  68. first.split(r[0],r[1]);
  69. children += 2;
  70. }
  71. if (second.hasLeafSize()) {
  72. r[children] = second;
  73. children++;
  74. }
  75. else {
  76. second.split(r[children+0],r[children+1]);
  77. children += 2;
  78. }
  79. return children;
  80. }
  81. };
  82. }