[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [39856] branches/cycles/intern/cycles: Cycles: compile opencl kernels in non-blocking thread, and don't crash on

Brecht Van Lommel brechtvanlommel at pandora.be
Fri Sep 2 02:10:07 CEST 2011


Revision: 39856
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=39856
Author:   blendix
Date:     2011-09-02 00:10:03 +0000 (Fri, 02 Sep 2011)
Log Message:
-----------
Cycles: compile opencl kernels in non-blocking thread, and don't crash on
build failure but show error message in status text.

Modified Paths:
--------------
    branches/cycles/intern/cycles/device/device.h
    branches/cycles/intern/cycles/device/device_cuda.cpp
    branches/cycles/intern/cycles/device/device_opencl.cpp
    branches/cycles/intern/cycles/render/session.cpp
    branches/cycles/intern/cycles/util/util_math.h
    branches/cycles/intern/cycles/util/util_string.cpp
    branches/cycles/intern/cycles/util/util_string.h

Modified: branches/cycles/intern/cycles/device/device.h
===================================================================
--- branches/cycles/intern/cycles/device/device.h	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/device/device.h	2011-09-02 00:10:03 UTC (rev 39856)
@@ -108,6 +108,9 @@
 	/* open shading language, only for CPU device */
 	virtual void *osl_memory() { return NULL; }
 
+	/* load/compile kernels, must be called before adding tasks */ 
+	virtual bool load_kernels() { return true; }
+
 	/* tasks */
 	virtual void task_add(DeviceTask& task) = 0;
 	virtual void task_wait() = 0;

Modified: branches/cycles/intern/cycles/device/device_cuda.cpp
===================================================================
--- branches/cycles/intern/cycles/device/device_cuda.cpp	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/device/device_cuda.cpp	2011-09-02 00:10:03 UTC (rev 39856)
@@ -137,7 +137,6 @@
 
 	CUDADevice(bool background_)
 	{
-		int major, minor;
 		background = background_;
 
 		cuDevId = 0;
@@ -153,11 +152,6 @@
 		else
 			cuda_assert(cuGLCtxCreate(&cuContext, 0, cuDevice))
 
-		/* open module */
-		cuDeviceComputeCapability(&major, &minor, cuDevId);
-		string cubin = string_printf("lib/kernel_sm_%d%d.cubin", major, minor);
-		cuda_assert(cuModuleLoad(&cuModule, path_get(cubin).c_str()))
-
 		cuda_pop_context();
 	}
 
@@ -179,6 +173,27 @@
 		return string("CUDA ") + deviceName;
 	}
 
+
+	bool load_kernels()
+	{
+		CUresult result;
+		int major, minor;
+
+		cuda_push_context();
+
+		/* open module */
+		cuDeviceComputeCapability(&major, &minor, cuDevId);
+		string cubin = path_get(string_printf("lib/kernel_sm_%d%d.cubin", major, minor));
+
+		result = cuModuleLoad(&cuModule, cubin.c_str());
+		if(result != CUDA_SUCCESS)
+			fprintf(stderr, "Failed loading CUDA kernel %s (%s).\n", cubin.c_str(), cuda_error_string(result));
+
+		cuda_pop_context();
+
+		return (result == CUDA_SUCCESS);
+	}
+
 	void mem_alloc(device_memory& mem, MemoryType type)
 	{
 		cuda_push_context();
@@ -231,7 +246,7 @@
 
 		cuda_push_context();
 		cuda_assert(cuModuleGetGlobal(&mem, &bytes, cuModule, name))
-		assert(bytes == size);
+		//assert(bytes == size);
 		cuda_assert(cuMemcpyHtoD(mem, host, size))
 		cuda_pop_context();
 	}

Modified: branches/cycles/intern/cycles/device/device_opencl.cpp
===================================================================
--- branches/cycles/intern/cycles/device/device_opencl.cpp	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/device/device_opencl.cpp	2011-09-02 00:10:03 UTC (rev 39856)
@@ -136,7 +136,16 @@
 
 		cqCommandQueue = clCreateCommandQueue(cxContext, cdDevice, 0, &ciErr);
 		opencl_assert(ciErr);
