[Bf-blender-cvs] [1a134c4c30a] master: Cycles: refactor API for render output

Brecht Van Lommel noreply at git.blender.org
Thu Sep 30 20:59:15 CEST 2021


Commit: 1a134c4c30a643ada1b9a7a037040b5f5c173a28
Author: Brecht Van Lommel
Date:   Thu Sep 30 16:51:03 2021 +0200
Branches: master
https://developer.blender.org/rB1a134c4c30a643ada1b9a7a037040b5f5c173a28

Cycles: refactor API for render output

* Add OutputDriver, replacing function callbacks in Session.
* Add PathTraceTile, replacing tile access methods in Session.
* Add more detailed comments about how this driver should be implemented.
* Add OIIOOutputDriver for Cycles standalone to output an image.

Differential Revision: https://developer.blender.org/D12627

===================================================================

M	intern/cycles/app/CMakeLists.txt
M	intern/cycles/app/cycles_standalone.cpp
M	intern/cycles/app/cycles_xml.cpp
A	intern/cycles/app/oiio_output_driver.cpp
A	intern/cycles/app/oiio_output_driver.h
M	intern/cycles/blender/CMakeLists.txt
A	intern/cycles/blender/blender_output_driver.cpp
A	intern/cycles/blender/blender_output_driver.h
M	intern/cycles/blender/blender_session.cpp
M	intern/cycles/blender/blender_session.h
M	intern/cycles/integrator/CMakeLists.txt
M	intern/cycles/integrator/path_trace.cpp
M	intern/cycles/integrator/path_trace.h
A	intern/cycles/integrator/path_trace_tile.cpp
A	intern/cycles/integrator/path_trace_tile.h
M	intern/cycles/render/CMakeLists.txt
A	intern/cycles/render/output_driver.h
M	intern/cycles/render/session.cpp
M	intern/cycles/render/session.h
M	intern/cycles/render/tile.cpp
M	intern/cycles/render/tile.h

===================================================================

diff --git a/intern/cycles/app/CMakeLists.txt b/intern/cycles/app/CMakeLists.txt
index f9dc5f00802..3ed3f54ef9f 100644
--- a/intern/cycles/app/CMakeLists.txt
+++ b/intern/cycles/app/CMakeLists.txt
@@ -64,6 +64,8 @@ if(WITH_CYCLES_STANDALONE)
     cycles_standalone.cpp
     cycles_xml.cpp
     cycles_xml.h
+    oiio_output_driver.cpp
+    oiio_output_driver.h
   )
   add_executable(cycles ${SRC} ${INC} ${INC_SYS})
   unset(SRC)
@@ -73,7 +75,7 @@ if(WITH_CYCLES_STANDALONE)
 
   if(APPLE)
     if(WITH_OPENCOLORIO)
-      set_property(TARGET cycles APPEND_STRING PROPERTY LINK_FLAGS " -framework IOKit")
+      set_property(TARGET cycles APPEND_STRING PROPERTY LINK_FLAGS " -framework IOKit -framework Carbon")
     endif()
     if(WITH_OPENIMAGEDENOISE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "arm64")
       # OpenImageDenoise uses BNNS from the Accelerate framework.
diff --git a/intern/cycles/app/cycles_standalone.cpp b/intern/cycles/app/cycles_standalone.cpp
index 258e67b3459..00dc140648a 100644
--- a/intern/cycles/app/cycles_standalone.cpp
+++ b/intern/cycles/app/cycles_standalone.cpp
@@ -36,6 +36,9 @@
 #include "util/util_unique_ptr.h"
 #include "util/util_version.h"
 
+#include "app/cycles_xml.h"
+#include "app/oiio_output_driver.h"
+
 #ifdef WITH_CYCLES_STANDALONE_GUI
 #  include "util/util_view.h"
 #endif
@@ -54,6 +57,7 @@ struct Options {
   bool quiet;
   bool show_help, interactive, pause;
   string output_filepath;
+  string output_pass;
 } options;
 
 static void session_print(const string &str)
