【作业 6】raytracing作业

关于渲染的作业,除了课程也参考借鉴了GAMES101,以及Ray tracing in one weekend这位朋友帖子的代码,主要是增加了三角形网格模型物体以及与三角形网格的bvh求交相关的实现,以及加载使用obj文件,以及texture;也尝试了混合重要性采样(感觉漫反射材质效果良好,增加了Dielectric材质物体到场景中就白噪偏多了,这个还在继续研究中),相应效果如下:




代码

遇到的问题:
1、kernel函数执行过长会报错,关于cuda编译timeout的错,当然像课程里的代码,或者上面提到的那位的代码那样单格像素的n次采样放到kernel外,循环多次,这样或许能避免此问题;
2、作业过程中另一大类问题是不知道如何OOP, 目前这一版写的代码感觉还有很多不满意的,后面再优化看吧
3、另外问下关于API文档有没有计划整理一个?感觉现在查找不是很方便,顺便问下Matrix对象有没有按行或列分割成n个Vector对象的方法,感觉有,但没找到就自己写了个实现;另外struct的成员变量的Vector能不能带shape?还有struct本身是不能作为另一个struct的成员的类型的吧
4、还有最开始用opengl做后端有各种报错,解决不了,刚发帖求助发现有其它贴说opengl的有问题,然后折腾安装我的cuda才好 :sweat:

4 Likes

Hello! Good work. (I wrote the original ray tracing in one weekend repo).

Nice to see you found it useful. I apologize I do not speak Chinese, but can respond to your post that I read in google translate…

  1. The kernel function is executed for a long time: I would suggest not executing all samples at once. For example, in my original code, for each kernel execution, it traces one more ray bounce per pixel. You could perhaps do one camera ray per pixel for each execution too. Which is what I do here: https://github.com/bsavery/ray-tracing-one-weekend-taichi/blob/77e78df574d8243b05f088db37f7032a448de87b/main.py#L115

  2. I don’t think OOP is the correct approach in Taichi. Think more like “functional programming” for example: https://github.com/bsavery/ray-tracing-one-weekend-taichi/blob/Ray-Tracing-One-Weekend/hittable/sphere.py

We have a “sphere” struct and a hit_sphere function that operates on it.

4 Likes

你好,@shihuan

作业很不错,渲染效果更好一点可以当大作业了。

关于你问的问题,我简单的回答一下。

  1. kernel过长会报错问题。这个应该和长度没有关系,你可以把错误的代码简化到一段代码贴出来么?

  2. 面向对象是太极图形课的一节课,可以看here

  3. API文档在: here
    目前Matrix还没有单独获取行列的API。
    Vector海不能带shape, 这有点类似SOA的结构了,这个可以通过设置带shape的field然后place到一起。
    “struct本身是不能作为另一个struct的成员的类型的吧”: Taichi是支持这个功能的,可以看文档:here

  4. 目前我们也注意到了这个问题,已经在找哪里出了问题。

1 Like

感谢回复解答,其它的我先学习下,关于单次kernel执行过长报错信息我放下面,我就是这个代码把spp调大到200多就会报错,或则执行过程中有大量print也会,可能也和我机器硬件有关,显卡太次了
:tired_face:

[E 11/29/21 14:59:19.866 5636] [taichi/backends/cuda/cuda_driver.h:taichi::lang::CUDADriverFunction<void *>::operator ()@86] CUDA Error CUDA_ERROR_LAUNCH_TIMEOUT: the launch timed out and was terminated while calling stream_synchronize (cuStreamSynchronize)


