3. 3モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MOTIVATION
} GI (MCRT) looks scary
– PBRT本は重すぎ…
– 数式多すぎ…
• Search for “Monte Carlo Integration”, “Importance
Sampling” on Wikipedia
} 本セッション
– より直感的な理解を
– Give more intuitive understanding
– Not for PBRT lovers
– Not for equation lovers
32. 32モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MONTE CARLO INTEGRATION
} A numerical integration suited for
– Integration of a complex high dimensional function
} Draw a random sample
} Calculate a weighted average
∫Ω
L(ω)dω ≈
i<8
∑
i=0
wiLi
=
1
n
i<n
∑
i=0
Li
pdfi
pdfi =
1
π
, n = 8
Formula we see in a textbook
34. 34モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MATT SURFACE EVALUATION
Ray0 Ray1 Ray2 Ray3 Ray4 Ray5 Ray6 Ray7
Hit? 0 0 0 0 0 0 1 1
Brdf 1/pi 1/pi 1/pi 1/pi 1/pi 1/pi 1/pi 1/pi
cos(n,l) c0 c1 c2 c3 c4 c5 c6 c7
Li(ωi) =
∫Ω
fwhiteLi(ωi)cos θdω
(
1
π
c6 +
1
π
c7)
π
8
∫Ω
L(ω)dω
+
x x x x x x x x
35. 35モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
WHERE PI COMES FROM?
} Lambert
} Distribute energy uniformly
} Pure white surface
– Incoming light == sum of outgoing light (reflected
light)
} BRDF for a Lambert surface
flambert(x, ωi, ωo) =
R
π
Li(ωi) =
∫Ω
fwhiteLi(ωi)cos θdω fwhite =
1
π
==
Incoming light Sum of reflected light
The image cannot be displayed. Your computer may not have enough memory to open the
image, or the image may have been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the image and then insert it again.
36. 36モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
REFLECTION SURFACE EVALUATION
Ray0 Ray1 Ray2 Ray3 Ray4 Ray5 Ray6 Ray7
Hit? 0 0 0 0 0 0 1 1
Brdf 0 0 0 0 0 0 0 b7
cos(n,l) c0 c1 c2 c3 c4 c5 c6 c7
(b7c7)
π
8
+
x x x x x x x x
38. 38モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
GLOSSY
} Microfacet
} 様々なglossinessを表現できる
– 鋭いspecularに近いものから鈍いmattに近いものまで
} Torrance-Sparrowモデル
– 表面の法線は分散を持っている (Distribution) D
– フレネル F
– 表面自体のocclusion G
Wide distribution Mid distribution Narrow distribution
fmf (n, l, e) =
DFG
4 cos(n, l)cos(n, e)
39. 39モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
GLOSSY (MICROFACET)
} Microfacet
} 様々なglossinessを表現できる
– 鋭いspecularに近いものから鈍いmattに近いものまで
} Torrance-Sparrowモデル
– 表面の法線は分散を持っている (Distribution) D
– フレネル F
– 表面自体のocclusion G
40. 40モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
GLOSSY SURFACE EVALUATION
Ray0 Ray1 Ray2 Ray3 Ray4 Ray5 Ray6 Ray7
Hit? 0 0 0 0 0 0 1 1
Brdf b0 b1 b2 b3 b4 b5 b6 b7
cos(n,l) c0 c1 c2 c3 c4 c5 c6 c7
(b6c6 + b7c7)
π
8
+
x x x x x x x x
41. ADVANCED TOPICS
} Why noisy result?
} Better sampling
} How can we make a realistic material?
} Complex materials
} Want to have light bounce
} Indirect illumination
} Where are nice effects?
} Distributed ray tracing
56. 56モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
REALISTIC MATT MATERIAL
} モデル化
– 2 layer model
– 1st layer: Specular
– 2nd layer: Matt
} Spec x (1-f) + Matt x f
– f == Fresnel
1-f
f
57. 57モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
WHAT WE HAVE SO FAR
} いくつかのBRDFモデルのみ
} どの表面でもフレネル効果が現れている
} これらだけでリアルなマテリアルの表現ができるのか?
Specular (R) Glossy MattSpecular (T)
77. 77モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MC RT IS SLOW!
} Computation takes a long time
– Monte Carlo ray tracing need to cast a lot of rays/pixel
} Slow iteration
– Inefficient development
– Cannot test a lot
} If we can make it faster, we can
– Test more
• Software reliability
– Go further
• Better algorithm
} Computer history == Make it faster!
} Options
– Algorithm improvement
– Exploit hardware
78. 78モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
WHY OPENCL?
} Using OpenCL is equivalent to
– Efficient usage of computational resources
– Use GPU
– Use multi-core CPU more efficiently
} GPU has high peak performance
– AMD Radeon HD 7970 (GCN Architecture)
• 3.8 TFLOPS (S)
• 974 GFLOPS (D)
• 264 GB/s
– Parallel computation
• 128 SIMD engines
– 64 wide SIMD
79. 79モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
OPENCL
} Open Compute Language (OpenCL) for parallel processors (including GPU)
} OpenCL 1.0 specification released in 2008
} Now v1.2
} ISO C99 with extensions and restrictions
} Software portability
– Cross platform support
• Windows, Mac, Linux
– Multi device support
• GPU
– AMD, NVIDIA, Intel
• CPU
• etc
– Write once, run on all the supported
} Direct Compute
– Need DX
– GPU only
80. 80モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
CPU VECTOR ADD
} CPU code is simple
float* a = new float[n];
float* b = new float[n];
float* c = new float[n];
for(int i=0; i<n; i++)
{
b[i] = i;
c[i] = n;
}
for(int i=0; i<n; i++)
{
a[i] = b[i] + c[i];
}
delete [] a;
delete [] b;
delete [] c;
Memory allocation
Initialization
Computation
Memory deallocation
81. 81モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
TO IMPLEMENT USING OPENCL
} Need to do 3 things
1. OpenCL memory has to be allocated, deallocated
2. Computation has to be written as OpenCL kernel
3. OpenCL kernel has to be executed via OpenCL APIs
82. 82モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MEMORY ALLOCATION/DEALLOCATION
} CPU
– Allocation
– Deallocation
} OpenCL
– Allocation
– Deallocation
float* a = new float[n];
delete [] a;
cl_mem a = clCreateBuffer( context, CL_MEM_READ_WRITE, sizeof(float)*n, 0, &e );
clReleaseMemObject( a );
Memory size in byte
83. 83モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
COMPUTE USING OPENCL KERNEL
} CPU
– n items are executed in serial
} OpenCL
– n items are executed in parallel
– A work item processes an item
– Write program (OpenCL Kernel) for a work item
– Not in the host C code
for(int i=0; i<n; i++)
{
a[i] = b[i] + c[i];
}
__kernel
void addKernel( __global float* a,
__global float* b,
__global float* c )
{
int i = get_global_id(0);
a[i] = b[i] + c[i];
}
__global : for a memory allocated in global memory
__local : for a memory allocated in local memory
Can use the code for computations in this pattern
84. 84モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
EXECUTE OPENCL KERNEL
} Set OpenCL memories as arguments
– Specify the index of the argument
} Execute kernel
clSetKernelArg(kernel1, 0, sizeof(cl_mem), (void*)&a);
clSetKernelArg(kernel1, 1, sizeof(cl_mem), (void*)&b);
clSetKernelArg(kernel1, 2, sizeof(cl_mem), (void*)&c);
clEnqueueNDRangeKernel( queue, kernel1, 1, 0, gSize, lSize, 0, 0, 0 );
__kernel
void addKernel( __global float* a,
__global float* b,
__global float* c )
{
int i = get_global_id(0);
a[i] = b[i] + c[i];
}
Order of an argument
Work group size [64, 1, 1]
Global work size [n,1,1]
90. 90モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
MULTIPLE KERNEL IMPLEMENTATION
} Split the pipeline into multiple kernels
} Pros
– Better HW utilization
– More room for optimization
} Cons
– Need more work than single kernel implementation
– Host has to queue more OpenCL commands
– Each kernel has to read/write ray info
Host:
while( i < maxSamples )
{
executeKernel(RayGenerationKernel);
executeKernel(RayCastKernel);
executeKernel(SampleRayKernel);
executeKernel(RayCastKernel);
executeKernel(AccumulationKernel);
}
Device:
__kernel
void RayCastKernel()
{
}
92. 92モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
DIVERGENCE 2 BOUNCES
} 1st
– Generate Ray
– Cast Ray
– Generate Sample Ray
– Cast Shadow Ray
– Accumulate Result
} 2st
– Generate Ray
– Cast Ray
– Generate Sample Ray
– Cast Shadow Ray
– Accumulate Result
93. 93モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
DIVERGENCE
} 1st
– Generate Ray
– Cast Ray
– Generate Sample Ray
– Cast Shadow Ray
– Accumulate Result
} 2st
– Generate Ray
– Cast Ray
– Generate Sample Ray
– Cast Shadow Ray
– Accumulate Result
94. 94モンテカルロレイトレーシングの基礎からOpenCLによる実装まで | AUG, 2013
TIPS
} Starting
– Single kernel implementation
– Share data types with host (float4)
• Easy to share functions
– Replace pointers to indices
} Debugging
– Printf
– Debug buffers
} Others
– Cache compiled kernel
• Reduce compilation time
Coumans, E., Multithreading and VFX Course note, SIGGRAPH 2013