Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…th-Tracer into master
  • Loading branch information
SYDNEY MILLER committed Oct 3, 2020
2 parents 4ec1b92 + f4db1fe commit db37af1
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ set(headers
src/sceneStructs.h
src/preview.h
src/utilities.h
src/timer.h
)

set(sources
Expand Down
Binary file added img/part1Chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/renders/part1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/renders/part1Diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/renders/part1Specular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int iteration;
int width;
int height;

double totalTime = 0.0;

//-------------------------------
//-------------MAIN--------------
//-------------------------------
Expand Down Expand Up @@ -135,10 +137,13 @@ void runCuda() {
// execute the kernel
int frame = 0;
pathtrace(pbo_dptr, frame, iteration);
double time = timer().getGpuElapsedTimeForPreviousOperation();
totalTime += time;

// unmap buffer object
cudaGLUnmapBufferObject(pbo);
} else {
//std::cout << "time: " << totalTime << std::endl;
saveImage();
pathtraceFree();
cudaDeviceReset();
Expand Down
30 changes: 23 additions & 7 deletions src/pathtrace.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
#include "pathtrace.h"
#include "intersections.h"
#include "interactions.h"
#include "timer.h"

PerformanceTimer& timer()
{
static PerformanceTimer timer;
return timer;
}

#define ERRORCHECK 1

Expand Down Expand Up @@ -77,11 +84,11 @@ static Material* dev_materials = NULL;
static PathSegment* dev_paths = NULL;
static ShadeableIntersection* dev_intersections = NULL;

static bool cache_first_intersections = false;
static bool cache_first_intersections = true;
static ShadeableIntersection* dev_first_intersections = NULL;


static bool sort_by_material = false;
static bool sort_by_material = true;

// TODO: static variables for device memory, any extra info you need, etc
// ...
Expand Down Expand Up @@ -365,12 +372,19 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
// Shoot ray into scene, bounce between objects, push shading chunks
dim3 numBlocksPixels = (pixelcount + blockSize1d - 1) / blockSize1d;
bool iterationComplete = false;

timer().startGpuTimer();
while (!iterationComplete) {
dim3 numblocksPathSegmentTracing = (num_paths + blockSize1d - 1) / blockSize1d;

if (cache_first_intersections && depth == 0 && iter != 1) {
// if it is the firts bounce of the noot first intersection, get the saved intersections
thrust::copy(thrust::device, dev_first_intersections, dev_first_intersections + num_paths_start, dev_intersections);

// sort intersections with similar materials together
if (sort_by_material) {
thrust::sort_by_key(thrust::device, dev_intersections, dev_intersections + num_paths, dev_paths, compareIntersections());
}
}
else {
// clean shading chunks
Expand All @@ -382,15 +396,16 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
checkCUDAError("trace one bounce");
cudaDeviceSynchronize();

// sort intersections with similar materials together
if (sort_by_material) {
thrust::sort_by_key(thrust::device, dev_intersections, dev_intersections + num_paths, dev_paths, compareIntersections());
}


// if it is the first bounce of the first iteration, store the intersections
if (cache_first_intersections && depth == 1 && iter == 1) {
if (cache_first_intersections && depth == 0 && iter == 1) {
thrust::copy(thrust::device, dev_intersections, dev_intersections + num_paths_start, dev_first_intersections);
}
else if (sort_by_material) {
// sort intersections with similar materials together
thrust::sort_by_key(thrust::device, dev_intersections, dev_intersections + num_paths, dev_paths, compareIntersections());
}
}
depth++;

Expand All @@ -404,6 +419,7 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
iterationComplete = true;
}
}
timer().endGpuTimer();

// Assemble this iteration and apply it to the image
//dim3 numBlocksPixels = (pixelcount + blockSize1d - 1) / blockSize1d;
Expand Down
3 changes: 3 additions & 0 deletions src/pathtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include <vector>
#include "scene.h"
#include "timer.h"

void pathtraceInit(Scene *scene);
void pathtraceFree();
void pathtrace(uchar4 *pbo, int frame, int iteration);

PerformanceTimer& timer();
103 changes: 103 additions & 0 deletions src/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <cuda.h>
#include <cuda_runtime.h>

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <chrono>
#include <stdexcept>

/**
* This class is used for timing the performance
* Uncopyable and unmovable
*
* Adapted from WindyDarian(https://github.com/WindyDarian)
*/
class PerformanceTimer
{
public:
PerformanceTimer()
{
cudaEventCreate(&event_start);
cudaEventCreate(&event_end);
}

~PerformanceTimer()
{
cudaEventDestroy(event_start);
cudaEventDestroy(event_end);
}

void startCpuTimer()
{
if (cpu_timer_started) { throw std::runtime_error("CPU timer already started"); }
cpu_timer_started = true;

time_start_cpu = std::chrono::high_resolution_clock::now();
}

void endCpuTimer()
{
time_end_cpu = std::chrono::high_resolution_clock::now();

if (!cpu_timer_started) { throw std::runtime_error("CPU timer not started"); }

std::chrono::duration<double, std::milli> duro = time_end_cpu - time_start_cpu;
prev_elapsed_time_cpu_milliseconds =
static_cast<decltype(prev_elapsed_time_cpu_milliseconds)>(duro.count());

cpu_timer_started = false;
}

void startGpuTimer()
{
if (gpu_timer_started) { throw std::runtime_error("GPU timer already started"); }
gpu_timer_started = true;

cudaEventRecord(event_start);
}

void endGpuTimer()
{
cudaEventRecord(event_end);
cudaEventSynchronize(event_end);

if (!gpu_timer_started) { throw std::runtime_error("GPU timer not started"); }

cudaEventElapsedTime(&prev_elapsed_time_gpu_milliseconds, event_start, event_end);
gpu_timer_started = false;
}

float getCpuElapsedTimeForPreviousOperation() //noexcept //(damn I need VS 2015
{
return prev_elapsed_time_cpu_milliseconds;
}

float getGpuElapsedTimeForPreviousOperation() //noexcept
{
return prev_elapsed_time_gpu_milliseconds;
}

// remove copy and move functions
PerformanceTimer(const PerformanceTimer&) = delete;
PerformanceTimer(PerformanceTimer&&) = delete;
PerformanceTimer& operator=(const PerformanceTimer&) = delete;
PerformanceTimer& operator=(PerformanceTimer&&) = delete;

private:
cudaEvent_t event_start = nullptr;
cudaEvent_t event_end = nullptr;

using time_point_t = std::chrono::high_resolution_clock::time_point;
time_point_t time_start_cpu;
time_point_t time_end_cpu;

bool cpu_timer_started = false;
bool gpu_timer_started = false;

float prev_elapsed_time_cpu_milliseconds = 0.f;
float prev_elapsed_time_gpu_milliseconds = 0.f;
};

0 comments on commit db37af1

Please sign in to comment.