0x00 前言

最近看到了我司大网红aras-p(Aras Pranckevičius)的博客开了一个很有趣的新系列《Daily Pathtracer~》,来实现一个简单的ToyPathTracer。



除了使用C++,aras还使用了两种不同的C#运行时来实现,即.NET Standard和Xamarin/Mono。效率上自然C++要强于.NET Standard,而.NET Standard的表现要强于Xamarin/Mono。

除此之外,aras当然还会用到Unity来实现,比较有意思的是Unity的实现中使用了2018中的Burst来提升性能,而结果甚至有点小惊人呢,之后可以再说。

而aras的这个小巧的ToyPathTracer事实上和最近在知乎上小火的小书《Ray Tracing in One Weekend》还有些关系。其实这是一套3本中的第一本书,另外还有两本,分别叫《Ray Tracing: the Next Week》和《Ray Tracing: The Rest Of Your Life》。



嗯,真是一入码门深似海。而图形学作为程序员的三大浪漫之一,真是诚不我欺也。

当然,这本书的作者Peter Shirley已经将此书作为PDF免费放出了(此处有地址:https://drive.google.com/drive/folders/14yayBb9XiL16lmuhbYhhvea8mKUUK77W),但是各位如果有兴趣的话建议还是买一下吧(seriously, just buy that minibook)。

所以前言部分先安利的,是这套小书。

0x01 Unity中实现PathTracer

aras的这一系列博客很长,目录摘录在下面。

我的这篇小文作为安利文,也就不详细介绍了每一篇的内容了,相信有兴趣的小伙伴都会去看的。

但是其中有一篇文章还是蛮有趣的,因为对比了不同语言、不同运行时的性能差异,而且还演示了一下Unity2018中的Burst compiler对C#代码性能的提升。所以稍微介绍下这部分吧。

当然,首先可以看到,场景是作为硬编码写死的:

  1. static Sphere[] s_SpheresData = {
  2. new Sphere(new float3(0,-100.5f,-1), 100),
  3. new Sphere(new float3(2,0,-1), 0.5f),
  4. new Sphere(new float3(0,0,-1), 0.5f),
  5. new Sphere(new float3(-2,0,-1), 0.5f),
  6. new Sphere(new float3(2,0,1), 0.5f),
  7. new Sphere(new float3(0,0,1), 0.5f),
  8. new Sphere(new float3(-2,0,1), 0.5f),
  9. new Sphere(new float3(0.5f,1,0.5f), 0.5f),
  10. new Sphere(new float3(-1.5f,1.5f,0f), 0.3f),
  11. #if DO_BIG_SCENE
  12. new Sphere(new float3(4,0,-3), 0.5f), new Sphere(new float3(3,0,-3), 0.5f), new Sphere(new float3(2,0,-3), 0.5f), new Sphere(new float3(1,0,-3), 0.5f), new Sphere(new float3(0,0,-3), 0.5f), new Sphere(new float3(-1,0,-3), 0.5f), new Sphere(new float3(-2,0,-3), 0.5f), new Sphere(new float3(-3,0,-3), 0.5f), new Sphere(new float3(-4,0,-3), 0.5f),
  13. new Sphere(new float3(4,0,-4), 0.5f), new Sphere(new float3(3,0,-4), 0.5f), new Sphere(new float3(2,0,-4), 0.5f), new Sphere(new float3(1,0,-4), 0.5f), new Sphere(new float3(0,0,-4), 0.5f), new Sphere(new float3(-1,0,-4), 0.5f), new Sphere(new float3(-2,0,-4), 0.5f), new Sphere(new float3(-3,0,-4), 0.5f), new Sphere(new float3(-4,0,-4), 0.5f),
  14. new Sphere(new float3(4,0,-5), 0.5f), new Sphere(new float3(3,0,-5), 0.5f), new Sphere(new float3(2,0,-5), 0.5f), new Sphere(new float3(1,0,-5), 0.5f), new Sphere(new float3(0,0,-5), 0.5f), new Sphere(new float3(-1,0,-5), 0.5f), new Sphere(new float3(-2,0,-5), 0.5f), new Sphere(new float3(-3,0,-5), 0.5f), new Sphere(new float3(-4,0,-5), 0.5f),
  15. new Sphere(new float3(4,0,-6), 0.5f), new Sphere(new float3(3,0,-6), 0.5f), new Sphere(new float3(2,0,-6), 0.5f), new Sphere(new float3(1,0,-6), 0.5f), new Sphere(new float3(0,0,-6), 0.5f), new Sphere(new float3(-1,0,-6), 0.5f), new Sphere(new float3(-2,0,-6), 0.5f), new Sphere(new float3(-3,0,-6), 0.5f), new Sphere(new float3(-4,0,-6), 0.5f),
  16. new Sphere(new float3(1.5f,1.5f,-2), 0.3f),
  17. #endif // #if DO_BIG_SCENE
  18. };
  19. static Material[] s_SphereMatsData = {
  20. new Material(Material.Type.Lambert, new float3(0.8f, 0.8f, 0.8f), new float3(0,0,0), 0, 0),
  21. new Material(Material.Type.Lambert, new float3(0.8f, 0.4f, 0.4f), new float3(0,0,0), 0, 0),
  22. new Material(Material.Type.Lambert, new float3(0.4f, 0.8f, 0.4f), new float3(0,0,0), 0, 0),
  23. new Material(Material.Type.Metal, new float3(0.4f, 0.4f, 0.8f), new float3(0,0,0), 0, 0),
  24. new Material(Material.Type.Metal, new float3(0.4f, 0.8f, 0.4f), new float3(0,0,0), 0, 0),
  25. new Material(Material.Type.Metal, new float3(0.4f, 0.8f, 0.4f), new float3(0,0,0), 0.2f, 0),
  26. new Material(Material.Type.Metal, new float3(0.4f, 0.8f, 0.4f), new float3(0,0,0), 0.6f, 0),
  27. new Material(Material.Type.Dielectric, new float3(0.4f, 0.4f, 0.4f), new float3(0,0,0), 0, 1.5f),
  28. new Material(Material.Type.Lambert, new float3(0.8f, 0.6f, 0.2f), new float3(30,25,15), 0, 0),
  29. #if DO_BIG_SCENE
  30. new Material(Material.Type.Lambert, new float3(0.1f, 0.1f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.2f, 0.2f, 0.2f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.3f, 0.3f, 0.3f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.4f, 0.4f, 0.4f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.5f, 0.5f, 0.5f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.6f, 0.6f, 0.6f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.7f, 0.7f, 0.7f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.8f, 0.8f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.9f, 0.9f, 0.9f), new float3(0,0,0), 0, 0),
  31. new Material(Material.Type.Metal, new float3(0.1f, 0.1f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.2f, 0.2f, 0.2f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.3f, 0.3f, 0.3f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.4f, 0.4f, 0.4f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.5f, 0.5f, 0.5f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.6f, 0.6f, 0.6f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.7f, 0.7f, 0.7f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.8f, 0.8f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.9f, 0.9f, 0.9f), new float3(0,0,0), 0, 0),
  32. new Material(Material.Type.Metal, new float3(0.8f, 0.1f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.8f, 0.5f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.8f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.4f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.1f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.1f, 0.8f, 0.5f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.1f, 0.8f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.1f, 0.1f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.5f, 0.1f, 0.8f), new float3(0,0,0), 0, 0),
  33. new Material(Material.Type.Lambert, new float3(0.8f, 0.1f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.8f, 0.5f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.8f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.4f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.1f, 0.8f, 0.1f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.1f, 0.8f, 0.5f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.1f, 0.8f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Lambert, new float3(0.1f, 0.1f, 0.8f), new float3(0,0,0), 0, 0), new Material(Material.Type.Metal, new float3(0.5f, 0.1f, 0.8f), new float3(0,0,0), 0, 0),
  34. new Material(Material.Type.Lambert, new float3(0.1f, 0.2f, 0.5f), new float3(3,10,20), 0, 0),
  35. #endif
  36. };

当然需要来判断是否相交的:

  1. static bool HitWorld(Ray r, float tMin, float tMax, ref Hit outHit, ref int outID, ref SpheresSoA spheres)
  2. {
  3. outID = spheres.HitSpheres(ref r, tMin, tMax, ref outHit);
  4. return outID != -1;
  5. }

光线的追踪:

  1. static float3 Trace(Ray r, int depth, ref int inoutRayCount, ref SpheresSoA spheres, NativeArray<Material> materials, ref uint randState, bool doMaterialE = true)
  2. {
  3. Hit rec = default(Hit);
  4. int id = 0;
  5. ++inoutRayCount;
  6. if (HitWorld(r, kMinT, kMaxT, ref rec, ref id, ref spheres))
  7. {
  8. Ray scattered;
  9. float3 attenuation;
  10. float3 lightE;
  11. var mat = materials[id];
  12. var matE = mat.emissive;
  13. ...

物体表面如何处理接收到的光线呢?可以看这里:

  1. static bool Scatter(Material mat, Ray r_in, Hit rec, out float3 attenuation, out Ray scattered, out float3 outLightE, ref int inoutRayCount, ref SpheresSoA spheres, NativeArray<Material> materials, ref uint randState)
  2. {
  3. outLightE = new float3(0, 0, 0);
  4. if (mat.type == Material.Type.Lambert)
  5. {
  6. // random point inside unit sphere that is tangent to the hit point
  7. float3 target = rec.pos + rec.normal + MathUtil.RandomUnitVector(ref randState);
  8. scattered = new Ray(rec.pos, normalize(target - rec.pos));
  9. attenuation = mat.albedo;
  10. // sample lights
  11. ...

最后,在Unity中使用了burst,并分发了任务:

  1. [ComputeJobOptimization]
  2. struct TraceRowJob : IJobParallelFor
  3. {
  4. public int screenWidth, screenHeight, frameCount;
  5. [NativeDisableParallelForRestriction] public NativeArray<UnityEngine.Color> backbuffer;
  6. public Camera cam;
  7. [NativeDisableParallelForRestriction] public NativeArray<int> rayCounter;
  8. [NativeDisableParallelForRestriction] public SpheresSoA spheres;
  9. [NativeDisableParallelForRestriction] public NativeArray<Material> materials;
  10. public void Execute(int y)
  11. {
  12. int backbufferIdx = y * screenWidth;
  13. float invWidth = 1.0f / screenWidth;
  14. float invHeight = 1.0f / screenHeight;
  15. float lerpFac = (float)frameCount / (float)(frameCount + 1);
  16. ...

通过UnityProfiler,我们可以看到经过Burst优化后的运行状态:

不过让我感到比较吃惊的是,在Unity2018中使用C#+Burst时的效率竟然超过了C++的实现,看来Burst的效率还是很惊人的。

下面是aras那里的结论:

  • .NET Core is about 2x slower than vanilla C++.
  • Mono (with default settings) is about 3x slower than .NET Core.
  • IL2CPP is 2x-3x faster than Mono, which is roughly .NET Core performance level.
  • Unity’s Burst compiler can get our C# code faster than vanilla C++. Note that right now Burst is very early tech, I expect it will get even better performance later on.

当然,aras的博客中有更详细的对比数据。

所以这部分安利的,是aras的博客(http://aras-p.info/blog/2018/03/28/Daily-Pathtracer-Part-0-Intro/)。

0x02 小结

当然,aras除了写了博客之外,也开源了这个ToyPathTracer(也许先有了ToyPathTracer才有了这系列的博客也未可知)。

所以各位可以去github上clone一份代码,除了可以分别在windows和mac上编译c++、c#的实现,也可以直接在unity中使用。

所以最后安利一下这个工程(https://github.com/aras-p/ToyPathTracer)。

怎么样,是不是还挺有趣的?

Ref

《Ray Tracing in One Weekend》

https://drive.google.com/drive/folders/14yayBb9XiL16lmuhbYhhvea8mKUUK77W

《Daily Pathtracer》

http://aras-p.info/blog/2018/03/28/Daily-Pathtracer-Part-0-Intro/

《ToyPathTracer》

https://github.com/aras-p/ToyPathTracer

-EOF-

最后打个广告,欢迎支持我的书《Unity 3D脚本编程》

欢迎大家关注我的公众号慕容的游戏编程:chenjd01

Daily Pathtracer!安利下不错的Pathtracer学习资料的更多相关文章

  1. 很不错的jQuery学习资料和实例

    这些都是学习Jquery很不错的资料,整理了一下,分享给大家. 希望能对大家的学习有帮助. 帕兰 Noupe带来的51个最佳jQuery教程和实例, 向大家介绍了jQuery的一些基本概念和使用的相关 ...

  2. 一个不错的linux学习资料下载的网址

    本文比较完整的讲述GNU make工具,涵盖GNU make的用法.语法.同时重点讨论如何为一个工程编写Makefile.作为一个Linux程序员,make工具的使用以及编写Makefile是必需的. ...

  3. GitHub上的计算机视觉学习资料推荐

    9月份将要读研,导师是做cv的,最近学习时找到了不少的计算机视觉的资料,记录一下,同时也分享给需要的朋友 assmdx/ComputerVisionDoc AceCoooool/interview-c ...

  4. Linux下C编程的学习_1

    0x0:为什么写这个系列的文章 博客原本的定位是安卓游戏的破解,可是为什么写这系列的文章呢? 由于在破解过程中,我们是无法避免来敲代码的,恢复算法,模拟算法,游戏中对数据的解密.游戏中对保存在clie ...

  5. MVC不错的学习资料

    MVC不错的学习资料: http://www.cnblogs.com/darrenji/

  6. (转)NoSQL——Redis在win7下安装配置的学习一

    NoSQL——Redis在win7下安装配置的学习一   有些也是从网上看来的 1.下载安装 Redis它没有windows的官方版本,但是又非官方的版本,到官网上去下载相应的版本,我的电脑是win7 ...

  7. linux下的IO模型---学习笔记

    1.linux文件系统和缓存 文件系统接口 文件系统-一种把数据组织成文件和目录的存储方式,提供了基于文件的存取接口,并通过文件权限控制访问. 存储层次 文件系统缓存 主存(通常时DRAM)的一块区域 ...

  8. Android 学习资料收集

    收集整理这份资料灵感来自于 trip_to_iOS, 征得同意引用了该资料的开头描述 收集整理这份资料主要帮助初学者学习 Android 开发, 希望能快速帮助到他们快速入门, 找到适合自己学习资料, ...

  9. iOS学习资料整理

    视频教程(英文) 视频 简介 Developing iOS 7 Apps for iPhone and iPad 斯坦福开放教程之一, 课程主要讲解了一些 iOS 开发工具和 API 以及 iOS S ...

随机推荐

  1. 三, 练习 python索引 (list和tuple)

    (1) 练习 请用索引取出下面list的指定元素: 1, # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java' ...

  2. MQTT控制---subscribe

    连接服务端 客户端向服务端发送SUBSCRIBE报文用于创建一个或多个订阅. 固定报头 报头长度:2 Bytes 1.报头控制类型(0x82) 报文SUBSCRIBE控制报固定报头的第3.2.1.0位 ...

  3. C中单引号中放多个字符

    在C中时常有类似这样的写法: #define EVENT_MAGIC 'evnt' int magic = EVENT_MAGIC; 乍一看来,这样的方式有点不理解.听网友说: C中单引号内最多存4个 ...

  4. 消息队列(MQ)

    1. 分类: 获取消息方式:A. push(推)方式:优点——可以尽可能快地将消息发送给消费者,缺点——如果消费者处理能力跟不上,消费者的缓冲区可能会溢出:     B. pull(拉)方式:优点—— ...

  5. eclipse如何更换工作空间

    关于工作空间的更换,小生也操作较少,今天做个记录并分享给大家. 1.找到文件——更换工作空间——其他 2.选择自己要更换的工作空间点击确定即可.

  6. centos7 安装Mysql8.0笔记

    下载MySQL yum源 wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm 安装yum源 yum lo ...

  7. PHP字符过滤方法

    function str_filter_replace($str) { if (empty($str)) return false; $str = htmlspecialchars($str); $s ...

  8. 《ServerSuperIO Designer IDE使用教程》-2.与硬件网关数据交互,并进行数据级联转发,直到云端。发布:v4.2.1版本

    v4.2.1 更新内容:1.重新定义数据转发文本协议,使网关与ServerSuperIO以及之间能够相关交互数据.2.扩展ServerSuperIO动态数据类的方法,更灵活.3.修复Designer增 ...

  9. 不可不知的表达式树(3)定制IQueryProvider

    前面我们说到利用表达式树技术实现LINQ-to-SQL,实际上可以针对任何数据源,实现LINQ-to-Everything.这里还涉及到两个重要的接口即IQueryable和IQueryProvide ...

  10. Manacher算法 (马拉车算法)

    #include<iostream> #include<string.h> #include<algorithm> using namespace std; ]; ...