Speeding up Computer Graphics Speeding up Computer Graphics
Making cool pictures fast!
Benson Muite
https://bkmgit.gitlab.io/cgintro
This work is licensed under a Creative Commons Attribution 4.0 International License .
Why is Computer Graphics Interesting?
Generating images using a program
Video games
Cartoons
Engineering simulation visualization
Architecture visualization
Medical scan visualization
...
Background Material
Many different algorithms
Consider Raytracing
Presentation follows descriptions from
Raytracing
Physics of Images
Light is reflected and refracted by objects
A small portion ends up in our eyes, for simplicity, only one ray is continued to the eye
M i r r o r ) E y e P r i s m L i g h t s o u r c e
Raytracing
Backwards physics of images
For simplicity only one ray is traced from the eye
M i r r o r ) E y e P r i s m L i g h t s o u r c e
Raytracing
M i r r o r ) E y e P r i s m L i g h t s o u r c e M o n i t o r
Image Generation Problem
The setup here is two dimensional
Corresponding image is a line
The line is represented by discrete points
What is the intensity of the color on the monitor?
Image Generation Algorithm
For each point on the monitor, determine vector to the eye
Continue the vector forward in space
Determine which objects get hit
Color the point on the monitor appropriately
Assume the light source has only one color to get binary image
Need to determine color on the monitor
Only a Light Source and a Monitor
) E y e L i g h t s o u r c e M o n i t o r
Coordinates
( 1 0 0 0 , 1 0 0 0 ) ) E y e L i g h t s o u r c e ( 0 , 2 0 0 0 ) C e n t e r : ( 5 0 0 0 , 2 0 0 0 ) M o n i t o r R a d i u s : 5 0 0 ( 1 0 0 0 , 3 0 0 0 )
When Does a Ray Intersect the Light Source?
Geometry
Coordinates of a ray $$(t(ex-mx) + ex,t(ey-my) + ey)$$
t: parameter; ex,ey: eye x,y coordinates; mx,my: monitor x,y coordinates
Equation of light source boundary $$lr^2 = (lcx - lx)^2 + (lcy - ly)^2$$
lx,ly: point on light source boundary; lr: light source radius; lcx,lcy: center of light source
When Does a Ray Intersect the Light Source?
Do line and disc intersect?
$$(t(ex-mx) + ex,t(ey-my) + ey)$$
$$lr^2 = (lcx - lx)^2 + (lcy - ly)^2$$
$$lx = t(ex-mx) + ex, \quad ly = t(ey-my) + ey$$
$$lr^2 = (lcx - t(ex-mx) - ex)^2 + \\ (lcy - t(ey-my) - ey)^2$$
Find Real Solutions Part 1
$$t^2((ex-mx)^2+(ey-my)^2) - \\
2t( (ex-mx)(lcx-ex) + (ey-my)(lcy-ey)) + \\
(lcx-ex)^2 + (lcy-ey)^2 - lr^2 =0$$
- Recall
$$ ax^2 + bx +c =0$$
$$ x = \frac{-b\pm\sqrt{b^2-4ac}}{2a}$$
Find Real Solutions Part 2
$$b^2 -4ac >= 0$$
- Cancelling factor of 4
$$( (ex-mx)(lcx-ex) + (ey-my)(lcy-ey))^2 - \\
((ex-mx)^2+(ey-my)^2) \times \\
( (lcx-ex)^2 + (lcy-ey)^2 - lr^2 ) >= 0$$
C Program
#include "stdio.h"
#include "math.h"
int main () {
const int myt = 1000 ;
const int myb = 3000 ;
const int mx = 1000 ;
const int ex = 0 ;
const int ey = 2000 ;
const int lcx = 5000 ;
const int lcy = 3000 ;
const int lr = 500 ;
int my;
int real_solution;
Listing 1: First part of program - variable declaration.
C Program
for (my = myt; my < myb; my++){
int b = 2 *((ex-mx)*(lcx-ex) + (ey-my)*(lcy-ey));
int a = pow (ex-mx,2 ) + pow (ey-my,2 );
int c = pow (lcx-ex,2 ) - pow (lcy-ey,2 ) - pow (lr,2 );
real_solution = ( (pow (b/2 ,2 ) - a*c) >=0 );
printf ("%d %d\n" ,my,real_solution);
}
return 0 ;
}
Listing 2: Second part of program - main loop.
Generated Image
)
Parallelization
Each of the rays can be computed independently
In one dimension, fast runtime
In two dimensions, significant improvement
One reason for using graphics cards with many compute units
Extension to Two Dimensional Projection
Geometry
Coordinates of a ray $$(t(ex-mx) + ex,t(ey-my) + ey, t(ez-mz) + ez)$$
t: parameter; ex,ey,ez: eye coordinates; mx,my,mz: monitor coordinates
Equation of light source boundary $$lr^2 = (lcx - lx)^2 + (lcy - ly)^2 + (lcz - lz)^2$$
lx,ly,lz: point on light source boundary; lr: light source radius; lcx,lcy,lcz: center of light source
When Does a Ray Intersect the Light Source?
Do line and sphere intersect?
$$(t(ex-mx) + ex,t(ey-my) + ey),t(ez-mz) + ez)$$
$$lr^2 = (lcx - lx)^2 + (lcy - ly)^2 + (lcz - lz)^2$$
$$lx = t(ex-mx) + ex, \quad ly = t(ey-my) + ey, \quad lz = t(ez-mz) + ez$$
$$lr^2 = (lcx - t(ex-mx) - ex)^2 + \\ (lcy - t(ey-my) - ey)^2 + \\ (lcz - t(ez-mz) - ez)^2$$
Condition for Real Solutions
$$( (ex-mx)(lcx-ex) + (ey-my)(lcy-ey) + (ez-mz)(lcz-ez))^2 - \\
((ex-mx)^2 + (ey-my)^2 + (ez-mz)^2) \times \\
( (lcx-ex)^2 + (lcy-ey)^2 + (lcz-ez)^2 - lr^2 ) >= 0$$
C Program Code Snippet
for (my = myt; my < myb; my++){
for (mz = mzt; mz < mzb; mz++){
int b = 2 *((mx-ex)*(lcx-ex) + (my-ey)*(lcy-ey) + (mz-ez)*(lcz-ez));
int a = pow (ex-mx,2 ) + pow (ey-my,2 ) + pow (ez-mz,2 );
int c = pow (lcx-ex,2 ) + pow (lcy-ey,2 ) + pow (lcz-ez,2 ) - pow (lr,2 );
real_solution[ (mz-mzt) + (my-myt)*(mzb-mzt) ] = (pow (b/2 ,2 ) - a*c >= 0 );
}
}
Listing 3: Two dimensions - main loop.
SYCL Program Code Snippet
sycl::default_selector deviceSelector;
sycl::queue queue (deviceSelector) ;
sycl::buffer<int , 1> d_real_solution (real_solution, (myb-myt)*(mzb-mzt)) ;
queue .submit([&d_real_solution,ex,ey,ez,mx,myb,myt,mzb,lcx,lcy,lcz,lr](
sycl::handler& cgh) {
auto real_solution = d_real_solution.get_access
<sycl::access::mode::write>(cgh);
Listing 4: Two dimensions - parallelization setup.
SYCL Program Code Snippet
cgh.parallel_for<class render >(
sycl : :nd_range<1 >{(myb-myt)*(mzb-mzt),1 },
[real_solution,ex,ey,ez,mx,myb,myt,mzb,lcx,lcy,lcz,lr](sycl::nd_item<1 > item){
int global_id = item.get_global_linear_id();
int kk = global_id % (mzb-mzt);
int jj = (global_id - kk) / (myb-myt);
int my = jj + myt;
int mz = kk + mzt;
int b = 2 *((mx-ex)*(lcx-ex) + (my-ey)*(lcy-ey) + (mz-ez)*(lcz-ez));
int a = pow (ex-mx,2 )+pow (ey-my,2 )+pow (ez-mz,2 ) );
int c = pow (lcx-ex,2 )+pow (lcy-ey,2 )+pow (lcz-ez,2 ) - pow (lr,2 );
real_solution[kk + jj*(mzb-mzt)] = pow ( b/2 , 2 ) - a * c >= 0 ;
});
});
queue .wait();
Listing 5: Two dimensions - parallel main loop.
Resulting image
Image of sphere that is created
Interactive Demonstration
Example Open Source Rendering Software
References and Further Reading
[
MacWright20 ] MacWright, T., Schaub, T., Diachok R. and Cutler D. (2020).
Literate RayTracer .
Acknowledgements
Slides made with Markdeep-slides