From 10ee4be624e665db9ad83c6bf877c6c5b5e3d443 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Fri, 1 Jan 2021 23:30:43 +0100 Subject: Add multi sampling --- include/color.hpp | 2 +- include/vk-renderer.hpp | 4 +++- shaders/compute.comp | 38 +++++++++++++++++++++++--------------- src/color.cpp | 7 +------ src/main.cpp | 9 +++++++-- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/include/color.hpp b/include/color.hpp index 34f739a..f300e76 100644 --- a/include/color.hpp +++ b/include/color.hpp @@ -5,6 +5,6 @@ #include "vec3.hpp" -void write_color(std::ostream& out, const color& pixel_color, uint32_t samples_per_pixel); +void write_color(std::ostream& out, const color& pixel_color); #endif // !__COLOR_HPP_ diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index ca3e182..b1f8d3f 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -11,10 +11,12 @@ struct input_data { color ground_color; vec3 camera_pos; + uint32_t samples_per_pixel; float viewport_width; float viewport_height; float proj_plane_distance; - float pad; + + float random_numbers[200]; struct sphere { vec3 position; diff --git a/shaders/compute.comp b/shaders/compute.comp index b9c165e..861da63 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -15,25 +15,27 @@ struct sphere { float radius; }; -vec3 at(ray r, float t) { - return r.origin + r.direction * t; -} - layout(set = 0, binding = 0) buffer Inputs { vec4 sky_color; vec4 ground_color; vec4 camera_pos; + uint samples_per_pixel; float viewport_width; float viewport_height; float proj_plane_distance; - float padding; + + float[200] random_numbers; sphere s; vec4[] image; }; +vec3 at(ray r, float t) { + return r.origin + r.direction * t; +} + bool hit_sphere(ray r, out hit_info info) { // Vector from the ray origin to sphere position vec3 origin_center = r.origin - s.position.xyz; @@ -65,20 +67,26 @@ void main() { uint height = gl_NumWorkGroups.y; vec2 uv = vec2(float(x) / (width - 1), 1.0 - (float(y) / (height - 1))); - vec3 first = camera_pos.xyz - vec3(viewport_width / 2.0, viewport_height / 2.0, proj_plane_distance); - vec3 proj_plane_pos = first + vec3(uv.x * viewport_width, uv.y * viewport_height, 0.0); - ray r = ray(camera_pos.xyz, proj_plane_pos - camera_pos.xyz); - hit_info info; + image[y * width + x] = vec4(0.0); + + for (uint sample_index = 0; sample_index < samples_per_pixel; sample_index++) { + vec2 jittered_uvs = uv + vec2(random_numbers[sample_index * 2] / (width - 1), random_numbers[sample_index * 2 + 1] / (height - 1)); + vec3 proj_plane_pos = first + vec3(jittered_uvs.x * viewport_width, jittered_uvs.y * viewport_height, 0.0); + + ray r = ray(camera_pos.xyz, proj_plane_pos - camera_pos.xyz); + hit_info info; - if (hit_sphere(r, info)) { - vec3 hit_normal = normalize(info.normal); - hit_normal = (hit_normal + 1.0) * 0.5; + if (hit_sphere(r, info)) { + vec3 hit_normal = normalize(info.normal); + hit_normal = (hit_normal + 1.0) * 0.5; - image[y * width + x] = vec4(hit_normal, 0.0); - return; + image[y * width + x] += vec4(hit_normal, 0.0); + } else { + image[y * width + x] += mix(ground_color, sky_color, uv.y); + } } - image[y * width + x] = mix(ground_color, sky_color, uv.y); + image[y * width + x] /= samples_per_pixel; } diff --git a/src/color.cpp b/src/color.cpp index c1036e0..33351a8 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -1,16 +1,11 @@ #include "color.hpp" #include "rt.hpp" -void write_color(std::ostream& out, const color& pixel_color, uint32_t samples_per_pixel) { +void write_color(std::ostream& out, const color& pixel_color) { auto r = pixel_color.x; auto g = pixel_color.y; auto b = pixel_color.z; - // auto scale = 1.0 / samples_per_pixel; - // r = sqrt(r * scale); - // g = sqrt(g * scale); - // b = sqrt(b * scale); - out << static_cast(256.0 * clamp(r, 0.0, 0.999)) << ' ' << static_cast(256.0 * clamp(g, 0.0, 0.999)) << ' ' << static_cast(256.0 * clamp(b, 0.0, 0.999)) << std::endl; diff --git a/src/main.cpp b/src/main.cpp index b769dd3..8811c78 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,7 +85,7 @@ int main() { const float aspect_ratio = 1.0 / 1.0; const size_t width = 400; const size_t height = width / aspect_ratio; - const uint32_t samples_per_pixel = 1; + const uint32_t samples_per_pixel = 100; const uint32_t max_depth = 100; // const vec3 camera_position{ 13.0, 2.0, 3.0 }; @@ -98,6 +98,7 @@ int main() { .sky_color = sky_color, .ground_color = ground_color, .camera_pos = camera_position, + .samples_per_pixel = samples_per_pixel, .viewport_width = 2.f * aspect_ratio, .viewport_height = 2.f, .proj_plane_distance = 1.f, @@ -105,6 +106,10 @@ int main() { // .sphere = { { 0.f, -100.5f, -1.f }, 100.f }, }; + for (size_t rand_number_index = 0; rand_number_index < samples_per_pixel * 2; rand_number_index++) { + inputs.random_numbers[rand_number_index] = randd(); + } + // World hittable objects hittable_list world = random_scene(); @@ -120,7 +125,7 @@ int main() { //------------------------- auto output_image = renderer.output_image(); for (size_t index = 0; index < height * width; index++) { - write_color(std::cout, output_image[index], samples_per_pixel); + write_color(std::cout, output_image[index]); } std::cerr << "Done !" << std::endl; -- cgit v1.3