***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7ffd1d33cb4a: taichi::print_traceback in taichi_core.pyd
0x7ffd1d20c1a9: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2a0c13: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2a35cc: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d355843: taichi::print_traceback in taichi_core.pyd
0x7ffd1d2158eb: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d215a8d: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2158eb: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d276569: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2158eb: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d355531: taichi::print_traceback in taichi_core.pyd
0x7ffd1d354cd5: taichi::print_traceback in taichi_core.pyd
0x7ffd1d399820: taichi::print_traceback in taichi_core.pyd
0x7ffd1d353923: taichi::print_traceback in taichi_core.pyd
0x7ffd1d351cf0: taichi::print_traceback in taichi_core.pyd
0x7ffd1d2c40c9: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2a30c1: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2d6f98: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2c35a5: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2c2f98: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d1540be: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d0ce6c6: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d09715f: pybind11::error_already_set::discard_as_unraisable in taichi_core.pyd
0x6079b055: PyCFunction_FastCallDict in python36.dll
0x607ea38f: Py_FatalError in python36.dll
0x6078b6dc: PyObject_Call in python36.dll
0x607d0247: PyObject_GC_Resize in python36.dll
0x6079be3f: PyObject_GenericGetAttr in python36.dll
0x6079d3ff: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x607cc30a: PyUnicode_Compare in python36.dll
0x6079fa10: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x6079783e: PyFunction_FastCallDict in python36.dll
0x60797625: PyObject_Hash in python36.dll
0x6078b6dc: PyObject_Call in python36.dll
0x607d0247: PyObject_GC_Resize in python36.dll
0x6079fa10: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x6079bf5b: PyObject_GenericGetAttr in python36.dll
0x6079d3ff: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x607cc30a: PyUnicode_Compare in python36.dll
0x6079fa10: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x6079bf5b: PyObject_GenericGetAttr in python36.dll
0x6079d3ff: PyEval_EvalFrameDefault in python36.dll
0x607991f6: PyErr_Occurred in python36.dll
0x6078392e: PyEval_EvalCodeEx in python36.dll
0x60783899: PyEval_EvalCode in python36.dll
0x60783843: PyArena_Free in python36.dll
0x60912e19: PyRun_FileExFlags in python36.dll
0x60913555: PyRun_SimpleFileExFlags in python36.dll
0x60912cf7: PyRun_AnyFileExFlags in python36.dll
0x60864d1c: Py_hashtable_size in python36.dll
0x607f9fba: Py_FatalError in python36.dll
0x1cbd126d: Unknown Function in python.exe
0x7ffd722584d4: BaseThreadInitThunk in KERNEL32.DLL
0x7ffd73bee871: RtlUserThreadStart in ntdll.dll

Internal error occurred. Check out this page for possible solutions:
https://docs.taichi.graphics/lang/articles/misc/install
Traceback (most recent call last):
  File "D:/workspace/CG/taichi/ray_tracing/main.py", line 170, in <module>
    ti.imwrite(film_pixels.to_numpy(), 'out.png')
  File "D:\dev_install\Python3_6\lib\site-packages\taichi\lang\util.py", line 207, in wrapped
    return func(*args, **kwargs)
  File "D:\dev_install\Python3_6\lib\site-packages\taichi\lang\matrix.py", line 1311, in to_numpy
    taichi.lang.meta.matrix_to_ext_arr(self, arr, as_vector)
  File "D:\dev_install\Python3_6\lib\site-packages\taichi\lang\kernel_impl.py", line 709, in wrapped
    return primal(*args, **kwargs)
  File "D:\dev_install\Python3_6\lib\site-packages\taichi\lang\kernel_impl.py", line 637, in __call__
    return self.compiled_functions[key](*args)
  File "D:\dev_install\Python3_6\lib\site-packages\taichi\lang\kernel_impl.py", line 595, in func__
    t_kernel(launch_ctx)
RuntimeError: [taichi/backends/cuda/cuda_driver.h:taichi::lang::CUDADriverFunction<void *>::operator ()@86] CUDA Error CUDA_ERROR_LAUNCH_TIMEOUT: the launch timed out and was terminated while calling stream_synchronize (cuStreamSynchronize)
[E 11/29/21 14:59:20.263 5636] [taichi/backends/cuda/cuda_driver.h:taichi::lang::CUDADriverFunction<void *>::operator ()@86] CUDA Error CUDA_ERROR_LAUNCH_TIMEOUT: the launch timed out and was terminated while calling stream_synchronize (cuStreamSynchronize)


***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7ffd1d33cb4a: ?print_traceback@taichi@@YAXXZ in taichi_core.pyd
0x7ffd1d20c1a9: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2a0c13: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d477f3e: ?print_traceback@taichi@@YAXXZ in taichi_core.pyd
0x7ffd1d2d9c06: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2d77fc: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2d489b: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d15a8e8: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d09522e: ?clear@error_already_set@pybind11@@QEAAXXZ in taichi_core.pyd
0x7ffd1d09cf72: PyInit_taichi_core in taichi_core.pyd
0x6079585c: PyObject_ClearWeakRefs in python36.dll
0x60803fed: Py_FatalError in python36.dll
0x607c4ac3: PyObject_GC_UnTrack in python36.dll
0x607c4e80: PyDict_Clear in python36.dll
0x607c3e3f: _PyGC_CollectNoFail in python36.dll
0x60787f3c: PyImport_Cleanup in python36.dll
0x607b5715: Py_FinalizeEx in python36.dll
0x607b4458: Py_Main in python36.dll
0x1cbd126d: Unknown Function in python.exe
0x7ffd722584d4: BaseThreadInitThunk in KERNEL32.DLL
0x7ffd73bee871: RtlUserThreadStart in ntdll.dll

Internal error occurred. Check out this page for possible solutions:
https://docs.taichi.graphics/lang/articles/misc/install
[E 11/29/21 14:59:20.312 5636] Received signal 22 (SIGABRT)


