blender/intern/cycles/kernel/svm/mapping_util.h
Weizhen Huang f2394a431e Cycles: Support automatic differentiation of shader nodes in SVM
This is an internal change preparing for the texture cache. Only implemented
for surfaces, and currently supports the following nodes:

- Geometry
- Tangent
- Mapping
- Attribute
- Texture Coordinate
- Environment Texture
- Image Texture
- Vector math
- UV Map
- Combine/Separate XYZ
- Bump

This has some impact on GPU rendering performance. Various changes were
made to optimize this, but rendering can still be a few % slower on some
GPUs. Some of the optimizations done:

* Use different node types enum for derivative nodes, consecutive to ensure the
  jump table works.
* Template various SVM derivative nodes to separate them from the
  non-derivative case, and avoid using dual types in those implementations.
* Use template on return type for stack_store and stack_load to make the above
  easier to implement.
* Use template on return type of primitive attribute reading to make derivative
  and non-derivative variations.
* Unify derivative and bump dx/dy nodes. Now it's a single derivative node
  that handles both cases.
* Derivative nodes are disabled in volume shaders for now.
* Tweak inlining on a few functions.

Co-authored-by: Brecht Van Lommel <brecht@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/155706
2026-03-16 18:20:54 +01:00

38 lines
1.2 KiB
C++

/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "kernel/svm/types.h"
#include "util/math.h"
#include "util/transform.h"
#include "util/types.h"
CCL_NAMESPACE_BEGIN
template<class Float3Type>
ccl_device Float3Type svm_mapping(NodeMappingType type,
const Float3Type vector,
const float3 location,
const float3 rotation,
const float3 scale)
{
const Transform rotationTransform = euler_to_transform(rotation);
switch (type) {
case NODE_MAPPING_TYPE_POINT:
return transform_direction(&rotationTransform, (vector * scale)) + location;
case NODE_MAPPING_TYPE_TEXTURE:
return safe_divide(transform_direction_transposed(&rotationTransform, (vector - location)),
scale);
case NODE_MAPPING_TYPE_VECTOR:
return transform_direction(&rotationTransform, (vector * scale));
case NODE_MAPPING_TYPE_NORMAL:
return safe_normalize(transform_direction(&rotationTransform, safe_divide(vector, scale)));
default:
return Float3Type(zero_float3());
}
}
CCL_NAMESPACE_END