-		
+
+		null_mem = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, 1, NULL, &ciErr);
+
+		cpProgram = NULL;
+		ckPathTraceKernel = NULL;
+		ckFilmConvertKernel = NULL;
+	}
+
+	bool load_kernels()
+	{
 		/* compile kernel */
 		string source = string_printf("#include \"kernel.cl\" // %lf\n", time_dt());
 		size_t source_len = source.size();
@@ -152,6 +161,7 @@
 		opencl_assert(ciErr);
 
 		ciErr = clBuildProgram(cpProgram, 0, NULL, build_options.c_str(), NULL, NULL);
+
 		if(ciErr != CL_SUCCESS) {
 			char *build_log;
 			size_t ret_val_size;
@@ -162,13 +172,11 @@
 			clGetProgramBuildInfo(cpProgram, cdDevice, CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
 
 			build_log[ret_val_size] = '\0';
-			printf("OpenCL build failed:\n %s\n", build_log);
+			fprintf(stderr, "OpenCL build failed:\n %s\n", build_log);
 
 			delete[] build_log;
 
-			opencl_assert(ciErr);
-
-			return;
+			return false;
 		}
 
 		ckPathTraceKernel = clCreateKernel(cpProgram, "kernel_ocl_path_trace", &ciErr);
@@ -176,25 +184,30 @@
 		ckFilmConvertKernel = clCreateKernel(cpProgram, "kernel_ocl_tonemap", &ciErr);
 		opencl_assert(ciErr);
 
-		null_mem = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, 1, NULL, &ciErr);
+		return true;
 	}
 
 	~OpenCLDevice()
 	{
+		if(null_mem)
+			clReleaseMemObject(CL_MEM_PTR(null_mem));
 
-		clReleaseMemObject(CL_MEM_PTR(null_mem));
-
 		map<string, device_vector<uchar>*>::iterator mt;
 		for(mt = const_mem_map.begin(); mt != const_mem_map.end(); mt++) {
 			mem_free(*(mt->second));
 			delete mt->second;
 		}
 
-		clReleaseKernel(ckPathTraceKernel);  
-		clReleaseKernel(ckFilmConvertKernel);  
-		clReleaseProgram(cpProgram);
-		clReleaseCommandQueue(cqCommandQueue);
-		clReleaseContext(cxContext);
+		if(ckPathTraceKernel)
+			clReleaseKernel(ckPathTraceKernel);  
+		if(ckFilmConvertKernel)
+			clReleaseKernel(ckFilmConvertKernel);  
+		if(cpProgram)
+			clReleaseProgram(cpProgram);
+		if(cqCommandQueue)
+			clReleaseCommandQueue(cqCommandQueue);
+		if(cxContext)
+			clReleaseContext(cxContext);
 	}
 
 	string description()

Modified: branches/cycles/intern/cycles/render/session.cpp
===================================================================
--- branches/cycles/intern/cycles/render/session.cpp	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/render/session.cpp	2011-09-02 00:10:03 UTC (rev 39856)
@@ -380,6 +380,15 @@
 
 void Session::run()
 {
+	/* load kernels */
+	progress.set_status("Loading render kernels");
+
+	if(!device->load_kernels()) {
+		progress.set_status("Failed loading render kernel, see console for errors");
+		progress.set_update();
+		return;
+	}
+
 	/* session thread loop */
 	progress.set_status("Waiting for render to start");
 

Modified: branches/cycles/intern/cycles/util/util_math.h
===================================================================
--- branches/cycles/intern/cycles/util/util_math.h	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/util/util_math.h	2011-09-02 00:10:03 UTC (rev 39856)
@@ -38,14 +38,20 @@
 
 CCL_NAMESPACE_BEGIN
 
-#ifndef __KERNEL_OPENCL__
-
+#ifndef M_PI_F
 #define M_PI_F		((float)3.14159265358979323846264338327950288)
+#endif
+#ifndef M_PI_2_F
 #define M_PI_2_F	((float)1.57079632679489661923132169163975144)
+#endif
+#ifndef M_PI_4_F
 #define M_PI_4_F	((float)0.785398163397448309615660845819875721)
+#endif
+#ifndef M_1_PI_F
 #define M_1_PI_F	((float)0.318309886183790671537767526745028724)
+#endif
+#ifndef M_2_PI_F
 #define M_2_PI_F	((float)0.636619772367581343075535053490057448)
-
 #endif
 
 /* Scalar */

Modified: branches/cycles/intern/cycles/util/util_string.cpp
===================================================================
--- branches/cycles/intern/cycles/util/util_string.cpp	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/util/util_string.cpp	2011-09-02 00:10:03 UTC (rev 39856)
@@ -77,11 +77,11 @@
 	return false;
 }
 
-void string_split(vector<string>& tokens, const string& str)
+void string_split(vector<string>& tokens, const string& str, const string& separators)
 {
 	vector<string> split;
 
-	boost::split(split, str, boost::is_any_of("\t "), boost::token_compress_on);
+	boost::split(split, str, boost::is_any_of(separators), boost::token_compress_on);
 
 	foreach(const string& token, split)
 		if(token != "")

Modified: branches/cycles/intern/cycles/util/util_string.h
===================================================================
--- branches/cycles/intern/cycles/util/util_string.h	2011-09-01 22:53:11 UTC (rev 39855)
+++ branches/cycles/intern/cycles/util/util_string.h	2011-09-02 00:10:03 UTC (rev 39856)
@@ -41,7 +41,7 @@
 string string_printf(const char *format, ...) PRINTF_ATTRIBUTE;
 
 bool string_iequals(const string& a, const string& b);
-void string_split(vector<string>& tokens, const string& str);
+void string_split(vector<string>& tokens, const string& str, const string& separators = "\t ");
 
 CCL_NAMESPACE_END
 




More information about the Bf-blender-cvs mailing list