***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7ffd1d33cb4a: ?print_traceback@taichi@@YAXXZ in taichi_core.pyd
0x7ffd1d20c1a9: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d326cf8: PyInit_taichi_core in taichi_core.pyd
0x7ffd70f1cfa7: raise in ucrtbase.dll
0x7ffd70f1de31: abort in ucrtbase.dll
0x7ffd70f1d4ef: terminate in ucrtbase.dll
0x7ffd61431ab3: __NLG_Return2 in VCRUNTIME140_1.dll
0x7ffd6143231f: __NLG_Return2 in VCRUNTIME140_1.dll
0x7ffd614340c9: __CxxFrameHandler4 in VCRUNTIME140_1.dll
0x7ffd73c2a49d: __chkstk in ntdll.dll
0x7ffd73bbfd43: RtlImageNtHeaderEx in ntdll.dll
0x7ffd73bc1a59: RtlRaiseException in ntdll.dll
0x7ffd70964f38: RaiseException in KERNELBASE.dll
0x7ffd4e3e4462: _CxxThrowException in VCRUNTIME140.dll
0x7ffd1d20c1f0: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2a0c13: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d477f3e: ?print_traceback@taichi@@YAXXZ in taichi_core.pyd
0x7ffd1d2d9c06: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2d77fc: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d2d489b: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d15a8e8: PyInit_taichi_core in taichi_core.pyd
0x7ffd1d09522e: ?clear@error_already_set@pybind11@@QEAAXXZ in taichi_core.pyd
0x7ffd1d09cf72: PyInit_taichi_core in taichi_core.pyd
0x6079585c: PyObject_ClearWeakRefs in python36.dll
0x60803fed: Py_FatalError in python36.dll
0x607c4ac3: PyObject_GC_UnTrack in python36.dll
0x607c4e80: PyDict_Clear in python36.dll
0x607c3e3f: _PyGC_CollectNoFail in python36.dll
0x60787f3c: PyImport_Cleanup in python36.dll
0x607b5715: Py_FinalizeEx in python36.dll
0x607b4458: Py_Main in python36.dll
0x1cbd126d: Unknown Function in python.exe
0x7ffd722584d4: BaseThreadInitThunk in KERNEL32.DLL
0x7ffd73bee871: RtlUserThreadStart in ntdll.dll

你把这一句去掉会怎么样?https://github.com/moxunbai/tc_raytracing/blob/3f6ed48fe066887fd5e01d5ee0aa1e37702ecd7c/main.py#L170

Thank you for your reply,about Q1 , I really understand why you coded like this until this problem had troubled me for a few days.
2. About OOP,maybe I want more
By the way ,I want ask another question, in function ‘hit_all’ ,judge the ray intersects with objects, I’m not sure we should choice the nearer hitted obj in every hitted objects. here https://github.com/bsavery/ray-tracing-one-weekend-taichi/blob/48f887a49eb03f34a66a611d0717ab11ec0606f9/hittable.py#L117
so in my code I update this place, compare every hitted obj for ray ‘t’ pick the min value fro result.

At last , forgive my ‘Plastic English‘ , thank you.

额,注释掉也会报错,,我记得以前注释掉是不报错的

By the way ,I want ask another question, in function ‘hit_all’ ,judge the ray intersects with objects, I’m not sure we should choice the nearer hitted obj in every hitted objects. here https://github.com/bsavery/ray-tracing-one-weekend-taichi/blob/48f887a49eb03f34a66a611d0717ab11ec0606f9/hittable.py#L117 1
so in my code I update this place, compare every hitted obj for ray ‘t’ pick the min value fro result.

Do not worry about the language, it’s amazing the we can communicate at all!

I’m not sure I follow exactly, can you point me to the code you did for this? The reason we do the “closest_so_far” in the code, is once you hit one object, if you don’t want to consider another ray hit unless it is LESS than the one you already found. You’ll notice that we then set t_max to the closest hit so far.

There might be other reasons (volume rendering, etc) where you do not want to do this, but for general ray tracing this is pretty common. But please point me to your example in the code.

1 Like

Sorry, I didn’t say it clearly.I got a little mistake before.I checked my code and look back my mind then I think got more clear.That in function ‘hit_all’ , let’s suppose,for the first time the ray hit a obj and ‘hit_anything’ are assigned True,Then second time the ray didn’t hit any obj and the loop is over, we get the returns of ‘hit_all’ function about hit_anything=True ,hit_index is the first hit obj, but the values of ‘p’,‘n’,‘front_facing’ are the second hitted results.Then I use temporary variables replace these values and when hit the obj every times ,the returned variables are assigned with this. My code here line 211-225

Yes, this temporary values are only assigned to the “return values” when there is a hit. Because we update the t_max to the last hit, the object hit() will only return true when the hit is closer. This works fine without further comparison, unless I’m misunderstanding something!