Blender  V2.93
svm_wireframe.h
Go to the documentation of this file.
1 /*
2  * Adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2013, Blender Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of Sony Pictures Imageworks nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
34 
35 /* Wireframe Node */
36 
38  KernelGlobals *kg, ShaderData *sd, float size, int pixel_size, float3 *P)
39 {
40 #ifdef __HAIR__
41  if (sd->prim != PRIM_NONE && sd->type & PRIMITIVE_ALL_TRIANGLE)
42 #else
43  if (sd->prim != PRIM_NONE)
44 #endif
45  {
46  float3 Co[3];
47  float pixelwidth = 1.0f;
48 
49  /* Triangles */
50  int np = 3;
51 
52  if (sd->type & PRIMITIVE_TRIANGLE)
53  triangle_vertices(kg, sd->prim, Co);
54  else
55  motion_triangle_vertices(kg, sd->object, sd->prim, sd->time, Co);
56 
57  if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
58  object_position_transform(kg, sd, &Co[0]);
59  object_position_transform(kg, sd, &Co[1]);
60  object_position_transform(kg, sd, &Co[2]);
61  }
62 
63  if (pixel_size) {
64  // Project the derivatives of P to the viewing plane defined
65  // by I so we have a measure of how big is a pixel at this point
66  float pixelwidth_x = len(sd->dP.dx - dot(sd->dP.dx, sd->I) * sd->I);
67  float pixelwidth_y = len(sd->dP.dy - dot(sd->dP.dy, sd->I) * sd->I);
68  // Take the average of both axis' length
69  pixelwidth = (pixelwidth_x + pixelwidth_y) * 0.5f;
70  }
71 
72  // Use half the width as the neighbor face will render the
73  // other half. And take the square for fast comparison
74  pixelwidth *= 0.5f * size;
75  pixelwidth *= pixelwidth;
76  for (int i = 0; i < np; i++) {
77  int i2 = i ? i - 1 : np - 1;
78  float3 dir = *P - Co[i];
79  float3 edge = Co[i] - Co[i2];
80  float3 crs = cross(edge, dir);
81  // At this point dot(crs, crs) / dot(edge, edge) is
82  // the square of area / length(edge) == square of the
83  // distance to the edge.
84  if (dot(crs, crs) < (dot(edge, edge) * pixelwidth))
85  return 1.0f;
86  }
87  }
88  return 0.0f;
89 }
90 
91 ccl_device void svm_node_wireframe(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
92 {
93  uint in_size = node.y;
94  uint out_fac = node.z;
95  uint use_pixel_size, bump_offset;
96  svm_unpack_node_uchar2(node.w, &use_pixel_size, &bump_offset);
97 
98  /* Input Data */
99  float size = stack_load_float(stack, in_size);
100  int pixel_size = (int)use_pixel_size;
101 
102  /* Calculate wireframe */
103 #ifdef __SPLIT_KERNEL__
104  /* TODO(sergey): This is because sd is actually a global space,
105  * which makes it difficult to re-use same wireframe() function.
106  *
107  * With OpenCL 2.0 it's possible to avoid this change, but for until
108  * then we'll be living with such an exception.
109  */
110  float3 P = sd->P;
111  float f = wireframe(kg, sd, size, pixel_size, &P);
112 #else
113  float f = wireframe(kg, sd, size, pixel_size, &sd->P);
114 #endif
115 
116  /* TODO(sergey): Think of faster way to calculate derivatives. */
117  if (bump_offset == NODE_BUMP_OFFSET_DX) {
118  float3 Px = sd->P - sd->dP.dx;
119  f += (f - wireframe(kg, sd, size, pixel_size, &Px)) / len(sd->dP.dx);
120  }
121  else if (bump_offset == NODE_BUMP_OFFSET_DY) {
122  float3 Py = sd->P - sd->dP.dy;
123  f += (f - wireframe(kg, sd, size, pixel_size, &Py)) / len(sd->dP.dy);
124  }
125 
126  if (stack_valid(out_fac))
127  stack_store_float(stack, out_fac, f);
128 }
129 
unsigned int uint
Definition: BLI_sys_types.h:83
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
OperationNode * node
ccl_device_inline void motion_triangle_vertices(KernelGlobals *kg, int object, int prim, float time, float3 verts[3])
ccl_device_inline void object_position_transform(KernelGlobals *kg, const ShaderData *sd, float3 *P)
Definition: geom_object.h:116
ccl_device_inline void triangle_vertices(KernelGlobals *kg, int prim, float3 P[3])
Definition: geom_triangle.h:70
ccl_device_inline float stack_load_float(float *stack, uint a)
ccl_device_inline void stack_store_float(float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar2(uint i, uint *x, uint *y)
ccl_device_inline bool stack_valid(uint a)
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
@ PRIMITIVE_ALL_TRIANGLE
Definition: kernel_types.h:697
@ PRIMITIVE_TRIANGLE
Definition: kernel_types.h:686
#define PRIM_NONE
Definition: kernel_types.h:60
ShaderData
@ SD_OBJECT_TRANSFORM_APPLIED
Definition: kernel_types.h:908
static float P(float k)
Definition: math_interp.c:41
@ NODE_BUMP_OFFSET_DY
Definition: svm_types.h:497
@ NODE_BUMP_OFFSET_DX
Definition: svm_types.h:496
ccl_device void svm_node_wireframe(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
Definition: svm_wireframe.h:91
CCL_NAMESPACE_BEGIN ccl_device_inline float wireframe(KernelGlobals *kg, ShaderData *sd, float size, int pixel_size, float3 *P)
Definition: svm_wireframe.h:37
__forceinline avxf cross(const avxf &a, const avxf &b)
Definition: util_avxf.h:119
ccl_device_inline float dot(const float2 &a, const float2 &b)
uint len