summaryrefslogtreecommitdiff
path: root/src/cl/spirt.cl
blob: 26bdf001ec6f5794cd56bb4569bcb5536e389c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* 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/>. */

#define RAY_ORIG(RAYS, INDEX) (RAYS[INDEX])
#define RAY_DIR(RAYS, INDEX) (RAYS[INDEX + 1])

__kernel void
gen_rays(__global float3 *const rays, float3 camera_centre,
         float3 pixel_delta_u, float3 pixel_delta_v, float3 corner00)
{
	size_t idx, w, i, j;

	w = get_global_size(0);

	i = get_global_id(0);
	j = get_global_id(1);

	idx = (w * j + i) * 2;

	RAY_ORIG(rays, idx) = camera_centre;
	RAY_DIR(rays, idx) = corner00 +
	                     ((i * pixel_delta_u) + (j * pixel_delta_v)) -
	                     camera_centre;
}

__kernel void
ray_colour(__global uchar *const canvas, __global float3 *const rays)
{
	size_t canvas_idx, ray_idx, w, i, j;
	float3 unit_direction, colour;
	float a;

	w = get_global_size(0);

	i = get_global_id(0);
	j = get_global_id(1);

	canvas_idx = (w * j + i) * 4;
	ray_idx = (w * j + i) * 2;

	unit_direction = normalize(RAY_DIR(rays, ray_idx));
	a = 0.5f * (unit_direction.y + 1.0f);

	colour = (1.0f - a) * (float3)(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;
}