summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAiden Gall <aiden@aidengall.xyz>2024-02-05 14:19:38 +0000
committerAiden Gall <aiden@aidengall.xyz>2024-02-05 14:19:38 +0000
commit71277d9af0a9c89ffeab47f01dcb1426f8ffa253 (patch)
tree6bdcb0504e08c508beba12a1a406ff1f255464da
parent27279a4e8db5f7d0e8d1f260cbe6190af92b2e67 (diff)
refactor canvas writing and add proper gamma correction
-rw-r--r--src/cl/spirt.cl22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/cl/spirt.cl b/src/cl/spirt.cl
index e037a38..943f317 100644
--- a/src/cl/spirt.cl
+++ b/src/cl/spirt.cl
@@ -67,12 +67,24 @@ hit_sphere(const float3 center, const float radius, const struct ray ray)
return (-half_b - sqrt(disc)) / a;
}
+void
+write_pixel(__global uchar *const pixel, float3 colour)
+{
+ /* gamma correction */
+ colour = 256.0f * clamp(pow(colour, 1.0f/2.2f), 0.0f, 0.999f);
+
+ pixel[0] = colour.x;
+ pixel[1] = colour.y;
+ pixel[2] = colour.z;
+ pixel[3] = 255;
+}
+
__kernel void
ray_colour(__global uchar *const canvas, __global const float3 *const rays)
{
struct ray ray;
- size_t canvas_idx, ray_idx, w, i, j;
+ size_t ray_idx, w, i, j;
float3 colour;
float t;
@@ -81,7 +93,6 @@ ray_colour(__global uchar *const canvas, __global const float3 *const rays)
i = get_global_id(0);
j = get_global_id(1);
- canvas_idx = (w * j + i) * 4;
ray_idx = (w * j + i) * 2;
ray.orig = rays[ray_idx];
@@ -99,12 +110,9 @@ ray_colour(__global uchar *const canvas, __global const float3 *const rays)
unit_direction = normalize(ray.dir);
a = 0.5f * (unit_direction.y + 1.0f);
+
colour = (1.0f - a) * 1.0f + a * (float3)(0.5f, 0.7f, 1.0f);
}
- colour = 256.0f * clamp(sqrt(colour), 0.0f, 0.999f);
- canvas[canvas_idx] = colour.x;
- canvas[canvas_idx + 1] = colour.y;
- canvas[canvas_idx + 2] = colour.z;
- canvas[canvas_idx + 3] = 255;
+ write_pixel(canvas + 4 * (w * j + i), colour);
}