Blender V5.3
gpu_shader_material_tex_image.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11
12[[node]]
14{
15 vout = vin * 2.0f - 1.0f;
16}
17
18[[node]]
20{
21 float2 half_texel_size = 0.5f / float2(textureSize(ima, 0).xy);
22 vout = clamp(vin, half_texel_size.xyy(), 1.0f - half_texel_size.xyy);
23}
24
25[[node]]
27{
28 float len = length(vin);
29 float v, u;
30 if (len > 0.0f) {
31 if (vin.x == 0.0f && vin.y == 0.0f) {
32 u = 0.0f;
33 }
34 else {
35 u = (1.0f - atan(vin.x, vin.y) / M_PI) / 2.0f;
36 }
37
38 v = 1.0f - acos(vin.z / len) / M_PI;
39 }
40 else {
41 v = u = 0.0f;
42 }
43
44 vout = float3(u, v, 0.0f);
45}
46
47[[node]]
49{
50 float u, v;
51 v = (vin.z + 1.0f) * 0.5f;
52 float len = sqrt(vin.x * vin.x + vin.y * vin[1]);
53 if (len > 0.0f) {
54 u = (1.0f - (atan(vin.x / len, vin.y / len) / M_PI)) * 0.5f;
55 }
56 else {
57 v = u = 0.0f;
58 }
59
60 vout = float3(u, v, 0.0f);
61}
62
63[[node]]
65 float3 co, sampler2D ima, [[resource_table]] KernelGlobals &kg, float4 &color, float &alpha)
66{
67#if defined(GPU_FRAGMENT_SHADER) || defined(GLSL_CPP_STUBS)
68 float2 dx = gpu_dfdx(co.xy) * texture_lod_bias_get(kg);
69 float2 dy = gpu_dfdy(co.xy) * texture_lod_bias_get(kg);
70
71 color = textureGrad(ima, co.xy, dx, dy);
72#else
73 color = texture(ima, co.xy);
74#endif
75
76 alpha = color.a;
77}
78
79[[node]]
81 sampler2D ima,
82 [[resource_table]] KernelGlobals & /*kg*/,
83 float4 &color,
84 float &alpha)
85{
86 color = texture_bicubic(ima, co.xy);
87 alpha = color.a;
88}
89
90[[node]]
92 float3 N,
93 sampler2D ima,
94 [[resource_table]] KernelGlobals & /*kg*/,
95 float4 &color1,
96 float4 &color2,
97 float4 &color3)
98{
99 /* X projection */
100 float2 uv = texco.yz;
101 if (N.x < 0.0f) {
102 uv.x = 1.0f - uv.x;
103 }
104 color1 = texture(ima, uv);
105 /* Y projection */
106 uv = texco.xz;
107 if (N.y > 0.0f) {
108 uv.x = 1.0f - uv.x;
109 }
110 color2 = texture(ima, uv);
111 /* Z projection */
112 uv = texco.yx;
113 if (N.z > 0.0f) {
114 uv.x = 1.0f - uv.x;
115 }
116 color3 = texture(ima, uv);
117}
118
119[[node]]
121 float3 N,
122 sampler2D ima,
124 float4 &color1,
125 float4 &color2,
126 float4 &color3)
127{
128 float alpha;
129 /* X projection */
130 float2 uv = texco.yz;
131 if (N.x < 0.0f) {
132 uv.x = 1.0f - uv.x;
133 }
134 node_tex_image_cubic(uv.xyy, ima, kg, color1, alpha);
135 /* Y projection */
136 uv = texco.xz;
137 if (N.y > 0.0f) {
138 uv.x = 1.0f - uv.x;
139 }
140 node_tex_image_cubic(uv.xyy, ima, kg, color2, alpha);
141 /* Z projection */
142 uv = texco.yx;
143 if (N.z > 0.0f) {
144 uv.x = 1.0f - uv.x;
145 }
146 node_tex_image_cubic(uv.xyy, ima, kg, color3, alpha);
147}
148
149[[node]]
151 float4 color1,
152 float4 color2,
153 float4 color3,
154 float blend,
155 float4 &color,
156 float &alpha)
157{
158 /* project from direction vector to barycentric coordinates in triangles */
159 N = abs(N);
160 N /= dot(N, float3(1.0f));
161
162 /* basic idea is to think of this as a triangle, each corner representing
163 * one of the 3 faces of the cube. in the corners we have single textures,
164 * in between we blend between two textures, and in the middle we a blend
165 * between three textures.
166 *
167 * the `Nxyz` values are the barycentric coordinates in an equilateral
168 * triangle, which in case of blending, in the middle has a smaller
169 * equilateral triangle where 3 textures blend. this divides things into
170 * 7 zones, with an if () test for each zone
171 * EDIT: Now there is only 4 if's. */
172
173 float limit = 0.5f + 0.5f * blend;
174
175 float3 weight;
176 weight = N.xyz / (N.xyx + N.yzz);
177 weight = clamp((weight - 0.5f * (1.0f - blend)) / max(1e-8f, blend), 0.0f, 1.0f);
178
179 /* test for mixes between two textures */
180 if (N.z < (1.0f - limit) * (N.y + N.x)) {
181 weight.z = 0.0f;
182 weight.y = 1.0f - weight.x;
183 }
184 else if (N.x < (1.0f - limit) * (N.y + N.z)) {
185 weight.x = 0.0f;
186 weight.z = 1.0f - weight.y;
187 }
188 else if (N.y < (1.0f - limit) * (N.x + N.z)) {
189 weight.y = 0.0f;
190 weight.x = 1.0f - weight.z;
191 }
192 else {
193 /* last case, we have a mix between three */
194 weight = ((2.0f - limit) * N + (limit - 1.0f)) / max(1e-8f, blend);
195 }
196
197 color = weight.x * color1 + weight.y * color2 + weight.z * color3;
198 alpha = color.a;
199}
200
201[[node]]
202void node_tex_image_empty(float3 /*co*/, float4 &color, float &alpha)
203{
204 color = float4(0.0f);
205 alpha = 1.0f;
206}
207
208[[node]]
210 float3 co, sampler2DArray ima, sampler1DArray map, float4 &color, float &alpha)
211{
212 if (tiled_image_lookup(co, ima, map)) {
213 color = texture(ima, co);
214 }
215 else {
216 color = float4(1.0f, 0.0f, 1.0f, 1.0f);
217 }
218
219 alpha = color.a;
220}
221
222[[node]]
224 float3 co, sampler2DArray ima, sampler1DArray map, float4 &color, float &alpha)
225{
226 if (tiled_image_lookup(co, ima, map)) {
227 float2 tex_size = float2(textureSize(ima, 0).xy);
228
229 co.xy *= tex_size;
230 /* texel center */
231 float2 tc = floor(co.xy - 0.5f) + 0.5f;
232 float2 w0, w1, w2, w3;
233 cubic_bspline_coefficients(co.xy - tc, w0, w1, w2, w3);
234
235 float2 s0 = w0 + w1;
236 float2 s1 = w2 + w3;
237
238 float2 f0 = w1 / (w0 + w1);
239 float2 f1 = w3 / (w2 + w3);
240
241 float4 final_co;
242 final_co.xy = tc - 1.0f + f0;
243 final_co.zw = tc + 1.0f + f1;
244 final_co /= tex_size.xyxy;
245
246 color = textureLod(ima, float3(final_co.xy, co.z), 0.0f) * s0.x * s0.y;
247 color += textureLod(ima, float3(final_co.zy, co.z), 0.0f) * s1.x * s0.y;
248 color += textureLod(ima, float3(final_co.xw, co.z), 0.0f) * s0.x * s1.y;
249 color += textureLod(ima, float3(final_co.zw, co.z), 0.0f) * s1.x * s1.y;
250 }
251 else {
252 color = float4(1.0f, 0.0f, 1.0f, 1.0f);
253 }
254
255 alpha = color.a;
256}
#define M_PI
btScalar max
float texture_lod_bias_get(KernelGlobals &kg)
void cubic_bspline_coefficients(float2 f, float2 &w0, float2 &w1, float2 &w2, float2 &w3)
float4 texture_bicubic(sampler2D sampler_2d, float2 coordinates)
#define resource_table
#define atan
T gpu_dfdx(T) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
#define sqrt
T gpu_dfdy(T) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
#define acos
SamplerBase< float, 1, false, true > sampler1DArray
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
TEX_TEMPLATE DataVec textureGrad(T, FltCoord, DerivVec, DerivVec) RET
SamplerBase< float, 2, false, true > sampler2DArray
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
void node_tex_image_linear(float3 co, sampler2D ima, KernelGlobals &kg, float4 &color, float &alpha)
void point_map_to_tube(float3 vin, float3 &vout)
void tex_box_sample_linear(float3 texco, float3 N, sampler2D ima, KernelGlobals &, float4 &color1, float4 &color2, float4 &color3)
void tex_box_blend(float3 N, float4 color1, float4 color2, float4 color3, float blend, float4 &color, float &alpha)
void point_texco_remap_square(float3 vin, float3 &vout)
void node_tex_image_empty(float3, float4 &color, float &alpha)
void node_tex_tile_cubic(float3 co, sampler2DArray ima, sampler1DArray map, float4 &color, float &alpha)
void tex_box_sample_cubic(float3 texco, float3 N, sampler2D ima, KernelGlobals &kg, float4 &color1, float4 &color2, float4 &color3)
void point_map_to_sphere(float3 vin, float3 &vout)
void point_texco_clamp(float3 vin, sampler2D ima, float3 &vout)
void node_tex_image_cubic(float3 co, sampler2D ima, KernelGlobals &, float4 &color, float &alpha)
void node_tex_tile_linear(float3 co, sampler2DArray ima, sampler1DArray map, float4 &color, float &alpha)
bool tiled_image_lookup(float3 &co, sampler2DArray, sampler1DArray map)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define N
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136