@@ -89,30 +93,6 @@ static void session_print_status()
   session_print(status);
 }
 
-static bool write_render(const uchar *pixels, int w, int h, int channels)
-{
-  string msg = string_printf("Writing image %s", options.output_path.c_str());
-  session_print(msg);
-
-  unique_ptr<ImageOutput> out = unique_ptr<ImageOutput>(ImageOutput::create(options.output_path));
-  if (!out) {
-    return false;
-  }
-
-  ImageSpec spec(w, h, channels, TypeDesc::UINT8);
-  if (!out->open(options.output_path, spec)) {
-    return false;
-  }
-
-  /* conversion for different top/bottom convention */
-  out->write_image(
-      TypeDesc::UINT8, pixels + (h - 1) * w * channels, AutoStride, -w * channels, AutoStride);
-
-  out->close();
-
-  return true;
-}
-
 static BufferParams &session_buffer_params()
 {
   static BufferParams buffer_params;
@@ -147,9 +127,14 @@ static void scene_init()
 
 static void session_init()
 {
-  options.session_params.write_render_cb = write_render;
+  options.output_pass = "combined";
   options.session = new Session(options.session_params, options.scene_params);
 
+  if (!options.output_filepath.empty()) {
+    options.session->set_output_driver(make_unique<OIIOOutputDriver>(
+        options.output_filepath, options.output_pass, session_print));
+  }
+
   if (options.session_params.background && !options.quiet)
     options.session->progress.set_update_callback(function_bind(&session_print_status));
 #ifdef WITH_CYCLES_STANDALONE_GUI
@@ -160,6 +145,11 @@ static void session_init()
   /* load scene */
   scene_init();
 
+  /* add pass for output. */
+  Pass *pass = options.scene->create_node<Pass>();
+  pass->set_name(ustring(options.output_pass.c_str()));
+  pass->set_type(PASS_COMBINED);
+
   options.session->reset(options.session_params, session_buffer_params());
   options.session->start();
 }
diff --git a/intern/cycles/app/cycles_xml.cpp b/intern/cycles/app/cycles_xml.cpp
index 54f97fddbd9..0b83c60f32d 100644
--- a/intern/cycles/app/cycles_xml.cpp
+++ b/intern/cycles/app/cycles_xml.cpp
@@ -333,6 +333,7 @@ static void xml_read_shader_graph(XMLReadState &state, Shader *shader, xml_node
       }
 
       snode = (ShaderNode *)node_type->create(node_type);
+      snode->set_owner(graph);
     }
 
     xml_read_node(graph_reader, snode, node);
