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?


   

Background Material


   

Raytracing


   

Physics of Images

Mirror)EyePrismLightsource


   

Raytracing

Mirror)EyePrismLightsource


   

Raytracing

Mirror)EyePrismLightsourceMonitor


   

Image Generation Problem


   

Image Generation Algorithm


   

Only a Light Source and a Monitor

)EyeLightsourceMonitor


   

Coordinates

(1000,1000))EyeLightsource(0,2000)Center:(5000,2000)MonitorRadius:500(1000,3000)


   

When Does a Ray Intersect the Light Source?


   

When Does a Ray Intersect the Light Source?


   

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"
// Compile with gcc OneDimensionalRender.c -lm
int main(){
  const int myt = 1000;   // monitor y location top
  const int myb = 3000;   // monitor y location bottom
  const int mx = 1000;    // monitor x location
  const int ex = 0;       // eye x location
  const int ey = 2000;    // eye y location
  const int lcx = 5000;   // light center x location
  const int lcy = 3000;   // light center y location
  const int lr = 500;     // light radius
  int my;                 // monitor y location
  int real_solution;      // 1 if real solution, 0 otherwise
 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


   

Extension to Two Dimensional Projection


   

When Does a Ray Intersect the Light Source?


   

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

[ Gambetta21] Gambetta, G. (2021). Computer Graphics From Scratch.
[ MacWright20] MacWright, T., Schaub, T., Diachok R. and Cutler D. (2020). Literate RayTracer.
[ Mirazchiyski20] Mirazchiyiski, G. (2020). Ray-tracing in a Weekend with SYCL: Basic sphere tracing
[ PharrJakobHumphreys18] Pharr, M., Jakob, W., Humphreys, G. (2018). Physically Based Rendering: From Theory to Implementation
[ Shirley20] Shirley, P. (2020). Ray Tracing in One Weekend

   

Acknowledgements

Slides made with Markdeep-slides

formatted by Markdeep 1.11