[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43567] trunk/blender/intern/cycles: Sample as Lamp option for world shaders, to enable multiple importance sampling.

Brecht Van Lommel brechtvanlommel at pandora.be
Fri Jan 20 18:49:27 CET 2012


Revision: 43567
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43567
Author:   blendix
Date:     2012-01-20 17:49:17 +0000 (Fri, 20 Jan 2012)
Log Message:
-----------
Sample as Lamp option for world shaders, to enable multiple importance sampling.

By default lighting from the world is computed solely with indirect light
sampling. However for more complex environment maps this can be too noisy, as
sampling the BSDF may not easily find the highlights in the environment map
image. By enabling this option, the world background will be sampled as a lamp,
with lighter parts automatically given more samples.

Map Resolution specifies the size of the importance map (res x res). Before
rendering starts, an importance map is generated by "baking" a grayscale image
from the world shader. This will then be used to determine which parts of the
background are light and so should receive more samples than darker parts.
Higher resolutions will result in more accurate sampling but take more setup
time and memory.

Patch by Mike Farnsworth, thanks!

Modified Paths:
--------------
    trunk/blender/intern/cycles/blender/addon/properties.py
    trunk/blender/intern/cycles/blender/addon/ui.py
    trunk/blender/intern/cycles/blender/blender_object.cpp
    trunk/blender/intern/cycles/blender/blender_shader.cpp
    trunk/blender/intern/cycles/blender/blender_sync.h
    trunk/blender/intern/cycles/device/device_cpu.cpp
    trunk/blender/intern/cycles/kernel/kernel.cl
    trunk/blender/intern/cycles/kernel/kernel.cpp
    trunk/blender/intern/cycles/kernel/kernel.cu
    trunk/blender/intern/cycles/kernel/kernel.h
    trunk/blender/intern/cycles/kernel/kernel_compat_cpu.h
    trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h
    trunk/blender/intern/cycles/kernel/kernel_differential.h
    trunk/blender/intern/cycles/kernel/kernel_displace.h
    trunk/blender/intern/cycles/kernel/kernel_emission.h
    trunk/blender/intern/cycles/kernel/kernel_light.h
    trunk/blender/intern/cycles/kernel/kernel_montecarlo.h
    trunk/blender/intern/cycles/kernel/kernel_optimized.cpp
    trunk/blender/intern/cycles/kernel/kernel_path.h
    trunk/blender/intern/cycles/kernel/kernel_textures.h
    trunk/blender/intern/cycles/kernel/kernel_types.h
    trunk/blender/intern/cycles/kernel/svm/svm_image.h
    trunk/blender/intern/cycles/render/light.cpp
    trunk/blender/intern/cycles/render/light.h
    trunk/blender/intern/cycles/render/mesh_displace.cpp
    trunk/blender/intern/cycles/render/scene.h

Modified: trunk/blender/intern/cycles/blender/addon/properties.py
===================================================================
--- trunk/blender/intern/cycles/blender/addon/properties.py	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/blender/addon/properties.py	2012-01-20 17:49:17 UTC (rev 43567)
@@ -155,6 +155,10 @@
     @classmethod
     def register(cls):
         bpy.types.World.cycles = PointerProperty(type=cls, name="Cycles World Settings", description="Cycles world settings")
+        cls.sample_as_light = BoolProperty(name="Sample as Lamp", description="Use direct light sampling for the environment, enabling for non-solid colors is recommended",
+            default=False)
+        cls.sample_map_resolution = IntProperty(name="Map Resolution", description="Importance map size is resolution x resolution; higher values potentially produce less noise, at the cost of memory and speed",
+            default=256, min=4, max=8096)
 
     @classmethod
     def unregister(cls):

Modified: trunk/blender/intern/cycles/blender/addon/ui.py
===================================================================
--- trunk/blender/intern/cycles/blender/addon/ui.py	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/blender/addon/ui.py	2012-01-20 17:49:17 UTC (rev 43567)
@@ -453,10 +453,38 @@
         layout = self.layout
 
         world = context.world
+
         if not panel_node_draw(layout, world, 'OUTPUT_WORLD', 'Surface'):
             layout.prop(world, "horizon_color", text="Color")
 
 
