[Bf-blender-cvs] [dd6a0dc] object_nodes: Basic image sampling node for reading interpolated color values from an Image datablock.

Lukas Tönne noreply at git.blender.org
Tue Jan 19 23:30:37 CET 2016


Commit: dd6a0dc91085c6c5ce5b837b58b82bf729e5c227
Author: Lukas Tönne
Date:   Tue Jan 19 23:29:51 2016 +0100
Branches: object_nodes
https://developer.blender.org/rBdd6a0dc91085c6c5ce5b837b58b82bf729e5c227

Basic image sampling node for reading interpolated color values from an Image datablock.

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

M	release/scripts/nodes/common_nodes.py
M	release/scripts/nodes/geometry_nodes.py
M	source/blender/blenvm/bvm/CMakeLists.txt
M	source/blender/blenvm/bvm/bvm_eval.cc
M	source/blender/blenvm/bvm/bvm_eval.h
A	source/blender/blenvm/bvm/bvm_eval_image.h
M	source/blender/blenvm/bvm/bvm_opcode.h
M	source/blender/blenvm/compile/bvm_nodegraph.cc
M	source/blender/blenvm/intern/bvm_api.cc
M	source/blender/depsgraph/DEG_depsgraph_build.h
M	source/blender/depsgraph/intern/depsgraph_build.cc
M	source/blender/depsgraph/intern/depsgraph_build.h
M	source/blender/depsgraph/intern/depsgraph_build_nodes.cc
M	source/blender/depsgraph/intern/depsgraph_build_relations.cc
M	source/blender/depsgraph/intern/depsnode_opcodes.h
M	source/blender/makesrna/intern/rna_depsgraph.c

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

diff --git a/release/scripts/nodes/common_nodes.py b/release/scripts/nodes/common_nodes.py
index 5196834..b5ef3b1 100644
--- a/release/scripts/nodes/common_nodes.py
+++ b/release/scripts/nodes/common_nodes.py
@@ -958,6 +958,36 @@ class TextureVoronoiNode(CommonNodeBase, ObjectNode):
 
 ###############################################################################
 
+class ImageSampleNode(CommonNodeBase, ObjectNode):
+    '''Sample an image'''
+    bl_idname = 'ImageSampleNode'
+    bl_label = 'Image Sample'
+
+    bl_id_property_type = 'IMAGE'
+
+    def draw_buttons(self, context, layout):
+        layout.template_ID(self, "id")
+
+    def eval_dependencies(self, depsnode):
+        ima = self.id
+        if ima:
+            depsnode.add_image_relation(ima, 'PARAMETERS')
+
+    def init(self, context):
+        self.inputs.new('NodeSocketVector', "UV")
+        self.outputs.new('NodeSocketColor', "Color")
+
+    def compile(self, compiler):
+        if self.id is None:
+            return
+        ima = compiler.get_id_key(self.id)
+        node = compiler.add_node("IMAGE_SAMPLE")
+        node.inputs[0].set_value(ima)
+        compiler.map_input(0, node.inputs[1])
+        compiler.map_output(0, node.outputs[0])
+
+###############################################################################
+
 def register():
     bpy.utils.register_module(__name__)
 
