<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass{rtg}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{subcaption}
\newcommand{\OpenGL}{OpenGL\xspace}
\newcommand*\diff{\mathop{}\!\mathrm{d}}
\newcommand{\f}[1]{\operatorname{#1}}
\newcommand{\todo}[1]{\color[red]{\textbf{#1}}}

\title{Assignment 3: Importance Sampling}
\deadline{2021-06-02 23:59}%2020-05-13 23:59
\teaser{
\hspace*{\fill}
\includegraphics[width=0.32\linewidth]{figures/ajax_pt_uniform.png}
\hfill
\includegraphics[width=0.32\linewidth]{figures/ajax_pt_cosine.png}
\hfill
\includegraphics[width=0.32\linewidth]{figures/ajax_pt_nee.png}
\hspace*{\fill}
\label{fig:figintro}
}

\setcounter{section}{0}

\begin{document}

\maketitle

In this assignment you will extend the Monte Carlo rendering system from the last assignment with importance sampling of various functions and next event estimation. In the above image, we see what these methods can do: the left scene is rendered with uniform hemisphere sampling. In the center, we use cosine-weighted hemisphere sampling (importance sampling). On the right, we use next event estimation to perform surface sampling in a recursive path tracer. Images were rendered with the same number of samples per pixel (32).

\textbf{We have updated the \texttt{assignments} repository. Please merge all upstream changes before starting to work.}
\begin{verbatim}
git checkout master
git pull
git merge submission2       # just to be sure
git push                    # just in case something fails, make a backup
git remote add upstream git@submission.cg.tuwien.ac.at:rendering-2020/assignments.git
git pull upstream master
# resolve any merge conflict, or just confirm the merge. 
git push
\end{verbatim}
We also provide a reference implementation for assignment 2, you can download it from TUWEL.

\section{Sample Warping (7 easy points, 9 bonus points)}
Random numbers are often generated uniformly in the range between 0 and 1. We can combine multiple such random numbers to sample cartesian domains uniformly, but different distributions are needed, e.g., to get uniform distribution in a non-cartesian domain (for recursive rendering, we need to sample the hemisphere for instance), or for importance sampling techniques. This task can be fully solved in \texttt{warp.cpp}.

The process of \emph{changing} an existing distribution is called warping.
In this assignment, you will start with easily obtainable, canonic random inputs, and convert them to new, useful distributions.
The input to all warping functions are two uniformly distributed ([0, 1)) random numbers, and the output are samples on the target domain. 
The input is always a 2D vector with values of two canonical random variables $\xi_1, \xi_2$. 

In many cases, we may have an existing sample and need to obtain its PDF value for a given sampling strategy, thus a method to produce the PDF from input samples is also required.
The input is always a sample $x$, 2D or 3D, for which a PDF value $p(x)$ value should be computed. 

To visualize and check your implementations, we will be using the \texttt{warptest} executable, which is part of the Nori framework. 
You should complete several of the warping functions that it tests. 
For an introduction on how to use \texttt{warptest} and what each distribution is supposed to do, please refer to the Assignment 3, Part 1 from the Nori home page (\url{https://wjakob.github.io/nori/}).
\textbf{Note that our scoring system is different, please find it below}.
\texttt{SquareToUniformHemisphere} is already there, some of you were already cleverly using it in the first assignment to do uniform hemisphere sampling. 

\begin{description}
	\item[squareToTent] 2 points, test your basic Monte Carlo sampling knowledge, \textbf{bonus}
	\item[squareToUniformDisk] 3 points, \textbf{required}
	
	\textbf{Sampling:} 
	Use the input canonic variables to generate samples $(r,\theta)$ in polar coordinates where $r \in [0, 1)$ and $\theta \in [0, 2\pi)$, such that they are uniformly distributed when transformed to Cartesian coordinates $(x,y)$. 
	Return the current sample $(x, y)$ at the end of the function body. 
	
	\begin{figure}[h]
		\includegraphics[trim={0 0 5cm 1cm},clip,height=4.7cm]{figures/Capture3.png}
		\hfill
		\includegraphics[trim={0 0 5cm 1cm},clip,height=4.7cm]{figures/Capture2.png}
		\caption{Reference solutions for uniformly distributed samples on the unit disk}
	\end{figure}
	
	\textbf{PDF:} The input is a 2D vector with a sample location in Cartesian coordinates $(x,y)$ on a square, with $x,y \in [-1, 1)$ . 
	Return the proper value for the corresponding result from the uniform distribution PDF $p(x,y)$ on the disk.
	Note: For a uniform distribution, the PDF is constant. 
	Just make sure that the sample location is valid!
	
	\item[squareToUniformSphere] 2 points, can use it to implement spherical lights, \textbf{bonus}
	
	\item[squareToCosineHemisphere] 4 points if inversion method, 1 point if Malley's, \textbf{required}
	
	\textbf{Sampling:} The input is a 2D vector \texttt{sample} that holds values of two canonical random variables $\xi_1, \xi_2$. 
	Use them to generate samples $(\theta, \phi)$ on the unit hemisphere such that they have a distribution proportional to $\cos(\theta)$ (i.e., more samples the closer we get to the pole of the hemisphere) and convert them to $\omega$ with the transformation for spherical coordinates.
	Return the sample $(x,y,z)$ at the end of the function body. 
	
	\begin{figure}[h]
		\includegraphics[trim={0 0 5cm 1cm},clip,height=4.7cm]{figures/Capture5.png}
		\hfill
		\includegraphics[trim={0 0 5cm 1cm},clip,height=4.7cm]{figures/Capture.png}
	\end{figure}
	
	\textbf{PDF:} Input is a 3D vector with a sample location $\omega$ on the unit \textbf{sphere} in Cartesian coordinates $(x,y,z)$ with all values in range $[0, 1)$ and $\sqrt{x^2+y^2+z^2} = 1$ ($z$ is up). 
	Return the appropriate result for the PDF value $p(\omega)$.
	Compute and return the appropriate result for a PDF with distribution $p(\omega) \propto \cos(\theta)$.
	
	\item[squareToBeckmann] 5 points, used for materials (needed in last assignment!), \textbf{bonus} 
	
	
	
\end{description}

\section{Importance Sampling (18 points, 20 bonus points)}
\paragraph*{Use cosine-weighted hemisphere samples for diffuse materials (8 easy points)}
Use the cosine-weighted hemisphere sampling method, as described in the lecture. First make sure that your direct lighting and path tracing integrators use the diffuse BSDF class appropriately, then extend the diffuse BSDF with cosine-weighted hemisphere sampling. Ideally, you can reuse your warping solutions from the first part of this assignment! The BSDF should switch between using cosine-weighted and uniform hemisphere sampling, depending on the value of the \texttt{use\_cosine} flag provided by each object's material (default: \texttt{false}). Note that this affects both the sampling and PDF computation! Confirm for yourself that cosine-weighted hemisphere sampling can reduce the noise in your scenes. To test this, compare the output of the test scenes that end in \texttt{uniform} with the ones that end in \texttt{cosine}. The latter use cosine-weighted hemisphere sampling and should give slightly cleaner results.

\paragraph*{Bonus: Multiple Importance Sampling for Direct Lighting (10 points)}
Implement MIS between hemisphere sampling and light surface sampling using the balance heuristic in your direct lighting integrator.
Whether or not MIS is used should be parameterizable via boolean \texttt{mis\_sampling} (default \texttt{false}) in the test files.
Choose between the two sampling strategies with equal proability, generate the sample using the chosen method and compute the sample's probability with both methods.
Make sure that you use the surface's BSDF to generate the hemisphere samples to benefit from cosine-weighted hemisphere sampling if it is enabled by a material. 
Then use the equations from the lecture to compute the proper MIS weight. Return the contribution that you would get with the chosen method, multiplied by the MIS weight and a (simple!) compensation term for the choice you made when picking one method over the other.
You should use the balance heuristic, simpler heuristics will count but not give full points.
You can test MIS on the \texttt{ajax-2lights\_dl*.xml} scenes, where you should be able to observe the following: the small area light is better suited for surface sampling, while the larger one is better with cosine-weighted hemisphere sampling, but MIS can give you the best of both worlds.
%The Sponza and San Miguel Scenes also have large skydomes in addition to a sun (but work only with the mesh based sampler).
Feel free to also explore ideas that we didn't describe here (rays that miss are black by default, but you could use a sky colour or an environment map).
These things do not go unseen :)

\paragraph*{Next Event Estimation (10 points, 10 bonus points)}
Implement next event estimation (NEE) for your diffuse path tracer, according to the lecture slides. It should be active depending on a boolen \texttt{nee} in the test file (default \texttt{false}).
That is, on every bounce, you create one light surface sample and try to connect to compute direct lighting with surface sampling.
Another ray is then sent out to retrieve indirect light in the next bounce. Make sure that you use the BSDF to generate the indirect sample to benefit from cosine-weighted hemisphere sampling of indirect light on materials that use it. 
If you implement NEE, be careful not to erroneously count the emittance twice (i.e., first when doing the light surface sampling and then when hitting a light source randomly). 
To get a correct image, the emittant surface points that your ray hits should only be considered on the first intersection or if the last material did not support hemisphere sampling (e.g., mirrors!).  
For all other light, the illumination is computed via direct lighting, i.e., one bounce in the future (hence, "next event").
For further details, please see the lecture slides. Just as a heads-up: implementing NEE will dramatically improve the quality of your renderings! In combination with spatial acceleration structures, you should now be able to render impressive scenes fast!  To test this, compare the output of the test scenes that end in \texttt{uniform} or \texttt{cosine} with the ones that end in \texttt{nee}. The latter use next event estimation and should give significantly cleaner results.

If you pay close attention, you may see that NEE is just a very special case of multiple importance sampling! The way that NEE was described in the lecture was using the 0/1 heuristic of MIS: at the first hit, we choose hemisphere sampling for light sources, afterwards we use light surface sampling. Hence, we can say that we use MIS weights for both techniques that are either 0 or 1, depending on the recursion depth. Instead of this 0/1 heuristic, you may also implement NEE with the balance heuristic (10 bonus points). This is a somewhat challenging exercise, not so much in code, but to get to the correct solution mathematically and making sense of it. It is mostly for you to test your limits on combining multiple complex concepts!

\section{Further Bonus Points}

\paragraph{Metropolis-Hastings for Cosine-Weighted Hemisphere Sampling (10 points)}
The Metropolis-Hastings algorithm is an advanced and very essential achievement of computer science. It actually enables you to importance-sample functions whose distribution you don't know!
However, depending on the problem you pose it, it might not be the most efficient method for getting a solution. For this task, familiarize yourself with the Metropolis-Hastings algorithm and Metropolis Sampling (Wikipedia and PBR book, chapter 13). Use the Metropolis-Hastings algorithm to perform cosine-weighted hemisphere sampling without relying on a closed-form solution. 
How does it compare to your previous solution for cosine-weighted hemisphere sampling?

\paragraph{Metropolis Light Transport (up to 1000 points)}
Metropolis Sampling is the basis of the Metropolis Light Transport algorithm. If you have too much time on your hands and want to go down as a hero in the history of this lecture, feel free to attempt an implementation of it. Chapter 16 of the PBR book contains some introductory information, but you will have to spend additional effort researching the required backgrounds. For something special like this you may take your time until the end of the semester (or even beyond). Contact us for details if you need further guidance or suggestions. Good luck!

\section*{Submission format}

%To be announced.
\input{submission.tex}

\section*{Words of wisdom}
\begin{itemize}
\item If you are having trouble with performance, consider changing the resolution and/or number of samples for your test cases.
\item The warp tests only check if the samples you generate match the corresponding PDFs you define. Best start with the PDFs and then try to match them with sampling.
\item Hemisphere sampling, next event estimation and MIS are all methods for integrating the same integral. Given enough samples, they all should converge to the same result.
\item If you have questions, please use TUWEL, but refrain from posting critical code sections.
\item You are encouraged to write your own test cases to experiment with challenging scenarios.
\item Tracing rays is expensive. You don't want to render high resolution images or complex scenes for testing. You may also want to avoid the \texttt{Debug} mode if you don't actually need it (use a release with debug info build!).
\item To reduce the waiting time, Nori runs multi-threaded by default. To make debugging easier, you will want to set the number of threads to 1. To do so, simply execute Nori with the additional arguments \texttt{-t 1}.
\end{itemize}

\end{document}


</pre></body></html>