22#define DSPLIT_NON_UNIFORM -1
23#define STITCH_NGON_CENTER_VERT_INDEX_OFFSET 0x60000000
24#define STITCH_NGON_SPLIT_EDGE_CENTER_VERT_TAG (0x60000000 - 1)
42 if (
b.x < a.x ||
b.y < a.y) {
54 float3 Plast = to_world(patch, Pstart);
59 float3 P = to_world(patch, Pstart + t * (Pend - Pstart));
70 L =
len(
P - Plast) / pixel_width;
82 int res =
max(tmax, 1);
85 if (!recursive_resolve) {
89 float2 P = (Pstart + Pend) * 0.5f;
90 res =
T(patch, Pstart,
P,
true) +
T(patch,
P, Pend,
true);
94 limit_edge_factor(res, patch, Pstart, Pend);
98void DiagSplit::partition_edge(
102 *
P = (Pstart + Pend) * 0.5f;
103 *t0 =
T(patch, Pstart, *
P);
104 *t1 =
T(patch, *
P, Pend);
110 *
P =
interp(Pstart, Pend,
I / (
float)t);
116void DiagSplit::limit_edge_factor(
int &T,
Patch *patch,
float2 Pstart,
float2 Pend)
119 int max_t_for_edge =
int(max_t *
len(Pstart - Pend));
122 max_t_for_edge >>= 1;
125 T = (max_t_for_edge <= 1) ? 1 :
min(T, max_t_for_edge);
130void DiagSplit::resolve_edge_factors(
Subpatch &sub)
147void DiagSplit::split(
Subpatch &sub,
int depth)
151 assert(!
"diagsplit recursion limit reached");
158 subpatches.push_back(sub);
184 if (split_u && split_v) {
188 if (!split_u && !split_v) {
190 subpatches.push_back(sub);
191 Subpatch &subpatch = subpatches[subpatches.size() - 1];
194 for (
int i = 0; i < 4; i++) {
197 edge.
offset = edge.edge->T;
198 edge.edge->T += edge.T;
213 float2 *Pa, *Pb, *Pc, *Pd;
219 sub_a_across_0 = &sub_a.
edge_u0;
220 sub_a_across_1 = &sub_a.
edge_u1;
221 sub_b_across_0 = &sub_b.
edge_u0;
222 sub_b_across_1 = &sub_b.
edge_u1;
235 sub_a_across_0 = &sub_a.
edge_v0;
236 sub_a_across_1 = &sub_a.
edge_v1;
237 sub_b_across_0 = &sub_b.
edge_v0;
238 sub_b_across_1 = &sub_b.
edge_v1;
253 sub.
patch, &P0, &sub_a_across_0->
T, &sub_b_across_0->
T, *Pd, *Pb, sub_across_0->
T);
255 sub.
patch, &P1, &sub_a_across_1->
T, &sub_b_across_1->
T, *Pc, *Pa, sub_across_1->
T);
264 int tsplit =
T(sub.
patch, P0, P1);
266 if (depth == -2 && tsplit == 1) {
270 sub_a_split->
T = tsplit;
271 sub_b_split->
T = tsplit;
273 resolve_edge_factors(sub_a);
274 resolve_edge_factors(sub_b);
279 sub_a_split->
edge = &edge;
280 sub_b_split->
edge = &edge;
296 split(sub_a, depth + 1);
301 edge.top_offset = sub_across_1->
edge->
T;
302 edge.bottom_offset = sub_across_0->
edge->
T;
305 split(sub_b, depth + 1);
307 assert(edge.T == edge_t);
309 edge.top = sub_across_1->
edge;
314int DiagSplit::alloc_verts(
int n)
316 int a = num_alloced_verts;
317 num_alloced_verts += n;
323 edges.emplace_back();
324 return &edges.back();
334 Patch *patch = (
Patch *)(((
char *)patches) + patch_index * patches_byte_stride);
336 if (face.is_quad()) {
342 patch_index += face.num_corners;
348 params.
mesh->vert_to_stitching_key_map.clear();
349 params.
mesh->vert_stitching_map.clear();
362 int a = mesh->get_subd_face_corners()[face.start_corner +
mod(corner + 0, face.num_corners)];
363 int b = mesh->get_subd_face_corners()[face.start_corner +
mod(corner + 1, face.num_corners)];
372 Edge *edge = split->alloc_edge();
375 edge->stitch_start_vert_index = a;
376 edge->stitch_end_vert_index =
b;
378 edge->start_vert_index = v0;
379 edge->end_vert_index = v1;
381 edge->stitch_edge_key = {a,
b};
390 int v = alloc_verts(4);
392 bool v0_reversed, u1_reversed, v1_reversed, u0_reversed;
394 this, params.
mesh, face, 3, v0_reversed,
v + 3,
v + 0);
396 this, params.
mesh, face, 2, u1_reversed,
v + 2,
v + 3);
398 this, params.
mesh, face, 1, v1_reversed,
v + 1,
v + 2);
400 this, params.
mesh, face, 0, u0_reversed,
v + 0,
v + 1);
431 Edge *edge = split->alloc_edge();
433 int a = mesh->get_subd_face_corners()[face.start_corner +
mod(corner + 0, face.num_corners)];
434 int b = mesh->get_subd_face_corners()[face.start_corner +
mod(corner + 1, face.num_corners)];
437 edge->stitch_edge_key = {
b, a};
440 edge->stitch_edge_key = {a,
b};
457 edge->is_stitch_edge =
true;
458 edge->stitch_start_vert_index = a;
459 edge->stitch_end_vert_index =
b;
461 edge->start_vert_index = v0;
462 edge->end_vert_index = v1;
469 Edge *prev_edge_u0 =
nullptr;
470 Edge *first_edge_v0 =
nullptr;
472 for (
int corner = 0; corner < face.num_corners; corner++) {
473 Patch *patch = (
Patch *)(((
char *)patches) + corner * patches_byte_stride);
477 int v = alloc_verts(4);
504 bool v0_reversed, u0_reversed;
546 resolve_edge_factors(subpatch);
574 if (corner == face.num_corners - 1) {
593 int num_stitch_verts = 0;
597 foreach (
Edge &edge, edges) {
598 if (edge.second_vert_index < 0) {
602 if (edge.is_stitch_edge) {
603 num_stitch_verts =
max(num_stitch_verts,
604 max(edge.stitch_start_vert_index, edge.stitch_end_vert_index));
608 num_stitch_verts += 1;
612 size_t operator()(
const pair<int, int> &k)
const
617 typedef unordered_map<pair<int, int>,
int, pair_hasher> edge_stitch_verts_map_t;
618 edge_stitch_verts_map_t edge_stitch_verts_map;
620 foreach (
Edge &edge, edges) {
621 if (edge.is_stitch_edge) {
622 if (edge.stitch_edge_T == 0) {
623 edge.stitch_edge_T = edge.T;
626 if (edge_stitch_verts_map.find(edge.stitch_edge_key) == edge_stitch_verts_map.end()) {
627 edge_stitch_verts_map[edge.stitch_edge_key] = num_stitch_verts;
628 num_stitch_verts += edge.stitch_edge_T - 1;
634 foreach (
Edge &edge, edges) {
635 if (edge.start_vert_index < 0) {
637 if (edge.top_indices_decrease) {
638 edge.top_offset = edge.top->T - edge.top_offset;
641 edge.start_vert_index = edge.top->get_vert_along_edge(edge.top_offset);
644 if (edge.end_vert_index < 0) {
645 if (edge.bottom_indices_decrease) {
646 edge.bottom_offset = edge.bottom->T - edge.bottom_offset;
649 edge.end_vert_index = edge.bottom->get_vert_along_edge(edge.bottom_offset);
653 int vert_offset = params.
mesh->verts.size();
656 foreach (
const Edge &edge, edges) {
657 if (edge.is_stitch_edge) {
658 int second_stitch_vert_index = edge_stitch_verts_map[edge.stitch_edge_key];
660 for (
int i = 0; i <= edge.T; i++) {
665 key = edge.stitch_start_vert_index;
667 else if (i == edge.T) {
668 key = edge.stitch_end_vert_index;
671 key = second_stitch_vert_index + i - 1 + edge.stitch_offset;
676 key = second_stitch_vert_index - 1 + edge.stitch_offset;
678 else if (i == edge.T) {
679 key = second_stitch_vert_index - 1 + edge.T;
682 else if (key < 0 && edge.top) {
683 int s = edge_stitch_verts_map[edge.top->stitch_edge_key];
684 if (edge.stitch_top_offset >= 0) {
685 key = s - 1 + edge.stitch_top_offset;
688 key = s - 1 + edge.top->stitch_edge_T + edge.stitch_top_offset;
693 int vert = edge.get_vert_along_edge(i) + vert_offset;
696 if (params.
mesh->vert_to_stitching_key_map.find(vert) ==
697 params.
mesh->vert_to_stitching_key_map.end())
699 params.
mesh->vert_to_stitching_key_map[vert] = key;
700 params.
mesh->vert_stitching_map.insert({key, vert});
709 int num_verts = num_alloced_verts;
710 int num_triangles = 0;
712 for (
size_t i = 0; i < subpatches.size(); i++) {
713 subpatches[i].inner_grid_vert_offset = num_verts;
714 num_verts += subpatches[i].calc_num_inner_verts();
715 num_triangles += subpatches[i].calc_num_triangles();
718 dice.
reserve(num_verts, num_triangles);
720 for (
size_t i = 0; i < subpatches.size(); i++) {
ATTR_WARN_UNUSED_RESULT const BMVert * v
void split_patches(Patch *patches, size_t patches_byte_stride)
void split_quad(const Mesh::SubdFace &face, Patch *patch)
void split_ngon(const Mesh::SubdFace &face, Patch *patches, size_t patches_byte_stride)
DiagSplit(const SubdParams ¶ms)
void reserve(int num_verts, int num_triangles)
virtual void eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v)=0
local_group_size(16, 16) .push_constant(Type b
#define CCL_NAMESPACE_END
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
ccl_device_inline uint hash_uint2(uint kx, uint ky)
ccl_device_inline float2 interp(const float2 a, const float2 b, float t)
float world_to_raster_size(float3 P)
int stitch_end_vert_index
pair< int, int > stitch_edge_key
int stitch_start_vert_index
size_t get_num_subd_faces() const
SubdFace get_subd_face(size_t index) const
bool sub_edges_created_in_reverse_order
bool indices_decrease_along_edge
#define DSPLIT_NON_UNIFORM
#define STITCH_NGON_CENTER_VERT_INDEX_OFFSET
static Edge * create_edge_from_corner(DiagSplit *split, const Mesh *mesh, const Mesh::SubdFace &face, int corner, bool &reversed, int v0, int v1)
static Edge * create_split_edge_from_corner(DiagSplit *split, const Mesh *mesh, const Mesh::SubdFace &face, int corner, int side, bool &reversed, int v0, int v1, int vc)
static void order_float2(float2 &a, float2 &b)
#define STITCH_NGON_SPLIT_EDGE_CENTER_VERT_TAG
ccl_device_inline int mod(int x, int m)