Blender  V3.3
wireframe.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Adapted from Open Shading Language
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011-2022 Blender Foundation. */
8 
9 #pragma once
10 
12 
13 /* Wireframe Node */
14 
17  float size,
18  int pixel_size,
20 {
21 #if defined(__HAIR__) || defined(__POINTCLOUD__)
22  if (sd->prim != PRIM_NONE && sd->type & PRIMITIVE_TRIANGLE)
23 #else
24  if (sd->prim != PRIM_NONE)
25 #endif
26  {
27  float3 Co[3];
28  float pixelwidth = 1.0f;
29 
30  /* Triangles */
31  int np = 3;
32 
33  if (sd->type & PRIMITIVE_MOTION) {
34  motion_triangle_vertices(kg, sd->object, sd->prim, sd->time, Co);
35  }
36  else {
37  triangle_vertices(kg, sd->prim, Co);
38  }
39 
40  if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
41  object_position_transform(kg, sd, &Co[0]);
42  object_position_transform(kg, sd, &Co[1]);
43  object_position_transform(kg, sd, &Co[2]);
44  }
45 
46  if (pixel_size) {
47  // Project the derivatives of P to the viewing plane defined
48  // by I so we have a measure of how big is a pixel at this point
49  float pixelwidth_x = len(sd->dP.dx - dot(sd->dP.dx, sd->I) * sd->I);
50  float pixelwidth_y = len(sd->dP.dy - dot(sd->dP.dy, sd->I) * sd->I);
51  // Take the average of both axis' length
52  pixelwidth = (pixelwidth_x + pixelwidth_y) * 0.5f;
53  }
54 
55  // Use half the width as the neighbor face will render the
56  // other half. And take the square for fast comparison
57  pixelwidth *= 0.5f * size;
58  pixelwidth *= pixelwidth;
59  for (int i = 0; i < np; i++) {
60  int i2 = i ? i - 1 : np - 1;
61  float3 dir = *P - Co[i];
62  float3 edge = Co[i] - Co[i2];
63  float3 crs = cross(edge, dir);
64  // At this point dot(crs, crs) / dot(edge, edge) is
65  // the square of area / length(edge) == square of the
66  // distance to the edge.
67  if (dot(crs, crs) < (dot(edge, edge) * pixelwidth))
68  return 1.0f;
69  }
70  }
71  return 0.0f;
72 }
73 
76  ccl_private float *stack,
77  uint4 node)
78 {
79  uint in_size = node.y;
80  uint out_fac = node.z;
81  uint use_pixel_size, bump_offset;
82  svm_unpack_node_uchar2(node.w, &use_pixel_size, &bump_offset);
83 
84  /* Input Data */
85  float size = stack_load_float(stack, in_size);
86  int pixel_size = (int)use_pixel_size;
87 
88  /* Calculate wireframe */
89  float f = wireframe(kg, sd, size, pixel_size, &sd->P);
90 
91  /* TODO(sergey): Think of faster way to calculate derivatives. */
92  if (bump_offset == NODE_BUMP_OFFSET_DX) {
93  float3 Px = sd->P - sd->dP.dx;
94  f += (f - wireframe(kg, sd, size, pixel_size, &Px)) / len(sd->dP.dx);
95  }
96  else if (bump_offset == NODE_BUMP_OFFSET_DY) {
97  float3 Py = sd->P - sd->dP.dy;
98  f += (f - wireframe(kg, sd, size, pixel_size, &Py)) / len(sd->dP.dy);
99  }
100 
101  if (stack_valid(out_fac))
102  stack_store_float(stack, out_fac, f);
103 }
104 
unsigned int uint
Definition: BLI_sys_types.h:67
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define ccl_device_noinline
Definition: cuda/compat.h:40
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
OperationNode * node
const KernelGlobalsCPU *ccl_restrict KernelGlobals
int len
Definition: draw_manager.c:108
ccl_device_inline void object_position_transform(KernelGlobals kg, ccl_private const ShaderData *sd, ccl_private float3 *P)
ccl_device_forceinline void svm_unpack_node_uchar2(uint i, ccl_private uint *x, ccl_private uint *y)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_inline float stack_load_float(ccl_private float *stack, uint a)
ccl_device_inline bool stack_valid(uint a)
@ NODE_BUMP_OFFSET_DY
@ NODE_BUMP_OFFSET_DX
@ PRIMITIVE_MOTION
Definition: kernel/types.h:558
@ PRIMITIVE_TRIANGLE
Definition: kernel/types.h:551
#define PRIM_NONE
Definition: kernel/types.h:41
ShaderData
Definition: kernel/types.h:925
@ SD_OBJECT_TRANSFORM_APPLIED
Definition: kernel/types.h:808
static float P(float k)
Definition: math_interp.c:25
ccl_device_inline void motion_triangle_vertices(KernelGlobals kg, int object, int prim, float time, float3 verts[3])
T dot(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
vec_base< T, 3 > cross(const vec_base< T, 3 > &a, const vec_base< T, 3 > &b)
ccl_device_inline void triangle_vertices(KernelGlobals kg, int prim, float3 P[3])
Definition: triangle.h:65
ccl_device_noinline void svm_node_wireframe(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node)
Definition: wireframe.h:74
CCL_NAMESPACE_BEGIN ccl_device_inline float wireframe(KernelGlobals kg, ccl_private ShaderData *sd, float size, int pixel_size, ccl_private float3 *P)
Definition: wireframe.h:15