Blender V4.3
COM_shader_node.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_string_ref.hh"
8#include "BLI_vector.hh"
9
10#include "DNA_node_types.h"
11
12#include "GPU_material.hh"
13
15
17
18using namespace nodes::derived_node_tree_types;
19
20/* ------------------------------------------------------------------------------------------------
21 * Shader Node
22 *
23 * A shader node encapsulates a compositor node tree that is capable of being used together with
24 * other shader nodes to construct a Shader Operation using the GPU material compiler. A GPU node
25 * stack for each of the node inputs and outputs is stored and populated during construction in
26 * order to represent the node as a GPU node inside the GPU material graph, see GPU_material.hh for
27 * more information. Derived classes should implement the compile method to add the node and link
28 * it to the GPU material given to the method. The compiler is expected to initialize the input
29 * links of the node before invoking the compile method. See the discussion in
30 * COM_shader_operation.hh for more information. */
32 private:
33 /* The node that this operation represents. */
34 DNode node_;
35 /* The GPU node stacks of the inputs of the node. Those are populated during construction in the
36 * populate_inputs method. The links of the inputs are initialized by the GPU material compiler
37 * prior to calling the compile method. There is an extra stack at the end to mark the end of the
38 * array, as this is what the GPU module functions expect. */
40 /* The GPU node stacks of the outputs of the node. Those are populated during construction in the
41 * populate_outputs method. There is an extra stack at the end to mark the end of the array, as
42 * this is what the GPU module functions expect. */
43 Vector<GPUNodeStack> outputs_;
44
45 public:
46 /* Construct the node by populating both its inputs and outputs. */
47 ShaderNode(DNode node);
48
49 virtual ~ShaderNode() = default;
50
51 /* Compile the node by adding the appropriate GPU material graph nodes and linking the
52 * appropriate resources. */
53 virtual void compile(GPUMaterial *material) = 0;
54
55 /* Returns a contiguous array containing the GPU node stacks of each input. */
57
58 /* Returns a contiguous array containing the GPU node stacks of each output. */
60
61 /* Returns the GPU node stack of the input with the given identifier. */
62 GPUNodeStack &get_input(StringRef identifier);
63
64 /* Returns the GPU node stack of the output with the given identifier. */
66
67 /* Returns the GPU node link of the input with the given identifier, if the input is not linked,
68 * a uniform link carrying the value of the input will be created a returned. It is expected that
69 * the caller will use the returned link in a GPU material, otherwise, the link may not be
70 * properly freed. */
72
73 protected:
74 /* Returns a reference to the derived node that this operation represents. */
75 const DNode &node() const;
76
77 /* Returns a reference to the node this operations represents. */
78 const bNode &bnode() const;
79
80 private:
81 /* Populate the inputs of the node. The input link is set to nullptr and is expected to be
82 * initialized by the GPU material compiler before calling the compile method. */
83 void populate_inputs();
84 /* Populate the outputs of the node. The output link is set to nullptr and is expected to be
85 * initialized by the compile method. */
86 void populate_outputs();
87};
88
89} // namespace blender::realtime_compositor
virtual void compile(GPUMaterial *material)=0
GPUNodeLink * get_input_link(StringRef identifier)
GPUNodeStack & get_input(StringRef identifier)
GPUNodeStack & get_output(StringRef identifier)