+class CyclesWorld_PT_settings(CyclesButtonsPanel, Panel):
+    bl_label = "Settings"
+    bl_context = "world"
+    bl_options = {'DEFAULT_CLOSED'}
+
+    @classmethod
+    def poll(cls, context):
+        return context.world and CyclesButtonsPanel.poll(context)
+
+    def draw(self, context):
+        layout = self.layout
+
+        world = context.world
+        cworld = world.cycles
+
+        split = layout.split()
+        col = split.column()
+
+        col.prop(cworld, "sample_as_light")
+        row = col.row()
+        row.active = cworld.sample_as_light
+        row.prop(cworld, "sample_map_resolution")
+
+        col = split.column()
+        col.label()
+
+
 class CyclesWorld_PT_volume(CyclesButtonsPanel, Panel):
     bl_label = "Volume"
     bl_context = "world"

Modified: trunk/blender/intern/cycles/blender/blender_object.cpp
===================================================================
--- trunk/blender/intern/cycles/blender/blender_object.cpp	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/blender/blender_object.cpp	2012-01-20 17:49:17 UTC (rev 43567)
@@ -16,10 +16,13 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include "graph.h"
 #include "light.h"
 #include "mesh.h"
 #include "object.h"
 #include "scene.h"
+#include "nodes.h"
+#include "shader.h"
 
 #include "blender_sync.h"
 #include "blender_util.h"
@@ -152,6 +155,35 @@
 	light->tag_update(scene);
 }
 
+void BlenderSync::sync_background_light()
+{
+	BL::World b_world = b_scene.world();
+
+	PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
+	bool sample_as_light = get_boolean(cworld, "sample_as_light");
+
+	if(sample_as_light) {
+		/* test if we need to sync */
+		Light *light;
+		ObjectKey key(b_world, 0, b_world);
+
+		if(light_map.sync(&light, b_world, b_world, key) ||
+		   world_recalc ||
+		   b_world.ptr.data != world_map)
+		{
+			light->type = LIGHT_BACKGROUND;
+			light->map_resolution  = get_int(cworld, "sample_map_resolution");
+			light->shader = scene->default_background;
+
+			light->tag_update(scene);
+			light_map.set_recalc(b_world);
+		}
+	}
+
+	world_map = b_world.ptr.data;
+	world_recalc = false;
+}
+
 /* Object */
 
 void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm, uint layer_flag)
@@ -263,6 +295,8 @@
 		}
 	}
 
+	sync_background_light();
+
 	/* handle removed data and modified pointers */
 	if(light_map.post_sync())
 		scene->light_manager->tag_update(scene);

Modified: trunk/blender/intern/cycles/blender/blender_shader.cpp
===================================================================
--- trunk/blender/intern/cycles/blender/blender_shader.cpp	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/blender/blender_shader.cpp	2012-01-20 17:49:17 UTC (rev 43567)
@@ -700,9 +700,6 @@
 
 	if(background->modified(prevbackground))
 		background->tag_update(scene);
-
-	world_map = b_world.ptr.data;
-	world_recalc = false;
 }
 
 /* Sync Lamps */

Modified: trunk/blender/intern/cycles/blender/blender_sync.h
===================================================================
--- trunk/blender/intern/cycles/blender/blender_sync.h	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/blender/blender_sync.h	2012-01-20 17:49:17 UTC (rev 43567)
@@ -80,6 +80,7 @@
 	Mesh *sync_mesh(BL::Object b_ob, bool object_updated);
 	void sync_object(BL::Object b_parent, int b_index, BL::Object b_object, Transform& tfm, uint layer_flag);
 	void sync_light(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm);
+	void sync_background_light();
 
 	/* util */
 	void find_shader(BL::ID id, vector<uint>& used_shaders, int default_shader);

