diff options
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | src/camera.c | 37 | ||||
-rw-r--r-- | src/camera.h | 26 | ||||
-rw-r--r-- | src/cl/spirt.cl | 2 | ||||
-rw-r--r-- | src/spirt.c | 42 |
5 files changed, 73 insertions, 41 deletions
@@ -19,9 +19,9 @@ include config.mk -SRC = src/spirt.c src/util.c src/util_cl.c +SRC = src/spirt.c src/util.c src/util_cl.c src/camera.c CLSRC = src/cl/spirt.cl -HDR = src/util.h src/util_cl.h +HDR = src/util.h src/util_cl.h src/camera.h LL = ${CLSRC:.cl=.ll} SPV = ${CLSRC:.cl=.spv} @@ -32,7 +32,8 @@ all: spirt src/util.o: config.mk src/util_cl.o: config.mk src/util.h -src/spirt.o: config.mk src/util.h src/util_cl.h +src/camera.o: config.mk src/camera.h +src/spirt.o: config.mk src/util.h src/util_cl.h src/camera.h src/cl/spirt.o: config.mk spirt: ${OBJ} diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..68376b8 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,37 @@ +#include "camera.h" + +struct camera +camera_init(const int image_width, const int image_height, + const float focal_length) +{ + struct camera cam; + float viewport_height, viewport_width; + + cam.centre.s[0] = 0.0f; + cam.centre.s[1] = 0.0f; + cam.centre.s[2] = 0.0f; + + viewport_height = 2.0f; + viewport_width = + viewport_height * ((float)image_width / (float)image_height); + + cam.pixel_delta_u.s[0] = viewport_width / (float)image_width; + cam.pixel_delta_u.s[1] = 0.0f; + cam.pixel_delta_u.s[2] = 0.0f; + + cam.pixel_delta_v.s[0] = 0.0f; + cam.pixel_delta_v.s[1] = -viewport_height / (float)image_height; + cam.pixel_delta_v.s[2] = 0.0f; + + cam.corner00.s[0] = + cam.centre.s[0] - viewport_width / 2.0f + + 0.5f * (cam.pixel_delta_u.s[0] + cam.pixel_delta_v.s[0]); + cam.corner00.s[1] = + cam.centre.s[1] + viewport_height / 2.0f + + 0.5f * (cam.pixel_delta_u.s[1] + cam.pixel_delta_v.s[1]); + cam.corner00.s[2] = + cam.centre.s[2] - focal_length + + 0.5f * (cam.pixel_delta_u.s[2] + cam.pixel_delta_v.s[2]); + + return cam; +} diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 0000000..1a23370 --- /dev/null +++ b/src/camera.h @@ -0,0 +1,26 @@ +/* Copyright (C) 2024 Aiden Gall + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <CL/cl_platform.h> + +struct camera { + cl_float3 centre; + cl_float3 pixel_delta_u; + cl_float3 pixel_delta_v; + cl_float3 corner00; +}; + +struct camera camera_init(int image_width, int image_height, + float focal_length); diff --git a/src/cl/spirt.cl b/src/cl/spirt.cl index 136fd81..9b77919 100644 --- a/src/cl/spirt.cl +++ b/src/cl/spirt.cl @@ -103,7 +103,7 @@ ray_colour(__global float *const canvas, __global const float3 *const rays) i = get_global_id(0); j = get_global_id(1); - ray_idx = (w * j + i) * 2; + ray_idx = 2 * (w * j + i); ray.orig = rays[ray_idx]; ray.dir = rays[ray_idx + 1]; diff --git a/src/spirt.c b/src/spirt.c index 612c36e..4103bff 100644 --- a/src/spirt.c +++ b/src/spirt.c @@ -13,6 +13,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "camera.h" #include "util.h" #include "util_cl.h" @@ -44,13 +45,6 @@ struct opts { long devidx; }; -struct camera { - cl_float3 centre; - cl_float3 pixel_delta_u; - cl_float3 pixel_delta_v; - cl_float3 corner00; -}; - static float *kernel_render(struct kernel_context *runtime, size_t image_width, size_t image_height, struct camera cam); @@ -217,7 +211,7 @@ get_args(int argc, char *argv[]) char flag; if (argv[0][2]) - die("arg_parse: Invalid flag '%s'\n", argv[0]); + die("get_args: Invalid flag '%s'\n", argv[0]); flag = argv[0][1]; argv++, argc--; @@ -238,13 +232,13 @@ get_args(int argc, char *argv[]) case '-': goto end_flags; default: - die("arg_parse: Unknown flag '-%c'\n", flag); + die("get_args: Unknown flag '-%c'\n", flag); } } end_flags: if (argc > 0) - die("arg_parse: Unexpected argument\n"); + die("get_args: Unexpected argument\n"); return opts; } @@ -257,7 +251,6 @@ main(int argc, char *argv[]) struct kernel_context runtime; float *h_canvas; struct camera cam; - float focal_length, viewport_width, viewport_height; Image img; Texture2D texture; @@ -266,32 +259,7 @@ main(int argc, char *argv[]) runtime = kernel_context_init(opts.platidx, opts.devidx); - focal_length = 1.0f; - viewport_height = 2.0f; - viewport_width = - viewport_height * ((float)opts.width / (float)opts.height); - - cam.centre.s[0] = 0.0f; - cam.centre.s[1] = 0.0f; - cam.centre.s[2] = 0.0f; - - cam.pixel_delta_u.s[0] = viewport_width / (float)opts.width; - cam.pixel_delta_u.s[1] = 0.0f; - cam.pixel_delta_u.s[2] = 0.0f; - - cam.pixel_delta_v.s[0] = 0.0f; - cam.pixel_delta_v.s[1] = -viewport_height / (float)opts.height; - cam.pixel_delta_v.s[2] = 0.0f; - - cam.corner00.s[0] = - cam.centre.s[0] - viewport_width / 2.0f + - 0.5f * (cam.pixel_delta_u.s[0] + cam.pixel_delta_v.s[0]); - cam.corner00.s[1] = - cam.centre.s[1] + viewport_height / 2.0f + - 0.5f * (cam.pixel_delta_u.s[1] + cam.pixel_delta_v.s[1]); - cam.corner00.s[2] = - cam.centre.s[2] - focal_length + - 0.5f * (cam.pixel_delta_u.s[2] + cam.pixel_delta_v.s[2]); + cam = camera_init(opts.width, opts.height, 1.0f); h_canvas = kernel_render(&runtime, opts.width, opts.height, cam); |