Skip to content

Commit

Permalink
Move more args handling to main()
Browse files Browse the repository at this point in the history
Moving a lot of the state set up that is scattered in random places to
where that setup is actually needed.
Render plane now gets quantized right before starting the render,
instead of in the setup code.
We also now apply resolution overrides to a copy of the camera, instead
of overwriting the value for every camera in the actual scene state.
  • Loading branch information
vkoskiv committed Nov 13, 2023
1 parent 7fab1cb commit 06fe172
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 83 deletions.
2 changes: 0 additions & 2 deletions src/api/c-ray.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ bool cr_renderer_set_num_pref(struct cr_renderer *ext, enum cr_renderer_param p,
}
case cr_renderer_override_width: {
r->prefs.override_width = num;
r->prefs.override_dimensions = true;
return true;
}
case cr_renderer_override_height: {
r->prefs.override_height = num;
r->prefs.override_dimensions = true;
return true;
}
case cr_renderer_override_cam: {
Expand Down
52 changes: 52 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,58 @@ int main(int argc, char *argv[]) {
goto done;
}

// FIXME: Remove global options table, store it in a local in main() and run overrides
// from there.

// Now check and apply potential CLI overrides.
if (args_is_set("thread_override")) {
size_t threads = args_int("thread_override");
int64_t curr = cr_renderer_get_num_pref(renderer, cr_renderer_threads);
if (curr != (int64_t)threads) {
logr(info, "Overriding thread count to %zu\n", threads);
cr_renderer_set_num_pref(renderer, cr_renderer_threads, threads);
// prefs->fromSystem = false; FIXME
}
}

if (args_is_set("samples_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override samples when in worker mode\n");
} else {
int samples = args_int("samples_override");
logr(info, "Overriding sample count to %i\n", samples);
cr_renderer_set_num_pref(renderer, cr_renderer_samples, samples);
}
}

if (args_is_set("dims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override dimensions when in worker mode\n");
} else {
int width = args_int("dims_width");
int height = args_int("dims_height");
logr(info, "Overriding image dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(renderer, cr_renderer_override_width, width);
cr_renderer_set_num_pref(renderer, cr_renderer_override_height, height);
}
}

if (args_is_set("tiledims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override tile dimensions when in worker mode\n");
} else {
int width = args_int("tile_width");
int height = args_int("tile_height");
logr(info, "Overriding tile dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(renderer, cr_renderer_tile_width, width);
cr_renderer_set_num_pref(renderer, cr_renderer_tile_height, height);
}
}

if (args_is_set("cam_index")) {
cr_renderer_set_num_pref(renderer, cr_renderer_override_cam, args_int("cam_index"));
}