Modified: trunk/blender/intern/cycles/device/device_cpu.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_cpu.cpp	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/device/device_cpu.cpp	2012-01-20 17:49:17 UTC (rev 43567)
@@ -217,7 +217,7 @@
 #ifdef WITH_OPTIMIZED_KERNEL
 		if(system_cpu_support_optimized()) {
 			for(int x = task.shader_x; x < task.shader_x + task.shader_w; x++) {
-				kernel_cpu_optimized_shader(kg, (uint4*)task.shader_input, (float3*)task.shader_output, task.shader_eval_type, x);
+				kernel_cpu_optimized_shader(kg, (uint4*)task.shader_input, (float4*)task.shader_output, task.shader_eval_type, x);
 
 				if(tasks.worker_cancel())
 					break;
@@ -227,7 +227,7 @@
 #endif
 		{
 			for(int x = task.shader_x; x < task.shader_x + task.shader_w; x++) {
-				kernel_cpu_shader(kg, (uint4*)task.shader_input, (float3*)task.shader_output, task.shader_eval_type, x);
+				kernel_cpu_shader(kg, (uint4*)task.shader_input, (float4*)task.shader_output, task.shader_eval_type, x);
 
 				if(tasks.worker_cancel())
 					break;

Modified: trunk/blender/intern/cycles/kernel/kernel.cl
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel.cl	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel.cl	2012-01-20 17:49:17 UTC (rev 43567)
@@ -80,7 +80,7 @@
 		kernel_film_tonemap(kg, rgba, buffer, sample, resolution, x, y, offset, stride);
 }
 
-/*__kernel void kernel_ocl_shader(__global uint4 *input, __global float3 *output, int type, int sx)
+/*__kernel void kernel_ocl_shader(__global uint4 *input, __global float *output, int type, int sx)
 {
 	int x = sx + get_global_id(0);
 

Modified: trunk/blender/intern/cycles/kernel/kernel.cpp
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel.cpp	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel.cpp	2012-01-20 17:49:17 UTC (rev 43567)
@@ -218,7 +218,7 @@
 
 /* Shader Evaluation */
 
-void kernel_cpu_shader(KernelGlobals *kg, uint4 *input, float3 *output, int type, int i)
+void kernel_cpu_shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int i)
 {
 	kernel_shader_evaluate(kg, input, output, (ShaderEvalType)type, i);
 }

Modified: trunk/blender/intern/cycles/kernel/kernel.cu
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel.cu	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel.cu	2012-01-20 17:49:17 UTC (rev 43567)
@@ -44,7 +44,7 @@
 		kernel_film_tonemap(NULL, rgba, buffer, sample, resolution, x, y, offset, stride);
 }
 
-extern "C" __global__ void kernel_cuda_shader(uint4 *input, float3 *output, int type, int sx)
+extern "C" __global__ void kernel_cuda_shader(uint4 *input, float4 *output, int type, int sx)
 {
 	int x = sx + blockDim.x*blockIdx.x + threadIdx.x;
 

Modified: trunk/blender/intern/cycles/kernel/kernel.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel.h	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel.h	2012-01-20 17:49:17 UTC (rev 43567)
@@ -40,7 +40,7 @@
 	int sample, int x, int y, int offset, int stride);
 void kernel_cpu_tonemap(KernelGlobals *kg, uchar4 *rgba, float4 *buffer,
 	int sample, int resolution, int x, int y, int offset, int stride);
-void kernel_cpu_shader(KernelGlobals *kg, uint4 *input, float3 *output,
+void kernel_cpu_shader(KernelGlobals *kg, uint4 *input, float4 *output,
 	int type, int i);
 
 #ifdef WITH_OPTIMIZED_KERNEL
@@ -48,7 +48,7 @@
 	int sample, int x, int y, int offset, int stride);
 void kernel_cpu_optimized_tonemap(KernelGlobals *kg, uchar4 *rgba, float4 *buffer,
 	int sample, int resolution, int x, int y, int offset, int stride);
-void kernel_cpu_optimized_shader(KernelGlobals *kg, uint4 *input, float3 *output,
+void kernel_cpu_optimized_shader(KernelGlobals *kg, uint4 *input, float4 *output,
 	int type, int i);
 #endif
 

Modified: trunk/blender/intern/cycles/kernel/kernel_compat_cpu.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_compat_cpu.h	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel_compat_cpu.h	2012-01-20 17:49:17 UTC (rev 43567)
@@ -141,6 +141,7 @@
 };
 
 typedef texture<float4> texture_float4;
+typedef texture<float2> texture_float2;
 typedef texture<float> texture_float;
 typedef texture<uint> texture_uint;
 typedef texture<int> texture_int;

Modified: trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h	2012-01-20 16:13:26 UTC (rev 43566)
+++ trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h	2012-01-20 17:49:17 UTC (rev 43567)
@@ -45,6 +45,7 @@
 /* Textures */
 
 typedef texture<float4, 1> texture_float4;
+typedef texture<float2, 1> texture_float2;
 typedef texture<float, 1> texture_float;
 typedef texture<uint, 1> texture_uint;

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-blender-cvs mailing list