Blender V5.3
cycles/kernel/svm/image.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8#include "kernel/image.h"
9
11
12#include "kernel/geom/object.h"
13
15#include "kernel/svm/util.h"
16
17#include "util/color.h"
18#include "util/types_image.h"
19
21
23 ccl_private ShaderData *sd,
24 const int id,
25 const dual2 uv,
26 const uint flags,
27 const float4 missing_rgba)
28{
29 float4 r = kernel_image_interp_with_udim(kg, sd, id, uv, missing_rgba);
30 const float alpha = r.w;
31
32 if ((flags & NODE_IMAGE_ALPHA_UNASSOCIATE) && alpha != 1.0f && alpha != 0.0f) {
33 r /= alpha;
34 r.w = alpha;
35 }
36
37 if (flags & NODE_IMAGE_COMPRESS_AS_SRGB) {
39 }
40
41 return r;
42}
43
44/* Remap coordinate from 0..1 box to -1..-1 */
45template<class Float3Type> ccl_device_inline Float3Type texco_remap_square(const Float3Type co)
46{
47 return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
48}
49
50template<class Float3Type>
51ccl_device_inline auto svm_node_tex_image_mapping(const Float3Type co, const uint proj)
52{
53 if (proj == NODE_IMAGE_PROJ_SPHERE) {
55 }
56 if (proj == NODE_IMAGE_PROJ_TUBE) {
58 }
59
60 return make_float2(co);
61}
62
63template<class Float3Type>
65 ccl_private ShaderData *sd,
66 ccl_private float *ccl_restrict stack,
68{
69 const float3 missing = stack_load(stack, node.missing);
70 const float missing_alpha = stack_load(stack, node.missing_alpha);
71 const Float3Type co = stack_load<Float3Type>(stack, node.co);
72 const dual2 tex_co(svm_node_tex_image_mapping(co, node.projection));
73
74 const float4 f = svm_image_texture(
75 kg, sd, node.id, tex_co, node.flags, make_float4(missing, missing_alpha));
76
77 if (stack_valid(node.out_offset)) {
78 stack_store_float3(stack, node.out_offset, make_float3(f));
79 }
80 if (stack_valid(node.alpha_offset)) {
81 stack_store_float(stack, node.alpha_offset, f.w);
82 }
83}
84
85template<class Float3Type>
87 ccl_private ShaderData *sd,
88 ccl_private float *ccl_restrict stack,
90 node)
91{
92 /* get object space normal */
93 float3 N = sd->N;
94
96
97 /* project from direction vector to barycentric coordinates in triangles */
98 const float3 signed_N = N;
99
100 N = fabs(N);
101
102 N /= (N.x + N.y + N.z);
103
104 /* basic idea is to think of this as a triangle, each corner representing
105 * one of the 3 faces of the cube. in the corners we have single textures,
106 * in between we blend between two textures, and in the middle we a blend
107 * between three textures.
108 *
109 * The `Nxyz` values are the barycentric coordinates in an equilateral
110 * triangle, which in case of blending, in the middle has a smaller
111 * equilateral triangle where 3 textures blend. this divides things into
112 * 7 zones, with an `if()` test for each zone. */
113
114 float3 weight = make_float3(0.0f, 0.0f, 0.0f);
115 const float blend = node.blend;
116 const float limit = 0.5f * (1.0f + blend);
117
118 /* first test for corners with single texture */
119 if (N.x > limit * (N.x + N.y) && N.x > limit * (N.x + N.z)) {
120 weight.x = 1.0f;
121 }
122 else if (N.y > limit * (N.x + N.y) && N.y > limit * (N.y + N.z)) {
123 weight.y = 1.0f;
124 }
125 else if (N.z > limit * (N.x + N.z) && N.z > limit * (N.y + N.z)) {
126 weight.z = 1.0f;
127 }
128 else if (blend > 0.0f) {
129 /* in case of blending, test for mixes between two textures */
130 if (N.z < (1.0f - limit) * (N.y + N.x)) {
131 weight.x = N.x / (N.x + N.y);
132 weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
133 weight.y = 1.0f - weight.x;
134 }
135 else if (N.x < (1.0f - limit) * (N.y + N.z)) {
136 weight.y = N.y / (N.y + N.z);
137 weight.y = saturatef((weight.y - 0.5f * (1.0f - blend)) / blend);
138 weight.z = 1.0f - weight.y;
139 }
140 else if (N.y < (1.0f - limit) * (N.x + N.z)) {
141 weight.x = N.x / (N.x + N.z);
142 weight.x = saturatef((weight.x - 0.5f * (1.0f - blend)) / blend);
143 weight.z = 1.0f - weight.x;
144 }
145 else {
146 /* last case, we have a mix between three */
147 weight.x = ((2.0f - limit) * N.x + (limit - 1.0f)) / (2.0f * limit - 1.0f);
148 weight.y = ((2.0f - limit) * N.y + (limit - 1.0f)) / (2.0f * limit - 1.0f);
149 weight.z = ((2.0f - limit) * N.z + (limit - 1.0f)) / (2.0f * limit - 1.0f);
150 }
151 }
152 else {
153 /* Desperate mode, no valid choice anyway, fall back to one side. */
154 weight.x = 1.0f;
155 }
156
157 /* now fetch textures */
158 float4 f = zero_float4();
159
160 const float3 missing = stack_load(stack, node.missing);
161 const float missing_alpha = stack_load(stack, node.missing_alpha);
162 const dual3 co = dual3(stack_load<Float3Type>(stack, node.co));
163
164 /* Map so that no textures are flipped, rotation is somewhat arbitrary. */
165 if (weight.x > 0.0f) {
166 const dual2 uv = make_float2((signed_N.x < 0.0f) ? 1.0f - co.y() : co.y(), co.z());
167 f += weight.x *
168 svm_image_texture(kg, sd, node.id, uv, node.flags, make_float4(missing, missing_alpha));
169 }
170 if (weight.y > 0.0f) {
171 const dual2 uv = make_float2((signed_N.y > 0.0f) ? 1.0f - co.x() : co.x(), co.z());
172 f += weight.y *
173 svm_image_texture(kg, sd, node.id, uv, node.flags, make_float4(missing, missing_alpha));
174 }
175 if (weight.z > 0.0f) {
176 const dual2 uv = make_float2((signed_N.z > 0.0f) ? 1.0f - co.y() : co.y(), co.x());
177 f += weight.z *
178 svm_image_texture(kg, sd, node.id, uv, node.flags, make_float4(missing, missing_alpha));
179 }
180
181 if (stack_valid(node.out_offset)) {
182 stack_store_float3(stack, node.out_offset, make_float3(f.x, f.y, f.z));
183 }
184 if (stack_valid(node.alpha_offset)) {
185 stack_store_float(stack, node.alpha_offset, f.w);
186 }
187}
188
189template<class Float3Type>
190ccl_device_inline auto svm_node_tex_environment_projection(Float3Type co, const uint proj)
191{
192 co = safe_normalize(co);
193 if (proj == 0) {
195 }
196 return direction_to_mirrorball(co);
197}
198
199template<class Float3Type>
201 KernelGlobals kg,
202 ccl_private ShaderData *sd,
203 ccl_private float *ccl_restrict stack,
205{
206 const float3 missing = stack_load(stack, node.missing);
207 const float missing_alpha = stack_load(stack, node.missing_alpha);
208 const Float3Type co = stack_load<Float3Type>(stack, node.co);
209 const dual2 uv(svm_node_tex_environment_projection(co, node.projection));
210
211 const float4 f = svm_image_texture(
212 kg, sd, node.id, uv, node.flags, make_float4(missing, missing_alpha));
213
214 if (stack_valid(node.out_offset)) {
215 stack_store_float3(stack, node.out_offset, make_float3(f.x, f.y, f.z));
216 }
217 if (stack_valid(node.alpha_offset)) {
218 stack_store_float(stack, node.alpha_offset, f.w);
219 }
220}
221
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
ccl_device float2 direction_to_mirrorball(float3 dir)
ccl_device float2 direction_to_equirectangular(const float3 dir)
ccl_device_forceinline float4 kernel_image_interp_with_udim(KernelGlobals kg, ccl_private ShaderData *sd, const int udim_id, dual2 uv, const float4 missing_rgba)
ccl_device_noinline void svm_node_tex_environment(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexEnvironment &ccl_restrict node)
ccl_device_inline Float3Type texco_remap_square(const Float3Type co)
CCL_NAMESPACE_BEGIN ccl_device float4 svm_image_texture(KernelGlobals kg, ccl_private ShaderData *sd, const int id, const dual2 uv, const uint flags, const float4 missing_rgba)
ccl_device_inline auto svm_node_tex_image_mapping(const Float3Type co, const uint proj)
ccl_device_noinline void svm_node_tex_image(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexImage &ccl_restrict node)
ccl_device_inline auto svm_node_tex_environment_projection(Float3Type co, const uint proj)
ccl_device_noinline void svm_node_tex_image_box(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexImageBox &ccl_restrict node)
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
#define ccl_restrict
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
VecT safe_normalize(VecT vector)
ccl_device_inline void object_inverse_normal_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *N)
@ NODE_IMAGE_COMPRESS_AS_SRGB
@ NODE_IMAGE_ALPHA_UNASSOCIATE
@ NODE_IMAGE_PROJ_SPHERE
@ NODE_IMAGE_PROJ_TUBE
ccl_device_inline dual2 map_to_tube(const dual3 co)
Definition math_dual.h:404
ccl_device_inline dual2 map_to_sphere(const dual3 co)
Definition math_dual.h:418
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define N
#define ccl_device
#define make_float2
#define make_float4
ccl_device_inline_method dual< float > x() const
Definition types_dual.h:62
ccl_device_inline_method dual< float > y() const
Definition types_dual.h:66
ccl_device_inline_method dual< float > z() const
Definition types_dual.h:70
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
dual< float3 > dual3
Definition types_dual.h:106
dual< float2 > dual2
Definition types_dual.h:105