struct timeval timer;
timer_start(&timer);
struct texture *final = cr_renderer_render(renderer);
Expand Down
26 changes: 25 additions & 1 deletion src/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ struct texture *renderFrame(struct renderer *r) {
if (registerHandler(sigint, sigHandler)) {
logr(warning, "Unable to catch SIGINT\n");
}

struct camera camera = r->scene->cameras.items[r->prefs.selected_camera];
if (r->prefs.override_width && r->prefs.override_height) {
camera.width = r->prefs.override_width ? (int)r->prefs.override_width : camera.width;
camera.height = r->prefs.override_height ? (int)r->prefs.override_height : camera.height;
cam_recompute_optics(&camera);
}

struct texture *output = newTexture(char_p, camera.width, camera.height, 3);

logr(info, "Starting c-ray renderer for frame %zu\n", r->prefs.imgCount);
Expand All @@ -77,6 +84,24 @@ struct texture *renderFrame(struct renderer *r) {
KNRM,
PLURAL(r->prefs.threads));

//Quantize image into renderTiles
tile_quantize(&r->state.tiles,
camera.width,
camera.height,
r->prefs.tileWidth,
r->prefs.tileHeight,
r->prefs.tileOrder);

for (size_t i = 0; i < r->state.tiles.count; ++i)
r->state.tiles.items[i].total_samples = r->prefs.sampleCount;

//Print a useful warning to user if the defined tile size results in less renderThreads
if (r->state.tiles.count < r->prefs.threads) {
logr(warning, "WARNING: Rendering with a less than optimal thread count due to large tile size!\n");
logr(warning, "Reducing thread count from %zu to %zu\n", r->prefs.threads, r->state.tiles.count);
r->prefs.threads = r->state.tiles.count;
}

logr(info, "Pathtracing%s...\n", args_is_set("interactive") ? " iteratively" : "");

r->state.rendering = true;
Expand Down Expand Up @@ -366,7 +391,6 @@ static struct prefs defaults() {
.assetPath = stringCopy("./"),
.imgFileName = stringCopy("rendered"),
.imgCount = 0,
.override_dimensions = false,
.imgType = png,
.window = {
.enabled = true,
Expand Down
1 change: 0 additions & 1 deletion src/renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ struct prefs {
unsigned tileHeight;

//Output prefs
bool override_dimensions;
unsigned override_width;
unsigned override_height;
size_t selected_camera;
Expand Down
78 changes: 0 additions & 78 deletions src/utils/loaders/sceneloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,57 +228,6 @@ void parse_prefs(struct cr_renderer *ext, const cJSON *data) {
cr_renderer_set_str_pref(ext, cr_renderer_output_filetype, fileType->valuestring);
}

// FIXME: Remove global options table, store it in a local in main() and run overrides
// from there.

// Now check and apply potential CLI overrides.
if (args_is_set("thread_override")) {
size_t threads = args_int("thread_override");
int64_t curr = cr_renderer_get_num_pref(ext, cr_renderer_threads);
if (curr != (int64_t)threads) {
logr(info, "Overriding thread count to %zu\n", threads);
cr_renderer_set_num_pref(ext, cr_renderer_threads, threads);
// prefs->fromSystem = false; FIXME
}
}

if (args_is_set("samples_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override samples when in worker mode\n");
} else {
int samples = args_int("samples_override");
logr(info, "Overriding sample count to %i\n", samples);
cr_renderer_set_num_pref(ext, cr_renderer_samples, samples);
}
}

if (args_is_set("dims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override dimensions when in worker mode\n");
} else {
int width = args_int("dims_width");
int height = args_int("dims_height");
logr(info, "Overriding image dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(ext, cr_renderer_override_width, width);
cr_renderer_set_num_pref(ext, cr_renderer_override_height, height);
}
}

if (args_is_set("tiledims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override tile dimensions when in worker mode\n");
} else {
int width = args_int("tile_width");
int height = args_int("tile_height");
logr(info, "Overriding tile dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(ext, cr_renderer_tile_width, width);
cr_renderer_set_num_pref(ext, cr_renderer_tile_height, height);
}
}

if (args_is_set("cam_index")) {
cr_renderer_set_num_pref(ext, cr_renderer_override_cam, args_int("cam_index"));
}
}

static void parseDisplay(struct sdl_prefs *win, const cJSON *data) {
Expand Down Expand Up @@ -824,15 +773,6 @@ int parse_json(struct cr_renderer *r, cJSON *json) {
return -1;
}

if (todo_remove_r->prefs.override_width && todo_remove_r->prefs.override_height) {
for (size_t i = 0; i < todo_remove_r->scene->cameras.count; ++i) {
struct camera *cam = &todo_remove_r->scene->cameras.items[i];
cam->width = todo_remove_r->prefs.override_width ? (int)todo_remove_r->prefs.override_width : cam->width;
cam->height = todo_remove_r->prefs.override_height ? (int)todo_remove_r->prefs.override_height : cam->height;
cam_recompute_optics(&todo_remove_r->scene->cameras.items[i]);
}
}

const cJSON *selected_camera = cJSON_GetObjectItem(json, "selected_camera");
if (cJSON_IsNumber(selected_camera)) {
todo_remove_r->prefs.selected_camera = (size_t)selected_camera->valueint;
Expand Down Expand Up @@ -926,23 +866,5 @@ int parse_json(struct cr_renderer *r, cJSON *json) {
logr(debug, "No local render threads, skipping local BVH construction.\n");
}

//Quantize image into renderTiles
tile_quantize(&todo_remove_r->state.tiles,
todo_remove_r->scene->cameras.items[todo_remove_r->prefs.selected_camera].width,
todo_remove_r->scene->cameras.items[todo_remove_r->prefs.selected_camera].height,
todo_remove_r->prefs.tileWidth,
todo_remove_r->prefs.tileHeight,
todo_remove_r->prefs.tileOrder);

for (size_t i = 0; i < todo_remove_r->state.tiles.count; ++i)
todo_remove_r->state.tiles.items[i].total_samples = todo_remove_r->prefs.sampleCount;

//Print a useful warning to user if the defined tile size results in less renderThreads
if (todo_remove_r->state.tiles.count < todo_remove_r->prefs.threads) {
logr(warning, "WARNING: Rendering with a less than optimal thread count due to large tile size!\n");
logr(warning, "Reducing thread count from %zu to %zu\n", todo_remove_r->prefs.threads, todo_remove_r->state.tiles.count);
todo_remove_r->prefs.threads = todo_remove_r->state.tiles.count;
}

return 0;
}
22 changes: 21 additions & 1 deletion src/utils/protocol/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,34 @@ static cJSON *startRender(int connectionSocket) {
struct cr_thread *worker_threads = calloc(threadCount, sizeof(*worker_threads));
struct workerThreadState *workerThreadStates = calloc(threadCount, sizeof(*workerThreadStates));

struct camera selected_cam = g_worker_renderer->scene->cameras.items[g_worker_renderer->prefs.selected_camera];
struct renderer *r = g_worker_renderer;
//Quantize image into renderTiles
tile_quantize(&r->state.tiles,
selected_cam.width,
selected_cam.height,
r->prefs.tileWidth,
r->prefs.tileHeight,
r->prefs.tileOrder);

for (size_t i = 0; i < r->state.tiles.count; ++i)
r->state.tiles.items[i].total_samples = r->prefs.sampleCount;

//Print a useful warning to user if the defined tile size results in less renderThreads
if (r->state.tiles.count < r->prefs.threads) {
logr(warning, "WARNING: Rendering with a less than optimal thread count due to large tile size!\n");
logr(warning, "Reducing thread count from %zu to %zu\n", r->prefs.threads, r->state.tiles.count);
r->prefs.threads = r->state.tiles.count;
}

//Create render threads (Nonblocking)
for (size_t t = 0; t < threadCount; ++t) {
workerThreadStates[t] = (struct workerThreadState){
.thread_num = t,
.connectionSocket = connectionSocket,
.socketMutex = g_worker_socket_mutex,
.renderer = g_worker_renderer,
.cam = &g_worker_renderer->scene->cameras.items[g_worker_renderer->prefs.selected_camera]};
.cam = &selected_cam};
worker_threads[t] = (struct cr_thread){.thread_fn = workerThread, .user_data = &workerThreadStates[t]};
if (thread_start(&worker_threads[t]))
logr(error, "Failed to create a crThread.\n");
Expand Down

0 comments on commit 06fe172

Please sign in to comment.