Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Add windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
raywan committed May 17, 2019
1 parent abc38cb commit da177fe
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,4 @@ MigrationBackup/
toy
renders
*.png
render.png
8 changes: 6 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[ ] build.bat for Windows
[ ] Create Windows entry point i.e. win32_main.cpp
Build:
[ ] Create windows target for ninja build script generator (configure.py)
[x] build.bat for Windows
[x] Create Windows entry point i.e. win32_main.cpp


[ ] Tile size independent multithreaded rendering
- Height and width are must be multiples of the tile size currently
8 changes: 8 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@echo off
if not exist build mkdir build
pushd build
REM NOTE(ray): This will try to compile the non-Windows main file
cl /O2 /EHsc /Zi /I ..\include /c ..\src\*.cpp
REM NOTE(ray): Just manually type in each object file for now
link win32_main.obj render.obj mesh.obj world.obj utils.obj metrics.obj ray.obj bvh.obj camera.obj primitive.obj /OUT:rays.exe
popd
3 changes: 2 additions & 1 deletion configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
sources = []
for root, dirnames, filenames in os.walk(SRC_DIR):
for filename in fnmatch.filter(filenames, '*.cpp'):
sources.append(os.path.join(root, filename))
if 'win32_' not in filename:
sources.append(os.path.join(root, filename))

for source in sources:
n.build(outputs=source.replace(".cpp", ".o").replace('src', BUILD_DIR),
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) {
pthread_join(threads[i], NULL);
}
#else
// Begin tracing
// Begin tracing
puts("Begin tracing...");
RenderArgs ra;
ra.world = &world;
Expand Down
2 changes: 1 addition & 1 deletion src/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool solve_quadratic(const float a, const float b, const float c, float *x0, flo
return true;
}

