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
33 CCL_NAMESPACE_BEGIN
34
35 /* Wireframe Node */
36
wireframe(KernelGlobals * kg,ShaderData * sd,float size,int pixel_size,float3 * P)37 ccl_device_inline float wireframe(
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
svm_node_wireframe(KernelGlobals * kg,ShaderData * sd,float * stack,uint4 node)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
130 CCL_NAMESPACE_END
131