# SGRT Notes
## Big Picture

* CPU 負責處理場景,與建立 BVH,並載入到 DRAM 中
* SRP 是一個 programmable shader core,進行大部分與 rendering 相關的工作,並將 traversal 及 intersection 相關的工作交由 T&I Unit 執行
* T&I Unit 基於 [T&I Engine](https://my.eng.utah.edu/~cs6958/papers/HWRT-seminar/a160-nah.pdf),是一個專用於 traversal 及 intersection 的 dedicated hardware
## SRP Unit

* SRP 基於 [ADRES](https://courses.cs.washington.edu/courses/cse591n/06au/papers/fpl_03_mei.pdf),有 VLIW engine 和 CGRA,VLIW engine 負責通用計算、CGRA 負責執行 compute-intensive 的 kernel。
* 有 320KB 的 Scratchpad Memory 及 4KB 的 data cache。
* camera、material、light、ray、intersection point 放置在 Scratchpad Memory
* primitive 太多,而且 access pattern 通常是 random 的,因此放在 external DRAM,透過 Data Cache 存取
* kernel 在編譯期透過 [modulo scheduling](https://past.date-conference.com/proceedings-archive/2003/DATE03/PDFFILES/04B_2.PDF) 將 loop 映射到 SRP 上
### VLIW engine
* 用途:drive T&I Unit、control flow in rendering
### CGRA
* 在每個 clock cycle,FU 會從 Configuration Memory 拿 configuration data,並使用 software pipeline 的方式對 kernel 進行平行運算
* 用途:ray generation、shading
### FU

### Texture Unit
* texel address calculation
* texture memory access via texture cache
* conversion
* filtering
## T&I Unit

* T&I 是最 resource-consuming 的操作,因此 hardwired logic 能減低能源使用量
* T&I Unit 分為 Ray Dispatch Unit (RD)、Traversal Unit (TRV)、Intersection Unit (IST)
### TRV Unit
* 負責 traversal 和 AABB intersection test
* traversal 基於 [Restart Trail for Stackless BVH Traversal](https://dl.acm.org/doi/pdf/10.5555/1921479.1921496)
* 不需要 scheduling logic,因為 ray 在 pipeline 結束後可以直接送到目的地
* 共有 4 個 floating-point adder、4 個 floating-point multiplier、11 個 floating-point comparator
### IST Unit
* triangle intersection 基於 [Wald's algorithm](http://www.sci.utah.edu/~wald/PhD/wald_phd.pdf)
* 共有 12 個 floating-point adder、10 個 floating-point multiplier、1 個 reciprocal unit、5 個 floating-point comparator
### Ray Accumulation Unit (RAU)
* TRV 和 IST 都有 RAU,當 cache miss 時,該 ray 會存在 RAU 中暫存
### Parallel Pipelined Traversal Unit

* 傳統的方法是使用三個 stage 的 pipeline(如左圖),分為 TRV_PRE(Leaf Node Test)、TRV_AABB(Ray-AABB Test)、TRV_POST(Stack Operation)
* 然而,不一定每個 ray 都會進入 TRV_AABB 及 TRV_POST,所以有時必須 inactivate TRV_AABB 和 TRV_POST,造成效率降低
* 右圖為這篇論文採用的方法,將原先的 pipeline 分為三個 independent 的 stage
* crossbar 不需要 fully-connected
## Kernel Optimization for SRP
### Iterative Batch-based Ray Tracing Algorithm
* 為了讓 CGRA 有夠多的 loop-level parallelism 並且減少 prolog/epilog 的 overhead,將 256 個 ray 集成一個 batch,透過 software pipeline 的方式平行處理
* 為了消除 rendering 中的 branch,使用 filter kernel 集合相同種類的 data,分配到其他 kernel 中進一步處理
* 盡可能利用 SRP 支援的 SIMD intrinsics,例如用於計算 position(xyzw)以及 color(rgba)
* 提前將 shading color 計算好並放到 shadow ray data 中,當 T&I 得到結果後,就能直接更新 pixel color 而不用儲存大量的 shading state

* 共有 8 個 kernel:
* Calculating normal vector of intersection point
* Filtering the intersection point which does not need to generate the reflection/refraction ray and which does not need to do texture mapping
* Reflection ray generation
* Refraction ray generation
* Filtering the intersection point which does not need to shade
* Shading
* Shadow ray generation
* Primary ray generation

* T&I Unit 會將光線種類與是否相交分為四類,每一類會讓 SRP 執行不同的 kernel:
* shadow ray & hit
* shadow ray & no hit
* other ray & hit
* other ray & no hit
### Tile-based Ray Tracing
* 採用 20x32 的 tile 進行 rendering,如此可以讓 frame buffer 放在 scratch-pad memory,並且能使 data cache、texture cache 及 T&I cache 的 hit ratio 增加
### Multiple Buffering for SRP-T&I Interface

* 為了讓 SRP 和 T&I 能並行處理,Ray Buffer 採用 triple buffer、T&I 採用 double buffer
* 上圖 Ray Buffer 的 batch 1 經由 T&I Unit 計算完交點資訊後,儲存至 Intersection Buffer 的 batch 1
* 上圖 Ray Buffer 的 batch 1 與 Intersection Buffer 的 batch 1 可計算得 secondary ray,並儲存至 Ray Buffer 的 batch 3
## Compilation and simulation
