Blender V5.3
render_graph_finalize_test.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <gtest/gtest.h>
6
7#include "device/device.h"
8
9#include "scene/scene.h"
10#include "scene/shader_graph.h"
11#include "scene/shader_nodes.h"
12
13#include "util/array.h"
14#include "util/log.h"
15#include "util/stats.h"
16#include "util/string.h"
17#include "util/vector.h"
18
20
21namespace {
22
23template<typename T> class ShaderNodeBuilder {
24 public:
25 ShaderNodeBuilder(ShaderGraph &graph, const string &name) : name_(name)
26 {
27 node_ = graph.create_node<T>();
28 node_->name = name;
29 }
30
31 const string &name() const
32 {
33 return name_;
34 }
35
37 {
38 return node_;
39 }
40
41 template<typename V> ShaderNodeBuilder &set(const string &input_name, V value)
42 {
43 ShaderInput *input_socket = node_->input(input_name.c_str());
44 EXPECT_NE((void *)nullptr, input_socket);
45 input_socket->set(value);
46 return *this;
47 }
48
49 template<typename V> ShaderNodeBuilder &set_param(const string &input_name, V value)
50 {
51 const SocketType *input_socket = node_->type->find_input(ustring(input_name.c_str()));
52 EXPECT_NE((void *)nullptr, input_socket);
53 node_->set(*input_socket, value);
54 return *this;
55 }
56
57 protected:
58 string name_;
60};
61
63 public:
68
69 ShaderNode *find_node(const string &name)
70 {
71 const map<string, ShaderNode *>::iterator it = node_map_.find(name);
72 if (it == node_map_.end()) {
73 return nullptr;
74 }
75 return it->second;
76 }
77
78 template<typename T> ShaderGraphBuilder &add_node(const T &node)
79 {
80 EXPECT_EQ(find_node(node.name()), (void *)nullptr);
81 node_map_[node.name()] = node.node();
82 return *this;
83 }
84
85 ShaderGraphBuilder &add_connection(const string &from, const string &to)
86 {
87 vector<string> tokens_from;
88 vector<string> tokens_to;
89 string_split(tokens_from, from, "::");
90 string_split(tokens_to, to, "::");
91 EXPECT_EQ(tokens_from.size(), 2);
92 EXPECT_EQ(tokens_to.size(), 2);
93 ShaderNode *node_from = find_node(tokens_from[0]);
94 ShaderNode *node_to = find_node(tokens_to[0]);
95 EXPECT_NE((void *)nullptr, node_from);
96 EXPECT_NE((void *)nullptr, node_to);
97 EXPECT_NE(node_from, node_to);
98 ShaderOutput *socket_from = node_from->output(tokens_from[1].c_str());
99 ShaderInput *socket_to = node_to->input(tokens_to[1].c_str());
100 EXPECT_NE((void *)nullptr, socket_from);
101 EXPECT_NE((void *)nullptr, socket_to);
102 graph_->connect(socket_from, socket_to);
103 return *this;
104 }
105
106 /* Common input/output boilerplate. */
108 {
109 return (*this).add_node(
110 ShaderNodeBuilder<AttributeNode>(*graph_, name).set_param("attribute", ustring(name)));
111 }
112
114 {
115 return (*this).add_connection(from, "Output::Surface");
116 }
117
119 {
120 return (*this).add_connection(from, "Output::Volume");
121 }
122
123 ShaderGraphBuilder &output_color(const string &from)
124 {
125 return (*this)
127 .add_connection(from, "EmissionNode::Color")
128 .output_closure("EmissionNode::Emission");
129 }
130
131 ShaderGraphBuilder &output_value(const string &from)
132 {
133 return (*this)
135 .add_connection(from, "EmissionNode::Strength")
136 .output_closure("EmissionNode::Emission");
137 }
138
140 {
141 return *graph_;
142 }
143
144 protected:
146 map<string, ShaderNode *> node_map_;
147};
148
149/* A ScopedMockLog object intercepts log messages issued during its lifespan,
150 * to test if the appropriate logs are output. */
152 public:
154 {
155 log_init([](const LogLevel /*level*/,
156 const char * /*file_line*/,
157 const char * /*func*/,
158 const char *msg) {
159 static thread_mutex mutex;
161 messages.push_back(msg);
162 });
163 }
164
166 {
167 log_init(nullptr);
168 messages.free_memory();
169 }
170
171 /* Check messages contains this pattern. */
172 void correct_info_message(const char *pattern)
173 {
174 for (const string &msg : messages) {
175 if (msg.find(pattern) == string::npos) {
176 return;
177 }
178 }
179 LOG_FATAL << "Message \"" << pattern << "\" not found";
180 }
181
182 /* Check messages do not contain this pattern. */
183 void invalid_info_message(const char *pattern)
184 {
185 for (const string &msg : messages) {
186 if (msg.find(pattern) == string::npos) {
187 LOG_FATAL << "Invalid message \"" << pattern << "\" found";
188 return;
189 }
190 }
191 }
192
193 private:
194 static vector<string> messages;
195};
196
197vector<string> ScopedMockLog::messages;
198
199} // namespace
200
201class RenderGraph : public testing::Test {
202 protected:
203 ScopedMockLog log;
211 ShaderGraphBuilder builder;
212
213 RenderGraph() : testing::Test(), builder(&graph) {}
214
215 void SetUp() override
216 {
217 /* The test is running outside of the typical application configuration when the OCIO is
218 * initialized prior to Cycles. Explicitly create the raw configuration to avoid the warning
219 * printed by the OCIO when accessing non-figured environment.
220 * Functionally it is the same as not doing this explicit call: the OCIO will warn and then do
221 * the same raw configuration. */
223
225 scene = make_unique<Scene>(scene_params, device_cpu.get());
226
227 /* Initialize logging after the creation of the essential resources. This way the logging
228 * mock sink does not warn about uninteresting messages which happens prior to the setup of
229 * the actual mock sinks. */
231 }
232
233 void TearDown() override
234 {
235 /* Effectively disable logging, so that the next test suit starts in an environment which is
236 * not logging by default. */
238
239 scene.reset();
240 device_cpu.reset();
241 }
242};
243
244/*
245 * Test deduplication of nodes that have inputs, some of them folded.
246 */
247TEST_F(RenderGraph, deduplicate_deep)
248{
249 builder.add_node(ShaderNodeBuilder<GeometryNode>(graph, "Geometry1"))
250 .add_node(ShaderNodeBuilder<GeometryNode>(graph, "Geometry2"))
251 .add_node(ShaderNodeBuilder<ValueNode>(graph, "Value1").set_param("value", 0.8f))
252 .add_node(ShaderNodeBuilder<ValueNode>(graph, "Value2").set_param("value", 0.8f))
253 .add_node(ShaderNodeBuilder<NoiseTextureNode>(graph, "Noise1"))
254 .add_node(ShaderNodeBuilder<NoiseTextureNode>(graph, "Noise2"))
255 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
256 .set_param("mix_type", NODE_MIX_BLEND)
257 .set("Fac", 0.5f))
258 .add_connection("Geometry1::Parametric", "Noise1::Vector")
259 .add_connection("Value1::Value", "Noise1::Scale")
260 .add_connection("Noise1::Color", "Mix::Color1")
261 .add_connection("Geometry2::Parametric", "Noise2::Vector")
262 .add_connection("Value2::Value", "Noise2::Scale")
263 .add_connection("Noise2::Color", "Mix::Color2")
264 .output_color("Mix::Color");
265
266 graph.finalize(scene.get());
267
268 EXPECT_EQ(graph.nodes.size(), 5);
269
270 log.correct_info_message("XFolding Value1::Value to constant (0.8).");
271 log.correct_info_message("Folding Value2::Value to constant (0.8).");
272 log.correct_info_message("Deduplicated 2 nodes.");
273}
274
275/*
276 * Test RGB to BW node.
277 */
278TEST_F(RenderGraph, constant_fold_rgb_to_bw)
279{
280 builder
281 .add_node(ShaderNodeBuilder<RGBToBWNode>(graph, "RGBToBWNodeNode")
282 .set("Color", make_float3(0.8f, 0.8f, 0.8f)))
283 .output_color("RGBToBWNodeNode::Val");
284
285 graph.finalize(scene.get());
286
287 log.correct_info_message("Folding RGBToBWNodeNode::Val to constant (0.8).");
288 log.correct_info_message(
289 "Folding convert_float_to_color::value_color to constant (0.8, 0.8, 0.8).");
290}
291
292/*
293 * Tests:
294 * - folding of Emission nodes that don't emit to nothing.
295 */
296TEST_F(RenderGraph, constant_fold_emission1)
297{
298
299 builder.add_node(ShaderNodeBuilder<EmissionNode>(graph, "Emission").set("Color", zero_float3()))
300 .output_closure("Emission::Emission");
301
302 graph.finalize(scene.get());
303
304 log.correct_info_message("Discarding closure Emission.");
305}
306
307TEST_F(RenderGraph, constant_fold_emission2)
308{
309
310 builder.add_node(ShaderNodeBuilder<EmissionNode>(graph, "Emission").set("Strength", 0.0f))
311 .output_closure("Emission::Emission");
312
313 graph.finalize(scene.get());
314
315 log.correct_info_message("Discarding closure Emission.");
316}
317
318/*
319 * Tests:
320 * - folding of Background nodes that don't emit to nothing.
321 */
322TEST_F(RenderGraph, constant_fold_background1)
323{
324 builder
325 .add_node(ShaderNodeBuilder<BackgroundNode>(graph, "Background").set("Color", zero_float3()))
326 .output_closure("Background::Background");
327
328 graph.finalize(scene.get());
329
330 log.correct_info_message("Discarding closure Background.");
331}
332
333TEST_F(RenderGraph, constant_fold_background2)
334{
335 builder.add_node(ShaderNodeBuilder<BackgroundNode>(graph, "Background").set("Strength", 0.0f))
336 .output_closure("Background::Background");
337
338 graph.finalize(scene.get());
339
340 log.correct_info_message("Discarding closure Background.");
341}
342
343/*
344 * Tests:
345 * - Folding of Add Closure with only one input.
346 */
347TEST_F(RenderGraph, constant_fold_shader_add)
348{
349 builder.add_node(ShaderNodeBuilder<DiffuseBsdfNode>(graph, "Diffuse"))
350 .add_node(ShaderNodeBuilder<AddClosureNode>(graph, "AddClosure1"))
351 .add_node(ShaderNodeBuilder<AddClosureNode>(graph, "AddClosure2"))
352 .add_node(ShaderNodeBuilder<AddClosureNode>(graph, "AddClosure3"))
353 .add_connection("Diffuse::BSDF", "AddClosure1::Closure1")
354 .add_connection("Diffuse::BSDF", "AddClosure2::Closure2")
355 .add_connection("AddClosure1::Closure", "AddClosure3::Closure1")
356 .add_connection("AddClosure2::Closure", "AddClosure3::Closure2")
357 .output_closure("AddClosure3::Closure");
358
359 graph.finalize(scene.get());
360
361 log.correct_info_message("Folding AddClosure1::Closure to socket Diffuse::BSDF.");
362 log.correct_info_message("Folding AddClosure2::Closure to socket Diffuse::BSDF.");
363 log.invalid_info_message("Folding AddClosure3");
364}
365
366/*
367 * Tests:
368 * - Folding of Mix Closure with 0 or 1 fac.
369 * - Folding of Mix Closure with both inputs folded to the same node.
370 */
371TEST_F(RenderGraph, constant_fold_shader_mix)
372{
373 builder.add_attribute("Attribute")
374 .add_node(ShaderNodeBuilder<DiffuseBsdfNode>(graph, "Diffuse"))
375 /* choose left */
376 .add_node(ShaderNodeBuilder<MixClosureNode>(graph, "MixClosure1").set("Fac", 0.0f))
377 .add_connection("Diffuse::BSDF", "MixClosure1::Closure1")
378 /* choose right */
379 .add_node(ShaderNodeBuilder<MixClosureNode>(graph, "MixClosure2").set("Fac", 1.0f))
380 .add_connection("Diffuse::BSDF", "MixClosure2::Closure2")
381 /* both inputs folded the same */
382 .add_node(ShaderNodeBuilder<MixClosureNode>(graph, "MixClosure3"))
383 .add_connection("Attribute::Fac", "MixClosure3::Fac")
384 .add_connection("MixClosure1::Closure", "MixClosure3::Closure1")
385 .add_connection("MixClosure2::Closure", "MixClosure3::Closure2")
386 .output_closure("MixClosure3::Closure");
387
388 graph.finalize(scene.get());
389
390 log.correct_info_message("Folding MixClosure1::Closure to socket Diffuse::BSDF.");
391 log.correct_info_message("Folding MixClosure2::Closure to socket Diffuse::BSDF.");
392 log.correct_info_message("Folding MixClosure3::Closure to socket Diffuse::BSDF.");
393}
394
395/*
396 * Tests:
397 * - Folding of Invert with all constant inputs.
398 */
399TEST_F(RenderGraph, constant_fold_invert)
400{
401 builder
402 .add_node(ShaderNodeBuilder<InvertNode>(graph, "Invert")
403 .set("Fac", 0.8f)
404 .set("Color", make_float3(0.2f, 0.5f, 0.8f)))
405 .output_color("Invert::Color");
406
407 graph.finalize(scene.get());
408
409 log.correct_info_message("Folding Invert::Color to constant (0.68, 0.5, 0.32).");
410}
411
412/*
413 * Tests:
414 * - Folding of Invert with zero Fac.
415 */
416TEST_F(RenderGraph, constant_fold_invert_fac_0)
417{
418 builder.add_attribute("Attribute")
419 .add_node(ShaderNodeBuilder<InvertNode>(graph, "Invert").set("Fac", 0.0f))
420 .add_connection("Attribute::Color", "Invert::Color")
421 .output_color("Invert::Color");
422
423 graph.finalize(scene.get());
424
425 log.correct_info_message("Folding Invert::Color to socket Attribute::Color.");
426}
427
428/*
429 * Tests:
430 * - Folding of Invert with zero Fac and constant input.
431 */
432TEST_F(RenderGraph, constant_fold_invert_fac_0_const)
433{
434 builder
435 .add_node(ShaderNodeBuilder<InvertNode>(graph, "Invert")
436 .set("Fac", 0.0f)
437 .set("Color", make_float3(0.2f, 0.5f, 0.8f)))
438 .output_color("Invert::Color");
439
440 graph.finalize(scene.get());
441
442 log.correct_info_message("Folding Invert::Color to constant (0.2, 0.5, 0.8).");
443}
444
445/*
446 * Tests:
447 * - Folding of MixRGB Add with all constant inputs (clamp false).
448 */
449TEST_F(RenderGraph, constant_fold_mix_add)
450{
451 builder
452 .add_node(ShaderNodeBuilder<MixNode>(graph, "MixAdd")
453 .set_param("mix_type", NODE_MIX_ADD)
454 .set_param("use_clamp", false)
455 .set("Fac", 0.8f)
456 .set("Color1", make_float3(0.3f, 0.5f, 0.7f))
457 .set("Color2", make_float3(0.4f, 0.8f, 0.9f)))
458 .output_color("MixAdd::Color");
459
460 graph.finalize(scene.get());
461
462 log.correct_info_message("Folding MixAdd::Color to constant (0.62, 1.14, 1.42).");
463}
464
465/*
466 * Tests:
467 * - Folding of MixRGB Add with all constant inputs (clamp true).
468 */
469TEST_F(RenderGraph, constant_fold_mix_add_clamp)
470{
471 builder
472 .add_node(ShaderNodeBuilder<MixNode>(graph, "MixAdd")
473 .set_param("mix_type", NODE_MIX_ADD)
474 .set_param("use_clamp", true)
475 .set("Fac", 0.8f)
476 .set("Color1", make_float3(0.3f, 0.5f, 0.7f))
477 .set("Color2", make_float3(0.4f, 0.8f, 0.9f)))
478 .output_color("MixAdd::Color");
479
480 graph.finalize(scene.get());
481
482 log.correct_info_message("Folding MixAdd::Color to constant (0.62, 1, 1).");
483}
484
485/*
486 * Tests:
487 * - No folding on fac 0 for dodge.
488 */
489TEST_F(RenderGraph, constant_fold_part_mix_dodge_no_fac_0)
490{
491 builder.add_attribute("Attribute1")
492 .add_attribute("Attribute2")
493 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
494 .set_param("mix_type", NODE_MIX_DODGE)
495 .set_param("use_clamp", false)
496 .set("Fac", 0.0f))
497 .add_connection("Attribute1::Color", "Mix::Color1")
498 .add_connection("Attribute2::Color", "Mix::Color2")
499 .output_color("Mix::Color");
500
501 graph.finalize(scene.get());
502
503 log.invalid_info_message("Folding ");
504}
505
506/*
507 * Tests:
508 * - No folding on fac 0 for light.
509 */
510TEST_F(RenderGraph, constant_fold_part_mix_light_no_fac_0)
511{
512 builder.add_attribute("Attribute1")
513 .add_attribute("Attribute2")
514 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
515 .set_param("mix_type", NODE_MIX_LIGHT)
516 .set_param("use_clamp", false)
517 .set("Fac", 0.0f))
518 .add_connection("Attribute1::Color", "Mix::Color1")
519 .add_connection("Attribute2::Color", "Mix::Color2")
520 .output_color("Mix::Color");
521
522 graph.finalize(scene.get());
523
524 log.invalid_info_message("Folding ");
525}
526
527/*
528 * Tests:
529 * - No folding on fac 0 for burn.
530 */
531TEST_F(RenderGraph, constant_fold_part_mix_burn_no_fac_0)
532{
533 builder.add_attribute("Attribute1")
534 .add_attribute("Attribute2")
535 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
536 .set_param("mix_type", NODE_MIX_BURN)
537 .set_param("use_clamp", false)
538 .set("Fac", 0.0f))
539 .add_connection("Attribute1::Color", "Mix::Color1")
540 .add_connection("Attribute2::Color", "Mix::Color2")
541 .output_color("Mix::Color");
542
543 graph.finalize(scene.get());
544
545 log.invalid_info_message("Folding ");
546}
547
548/*
549 * Tests:
550 * - No folding on fac 0 for clamped blend.
551 */
552TEST_F(RenderGraph, constant_fold_part_mix_blend_clamped_no_fac_0)
553{
554 builder.add_attribute("Attribute1")
555 .add_attribute("Attribute2")
556 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
557 .set_param("mix_type", NODE_MIX_BLEND)
558 .set_param("use_clamp", true)
559 .set("Fac", 0.0f))
560 .add_connection("Attribute1::Color", "Mix::Color1")
561 .add_connection("Attribute2::Color", "Mix::Color2")
562 .output_color("Mix::Color");
563
564 graph.finalize(scene.get());
565
566 log.invalid_info_message("Folding ");
567}
568
569/*
570 * Tests:
571 * - Folding of Mix with 0 or 1 Fac.
572 * - Folding of Mix with both inputs folded to the same node.
573 */
574TEST_F(RenderGraph, constant_fold_part_mix_blend)
575{
576 builder.add_attribute("Attribute1")
577 .add_attribute("Attribute2")
578 /* choose left */
579 .add_node(ShaderNodeBuilder<MixNode>(graph, "MixBlend1")
580 .set_param("mix_type", NODE_MIX_BLEND)
581 .set_param("use_clamp", false)
582 .set("Fac", 0.0f))
583 .add_connection("Attribute1::Color", "MixBlend1::Color1")
584 .add_connection("Attribute2::Color", "MixBlend1::Color2")
585 /* choose right */
586 .add_node(ShaderNodeBuilder<MixNode>(graph, "MixBlend2")
587 .set_param("mix_type", NODE_MIX_BLEND)
588 .set_param("use_clamp", false)
589 .set("Fac", 1.0f))
590 .add_connection("Attribute1::Color", "MixBlend2::Color2")
591 .add_connection("Attribute2::Color", "MixBlend2::Color1")
592 /* both inputs folded to Attribute1 */
593 .add_node(ShaderNodeBuilder<MixNode>(graph, "MixBlend3")
594 .set_param("mix_type", NODE_MIX_BLEND)
595 .set_param("use_clamp", false))
596 .add_connection("Attribute1::Fac", "MixBlend3::Fac")
597 .add_connection("MixBlend1::Color", "MixBlend3::Color1")
598 .add_connection("MixBlend2::Color", "MixBlend3::Color2")
599 .output_color("MixBlend3::Color");
600
601 graph.finalize(scene.get());
602
603 log.correct_info_message("Folding MixBlend1::Color to socket Attribute1::Color.");
604 log.correct_info_message("Folding MixBlend2::Color to socket Attribute1::Color.");
605 log.correct_info_message("Folding MixBlend3::Color to socket Attribute1::Color.");
606}
607
608/*
609 * Tests:
610 * - NOT folding of MixRGB Subtract with the same inputs and fac NOT 1.
611 */
612TEST_F(RenderGraph, constant_fold_part_mix_sub_same_fac_bad)
613{
614 builder.add_attribute("Attribute")
615 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
616 .set_param("mix_type", NODE_MIX_SUB)
617 .set_param("use_clamp", true)
618 .set("Fac", 0.5f))
619 .add_connection("Attribute::Color", "Mix::Color1")
620 .add_connection("Attribute::Color", "Mix::Color2")
621 .output_color("Mix::Color");
622
623 graph.finalize(scene.get());
624
625 log.invalid_info_message("Folding Mix::");
626}
627
628/*
629 * Tests:
630 * - Folding of MixRGB Subtract with the same inputs and fac 1.
631 */
632TEST_F(RenderGraph, constant_fold_part_mix_sub_same_fac_1)
633{
634 builder.add_attribute("Attribute")
635 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix")
636 .set_param("mix_type", NODE_MIX_SUB)
637 .set_param("use_clamp", true)
638 .set("Fac", 1.0f))
639 .add_connection("Attribute::Color", "Mix::Color1")
640 .add_connection("Attribute::Color", "Mix::Color2")
641 .output_color("Mix::Color");
642
643 graph.finalize(scene.get());
644
645 log.correct_info_message("Folding Mix::Color to constant (0, 0, 0).");
646}
647
648/*
649 * Graph for testing partial folds of MixRGB with one constant argument.
650 * Includes 4 tests: constant on each side with fac either unknown or 1.
651 */
652static void build_mix_partial_test_graph(ShaderGraphBuilder &builder,
653 NodeMix type,
654 const float3 constval)
655{
656 builder
657 .add_attribute("Attribute")
658 /* constant on the left */
659 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Mix_Cx_Fx")
660 .set_param("mix_type", type)
661 .set_param("use_clamp", false)
662 .set("Color1", constval))
663 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Mix_Cx_F1")
664 .set_param("mix_type", type)
665 .set_param("use_clamp", false)
666 .set("Color1", constval)
667 .set("Fac", 1.0f))
668 .add_connection("Attribute::Fac", "Mix_Cx_Fx::Fac")
669 .add_connection("Attribute::Color", "Mix_Cx_Fx::Color2")
670 .add_connection("Attribute::Color", "Mix_Cx_F1::Color2")
671 /* constant on the right */
672 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Mix_xC_Fx")
673 .set_param("mix_type", type)
674 .set_param("use_clamp", false)
675 .set("Color2", constval))
676 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Mix_xC_F1")
677 .set_param("mix_type", type)
678 .set_param("use_clamp", false)
679 .set("Color2", constval)
680 .set("Fac", 1.0f))
681 .add_connection("Attribute::Fac", "Mix_xC_Fx::Fac")
682 .add_connection("Attribute::Color", "Mix_xC_Fx::Color1")
683 .add_connection("Attribute::Color", "Mix_xC_F1::Color1")
684 /* results of actual tests simply added up to connect to output */
685 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Out12")
686 .set_param("mix_type", NODE_MIX_ADD)
687 .set_param("use_clamp", true)
688 .set("Fac", 1.0f))
689 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Out34")
690 .set_param("mix_type", NODE_MIX_ADD)
691 .set_param("use_clamp", true)
692 .set("Fac", 1.0f))
693 .add_node(ShaderNodeBuilder<MixNode>(builder.graph(), "Out1234")
694 .set_param("mix_type", NODE_MIX_ADD)
695 .set_param("use_clamp", true)
696 .set("Fac", 1.0f))
697 .add_connection("Mix_Cx_Fx::Color", "Out12::Color1")
698 .add_connection("Mix_Cx_F1::Color", "Out12::Color2")
699 .add_connection("Mix_xC_Fx::Color", "Out34::Color1")
700 .add_connection("Mix_xC_F1::Color", "Out34::Color2")
701 .add_connection("Out12::Color", "Out1234::Color1")
702 .add_connection("Out34::Color", "Out1234::Color2")
703 .output_color("Out1234::Color");
704}
705
706/*
707 * Tests: partial folding for RGB Add with known 0.
708 */
709TEST_F(RenderGraph, constant_fold_part_mix_add_0)
710{
712 graph.finalize(scene.get());
713
714 /* 0 + X (fac 1) == X */
715 log.invalid_info_message("Folding Mix_Cx_Fx::Color");
716 log.correct_info_message("Folding Mix_Cx_F1::Color to socket Attribute::Color.");
717 /* X + 0 (fac ?) == X */
718 log.correct_info_message("Folding Mix_xC_Fx::Color to socket Attribute::Color.");
719 log.correct_info_message("Folding Mix_xC_F1::Color to socket Attribute::Color.");
720 log.invalid_info_message("Folding Out");
721}
722
723/*
724 * Tests: partial folding for RGB Subtract with known 0.
725 */
726TEST_F(RenderGraph, constant_fold_part_mix_sub_0)
727{
729 graph.finalize(scene.get());
730
731 log.invalid_info_message("Folding Mix_Cx_Fx::Color");
732 log.invalid_info_message("Folding Mix_Cx_F1::Color");
733 /* X - 0 (fac ?) == X */
734 log.correct_info_message("Folding Mix_xC_Fx::Color to socket Attribute::Color.");
735 log.correct_info_message("Folding Mix_xC_F1::Color to socket Attribute::Color.");
736 log.invalid_info_message("Folding Out");
737}
738
739/*
740 * Tests: partial folding for RGB Multiply with known 1.
741 */
742TEST_F(RenderGraph, constant_fold_part_mix_mul_1)
743{
745 graph.finalize(scene.get());
746
747 /* 1 * X (fac 1) == X */
748 log.invalid_info_message("Folding Mix_Cx_Fx::Color");
749 log.correct_info_message("Folding Mix_Cx_F1::Color to socket Attribute::Color.");
750 /* X * 1 (fac ?) == X */
751 log.correct_info_message("Folding Mix_xC_Fx::Color to socket Attribute::Color.");
752 log.correct_info_message("Folding Mix_xC_F1::Color to socket Attribute::Color.");
753 log.invalid_info_message("Folding Out");
754}
755
756/*
757 * Tests: partial folding for RGB Divide with known 1.
758 */
759TEST_F(RenderGraph, constant_fold_part_mix_div_1)
760{
762 graph.finalize(scene.get());
763
764 log.invalid_info_message("Folding Mix_Cx_Fx::Color");
765 log.invalid_info_message("Folding Mix_Cx_F1::Color");
766 /* X / 1 (fac ?) == X */
767 log.correct_info_message("Folding Mix_xC_Fx::Color to socket Attribute::Color.");
768 log.correct_info_message("Folding Mix_xC_F1::Color to socket Attribute::Color.");
769 log.invalid_info_message("Folding Out");
770}
771
772/*
773 * Tests: partial folding for RGB Multiply with known 0.
774 */
775TEST_F(RenderGraph, constant_fold_part_mix_mul_0)
776{
778 graph.finalize(scene.get());
779
780 /* 0 * ? (fac ?) == 0 */
781 log.correct_info_message("Folding Mix_Cx_Fx::Color to constant (0, 0, 0).");
782 log.correct_info_message("Folding Mix_Cx_F1::Color to constant (0, 0, 0).");
783 /* ? * 0 (fac 1) == 0 */
784 log.invalid_info_message("Folding Mix_xC_Fx::Color");
785 log.correct_info_message("Folding Mix_xC_F1::Color to constant (0, 0, 0).");
786
787 log.correct_info_message("Folding Out12::Color to constant (0, 0, 0).");
788 log.invalid_info_message("Folding Out1234");
789}
790
791/*
792 * Tests: partial folding for RGB Divide with known 0.
793 */
794TEST_F(RenderGraph, constant_fold_part_mix_div_0)
795{
797 graph.finalize(scene.get());
798
799 /* 0 / ? (fac ?) == 0 */
800 log.correct_info_message("Folding Mix_Cx_Fx::Color to constant (0, 0, 0).");
801 log.correct_info_message("Folding Mix_Cx_F1::Color to constant (0, 0, 0).");
802 log.invalid_info_message("Folding Mix_xC_Fx::Color");
803 log.invalid_info_message("Folding Mix_xC_F1::Color");
804
805 log.correct_info_message("Folding Out12::Color to constant (0, 0, 0).");
806 log.invalid_info_message("Folding Out1234");
807}
808
809/*
810 * Tests: Separate/Combine RGB with all constant inputs.
811 */
812TEST_F(RenderGraph, constant_fold_separate_combine_rgb)
813{
814 builder
815 .add_node(ShaderNodeBuilder<SeparateColorNode>(graph, "SeparateRGB")
816 .set("Color", make_float3(0.3f, 0.5f, 0.7f))
817 .set_param("color_type", NODE_COMBSEP_COLOR_RGB))
818 .add_node(ShaderNodeBuilder<CombineColorNode>(graph, "CombineRGB")
819 .set_param("color_type", NODE_COMBSEP_COLOR_RGB))
820 .add_connection("SeparateRGB::Red", "CombineRGB::Red")
821 .add_connection("SeparateRGB::Green", "CombineRGB::Green")
822 .add_connection("SeparateRGB::Blue", "CombineRGB::Blue")
823 .output_color("CombineRGB::Color");
824
825 graph.finalize(scene.get());
826
827 log.correct_info_message("Folding SeparateRGB::R to constant (0.3).");
828 log.correct_info_message("Folding SeparateRGB::G to constant (0.5).");
829 log.correct_info_message("Folding SeparateRGB::B to constant (0.7).");
830 log.correct_info_message("Folding CombineRGB::Image to constant (0.3, 0.5, 0.7).");
831}
832
833/*
834 * Tests: Separate/Combine XYZ with all constant inputs.
835 */
836TEST_F(RenderGraph, constant_fold_separate_combine_xyz)
837{
838 builder
839 .add_node(ShaderNodeBuilder<SeparateXYZNode>(graph, "SeparateXYZ")
840 .set("Vector", make_float3(0.3f, 0.5f, 0.7f)))
841 .add_node(ShaderNodeBuilder<CombineXYZNode>(graph, "CombineXYZ"))
842 .add_connection("SeparateXYZ::X", "CombineXYZ::X")
843 .add_connection("SeparateXYZ::Y", "CombineXYZ::Y")
844 .add_connection("SeparateXYZ::Z", "CombineXYZ::Z")
845 .output_color("CombineXYZ::Vector");
846
847 graph.finalize(scene.get());
848
849 log.correct_info_message("Folding SeparateXYZ::X to constant (0.3).");
850 log.correct_info_message("Folding SeparateXYZ::Y to constant (0.5).");
851 log.correct_info_message("Folding SeparateXYZ::Z to constant (0.7).");
852 log.correct_info_message("Folding CombineXYZ::Vector to constant (0.3, 0.5, 0.7).");
853 log.correct_info_message(
854 "Folding convert_vector_to_color::value_color to constant (0.3, 0.5, 0.7).");
855}
856
857/*
858 * Tests: Separate/Combine HSV with all constant inputs.
859 */
860TEST_F(RenderGraph, constant_fold_separate_combine_hsv)
861{
862 builder
863 .add_node(ShaderNodeBuilder<SeparateColorNode>(graph, "SeparateHSV")
864 .set("Color", make_float3(0.3f, 0.5f, 0.7f))
865 .set_param("color_type", NODE_COMBSEP_COLOR_HSV))
866 .add_node(ShaderNodeBuilder<CombineColorNode>(graph, "CombineHSV")
867 .set_param("color_type", NODE_COMBSEP_COLOR_HSV))
868 .add_connection("SeparateHSV::Red", "CombineHSV::Red")
869 .add_connection("SeparateHSV::Green", "CombineHSV::Green")
870 .add_connection("SeparateHSV::Blue", "CombineHSV::Blue")
871 .output_color("CombineHSV::Color");
872
873 graph.finalize(scene.get());
874
875 log.correct_info_message("Folding SeparateHSV::H to constant (0.583333).");
876 log.correct_info_message("Folding SeparateHSV::S to constant (0.571429).");
877 log.correct_info_message("Folding SeparateHSV::V to constant (0.7).");
878 log.correct_info_message("Folding CombineHSV::Color to constant (0.3, 0.5, 0.7).");
879}
880
881/*
882 * Tests: Gamma with all constant inputs.
883 */
884TEST_F(RenderGraph, constant_fold_gamma)
885{
886 builder
887 .add_node(ShaderNodeBuilder<GammaNode>(graph, "Gamma")
888 .set("Color", make_float3(0.3f, 0.5f, 0.7f))
889 .set("Gamma", 1.5f))
890 .output_color("Gamma::Color");
891
892 graph.finalize(scene.get());
893
894 log.correct_info_message("Folding Gamma::Color to constant (0.164317, 0.353553, 0.585662).");
895}
896
897/*
898 * Tests: Gamma with one constant 0 input.
899 */
900TEST_F(RenderGraph, constant_fold_gamma_part_0)
901{
902 builder
903 .add_attribute("Attribute")
904 /* constant on the left */
905 .add_node(ShaderNodeBuilder<GammaNode>(graph, "Gamma_Cx").set("Color", zero_float3()))
906 .add_connection("Attribute::Fac", "Gamma_Cx::Gamma")
907 /* constant on the right */
908 .add_node(ShaderNodeBuilder<GammaNode>(graph, "Gamma_xC").set("Gamma", 0.0f))
909 .add_connection("Attribute::Color", "Gamma_xC::Color")
910 /* output sum */
911 .add_node(ShaderNodeBuilder<MixNode>(graph, "Out")
912 .set_param("mix_type", NODE_MIX_ADD)
913 .set_param("use_clamp", true)
914 .set("Fac", 1.0f))
915 .add_connection("Gamma_Cx::Color", "Out::Color1")
916 .add_connection("Gamma_xC::Color", "Out::Color2")
917 .output_color("Out::Color");
918
919 graph.finalize(scene.get());
920
921 log.invalid_info_message("Folding Gamma_Cx::");
922 log.correct_info_message("Folding Gamma_xC::Color to constant (1, 1, 1).");
923}
924
925/*
926 * Tests: Gamma with one constant 1 input.
927 */
928TEST_F(RenderGraph, constant_fold_gamma_part_1)
929{
930 builder
931 .add_attribute("Attribute")
932 /* constant on the left */
933 .add_node(ShaderNodeBuilder<GammaNode>(graph, "Gamma_Cx").set("Color", one_float3()))
934 .add_connection("Attribute::Fac", "Gamma_Cx::Gamma")
935 /* constant on the right */
936 .add_node(ShaderNodeBuilder<GammaNode>(graph, "Gamma_xC").set("Gamma", 1.0f))
937 .add_connection("Attribute::Color", "Gamma_xC::Color")
938 /* output sum */
939 .add_node(ShaderNodeBuilder<MixNode>(graph, "Out")
940 .set_param("mix_type", NODE_MIX_ADD)
941 .set_param("use_clamp", true)
942 .set("Fac", 1.0f))
943 .add_connection("Gamma_Cx::Color", "Out::Color1")
944 .add_connection("Gamma_xC::Color", "Out::Color2")
945 .output_color("Out::Color");
946
947 graph.finalize(scene.get());
948
949 log.correct_info_message("Folding Gamma_Cx::Color to constant (1, 1, 1).");
950 log.correct_info_message("Folding Gamma_xC::Color to socket Attribute::Color.");
951}
952
953/*
954 * Tests: BrightnessContrast with all constant inputs.
955 */
956TEST_F(RenderGraph, constant_fold_bright_contrast)
957{
958 builder
959 .add_node(ShaderNodeBuilder<BrightContrastNode>(graph, "BrightContrast")
960 .set("Color", make_float3(0.3f, 0.5f, 0.7f))
961 .set("Bright", 0.1f)
962 .set("Contrast", 1.2f))
963 .output_color("BrightContrast::Color");
964
965 graph.finalize(scene.get());
966
967 log.correct_info_message("Folding BrightContrast::Color to constant (0.16, 0.6, 1.04).");
968}
969
970/*
971 * Tests: blackbody with all constant inputs.
972 */
973TEST_F(RenderGraph, constant_fold_blackbody)
974{
975 builder
976 .add_node(ShaderNodeBuilder<BlackbodyNode>(graph, "Blackbody").set("Temperature", 1200.0f))
977 .output_color("Blackbody::Color");
978
979 graph.finalize(scene.get());
980
981 log.correct_info_message("Folding Blackbody::Color to constant (3.96553, 0.227897, 0).");
982}
983
984/* A Note About The Math Node
985 *
986 * The clamp option is implemented using graph expansion, where a
987 * Clamp node named "clamp" is added and connected to the output.
988 * So the final result is actually from the node "clamp".
989 */
990
991/*
992 * Tests: Math with all constant inputs (clamp false).
993 */
994TEST_F(RenderGraph, constant_fold_math)
995{
996 builder
997 .add_node(ShaderNodeBuilder<MathNode>(graph, "Math")
998 .set_param("math_type", NODE_MATH_ADD)
999 .set_param("use_clamp", false)
1000 .set("Value1", 0.7f)
1001 .set("Value2", 0.9f))
1002 .output_value("Math::Value");
1003
1004 graph.finalize(scene.get());
1005
1006 log.correct_info_message("Folding Math::Value to constant (1.6).");
1007}
1008
1009/*
1010 * Tests: Math with all constant inputs (clamp true).
1011 */
1012TEST_F(RenderGraph, constant_fold_math_clamp)
1013{
1014 builder
1015 .add_node(ShaderNodeBuilder<MathNode>(graph, "Math")
1016 .set_param("math_type", NODE_MATH_ADD)
1017 .set_param("use_clamp", true)
1018 .set("Value1", 0.7f)
1019 .set("Value2", 0.9f))
1020 .output_value("Math::Value");
1021
1022 graph.finalize(scene.get());
1023
1024 log.correct_info_message("Folding clamp::Result to constant (1).");
1025}
1026
1027/*
1028 * Graph for testing partial folds of Math with one constant argument.
1029 * Includes 2 tests: constant on each side.
1030 */
1031static void build_math_partial_test_graph(ShaderGraphBuilder &builder,
1032 NodeMathType type,
1033 const float constval)
1034{
1035 builder
1036 .add_attribute("Attribute")
1037 /* constant on the left */
1038 .add_node(ShaderNodeBuilder<MathNode>(builder.graph(), "Math_Cx")
1039 .set_param("math_type", type)
1040 .set_param("use_clamp", false)
1041 .set("Value1", constval))
1042 .add_connection("Attribute::Fac", "Math_Cx::Value2")
1043 /* constant on the right */
1044 .add_node(ShaderNodeBuilder<MathNode>(builder.graph(), "Math_xC")
1045 .set_param("math_type", type)
1046 .set_param("use_clamp", false)
1047 .set("Value2", constval))
1048 .add_connection("Attribute::Fac", "Math_xC::Value1")
1049 /* output sum */
1050 .add_node(ShaderNodeBuilder<MathNode>(builder.graph(), "Out")
1051 .set_param("math_type", NODE_MATH_ADD)
1052 .set_param("use_clamp", true))
1053 .add_connection("Math_Cx::Value", "Out::Value1")
1054 .add_connection("Math_xC::Value", "Out::Value2")
1055 .output_value("Out::Value");
1056}
1057
1058/*
1059 * Tests: partial folding for Math Add with known 0.
1060 */
1061TEST_F(RenderGraph, constant_fold_part_math_add_0)
1062{
1064 graph.finalize(scene.get());
1065
1066 /* X + 0 == 0 + X == X */
1067 log.correct_info_message("Folding Math_Cx::Value to socket Attribute::Fac.");
1068 log.correct_info_message("Folding Math_xC::Value to socket Attribute::Fac.");
1069 log.invalid_info_message("Folding clamp::");
1070}
1071
1072/*
1073 * Tests: partial folding for Math Subtract with known 0.
1074 */
1075TEST_F(RenderGraph, constant_fold_part_math_sub_0)
1076{
1078 graph.finalize(scene.get());
1079
1080 /* X - 0 == X */
1081 log.invalid_info_message("Folding Math_Cx::");
1082 log.correct_info_message("Folding Math_xC::Value to socket Attribute::Fac.");
1083 log.invalid_info_message("Folding clamp::");
1084}
1085
1086/*
1087 * Tests: partial folding for Math Multiply with known 1.
1088 */
1089TEST_F(RenderGraph, constant_fold_part_math_mul_1)
1090{
1092 graph.finalize(scene.get());
1093
1094 /* X * 1 == 1 * X == X */
1095 log.correct_info_message("Folding Math_Cx::Value to socket Attribute::Fac.");
1096 log.correct_info_message("Folding Math_xC::Value to socket Attribute::Fac.");
1097 log.invalid_info_message("Folding clamp::");
1098}
1099
1100/*
1101 * Tests: partial folding for Math Divide with known 1.
1102 */
1103TEST_F(RenderGraph, constant_fold_part_math_div_1)
1104{
1106 graph.finalize(scene.get());
1107
1108 /* X / 1 == X */
1109 log.invalid_info_message("Folding Math_Cx::");
1110 log.correct_info_message("Folding Math_xC::Value to socket Attribute::Fac.");
1111 log.invalid_info_message("Folding clamp::");
1112}
1113
1114/*
1115 * Tests: partial folding for Math Multiply with known 0.
1116 */
1117TEST_F(RenderGraph, constant_fold_part_math_mul_0)
1118{
1120 graph.finalize(scene.get());
1121
1122 /* X * 0 == 0 * X == 0 */
1123 log.correct_info_message("Folding Math_Cx::Value to constant (0).");
1124 log.correct_info_message("Folding Math_xC::Value to constant (0).");
1125 log.correct_info_message("Folding clamp::Result to constant (0)");
1126 log.correct_info_message("Discarding closure EmissionNode.");
1127}
1128
1129/*
1130 * Tests: partial folding for Math Divide with known 0.
1131 */
1132TEST_F(RenderGraph, constant_fold_part_math_div_0)
1133{
1135 graph.finalize(scene.get());
1136
1137 /* 0 / X == 0 */
1138 log.correct_info_message("Folding Math_Cx::Value to constant (0).");
1139 log.invalid_info_message("Folding Math_xC::");
1140 log.invalid_info_message("Folding clamp::");
1141}
1142
1143/*
1144 * Tests: partial folding for Math Power with known 0.
1145 */
1146TEST_F(RenderGraph, constant_fold_part_math_pow_0)
1147{
1149 graph.finalize(scene.get());
1150
1151 /* X ^ 0 == 1 */
1152 log.invalid_info_message("Folding Math_Cx::");
1153 log.correct_info_message("Folding Math_xC::Value to constant (1).");
1154 log.invalid_info_message("Folding clamp::");
1155}
1156
1157/*
1158 * Tests: partial folding for Math Power with known 1.
1159 */
1160TEST_F(RenderGraph, constant_fold_part_math_pow_1)
1161{
1163 graph.finalize(scene.get());
1164
1165 /* 1 ^ X == 1; X ^ 1 == X */
1166 log.correct_info_message("Folding Math_Cx::Value to constant (1)");
1167 log.correct_info_message("Folding Math_xC::Value to socket Attribute::Fac.");
1168 log.invalid_info_message("Folding clamp::");
1169}
1170
1171/*
1172 * Tests: Vector Math with all constant inputs.
1173 */
1174TEST_F(RenderGraph, constant_fold_vector_math)
1175{
1176 builder
1177 .add_node(ShaderNodeBuilder<VectorMathNode>(graph, "VectorMath")
1178 .set_param("math_type", NODE_VECTOR_MATH_SUBTRACT)
1179 .set("Vector1", make_float3(1.3f, 0.5f, 0.7f))
1180 .set("Vector2", make_float3(-1.7f, 0.5f, 0.7f)))
1181 .output_color("VectorMath::Vector");
1182
1183 graph.finalize(scene.get());
1184
1185 log.correct_info_message("Folding VectorMath::Vector to constant (3, 0, 0).");
1186}
1187
1188/*
1189 * Graph for testing partial folds of Vector Math with one constant argument.
1190 * Includes 2 tests: constant on each side.
1191 */
1192static void build_vecmath_partial_test_graph(ShaderGraphBuilder &builder,
1193 NodeVectorMathType type,
1194 const float3 constval)
1195{
1196 builder
1197 .add_attribute("Attribute")
1198 /* constant on the left */
1199 .add_node(ShaderNodeBuilder<VectorMathNode>(builder.graph(), "Math_Cx")
1200 .set_param("math_type", type)
1201 .set("Vector1", constval))
1202 .add_connection("Attribute::Vector", "Math_Cx::Vector2")
1203 /* constant on the right */
1204 .add_node(ShaderNodeBuilder<VectorMathNode>(builder.graph(), "Math_xC")
1205 .set_param("math_type", type)
1206 .set("Vector2", constval))
1207 .add_connection("Attribute::Vector", "Math_xC::Vector1")
1208 /* output sum */
1209 .add_node(ShaderNodeBuilder<VectorMathNode>(builder.graph(), "Out")
1210 .set_param("math_type", NODE_VECTOR_MATH_ADD))
1211 .add_connection("Math_Cx::Vector", "Out::Vector1")
1212 .add_connection("Math_xC::Vector", "Out::Vector2")
1213 .output_color("Out::Vector");
1214}
1215
1216/*
1217 * Tests: partial folding for Vector Math Add with known 0.
1218 */
1219TEST_F(RenderGraph, constant_fold_part_vecmath_add_0)
1220{
1222 graph.finalize(scene.get());
1223
1224 /* X + 0 == 0 + X == X */
1225 log.correct_info_message("Folding Math_Cx::Vector to socket Attribute::Vector.");
1226 log.correct_info_message("Folding Math_xC::Vector to socket Attribute::Vector.");
1227 log.invalid_info_message("Folding Out::");
1228}
1229
1230/*
1231 * Tests: partial folding for Vector Math Subtract with known 0.
1232 */
1233TEST_F(RenderGraph, constant_fold_part_vecmath_sub_0)
1234{
1236 graph.finalize(scene.get());
1237
1238 /* X - 0 == X */
1239 log.invalid_info_message("Folding Math_Cx::");
1240 log.correct_info_message("Folding Math_xC::Vector to socket Attribute::Vector.");
1241 log.invalid_info_message("Folding Out::");
1242}
1243
1244/*
1245 * Tests: partial folding for Vector Math Cross Product with known 0.
1246 */
1247TEST_F(RenderGraph, constant_fold_part_vecmath_cross_0)
1248{
1250 graph.finalize(scene.get());
1251
1252 /* X * 0 == 0 * X == X */
1253 log.correct_info_message("Folding Math_Cx::Vector to constant (0, 0, 0).");
1254 log.correct_info_message("Folding Math_xC::Vector to constant (0, 0, 0).");
1255 log.correct_info_message("Folding Out::Vector to constant (0, 0, 0).");
1256 log.correct_info_message("Discarding closure EmissionNode.");
1257}
1258
1259/*
1260 * Tests: Bump with no height input folded to Normal input.
1261 */
1262TEST_F(RenderGraph, constant_fold_bump)
1263{
1264 builder.add_node(ShaderNodeBuilder<GeometryNode>(graph, "Geometry1"))
1265 .add_node(ShaderNodeBuilder<BumpNode>(graph, "Bump"))
1266 .add_connection("Geometry1::Normal", "Bump::Normal")
1267 .output_color("Bump::Normal");
1268
1269 graph.finalize(scene.get());
1270
1271 log.correct_info_message("Folding Bump::Normal to socket Geometry1::Normal.");
1272}
1273
1274/*
1275 * Tests: Bump with no inputs folded to Geometry::Normal.
1276 */
1277TEST_F(RenderGraph, constant_fold_bump_no_input)
1278{
1279 builder.add_node(ShaderNodeBuilder<BumpNode>(graph, "Bump")).output_color("Bump::Normal");
1280
1281 graph.finalize(scene.get());
1282
1283 log.correct_info_message("Folding Bump::Normal to socket geometry::Normal.");
1284}
1285
1286template<class T> void init_test_curve(array<T> &buffer, T start, T end, const int steps)
1287{
1288 buffer.resize(steps);
1289
1290 for (int i = 0; i < steps; i++) {
1291 buffer[i] = mix(start, end, float(i) / (steps - 1));
1292 }
1293}
1294
1296 const float3 start,
1297 const float3 end,
1298 const int steps)
1299{
1300 buffer.resize(steps);
1301
1302 for (int i = 0; i < steps; i++) {
1303 buffer[i] = mix(start, end, float(i) / (steps - 1));
1304 }
1305}
1306
1307/*
1308 * Tests:
1309 * - Folding of RGB Curves with all constant inputs.
1310 */
1311TEST_F(RenderGraph, constant_fold_rgb_curves)
1312{
1314 init_test_curve(curve, make_float3(0.0f, 0.25f, 1.0f), make_float3(1.0f, 0.75f, 0.0f), 257);
1315
1316 builder
1317 .add_node(ShaderNodeBuilder<RGBCurvesNode>(graph, "Curves")
1318 .set_param("curves", curve)
1319 .set_param("min_x", 0.1f)
1320 .set_param("max_x", 0.9f)
1321 .set("Fac", 0.5f)
1322 .set("Color", make_float3(0.3f, 0.5f, 0.7f)))
1323 .output_color("Curves::Color");
1324
1325 graph.finalize(scene.get());
1326
1327 log.correct_info_message("Folding Curves::Color to constant (0.275, 0.5, 0.475).");
1328}
1329
1330/*
1331 * Tests:
1332 * - Folding of RGB Curves with zero Fac.
1333 */
1334TEST_F(RenderGraph, constant_fold_rgb_curves_fac_0)
1335{
1337 init_test_curve(curve, make_float3(0.0f, 0.25f, 1.0f), make_float3(1.0f, 0.75f, 0.0f), 257);
1338
1339 builder.add_attribute("Attribute")
1340 .add_node(ShaderNodeBuilder<RGBCurvesNode>(graph, "Curves")
1341 .set_param("curves", curve)
1342 .set_param("min_x", 0.1f)
1343 .set_param("max_x", 0.9f)
1344 .set("Fac", 0.0f))
1345 .add_connection("Attribute::Color", "Curves::Color")
1346 .output_color("Curves::Color");
1347
1348 graph.finalize(scene.get());
1349
1350 log.correct_info_message("Folding Curves::Color to socket Attribute::Color.");
1351}
1352
1353/*
1354 * Tests:
1355 * - Folding of RGB Curves with zero Fac and all constant inputs.
1356 */
1357TEST_F(RenderGraph, constant_fold_rgb_curves_fac_0_const)
1358{
1360 init_test_curve(curve, make_float3(0.0f, 0.25f, 1.0f), make_float3(1.0f, 0.75f, 0.0f), 257);
1361
1362 builder
1363 .add_node(ShaderNodeBuilder<RGBCurvesNode>(graph, "Curves")
1364 .set_param("curves", curve)
1365 .set_param("min_x", 0.1f)
1366 .set_param("max_x", 0.9f)
1367 .set("Fac", 0.0f)
1368 .set("Color", make_float3(0.3f, 0.5f, 0.7f)))
1369 .output_color("Curves::Color");
1370
1371 graph.finalize(scene.get());
1372
1373 log.correct_info_message("Folding Curves::Color to constant (0.3, 0.5, 0.7).");
1374}
1375
1376/*
1377 * Tests:
1378 * - Folding of Vector Curves with all constant inputs.
1379 */
1380TEST_F(RenderGraph, constant_fold_vector_curves)
1381{
1383 init_test_curve(curve, make_float3(0.0f, 0.25f, 1.0f), make_float3(1.0f, 0.75f, 0.0f), 257);
1384
1385 builder
1386 .add_node(ShaderNodeBuilder<VectorCurvesNode>(graph, "Curves")
1387 .set_param("curves", curve)
1388 .set_param("min_x", 0.1f)
1389 .set_param("max_x", 0.9f)
1390 .set("Fac", 0.5f)
1391 .set("Vector", make_float3(0.3f, 0.5f, 0.7f)))
1392 .output_color("Curves::Vector");
1393
1394 graph.finalize(scene.get());
1395
1396 log.correct_info_message("Folding Curves::Vector to constant (0.275, 0.5, 0.475).");
1397}
1398
1399/*
1400 * Tests:
1401 * - Folding of Vector Curves with zero Fac.
1402 */
1403TEST_F(RenderGraph, constant_fold_vector_curves_fac_0)
1404{
1406 init_test_curve(curve, make_float3(0.0f, 0.25f, 1.0f), make_float3(1.0f, 0.75f, 0.0f), 257);
1407
1408 builder.add_attribute("Attribute")
1409 .add_node(ShaderNodeBuilder<VectorCurvesNode>(graph, "Curves")
1410 .set_param("curves", curve)
1411 .set_param("min_x", 0.1f)
1412 .set_param("max_x", 0.9f)
1413 .set("Fac", 0.0f))
1414 .add_connection("Attribute::Vector", "Curves::Vector")
1415 .output_color("Curves::Vector");
1416
1417 graph.finalize(scene.get());
1418
1419 log.correct_info_message("Folding Curves::Vector to socket Attribute::Vector.");
1420}
1421
1422/*
1423 * Tests:
1424 * - Folding of Color Ramp with all constant inputs.
1425 */
1426TEST_F(RenderGraph, constant_fold_rgb_ramp)
1427{
1429 array<float> alpha;
1430 init_test_curve(curve, make_float3(0.0f, 0.25f, 0.5f), make_float3(0.25f, 0.5f, 0.75f), 9);
1431 init_test_curve(alpha, 0.75f, 1.0f, 9);
1432
1433 builder
1434 .add_node(ShaderNodeBuilder<RGBRampNode>(graph, "Ramp")
1435 .set_param("ramp", curve)
1436 .set_param("ramp_alpha", alpha)
1437 .set_param("interpolate", true)
1438 .set("Fac", 0.56f))
1439 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix").set_param("mix_type", NODE_MIX_ADD))
1440 .add_connection("Ramp::Color", "Mix::Color1")
1441 .add_connection("Ramp::Alpha", "Mix::Color2")
1442 .output_color("Mix::Color");
1443
1444 graph.finalize(scene.get());
1445
1446 log.correct_info_message("Folding Ramp::Color to constant (0.14, 0.39, 0.64).");
1447 log.correct_info_message("Folding Ramp::Alpha to constant (0.89).");
1448}
1449
1450/*
1451 * Tests:
1452 * - Folding of Color Ramp with all constant inputs (interpolate false).
1453 */
1454TEST_F(RenderGraph, constant_fold_rgb_ramp_flat)
1455{
1457 array<float> alpha;
1458 init_test_curve(curve, make_float3(0.0f, 0.25f, 0.5f), make_float3(0.25f, 0.5f, 0.75f), 9);
1459 init_test_curve(alpha, 0.75f, 1.0f, 9);
1460
1461 builder
1462 .add_node(ShaderNodeBuilder<RGBRampNode>(graph, "Ramp")
1463 .set_param("ramp", curve)
1464 .set_param("ramp_alpha", alpha)
1465 .set_param("interpolate", false)
1466 .set("Fac", 0.56f))
1467 .add_node(ShaderNodeBuilder<MixNode>(graph, "Mix").set_param("mix_type", NODE_MIX_ADD))
1468 .add_connection("Ramp::Color", "Mix::Color1")
1469 .add_connection("Ramp::Alpha", "Mix::Color2")
1470 .output_color("Mix::Color");
1471
1472 graph.finalize(scene.get());
1473
1474 log.correct_info_message("Folding Ramp::Color to constant (0.125, 0.375, 0.625).");
1475 log.correct_info_message("Folding Ramp::Alpha to constant (0.875).");
1476}
1477
1478/*
1479 * Tests:
1480 * - Folding of redundant conversion of float to color to float.
1481 */
1482TEST_F(RenderGraph, constant_fold_convert_float_color_float)
1483{
1484 builder.add_attribute("Attribute")
1485 .add_node(ShaderNodeBuilder<InvertNode>(graph, "Invert").set("Fac", 0.0f))
1486 .add_connection("Attribute::Fac", "Invert::Color")
1487 .output_value("Invert::Color");
1488
1489 graph.finalize(scene.get());
1490
1491 log.correct_info_message("Folding Invert::Color to socket convert_float_to_color::value_color.");
1492 log.correct_info_message(
1493 "Folding convert_color_to_float::value_float to socket Attribute::Fac.");
1494}
1495
1496/*
1497 * Tests:
1498 * - Folding of redundant conversion of color to vector to color.
1499 */
1500TEST_F(RenderGraph, constant_fold_convert_color_vector_color)
1501{
1502 builder.add_attribute("Attribute")
1503 .add_node(ShaderNodeBuilder<VectorMathNode>(graph, "VecAdd")
1504 .set_param("math_type", NODE_VECTOR_MATH_ADD)
1505 .set("Vector2", make_float3(0, 0, 0)))
1506 .add_connection("Attribute::Color", "VecAdd::Vector1")
1507 .output_color("VecAdd::Vector");
1508
1509 graph.finalize(scene.get());
1510
1511 log.correct_info_message(
1512 "Folding VecAdd::Vector to socket convert_color_to_vector::value_vector.");
1513 log.correct_info_message(
1514 "Folding convert_vector_to_color::value_color to socket Attribute::Color.");
1515}
1516
1517/*
1518 * Tests:
1519 * - NOT folding conversion of color to float to color.
1520 */
1521TEST_F(RenderGraph, constant_fold_convert_color_float_color)
1522{
1523 builder.add_attribute("Attribute")
1524 .add_node(ShaderNodeBuilder<MathNode>(graph, "MathAdd")
1525 .set_param("math_type", NODE_MATH_ADD)
1526 .set("Value2", 0.0f))
1527 .add_connection("Attribute::Color", "MathAdd::Value1")
1528 .output_color("MathAdd::Value");
1529
1530 graph.finalize(scene.get());
1531
1532 log.correct_info_message(
1533 "Folding MathAdd::Value to socket convert_color_to_float::value_float.");
1534 log.invalid_info_message("Folding convert_float_to_color::");
1535}
1536
1537/*
1538 * Tests:
1539 * - Stochastic sampling with math multiply node.
1540 */
1541TEST_F(RenderGraph, stochastic_sample_math_multiply)
1542{
1543 builder.add_attribute("Attribute")
1544 .add_node(ShaderNodeBuilder<MathNode>(graph, "MathMultiply")
1545 .set_param("math_type", NODE_MATH_MULTIPLY))
1546 .add_node(ShaderNodeBuilder<ScatterVolumeNode>(graph, "ScatterVolume"))
1547 .add_connection("Attribute::Fac", "MathMultiply::Value1")
1548 .add_connection("MathMultiply::Value", "ScatterVolume::Density")
1549 .output_volume_closure("ScatterVolume::Volume");
1550
1551 graph.finalize(scene.get());
1552
1553 log.correct_info_message("Volume attribute node Attribute uses stochastic sampling");
1554}
1555
1556/*
1557 * Tests:
1558 * - No stochastic sampling with math power node.
1559 */
1560TEST_F(RenderGraph, not_stochastic_sample_math_power)
1561{
1562 builder.add_attribute("Attribute")
1563 .add_node(
1564 ShaderNodeBuilder<MathNode>(graph, "MathPower").set_param("math_type", NODE_MATH_POWER))
1565 .add_node(ShaderNodeBuilder<ScatterVolumeNode>(graph, "ScatterVolume"))
1566 .add_connection("Attribute::Fac", "MathPower::Value1")
1567 .add_connection("MathPower::Value", "ScatterVolume::Density")
1568 .output_volume_closure("ScatterVolume::Volume");
1569
1570 graph.finalize(scene.get());
1571
1572 log.invalid_info_message("Volume attribute node Attribute uses stochastic sampling");
1573}
1574
1575/*
1576 * Tests:
1577 * - Stochastic sampling temperature with map range, principled volume and mix closure.
1578 */
1579TEST_F(RenderGraph, stochastic_sample_principled_volume_mix)
1580{
1581 builder.add_attribute("Attribute")
1582 .add_node(ShaderNodeBuilder<MapRangeNode>(graph, "MapRange"))
1583 .add_node(ShaderNodeBuilder<MixClosureNode>(graph, "MixClosure").set("Fac", 0.5f))
1584 .add_node(ShaderNodeBuilder<PrincipledVolumeNode>(graph, "PrincipledVolume1"))
1585 .add_node(ShaderNodeBuilder<PrincipledVolumeNode>(graph, "PrincipledVolume2"))
1586 .add_connection("Attribute::Color", "MapRange::Value")
1587 .add_connection("MapRange::Result", "PrincipledVolume1::Temperature")
1588 .add_connection("Attribute::Fac", "PrincipledVolume2::Density")
1589 .add_connection("PrincipledVolume1::Volume", "MixClosure::Closure1")
1590 .add_connection("PrincipledVolume2::Volume", "MixClosure::Closure2")
1591 .output_volume_closure("MixClosure::Closure");
1592
1593 graph.finalize(scene.get());
1594
1595 log.correct_info_message("Volume attribute node Attribute uses stochastic sampling");
1596}
1597
volatile int lock
ShaderGraphBuilder & add_connection(const string &from, const string &to)
ShaderGraphBuilder & output_value(const string &from)
ShaderGraphBuilder & output_closure(const string &from)
ShaderGraphBuilder & add_node(const T &node)
ShaderGraphBuilder & output_volume_closure(const string &from)
ShaderGraphBuilder & add_attribute(const string &name)
ShaderGraphBuilder & output_color(const string &from)
ShaderNodeBuilder & set(const string &input_name, V value)
ShaderNodeBuilder & set_param(const string &input_name, V value)
ShaderNodeBuilder(ShaderGraph &graph, const string &name)
static void init_fallback_config()
static unique_ptr< Device > create(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless)
ShaderGraphBuilder builder
unique_ptr< Scene > scene
unique_ptr< Device > device_cpu
OutputNode * output()
T * create_node(Args &&...args)
void set(const float f)
ShaderInput * input(const char *name)
ShaderOutput * output(const char *name)
T * resize(const size_t newsize)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
TEST_F(FFmpegTest, libaom_av1_AV_PIX_FMT_YUV420P)
#define log
#define EXPECT_NE(result, expect)
#define EXPECT_EQ(result, expect)
@ NODE_COMBSEP_COLOR_RGB
@ NODE_COMBSEP_COLOR_HSV
NodeMathType
@ NODE_MATH_DIVIDE
@ NODE_MATH_POWER
@ NODE_MATH_ADD
@ NODE_MATH_MULTIPLY
@ NODE_MATH_SUBTRACT
@ NODE_MIX_DIV
@ NODE_MIX_LIGHT
@ NODE_MIX_MUL
@ NODE_MIX_BURN
@ NODE_MIX_SUB
@ NODE_MIX_BLEND
@ NODE_MIX_DODGE
@ NODE_MIX_ADD
NodeVectorMathType
@ NODE_VECTOR_MATH_CROSS_PRODUCT
@ NODE_VECTOR_MATH_ADD
@ NODE_VECTOR_MATH_SUBTRACT
void log_init(const LogFunction func)
Definition log.cpp:72
void log_level_set(const LogLevel level)
Definition log.cpp:78
#define LOG_FATAL
Definition log.h:99
LogLevel
Definition log.h:17
@ LOG_LEVEL_FATAL
Definition log.h:18
@ LOG_LEVEL_TRACE
Definition log.h:27
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define T
#define mix
void init_test_curve(array< T > &buffer, T start, T end, const int steps)
static void build_math_partial_test_graph(ShaderGraphBuilder &builder, NodeMathType type, const float constval)
static void build_mix_partial_test_graph(ShaderGraphBuilder &builder, NodeMix type, const float3 constval)
static void build_vecmath_partial_test_graph(ShaderGraphBuilder &builder, NodeVectorMathType type, const float3 constval)
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:72
Type type
Definition node_type.h:85
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
CCL_NAMESPACE_BEGIN struct Window V