Sphere sphere_create(float radius, Vec3 world_pos) {
Sphere sphere_create(Vec3 world_pos, float radius) {
Sphere result;
result.radius = radius;
result.position = world_pos;
Expand Down
2 changes: 1 addition & 1 deletion src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct IntersectInfo {
Material *material;
};

Sphere sphere_create(float radius, Vec3 world_pos);
Sphere sphere_create(Vec3 world_pos, float radius);
bool sphere_intersect(Sphere *s, Ray *r, IntersectInfo *out_ii);
Rect3 sphere_get_bounds(Sphere *s);
bool triangle_intersect(Triangle *tri, Ray *r, IntersectInfo *out_ii);
Expand Down
1 change: 1 addition & 0 deletions src/ray.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ray.h"
#include <math.h>
#include <stdlib.h>
#include <algorithm>

Ray ray_init(Vec3 origin, Vec3 dir) {
Ray result;
Expand Down
146 changes: 146 additions & 0 deletions src/win32_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <queue>

#include <rw/rw_time.h>
#define RWM_IMPLEMENTATION
#include <rw/rw_math.h>

#include "render.h"
#include "mesh.h"
#include "global.h"
#include "ray.h"
#include "camera.h"
#include "primitive.h"
#include "world.h"
#include "bvh.h"
#include "utils.h"
#include "metrics.h"

HANDLE jq_mutex;

unsigned int worker(void *t_arg) {
WorkerData *data = (WorkerData *) t_arg;
while (1) {
switch (WaitForSingleObject(jq_mutex, 0xFFFFFFFF)) {
case WAIT_OBJECT_0: {
if (data->job_queue->size() == 0) {
ReleaseMutex(jq_mutex);
return 1;
}
Tile t = data->job_queue->front();
data->job_queue->pop();
ReleaseMutex(jq_mutex);
// Do work
render(data, t);
} break;
case WAIT_ABANDONED:
return 0;
}
}
return 1;
}

int main(int argc, char *argv[]) {
rwtm_init();
mtr_start_time = rwtm_now();

// Initialize the camera
puts("Initializing camera...");
int camera_shot = 0;
if (argc > 1) {
camera_shot = atoi(argv[1]);
if (argc == 3) {
output_name = argv[2];
}
}

print_run_info();

Camera camera;
if (camera_shot == 0) {
#if 0
camera = camera_init_default();
} else if (camera_shot == 1) {
#endif
camera = camera_init(
rwm_v3_init(0.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
} else if (camera_shot == 2) {
camera = camera_init(
rwm_v3_init(-1.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, 0.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
}

// Intialize scene
World world;
create_world(&world);
world.bvh_prims = bvh_preprocess_world(&world);

int *data = (int *) malloc(WIDTH * HEIGHT * sizeof(int));
int *cur_data = data;

#if NUM_THREADS != 1
puts("Begin multithreaded tracing...");

std::queue<Tile> job_queue;
construct_tiles(&job_queue);

HANDLE threads[NUM_THREADS];
WorkerData worker_data[NUM_THREADS];

jq_mutex = CreateMutex(NULL, false, NULL);
if (jq_mutex == NULL) {
puts("Create jq_mutex failed");
return 1;
}

unsigned long tid;
for (int i = 0; i < NUM_THREADS; i++) {
worker_data[i].tid = i;
worker_data[i].job_queue = &job_queue;
worker_data[i].film = data;
worker_data[i].world = &world;
worker_data[i].camera = &camera;
threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) worker, &worker_data[i], 0, &tid);
}
WaitForMultipleObjects(NUM_THREADS, threads, TRUE, 0xFFFFFFFF);
for (int i = 0; i < NUM_THREADS; i++) {
CloseHandle(threads[i]);
}
CloseHandle(jq_mutex);
#else
// Begin tracing
puts("Begin tracing...");
RenderArgs ra;
ra.world = &world;
ra.camera = &camera;
ra.film = data;
render(&ra);

#endif

puts("Writing results to PNG...");
int result;
if (output_name != NULL) {
result = write_png(output_name, data, WIDTH, HEIGHT);
} else {
result = write_png("render.png", data, WIDTH, HEIGHT);
}
assert(result == 1);
puts("");

print_post_run_metrics();
puts("Done.");

return 0;
}
14 changes: 7 additions & 7 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ void create_world(World *world) {
puts("Creating world...");
// world->spheres.push_back((Sphere) {rwm_v3_init(0,0.25,-0.8), 0.5});
// world->spheres.push_back((Sphere) {rwm_v3_init(-1,0.25,-0.5), 0.5});
world->spheres.push_back((Sphere) {rwm_v3_init(0.6,0.12,0.0), 0.25});
world->spheres.push_back((Sphere) {rwm_v3_init(0,0.25,-0.5), 0.5});
world->spheres.push_back(sphere_create(rwm_v3_init(0.6,0.12,0.0), 0.25));
world->spheres.push_back(sphere_create(rwm_v3_init(0,0.25,-0.5), 0.5));
// world->spheres.push_back((Sphere) {rwm_v3_init(1,0.25,-1), 0.5});
// world->spheres.push_back((Sphere) {rwm_v3_init(-1,0.25,-1), 0.5});
// world->spheres.push_back((Sphere) {rwm_v3_init(-1,0.25,-2), 1.5});
world->sphere_materials.push_back((Material) {M_REFLECT, rwm_v3_init(1.0, 1.0, 1.0)});
world->sphere_materials.push_back({M_REFLECT, rwm_v3_init(1.0, 1.0, 1.0)});
// world->sphere_materials.push_back((Material) {M_RR, rwm_v3_init(1.0, 1.0, 1.0), 1.55});
world->sphere_materials.push_back((Material) {M_DIFFUSE, rwm_v3_init(1.0, 0.0, 0.0)});
world->sphere_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 0.0, 0.0)});
// world->sphere_materials.push_back((Material) {M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0)});

// world->planes.push_back((Plane) {rwm_v3_init(0,-0.5,-1), rwm_v3_init(0,1.0,0)});
Expand All @@ -35,10 +35,10 @@ void create_world(World *world) {
// world->meshes.push_back(&suz);
world->meshes.push_back(plane);
// world->mesh_materials.push_back((Material) {M_REFLECT, rwm_v3_zero()});
world->mesh_materials.push_back((Material) {M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, true});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, true});
puts("Adding lights");

world->lights.push_back((Light) {
world->lights.push_back({
LT_SPHERE,
rwm_v3_init(2.0, 2.0, 1.0), // position
rwm_v3_init(1.0f, 1.0f, 1.0f), // color
Expand All @@ -53,7 +53,7 @@ void create_world(World *world) {
// });

#if 1
world->lights.push_back((Light) {
world->lights.push_back({
LT_DIRECTION,
rwm_v3_zero(),
rwm_v3_init(1.0f, 1.0f, 1.0f), // color
Expand Down

0 comments on commit da177fe

Please sign in to comment.