diff --git a/intern/cycles/app/oiio_output_driver.cpp b/intern/cycles/app/oiio_output_driver.cpp
new file mode 100644
index 00000000000..d791c89772f
--- /dev/null
+++ b/intern/cycles/app/oiio_output_driver.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2021 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "app/oiio_output_driver.h"
+
+CCL_NAMESPACE_BEGIN
+
+OIIOOutputDriver::OIIOOutputDriver(const string_view filepath,
+                                   const string_view pass,
+                                   LogFunction log)
+    : filepath_(filepath), pass_(pass), log_(log)
+{
+}
+
+OIIOOutputDriver::~OIIOOutputDriver()
+{
+}
+
+void OIIOOutputDriver::write_render_tile(const Tile &tile)
+{
+  /* Only write the full buffer, no intermediate tiles. */
+  if (!(tile.size == tile.full_size)) {
+    return;
+  }
+
+  log_(string_printf("Writing image %s", filepath_.c_str()));
+
+  unique_ptr<ImageOutput> image_output(ImageOutput::create(filepath_));
+  if (image_output == nullptr) {
+    log_("Failed to create image file");
+    return;
+  }
+
+  const int width = tile.size.x;
+  const int height = tile.size.y;
+
+  ImageSpec spec(width, height, 4, TypeDesc::FLOAT);
+  if (!image_output->open(filepath_, spec)) {
+    log_("Failed to create image file");
+    return;
+  }
+
+  vector<float> pixels(width * height * 4);
+  if (!tile.get_pass_pixels(pass_, 4, pixels.data())) {
+    log_("Failed to read render pass pixels");
+    return;
+  }
+
+  /* Manipulate offset and stride to convert from bottom-up to top-down convention. */
+  image_output->write_image(TypeDesc::FLOAT,
+                            pixels.data() + (height - 1) * width * 4,
+                            AutoStride,
+                            -width * 4 * sizeof(float),
+                            AutoStride);
+  image_output->close();
+}
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/app/oiio_output_driver.h b/intern/cycles/app/oiio_output_driver.h
new file mode 100644
index 00000000000..cdc4085d962
--- /dev/null
+++ b/intern/cycles/app/oiio_output_driver.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2021 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "render/output_driver.h"
+
+#include "util/util_function.h"
+#include "util/util_image.h"
+#include "util/util_string.h"
+#include "util/util_unique_ptr.h"
+#include "util/util_vector.h"
+
+CCL_NAMESPACE_BEGIN
+
+class OIIOOutputDriver : public OutputDriver {
+ public:
+  typedef function<void(const string &)> LogFunction;
+
+  OIIOOutputDriver(const string_view filepath, const string_view pass, LogFunction log);
+  virtual ~OIIOOutputDriver();
+
+  void write_render_tile(const Tile &tile) override;
+
+ protected:
+  string filepath_;
+  string pass_;
+  LogFunction log_;
+};
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/blender/CMakeLists.txt b/intern/cycles/blender/CMakeLists.txt
index 2660eee017b..a0442b3394b 100644
--- a/intern/cycles/blender/CMakeLists.txt
+++ b/intern/cycles/blender/CMakeLists.txt
@@ -38,6 +38,7 @@ set(SRC
   blender_mesh.cpp
   blender_object.cpp
   blender_object_cull.cpp
+  blender_output_driver.cpp
   blender_particles.cpp
   blender_curves.cpp
   blender_logging.cpp
@@ -55,6 +56,7 @@ set(SRC
   blender_id_map.h
   blender_image.h
   blender_object_cull.h
+  blender_output_driver.h
   blender_sync.h
   blender_session.h
   blender_texture.h
diff --git a/intern/cycles/blender/blender_output_driver.cpp b/intern/cycles/blender/blender_output_driver.cpp
new file mode 100644
index 00000000000..f380b7b3bb1
--- /dev/null
+++ b/intern/cycles/blender/blender_output_driver.cpp
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2021 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "blender/blender_output_driver.h"
+
+CCL_NAMESPACE_BEGIN
+
+BlenderOutputDriver::BlenderOutputDriver(BL::RenderEngine &b_engine) : b_engine_(b_engine)
+{
+}
+
+BlenderOutputDriver::~BlenderOutputDriver()
+{
+}
+
+bool BlenderOutputDriver::read_render_tile(const Tile &tile)
+{
+  /* Get render result. */
+  BL::RenderResult b_rr = b_engine_.begin_result(tile.offset.x,
+                                                 tile.offset.y,
+                                                 tile.size.x,
+                                                 tile.size.y,
+                                                 tile.layer.c_str(),
+                                                 tile.view.c_str());
+
+  /* Can happen if the intersected rectangle gives 0 width or height. */
+  if (b_rr.ptr.data == NULL) {
+    return false;
+  }
+
+  BL::RenderResult::layers_iterator b_single_rlay;
+  b_rr.layers.begin(b_single_rlay);
+
+  /* layer will be missing if it was disabled in the UI */
+  if (b_single_rlay == b_rr.layers.end()) {
+    return false;
+  }
+
+  BL::RenderLayer b_rlay = *b_single_rlay;
+
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list