diff --git a/release/scripts/nodes/geometry_nodes.py b/release/scripts/nodes/geometry_nodes.py
index 5122a1c..7ddc266 100644
--- a/release/scripts/nodes/geometry_nodes.py
+++ b/release/scripts/nodes/geometry_nodes.py
@@ -469,6 +469,7 @@ def register():
             NodeItem("GeometryClosestPointNode"),
             ]),
         GeometryNodeCategory("GEO_TEXTURE", "Texture", items=[
+            NodeItem("ImageSampleNode"),
             NodeItem("ObjectTextureCloudsNode"),
             NodeItem("ObjectTextureDistNoiseNode"),
             NodeItem("ObjectTextureMagicNode"),
diff --git a/source/blender/blenvm/bvm/CMakeLists.txt b/source/blender/blenvm/bvm/CMakeLists.txt
index fd8add6..6f93d9b 100644
--- a/source/blender/blenvm/bvm/CMakeLists.txt
+++ b/source/blender/blenvm/bvm/CMakeLists.txt
@@ -30,6 +30,7 @@ set(INC
 	../../blenkernel
 	../../blenlib
 	../../bmesh
+	../../imbuf
 	../../makesdna
 	../../makesrna
 	../../../../intern/guardedalloc
@@ -43,6 +44,7 @@ set(SRC
 	bvm_eval.h
 	bvm_eval_common.h
 	bvm_eval_curve.h
+	bvm_eval_image.h
 	bvm_eval_math.h
 	bvm_eval_mesh.h
 	bvm_eval_texture.h
diff --git a/source/blender/blenvm/bvm/bvm_eval.cc b/source/blender/blenvm/bvm/bvm_eval.cc
index e4cb57a..916844c 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -41,12 +41,16 @@ extern "C" {
 #include "BKE_bvhutils.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_DerivedMesh.h"
+#include "BKE_image.h"
 #include "BKE_material.h"
+
+#include "IMB_imbuf_types.h"
 }
 
 #include "bvm_eval.h"
 #include "bvm_eval_common.h"
 #include "bvm_eval_curve.h"
+#include "bvm_eval_image.h"
 #include "bvm_eval_math.h"
 #include "bvm_eval_mesh.h"
 #include "bvm_eval_texture.h"
@@ -55,6 +59,16 @@ extern "C" {
 
 namespace bvm {
 
+EvalGlobals::EvalGlobals()
+{
+	m_image_pool = BKE_image_pool_new();
+}
+
+EvalGlobals::~EvalGlobals()
+{
+	BKE_image_pool_free(m_image_pool);
+}
+
 int EvalGlobals::get_id_key(ID *id)
 {
 	int hash = BLI_ghashutil_strhash(id->name);
@@ -82,6 +96,31 @@ PointerRNA EvalGlobals::lookup_object(int key) const
 	}
 }
 
+void EvalGlobals::add_image(int key, Image *ima)
+{
+	m_images[key] = ima;
+}
+
+ImBuf *EvalGlobals::lookup_imbuf(int key, ImageUser *iuser) const
+{
+	ImageMap::const_iterator ima_it = m_images.find(key);
+	Image *ima = (ima_it != m_images.end()) ? ima_it->second : NULL;
+	if (!ima)
+		return NULL;
+	
+	/* local changes to the original ImageUser */
+//	if (!BKE_image_is_multilayer(ima))
+//		iuser->multi_index = BKE_scene_multiview_view_id_get(this->m_rd, this->m_viewName);
+	
+	ImBuf *ibuf = BKE_image_pool_acquire_ibuf(ima, iuser, m_image_pool);
+	if (!ibuf || (!ibuf->rect && !ibuf->rect_float)) {
+		BKE_image_pool_release_ibuf(ima, ibuf, m_image_pool);
+		return NULL;
+	}
+	
+	return ibuf;
+}
+
 /* ------------------------------------------------------------------------- */
 
 int EvalStack::stack_size(size_t datasize)
@@ -1094,6 +1133,14 @@ void EvalContext::eval_instructions(const EvalGlobals *globals, const Instructio
 				break;
 			}
 			
+			case OP_IMAGE_SAMPLE: {
+				StackIndex offset_image = fn->read_stack_index(&instr);
+				StackIndex offset_uv = fn->read_stack_index(&instr);
+				StackIndex offset_color = fn->read_stack_index(&instr);
+				eval_op_image_sample(globals, stack, offset_image, offset_uv, offset_color);
+				break;
+			}
+			
 			case OP_MAKE_DUPLI: {
 				StackIndex offset_object = fn->read_stack_index(&instr);
 				StackIndex offset_transform = fn->read_stack_index(&instr);
diff --git a/source/blender/blenvm/bvm/bvm_eval.h b/source/blender/blenvm/bvm/bvm_eval.h
index 2e167f7..40095f2 100644
--- a/source/blender/blenvm/bvm/bvm_eval.h
+++ b/source/blender/blenvm/bvm/bvm_eval.h
@@ -32,6 +32,7 @@
  *  \ingroup bvm
  */
 
+#include <set>
 #include <vector>
 
 #include "MEM_guardedalloc.h"
@@ -44,6 +45,10 @@ extern "C" {
 #include "bvm_util_string.h"
 
 struct ID;
+struct Image;
+struct ImagePool;
+struct ImageUser;
+struct ImBuf;
 struct Object;
 
 namespace bvm {
@@ -54,14 +59,23 @@ struct InstructionList;
 
 struct EvalGlobals {
 	typedef unordered_map<int, Object *> ObjectMap;
+	typedef unordered_map<int, Image *> ImageMap;
+	
+	EvalGlobals();
+	~EvalGlobals();
 	
 	static int get_id_key(ID *id);
 	
 	void add_object(int key, Object *ob);
 	PointerRNA lookup_object(int key) const;
 	
+	void add_image(int key, Image *ima);
+	ImBuf *lookup_imbuf(int key, ImageUser *iuser) const;
+	
 private:
 	ObjectMap m_objects;
+	ImageMap m_images;
+	ImagePool *m_image_pool;
 
 	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:EvalGlobals")
 };
diff --git a/source/blender/blenvm/bvm/bvm_eval_image.h b/source/blender/blenvm/bvm/bvm_eval_image.h
new file mode 100644
index 0000000..041d905
--- /dev/null
+++ b/source/blender/blenvm/bvm/bvm_eval_image.h
@@ -0,0 +1,104 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Lukas Toenne
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef __BVM_EVAL_IMAGE_H__
+#define __BVM_EVAL_IMAGE_H__
+
+/** \file bvm_eval_image.h
+ *  \ingroup bvm
+ */
+
+extern "C" {
+#include "BLI_math_color.h"
+
+#include "DNA_image_types.h"
+
+#include "BKE_image.h"
+#include "BKE_scene.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "IMB_colormanagement.h"
+}
+
+#include "bvm_eval_common.h"
+
+#include "bvm_util_math.h"
+
+namespace bvm {
+
+/* nearest sampling mode */
+inline static void imbuf_sample_nearest(struct ImBuf *ibuf, const float3 &uv, float4 &color)
+{
+	/* ImBuf in must have a valid rect or rect_float, assume this is already checked */
+	
+	int x1 = (int)uv.x;
+	int y1 = (int)uv.y;
+	
+	/* sample area entirely outside image? */
+	if (x1 < 0 || x1 > ibuf->x - 1 || y1 < 0 || y1 > ibuf->y - 1) {
+		color.x = color.y = color.z = color.w = 0.0f;
+		return;
+	}
+	
+	if (ibuf->rect_float) {
+		const float *data = ibuf->rect_float + ((size_t)ibuf->x * y1 + x1) * 4;
+		copy_v4_v4(color.data(), data);
+	}
+	else {
+		const unsigned char *data = (unsigned char *)ibuf->rect + ((size_t)ibuf->x * y1 + x1) * 4;
+		rgba_uchar_to_float(color.data(), data);
+	}
+}
+
+static void eval_op_image_sample(const EvalGlobals *globals,
+                                 EvalStack *stack,
+                                 StackIndex offset_image,
+                                 StackIndex offset_uv,
+                                 StackIndex offset_color)
+{
+	float4 color(0.0f, 0.0f, 0.0f, 0.0f);
+	
+	int ima_key = stack_load_int(stack, offset_image);
+	
+	/* TODO just a dummy ImageUser for now */
+	ImageUser iuser = {0};
+	iuser.ok = true;
+	
+	ImBuf *ibuf = globals->lookup_imbuf(ima_key, &iuser);
+	if (ibuf) {
+		float3 uv = stack_load_float3(stack, offset_uv);
+		
+		imbuf_sample_nearest(ibuf, uv, color);
+	}
+	
+	stack_store_float4(stack, offset_color, color);
+}
+
+} /* namespace bvm */
+
+#endif /* __BVM_EVAL_IMAGE_H__ */
diff --git a/source/blender/blenvm/bvm/bvm_opcode.h b/source/blender/blenvm/bvm/bvm_opcode.h
index b635ec3..abaad0d 100644
--- a/source/blender/blenvm/bvm/bvm_opcode.h
+++ b/source/blender/blenvm/bvm/bvm_opcode.h
@@ -152,6 +152,8 @@ namespace bvm {
 	\
 	DEF_OPCODE(CURVE_PATH) \
 	\
+	DEF_OPCODE(IMAGE_SAMPLE) \
+	\
 	DEF_OPCODE(MAKE_DUPLI) \
 	DEF_OPCODE(DUPLIS_COMBINE) \
 	\
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.cc b/source/blender/blenvm/compile/bvm_nodegraph.cc
index 98cafa7..17751f2 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.cc
+++ b/source/blender/blenvm/compile/bvm_nodegraph.cc
@@ -1358,6 +1358,8 @@ OpCode get_opcode_from_node_type(const string &node)
 	
 	NODETYPE(CURVE_PATH);
 	
+	NODETYPE(IMAGE_SAMPLE);
+	
 	NODETYPE(MAKE_DUPLI);
 	NODETYPE(DUPLIS_COMBINE);
 	
@@ -1837,6 +1839,11 @@ static void register_opcode_node_types()
 	nt->add_input("duplis_b", "DUPLIS", __empty_duplilist__);
 	nt->add_output("duplis", "DUPLIS");
 	
+	nt = NodeGraph::add_function_node_type("IMAGE_SAMPLE");
+	nt->add_input("image", "INT", 0);
+	nt->add_input("uv", "FLOAT3", float3(0.0f, 0.0f, 0.0f))

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list