diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2021-01-09 20:42:24 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2021-01-09 20:43:09 +0100 |
| commit | 2db0d27e7f1744443958717c19b732eed57455f2 (patch) | |
| tree | 839b8ea513c8c239f015071ab083bd50f03854a5 | |
| parent | 4379b46b46563c5a709df8d567ef75affc3fd31c (diff) | |
Replace old classes with compute shader structs
| -rw-r--r-- | include/camera.hpp | 24 | ||||
| -rw-r--r-- | include/dielectric.hpp | 18 | ||||
| -rw-r--r-- | include/hittable.hpp | 28 | ||||
| -rw-r--r-- | include/hittable_list.hpp | 21 | ||||
| -rw-r--r-- | include/lambertian.hpp | 15 | ||||
| -rw-r--r-- | include/material.hpp | 17 | ||||
| -rw-r--r-- | include/metal.hpp | 16 | ||||
| -rw-r--r-- | include/ray.hpp | 18 | ||||
| -rw-r--r-- | include/rt.hpp | 35 | ||||
| -rw-r--r-- | include/sphere.hpp | 15 | ||||
| -rw-r--r-- | include/utils.hpp | 20 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 34 | ||||
| -rw-r--r-- | shaders/compute.comp | 18 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/camera.cpp | 28 | ||||
| -rw-r--r-- | src/dielectric.cpp | 32 | ||||
| -rw-r--r-- | src/hittable_list.cpp | 28 | ||||
| -rw-r--r-- | src/lambertian.cpp | 18 | ||||
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/metal.cpp | 14 | ||||
| -rw-r--r-- | src/ray.cpp | 11 | ||||
| -rw-r--r-- | src/sphere.cpp | 38 |
22 files changed, 75 insertions, 380 deletions
diff --git a/include/camera.hpp b/include/camera.hpp index 858833b..43ed755 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -1,35 +1,31 @@ #ifndef __CAMERA_HPP_ #define __CAMERA_HPP_ -#include "ray.hpp" +#include "vec3.hpp" class camera { public: camera( point3 position, point3 target, - double v_fov, - double aspect_ratio, - double aperture, - double focus_dist + float v_fov, + float aspect_ratio, + float aperture, + float focus_dist ); - ray get_ray(double u, double v) const; - point3 position; - float lens_radius; - float viewport_width; - float viewport_height; + vec3 right; + vec3 up; vec3 horizontal; vec3 vertical; - vec3 forward; - vec3 right; - vec3 up; + vec3 first_pixel; - point3 bottom_left_pixel; + float lens_radius; + float padding[3]; }; #endif // !__CAMERA_HPP_ diff --git a/include/dielectric.hpp b/include/dielectric.hpp deleted file mode 100644 index 91f1541..0000000 --- a/include/dielectric.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __DIELECTRIC_HPP_ -#define __DIELECTRIC_HPP_ - -#include "material.hpp" - -class dielectric : public material { - public: - dielectric(double index_of_refraction); - - virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const; - - double ior; - - private: - static double reflectance(double cosine, double ref_idx); -}; - -#endif // !__DIELECTRIC_HPP_ diff --git a/include/hittable.hpp b/include/hittable.hpp deleted file mode 100644 index 074de55..0000000 --- a/include/hittable.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __HITTABLE_HPP_ -#define __HITTABLE_HPP_ - -#include <memory> - -#include "ray.hpp" - -class material; - -struct hit_info { - double t; - point3 point; - vec3 normal; - bool front_face; - std::shared_ptr<material> mat_ptr; - - inline void set_face_normal(const ray& r, const vec3& outward_normal) { - front_face = r.direction.dot(outward_normal) < 0.0; - normal = front_face ? outward_normal : (-outward_normal); - } -}; - -class hittable { - public: - virtual bool hit(const ray& r, double t_min, double t_max, struct hit_info& info) const = 0; -}; - -#endif // !__HITTABLE_HPP_ diff --git a/include/hittable_list.hpp b/include/hittable_list.hpp deleted file mode 100644 index 41d8f4a..0000000 --- a/include/hittable_list.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __HITTABLE_LIST_HPP_ -#define __HITTABLE_LIST_HPP_ - -#include <vector> -#include <memory> - -#include "hittable.hpp" - -class hittable_list : public hittable { - public: - hittable_list(); - - void add(std::shared_ptr<hittable> hittable); - void clear(); - - virtual bool hit(const ray&r, double t_min, double t_max, struct hit_info& info) const; - - std::vector<std::shared_ptr<hittable>> hittables; -}; - -#endif // !__HITTABLE_LIST_HPP_ diff --git a/include/lambertian.hpp b/include/lambertian.hpp deleted file mode 100644 index b9b7509..0000000 --- a/include/lambertian.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __LAMBERTIAN_HPP_ -#define __LAMBERTIAN_HPP_ - -#include "material.hpp" - -class lambertian : public material { - public: - lambertian(const color& albedo); - - virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const; - - color albedo; -}; - -#endif // !__LAMBERTIAN_HPP_ diff --git a/include/material.hpp b/include/material.hpp index 5e6f095..10886da 100644 --- a/include/material.hpp +++ b/include/material.hpp @@ -1,13 +1,20 @@ #ifndef __MATERIAL_HPP_ #define __MATERIAL_HPP_ -#include "ray.hpp" +#include "vec3.hpp" -struct hit_info; +enum MATERIAL_TYPE: uint32_t { + LAMBERTIAN = 0, + METAL = 1, + DIELECTRIC = 2, +}; -class material { - public: - virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scaterred) const = 0; +struct material { + color albedo; + float fuzz; + float ior; + uint32_t type; + uint32_t padding; }; #endif // !__MATERIAL_HPP_ diff --git a/include/metal.hpp b/include/metal.hpp deleted file mode 100644 index 5516890..0000000 --- a/include/metal.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __METAL_HPP_ -#define __METAL_HPP_ - -#include "material.hpp" - -class metal : public material { - public: - metal(const color& albedo, double f); - - virtual bool scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const; - - color albedo; - double fuzz; -}; - -#endif // !__METAL_HPP_ diff --git a/include/ray.hpp b/include/ray.hpp deleted file mode 100644 index e18812e..0000000 --- a/include/ray.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __RAY_HPP_ -#define __RAY_HPP_ - -#include "vec3.hpp" - -class ray { - public: - - ray(); - ray(const point3& origin, const vec3& direction); - - point3 at(double t) const; - - point3 origin; - vec3 direction; -}; - -#endif // !__RAY_HPP_ diff --git a/include/rt.hpp b/include/rt.hpp index 09cf1b0..2898045 100644 --- a/include/rt.hpp +++ b/include/rt.hpp @@ -1,40 +1,11 @@ #ifndef __RT_HPP_ #define __RT_HPP_ -#include <limits> -#include <cstdlib> - -const float pi = 3.1415926535897932385f; -const float infinity = std::numeric_limits<float>::infinity(); - -inline float deg_to_rad(float deg) { - return deg * pi / 180.0; -} - -inline float randd() { - return rand() / (RAND_MAX + 1.0); -} - -inline float randd(float min, float max) { - return min + randd() * (max - min); -} - -inline float clamp(float x, float min, float max) { - if (x < min) return min; - if (x > max) return max; - return x; -} - - +#include "utils.hpp" #include "vec3.hpp" -#include "ray.hpp" #include "color.hpp" -// #include "sphere.hpp" +#include "sphere.hpp" #include "camera.hpp" -// #include "material.hpp" -// #include "lambertian.hpp" -// #include "metal.hpp" -// #include "dielectric.hpp" -// #include "hittable_list.hpp" +#include "material.hpp" #endif // !__RT_HPP_ diff --git a/include/sphere.hpp b/include/sphere.hpp index c33a796..a338f2e 100644 --- a/include/sphere.hpp +++ b/include/sphere.hpp @@ -1,19 +1,18 @@ #ifndef __SPHERE_HPP_ #define __SPHERE_HPP_ -#include "hittable.hpp" #include "material.hpp" -class sphere : public hittable { - public: - sphere(point3 center, double radius, std::shared_ptr<material> material); +struct sphere{ + sphere() = default; + sphere(point3 center, material material, float radius); - virtual bool hit(const ray& r, double t_min, double t_max, struct hit_info& info) const; + point3 center; - point3 center; - double radius; + material mat; - std::shared_ptr<material> mat_ptr; + float radius; + float padding[3]; }; #endif // !__SPHERE_HPP_ diff --git a/include/utils.hpp b/include/utils.hpp index efa007d..0bef213 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -5,6 +5,26 @@ #include <vector> +#include <cmath> +#include <cstdlib> + std::vector<uint8_t> get_shader_code(const char* path); +inline float deg_to_rad(float deg) { + return deg * M_PI / 180.0; +} + +inline float randd() { + return rand() / (RAND_MAX + 1.0); +} + +inline float randd(float min, float max) { + return min + randd() * (max - min); +} +inline float clamp(float x, float min, float max) { + if (x < min) return min; + if (x > max) return max; + return x; +} + #endif // !__UTILS_HPP_ diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 70fcc7e..37a3494 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -5,42 +5,14 @@ #include "thirdparty/vk_mem_alloc.h" #include "vec3.hpp" - -enum MATERIAL_TYPE: uint32_t { - LAMBERTIAN = 0, - METAL = 1, - DIELECTRIC = 2, -}; - -struct material { - color albedo; - float fuzz; - float ior; - uint32_t type; - uint32_t padding; -}; - -struct sphere { - vec3 position; - material mat; - float radius; - float padding[3]; -}; +#include "camera.hpp" +#include "sphere.hpp" struct input_data { color sky_color; color ground_color; - struct camera { - vec3 position; - vec3 up; - vec3 right; - vec3 forward; - float viewport_width; - float viewport_height; - float lens_radius; - float focus_distance; - } cam; + camera cam; float random_offset[2000]; float random_disk[2000]; diff --git a/shaders/compute.comp b/shaders/compute.comp index 6e6e8a1..f179b63 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -34,13 +34,16 @@ struct sphere { struct camera { vec4 position; + vec4 up; vec4 right; - vec4 forward; - float viewport_width; - float viewport_height; + + vec4 horizontal; + vec4 vertical; + vec4 first_pixel; + float lens_radius; - float focus_distance; + float[3] padding; }; layout(set = 0, binding = 0) buffer Inputs { @@ -207,9 +210,6 @@ void main() { uint y = gl_GlobalInvocationID.y; vec2 uv = vec2(float(x) / (width - 1), 1.0 - (float(y) / (height - 1))); - vec3 horizontal = cam.focus_distance * cam.right.xyz * cam.viewport_width; - vec3 vertical = cam.focus_distance * cam.up.xyz * cam.viewport_height; - vec3 first = cam.position.xyz - horizontal / 2.0 - vertical / 2.0 + cam.focus_distance * cam.forward.xyz; vec4 out_color = vec4(0.0); @@ -217,9 +217,9 @@ void main() { vec2 jittered_uvs = uv + vec2(random_disk[sample_index].x / (width - 1), random_disk[sample_index].y / (height - 1)); vec2 lens_disk = random_disk[sample_index] * cam.lens_radius; vec3 offset = lens_disk.x * cam.right.xyz + lens_disk.y * cam.up.xyz; - vec3 proj_plane_pos = first + jittered_uvs.x * horizontal + jittered_uvs.y * vertical; + vec4 proj_plane_pos = cam.first_pixel + jittered_uvs.x * cam.horizontal + jittered_uvs.y * cam.vertical; - out_color += ray_color(ray(cam.position.xyz + offset, proj_plane_pos - cam.position.xyz - offset), sample_index); + out_color += ray_color(ray(cam.position.xyz + offset, proj_plane_pos.xyz - cam.position.xyz - offset), sample_index); } float spp_scale = (1.0 / samples_per_pixel); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c185ebe..1fd25b7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,8 @@ add_executable( main.cpp vec3.cpp color.cpp - ray.cpp sphere.cpp - hittable_list.cpp camera.cpp - lambertian.cpp - metal.cpp - dielectric.cpp vulkan-loader.cpp utils.cpp vk-renderer.cpp diff --git a/src/camera.cpp b/src/camera.cpp index c7e2e35..6cb7b83 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -5,34 +5,22 @@ camera::camera( point3 position, point3 target, - double v_fov, - double aspect_ratio, - double aperture, - double focus_dist + float v_fov, + float aspect_ratio, + float aperture, + float focus_dist ) : position(position), lens_radius(aperture / 2.0) { - forward = (target - position).unit(); + auto forward = (target - position).unit(); right = forward.cross(vec3{ 0.0, 1.0, 0.0 }).unit(); up = right.cross(forward).unit(); auto h = tan(deg_to_rad(v_fov) / 2.0); - viewport_height = 2.0 * h; - viewport_width = viewport_height * aspect_ratio; + auto viewport_height = 2.0 * h; + auto viewport_width = viewport_height * aspect_ratio; horizontal = focus_dist * viewport_width * right; vertical = focus_dist * viewport_height * up; - bottom_left_pixel = position - horizontal / 2.0 - vertical / 2.0 - forward * focus_dist; -} - -ray camera::get_ray(double u, double v) const { - vec3 rd = lens_radius * random_in_unit_disk(); - vec3 offset = rd.x * right + rd.y * up; - - point3 viewport_pixel = bottom_left_pixel + u * horizontal + v * vertical; - - return ray { - position + offset, - viewport_pixel - position - offset - }; + first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_dist * forward; } diff --git a/src/dielectric.cpp b/src/dielectric.cpp deleted file mode 100644 index 80ca069..0000000 --- a/src/dielectric.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "dielectric.hpp" -#include "rt.hpp" -#include "hittable.hpp" - -dielectric::dielectric(double index_of_refraction) - : ior(index_of_refraction) {} - -bool dielectric::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - auto unit_direction = r.direction.unit(); - auto refraction_ratio = info.front_face ? (1.0 / ior) : ior; - double cos_theta = fmin(info.normal.dot(-unit_direction), 1.0); - double sin_theta = sqrt(1.0 - cos_theta * cos_theta); - - vec3 direction; - if (refraction_ratio * sin_theta > 1.0 || reflectance(cos_theta, refraction_ratio) > randd()) { - direction = reflect(unit_direction, info.normal); - } else { - direction = refract(unit_direction, info.normal, refraction_ratio); - } - - scattered = ray { info.point, direction }; - attenuation = { 1.0, 1.0, 1.0 }; - - return true; -} - -double dielectric::reflectance(double cosine, double ref_idx) { - // Use Schlick's approximation for reflectance. - auto r0 = (1 - ref_idx) / (1 + ref_idx); - r0 = r0 * r0; - return r0 + (1 - r0) * pow ((1 - cosine), 5); -} diff --git a/src/hittable_list.cpp b/src/hittable_list.cpp deleted file mode 100644 index 9140304..0000000 --- a/src/hittable_list.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "hittable_list.hpp" - -hittable_list::hittable_list() {}; - -void hittable_list::add(std::shared_ptr<hittable> hittable) { - hittables.push_back(hittable); -} - -void hittable_list::clear() { - hittables.clear(); -} - - -bool hittable_list::hit(const ray& r, double t_min, double t_max, struct hit_info& info) const { - struct hit_info temp_info; - bool hit_anything = false; - double closest_hit = t_max; - - for (auto& hittable: hittables) { - if (hittable->hit(r, t_min, closest_hit, temp_info)) { - hit_anything = true; - closest_hit = temp_info.t; - info = temp_info; - } - } - - return hit_anything; -} diff --git a/src/lambertian.cpp b/src/lambertian.cpp deleted file mode 100644 index 16ab146..0000000 --- a/src/lambertian.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "lambertian.hpp" -#include "hittable.hpp" - -lambertian::lambertian(const color& albedo) : albedo(albedo) {} - -bool lambertian::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - // auto scatter_direction = info.normal + random_in_unit_sphere(); - // auto scatter_direction = info.normal + random_unit_vector(); - auto scatter_direction = info.normal + random_in_hemisphere(info.normal); - - if (scatter_direction.near_zero()) { - scatter_direction = info.normal; - } - - scattered = ray { info.point, scatter_direction }; - attenuation = albedo; - return true; -} diff --git a/src/main.cpp b/src/main.cpp index 06a602a..03706b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,7 +98,7 @@ int main() { .sky_color = sky_color, .ground_color = ground_color, - .cam = { camera_position, camera.up, camera.right, camera.forward, camera.viewport_width, camera.viewport_height, camera.lens_radius, distance_to_focus }, + .cam = camera, .spheres = {}, diff --git a/src/metal.cpp b/src/metal.cpp deleted file mode 100644 index 196c95d..0000000 --- a/src/metal.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "metal.hpp" -#include "hittable.hpp" -#include "vec3.hpp" - -metal::metal(const color& albedo, double f) - : albedo(albedo), fuzz(f < 1 ? f : 1) {} - -bool metal::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - auto reflected = reflect(r.direction.unit(), info.normal); - - scattered = ray { info.point, reflected + fuzz * random_in_unit_sphere() }; - attenuation = albedo; - return (scattered.direction.dot(info.normal) > 0.0); -} diff --git a/src/ray.cpp b/src/ray.cpp deleted file mode 100644 index 9a8bd36..0000000 --- a/src/ray.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "ray.hpp" - -ray::ray() -{} - -ray::ray(const point3& origin, const vec3& direction): origin(origin), direction(direction) -{} - -point3 ray::at(double t) const { - return origin + direction * t; -} diff --git a/src/sphere.cpp b/src/sphere.cpp index e7be715..7ac4185 100644 --- a/src/sphere.cpp +++ b/src/sphere.cpp @@ -1,39 +1,5 @@ #include "sphere.hpp" -sphere::sphere(point3 center, double radius, std::shared_ptr<material> material) - : center(center), radius(radius), mat_ptr(material) +sphere::sphere(point3 center, material material, float radius) + : center(center), radius(radius), mat(material) {} - -bool sphere::hit(const ray& r, double t_min, double t_max, struct hit_info& info) const { - // Vector from the ray origin to sphere position - vec3 origin_center = r.origin - center; - - double a = r.direction.dot(r.direction); - double half_b = r.direction.dot(origin_center); - double c = origin_center.dot(origin_center) - radius * radius; - double discriminant = half_b * half_b - a * c; - - if (discriminant < 0) { - return false; - } - - double sq_discriminant = sqrt(discriminant); - double t = (-half_b - sq_discriminant) / a ; - - if (t_min > t || t_max < t) { - t = (-half_b + sq_discriminant) / a ; - - if (t_min > t || t_max < t) { - return false; - } - } - - info.t = t; - info.point = r.at(t); - info.mat_ptr = mat_ptr; - - vec3 out_normal = (info.point - center) / radius; - info.set_face_normal(r, out_normal); - - return true; -} |
