Hah! Was able to get the ray redirection working, very large step in the way towards multi light, refraction, reflection, and indirect lightinggit status

float DistanceBetweenPoints(float3 a, float3 b) {
	return fast_distance(a, b);
	//return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}

float Distance(float3 a) {
	return fast_length(a);
	//return sqrt(pow(a.x, 2) + pow(a.y, 2) + pow(a.z, 2));
}

// Naive incident ray light
float4 white_light(float4 input, float3 light, int3 mask) {

	input.w = input.w + acos(
		dot(
			normalize(light),
			normalize(convert_float3(mask * (-mask)))
			)
		) / 32;

	input.w += 0.25f;

	return input;

}

// Phong + diffuse lighting function for g
//  0  1  2  3  4  5  6  7   8   9
// {r, g, b, i, x, y, z, x', y', z'}

float4 view_light(float4 in_color, float3 light, float4 light_color, float3 view, int3 mask) {

	if (all(light == (0.0f,0.0f,0.0f)))
		return (0,0,0,0);

	float d = Distance(light) / 100.0f;
	d *= d;

	float diffuse = max(dot(normalize(convert_float3(mask)), normalize(light)), 0.0f);
	in_color += diffuse * light_color * 0.5f / d;

	if (dot(light, normalize(convert_float3(mask))) > 0.0f)
	{
		// Small dots of light are caused by floating point error
		// flipping bits on the face mask and screwing up this calculation
		float3 halfwayVector = normalize(normalize(light) + normalize(view));
		float specTmp = max(dot(normalize(convert_float3(mask)), halfwayVector), 0.0f);
		in_color += pow(specTmp, 8.0f) * light_color * 0.5f / d;
	}
	if (in_color.w > 1.0f){
		in_color.xyz *= in_color.w;
	}

	return in_color;
}

int rand(int* seed) // 1 <= *seed < m
{
	int const a = 16807; //ie 7**5
	int const m = 2147483647; //ie 2**31-1

	*seed = ((*seed) * a) % m;
	return(*seed);
}

bool get_oct_vox(
	int3 position,
	global ulong *octree_descriptor_buffer,
	global uint *octree_attachment_lookup_buffer,
	global ulong *octree_attachment_buffer,
	global ulong *settings_buffer
){

	// (X, Y, Z) mask for the idx
	const uchar idx_set_x_mask = 0x1;
	const uchar idx_set_y_mask = 0x2;
	const uchar idx_set_z_mask = 0x4;

	const uchar mask_8[8] = {
		0x1,  0x2,  0x4,  0x8,
		0x10, 0x20, 0x40, 0x80
	};

	// Mask for counting the previous valid bits
	const uchar count_mask_8[8] = {
		0x1,  0x3,  0x7,  0xF,
		0x1F, 0x3F, 0x7F, 0xFF
	};

		// uint64_t manipulation masks
	const ulong child_pointer_mask = 0x0000000000007fff;
	const ulong far_bit_mask = 0x8000;
	const ulong valid_mask = 0xFF0000;
	const ulong leaf_mask = 0xFF000000;
	const ulong contour_pointer_mask = 0xFFFFFF00000000;
	const ulong contour_mask = 0xFF00000000000000;

	// push the root node to the parent stack
	ulong current_index = *settings_buffer;
	ulong head = octree_descriptor_buffer[current_index];

	uint parent_stack_position = 0;
	ulong parent_stack[32];

	uchar scale = 0;
	uchar idx_stack[32];

	ulong current_descriptor = 0;

	bool found = false;

	parent_stack[parent_stack_position] = head;

	// Set our initial dimension and the position at the corner of the oct to keep track of our position
	int dimension = 128;
	int3 quad_position = (0, 0, 0);

	// While we are not at the required resolution
	//		Traverse down by setting the valid/leaf mask to the subvoxel
	//		Check to see if it is valid
	//			Yes?
	//				Check to see if it is a leaf
	//				No? Break
	//				Yes? Scale down to the next hierarchy, push the parent to the stack
	//
	//			No?
	//				Break
	while (dimension > 1) {

		// So we can be a little bit tricky here and increment our
		// array index that holds our masks as we build the idx.
		// Adding 1 for X, 2 for Y, and 4 for Z
		int mask_index = 0;

		// Do the logic steps to find which sub oct we step down into
		if (position.x >= (dimension / 2) + quad_position.x) {

			// Set our voxel position to the (0,0) of the correct oct
			quad_position.x += (dimension / 2);

			// increment the mask index and mentioned above
			mask_index += 1;

			// Set the idx to represent the move
			idx_stack[scale] |= idx_set_x_mask;

		}
		if (position.y >= (dimension / 2) + quad_position.y) {

			quad_position.y |= (dimension / 2);

			mask_index += 2;

			// TODO What is up with the binary operator on this one?
			idx_stack[scale] ^= idx_set_y_mask;

		}
		if (position.z >= (dimension / 2) + quad_position.z) {

			quad_position.z += (dimension / 2);

			mask_index += 4;

			idx_stack[scale] |= idx_set_z_mask;

		}

		// Check to see if we are on a valid oct
		if ((head >> 16) & mask_8[mask_index]) {

			// Check to see if it is a leaf
			if ((head >> 24) & mask_8[mask_index]) {

				// If it is, then we cannot traverse further as CP's won't have been generated
				found = true;
				return found;
			}

			// If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy
			scale++;
			dimension /= 2;

			// Count the number of valid octs that come before and add it to the index to get the position
			// Negate it by one as it counts itself
			int count = popcount((uchar)(head >> 16) & count_mask_8[mask_index]) - 1;

			// access the element at which head points to and then add the specified number of indices
			// to get to the correct child descriptor
			current_index = current_index + (head & child_pointer_mask) + count;
			head = octree_descriptor_buffer[current_index];

			// Increment the parent stack position and put the new oct node as the parent
			parent_stack_position++;
			parent_stack[parent_stack_position] = head;

		}
		else {
			// If the oct was not valid, then no CP's exists any further
			// This implicitly says that if it's non-valid then it must be a leaf!!

			// It appears that the traversal is now working but I need
			// to focus on how to now take care of the end condition.
			// Currently it adds the last parent on the second to lowest
			// oct CP. Not sure if thats correct
			found = 0;
			return found;
		}
	}

	found = 1;
	return found;
}

// =================================== Boolean ray intersection ============================
// =========================================================================================

bool cast_light_intersection_ray(
	global char* map,
	global int3* map_dim,
	float3 ray_dir,
    float3 ray_pos,
	global float* lights,
	global int* light_count

	){

	float distance_to_light = DistanceBetweenPoints(ray_pos, (float3)(lights[4], lights[5], lights[6]));
	//if (distance_to_light > 200.0f){
	//	return false;
	//}

	// Setup the voxel step based on what direction the ray is pointing
	int3 voxel_step = { 1, 1, 1 };
	voxel_step *= (ray_dir > 0) - (ray_dir < 0);

	if (any(ray_dir == (0.0f,0.0f,0.0f)))
		return false;

	// Setup the voxel coords from the camera origin
	int3 voxel = convert_int3(ray_pos);

	// Delta T is the units a ray must travel along an axis in order to traverse an integer split
	float3 delta_t = fabs(1.0f / ray_dir);

	// Compute intersection_t and add in the offset
	float3 intersection_t = delta_t * ((ray_pos)-floor(ray_pos)) * convert_float3(voxel_step);

	// for negative values, wrap around the delta_t
	intersection_t += delta_t * -convert_float3(isless(intersection_t, 0));

	int3 face_mask = { 0, 0, 0 };
	int length_cutoff = 0;

	// Andrew Woo's raycasting algo
	do {

		// Fancy no branch version of the logic step
		face_mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy);
		intersection_t += delta_t * fabs(convert_float3(face_mask.xyz));
		voxel.xyz += voxel_step.xyz * face_mask.xyz;

		if (any(voxel >= *map_dim) ||
			any(voxel < 0)) {
			return false;
		}

		// If we hit a voxel
		int voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))];

		if (voxel_data != 0)
			return true;

		if (++length_cutoff > 300)
			return false;

	} while (any(isless(intersection_t, (float3)(distance_to_light - 1))));

	return false;
}

// ====================================== Raycaster entry point =====================================
// ==================================================================================================

constant float4 fog_color = { 0.73f, 0.81f, 0.89f, 0.8f };
constant float4 overshoot_color = { 0.00f, 0.00f, 0.00f, 0.00f };
constant float4 overshoot_color_2 = { 0.00f, 0.00f, 0.00f, 0.00f };

__kernel void raycaster(
	global char* map,
	global int3* map_dim,
	global int2* resolution,
	global float3* projection_matrix,
	global float2* cam_dir,
	global float3* cam_pos,
	global float* lights,
	global int* light_count,
	__write_only image2d_t image,
	//global int* seed_memory,
	__read_only image2d_t texture_atlas,
	global int2 *atlas_dim,
	global int2 *tile_dim,
	global ulong *octree_descriptor_buffer,
	global uint *octree_attachment_lookup_buffer,
	global ulong *octree_attachment_buffer,
	global ulong *settings_buffer
){
	//	int global_id = x * y;

	// Get and set the random seed from seed memory
	//int seed = seed_memory[global_id];
	//int random_number = rand(&seed);
	//seed_memory[global_id] = seed;

	// Get the pixel on the viewport, and find the view matrix ray that matches it
	int2 pixel = (int2)(get_global_id(0), get_global_id(1));
    float3 ray_dir = projection_matrix[pixel.x + (*resolution).x * pixel.y];

	// Pitch
	ray_dir = (float3)(
		ray_dir.z * sin((*cam_dir).x) + ray_dir.x * cos((*cam_dir).x),
		ray_dir.y,
		ray_dir.z * cos((*cam_dir).x) - ray_dir.x * sin((*cam_dir).x)
		);

	// Yaw
    ray_dir = (float3)(
        ray_dir.x * cos((*cam_dir).y) - ray_dir.y * sin((*cam_dir).y),
        ray_dir.x * sin((*cam_dir).y) + ray_dir.y * cos((*cam_dir).y),
        ray_dir.z
    );

	// Setup the voxel step based on what direction the ray is pointing
    int3 voxel_step = {1, 1, 1};
	voxel_step *= (ray_dir > 0) - (ray_dir < 0);

    // Setup the voxel coords from the camera origin
	int3 voxel = convert_int3(*cam_pos);

    // Delta T is the units a ray must travel along an axis in order to
    // traverse an integer split
	if (any(ray_dir == (0.0f,0.0f,0.0f)))
		return;
	float3 delta_t = fabs(1.0f / ray_dir);

	// Intersection T is the collection of the next intersection points
	// for all 3 axis XYZ. We take the full positive cardinality when
	// subtracting the floor, so we must transfer the sign over from
	// the voxel step
	float3 intersection_t = delta_t * ((*cam_pos) - ceil(*cam_pos)) * convert_float3(voxel_step);

	// When we transfer the sign over, we get the correct direction of
	// the offset, but we merely transposed over the value instead of mirroring
	// it over the axis like we want. So here, isless returns a boolean if intersection_t
	// is less than 0 which dictates whether or not we subtract the delta which in effect
	// mirrors the offset
	intersection_t -= delta_t * convert_float3(isless(intersection_t, 0));

	int distance_traveled = 0;
	int max_distance = 700;
	uint bounce_count = 0;
	int3 face_mask = { 0, 0, 0 };
	int voxel_data = 0;
	float3 face_position = (0,0,0);
	float4 voxel_color= (0,0,0,0);
	float2 tile_face_position = (0,0);
	float3 sign = (0,0,0);
	float4 first_strike = (0,0,0,0);

	// Andrew Woo's raycasting algo
    while (distance_traveled < max_distance && bounce_count < 2) {

		// Fancy no branch version of the logic step
		face_mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy);
		intersection_t += delta_t * fabs(convert_float3(face_mask.xyz));
		voxel.xyz += voxel_step.xyz * face_mask.xyz;

		// Test for out of bounds contions, add fog
		if (any(voxel >= *map_dim)){
			write_imagef(image, pixel, white_light(mix(fog_color, overshoot_color, 1.0 - max(distance_traveled / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask));
			return;
		}
		if (any(voxel < 0)) {
			write_imagef(image, pixel, white_light(mix(fog_color, overshoot_color_2, 1.0 - max(distance_traveled / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask));
			return;
		}

        // If we hit a voxel
		// if (voxel.x < 128 && voxel.y < 128 && voxel.z < 128){
		// 	if (get_oct_vox(
		// 		voxel,
		// 		octree_descriptor_buffer,
		// 		octree_attachment_lookup_buffer,
		// 		octree_attachment_buffer,
		// 		settings_buffer
		// 		)){
		// 			voxel_data = 1;
		// 		} else {
		// 			voxel_data = 0;
		// 		}
		// } else {
			voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))];
		//}

		if (voxel_data != 0) {

			// Determine where on the 2d plane the ray intersected
			face_position = (float3)(0);
			tile_face_position = (float2)(0);
			sign = (float3)(1.0f, 1.0f, 1.0f);

			// First determine the percent of the way the ray is towards the next intersection_t
			// in relation to the xyz position on the plane
			if (face_mask.x == -1) {

				sign.x *= -1.0;

				// the next intersection for this plane - the last intersection of the passed plane / delta of this plane
				// basically finds how far in on the other 2 axis we are when the ray traversed the plane
				float z_percent = (intersection_t.z - (intersection_t.x - delta_t.x)) / delta_t.z;
				float y_percent = (intersection_t.y - (intersection_t.x - delta_t.x)) / delta_t.y;

				// Since we intersected face x, we know that we are at the face (1.0)
				// I think the 1.001f rendering bug is the ray thinking it's within the voxel
				// even though it's sitting on the very edge
				face_position = (float3)(1.0001f, y_percent, z_percent);
				tile_face_position = (float2)(y_percent, z_percent);
			}
			else if (face_mask.y == -1) {

				sign.y *= -1.0;
				float x_percent = (intersection_t.x - (intersection_t.y - delta_t.y)) / delta_t.x;
				float z_percent = (intersection_t.z - (intersection_t.y - delta_t.y)) / delta_t.z;

				face_position = (float3)(x_percent, 1.0001f, z_percent);
				tile_face_position = (float2)(x_percent, z_percent);
			}

			else if (face_mask.z == -1) {

				sign.z *= -1.0;
				float x_percent = (intersection_t.x - (intersection_t.z - delta_t.z)) / delta_t.x;
				float y_percent = (intersection_t.y - (intersection_t.z - delta_t.z)) / delta_t.y;

				face_position = (float3)(x_percent, y_percent, 1.0001f);
				tile_face_position = (float2)(x_percent, y_percent);

			}

			// Because the raycasting process is agnostic to the quadrant
			// it's working in, we need to transpose the sign over to the face positions.
			// If we don't it will think that it is always working in the (1, 1, 1) quadrant
			// and will just "copy" the quadrant. This includes shadows as they use the face_position
			// in order to cast the intersection ray!!

			face_position.x = select((float)(face_position.x), (float)(-face_position.x + 1.0f), (int)(ray_dir.x > 0));
			tile_face_position.x = select((float)(tile_face_position.x), (float)(-tile_face_position.x + 1.0f), (int)(ray_dir.x < 0));

			if (ray_dir.y > 0){
				face_position.y =  - face_position.y + 1;
			} else {
				tile_face_position.x = 1.0 - tile_face_position.x;

				// We run into the Hairy ball problem, so we need to define
				// a special case for the zmask
				if (face_mask.z == -1) {
					tile_face_position.x = 1.0 - tile_face_position.x;
					tile_face_position.y = 1.0 - tile_face_position.y;
				}
			}

			face_position.z = select((float)(face_position.z), (float)(-face_position.z + 1.0f), (int)(ray_dir.z > 0));
			tile_face_position.y = select((float)(tile_face_position.y), (float)(-tile_face_position.y + 1.0f), (int)(ray_dir.z < 0));

			// Now either use the face position to retrieve a texture sample, or
			// just a plain color for the voxel color. Notice the JANK -1 after the
			// conditionals in the select statement. That's because select works on negs
			// and pos's. So a false equality will still eval as true as it is technically
			// a positive result (0)
			// voxel_color = select(
			// 	(float4)(0.25f, 0.64f, 0.87f, 0.0f),
			// 	(float4)voxel_color,
			//  	(int4)((voxel_data == 5) - 1)
			// );

			if (bounce_count == 0){
				voxel_color = (float4)read_imagef(
						 texture_atlas,
						 convert_int2(tile_face_position * convert_float2(*atlas_dim / *tile_dim)) +
						 convert_int2((float2)(3, 0) * convert_float2(*atlas_dim / *tile_dim))
				);

				voxel_color.w = 0.0f;
				first_strike = view_light(
			 			voxel_color,
			 			(convert_float3(voxel) + face_position) - (float3)(lights[4], lights[5], lights[6]),
			 			(float4)(lights[0], lights[1], lights[2], lights[3]),
			 			(convert_float3(voxel) + face_position) - (*cam_pos),
			 			face_mask * voxel_step
			 	);
				max_distance = 10;
				distance_traveled = 0;
			} else {
				// voxel_color = (float4)read_imagef(
				// 		 texture_atlas,
				// 		 convert_int2(tile_face_position * convert_float2(*atlas_dim / *tile_dim)) +
				// 		 convert_int2((float2)(3, 0) * convert_float2(*atlas_dim / *tile_dim))
				// );

				voxel_color.w = 0.2f;
				first_strike = white_light(voxel_color, (float3)(1.0f, 1.0f, 1.0f), face_mask);
			}
			//
			// voxel_color = select(
			// 	(float4)(0.0f, 0.239f, 0.419f, 0.0f),
			// 	(float4)read_imagef(
			// 		 texture_atlas,
			// 		 convert_int2(tile_face_position * convert_float2(*atlas_dim / *tile_dim)) +
			// 		 convert_int2((float2)(3, 0) * convert_float2(*atlas_dim / *tile_dim))
			// 	 ),
			// 	 (int4)((voxel_data == 6) - 1)
			// );

			// The new direction of the ray
			// The list of lights and their distances

			// a way to accumulate color and light intensity through multiple bounces??
			//		Only for indirect lighting and refraction
			//

			float3 hit_pos = convert_float3(voxel) + face_position;
			ray_dir = normalize((float3)(lights[4], lights[5], lights[6]) - hit_pos);
			if (any(ray_dir == (0.0f,0.0f,0.0f)))
				return;

			voxel -= voxel_step * face_mask;
			voxel_step = ( 1, 1, 1 );
			voxel_step *= (ray_dir > 0) - (ray_dir < 0);

			//voxel = convert_int3(hit_pos);

			delta_t = fabs(1.0f / ray_dir);
			intersection_t = delta_t * ((hit_pos)-floor(hit_pos)) * convert_float3(voxel_step);
			intersection_t += delta_t * -convert_float3(isless(intersection_t, 0));

			bounce_count += 1;

			// max_distance = DistanceBetweenPoints(convert_float3(voxel) + face_position, (float3)(lights[4], lights[5], lights[6]));

			// // This has a very large performance hit, I assume CL doesn't really
			// like calling into other functions with lots of state.
			// if (cast_light_intersection_ray(
			// 	map,
			// 	map_dim,
			// 	normalize((float3)(lights[4], lights[5], lights[6]) - (convert_float3(voxel) + face_position)),
			// 	(convert_float3(voxel) + face_position),
			// 	lights,
			// 	light_count
			// )) {
			//
			// 	// If the light ray intersected an object on the way to the light point
			// 	write_imagef(image, pixel, white_light(voxel_color, (float3)(1.0f, 1.0f, 1.0f), face_mask));
			// 	return;
			// }

			//return;
		}

		distance_traveled++;

    }
	write_imagef(
		image,
		pixel,
		first_strike
		);
	// write_imagef(
	// 	image,
	// 	pixel,
	// 	view_light(
	// 		voxel_color,
	// 		(convert_float3(voxel) + face_position) - (float3)(lights[4], lights[5], lights[6]),
	// 		(float4)(lights[0], lights[1], lights[2], lights[3]),
	// 		(convert_float3(voxel) + face_position) - (*cam_pos),
	// 		face_mask * voxel_step
	// 		)
	// 	);
    return;
}

float DistanceBetweenPoints(float3 a, float3 b) {
	return fast_distance(a, b);
	//return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}

float Distance(float3 a) {
	return fast_length(a);
	//return sqrt(pow(a.x, 2) + pow(a.y, 2) + pow(a.z, 2));
}

// Naive incident ray light
float4 white_light(float4 input, float3 light, int3 mask) {

	input.w = input.w + acos(
		dot(
			normalize(light),
			normalize(convert_float3(mask * (-mask)))
			)
		) / 32;

	input.w += 0.25f;

	return input;

}

// Phong + diffuse lighting function for g
//  0  1  2  3  4  5  6  7   8   9
// {r, g, b, i, x, y, z, x', y', z'}

float4 view_light(float4 in_color, float3 light, float4 light_color, float3 view, int3 mask) {

	float d = Distance(light) / 100.0f;
	d *= d;

	float diffuse = max(dot(normalize(convert_float3(mask)), normalize(light)), 0.0f);
	in_color += diffuse * light_color * 0.5f / d;

	if (dot(light, normalize(convert_float3(mask))) > 0.0f)
	{
		float3 halfwayVector = normalize(normalize(light) + normalize(view));
		float specTmp = max(dot(normalize(convert_float3(mask)), halfwayVector), 0.0f);
		in_color += pow(specTmp, 8.0f) * light_color * 0.5f / d;
	}
	if (in_color.w > 1.0f){
		in_color.xyz *= in_color.w;
	}

	return in_color;
}

int rand(int* seed) // 1 <= *seed < m
{
	int const a = 16807; //ie 7**5
	int const m = 2147483647; //ie 2**31-1

	*seed = ((*seed) * a) % m;
	return(*seed);
}

bool get_oct_vox(
	int3 position,
	global ulong *octree_descriptor_buffer,
	global uint *octree_attachment_lookup_buffer,
	global ulong *octree_attachment_buffer,
	global ulong *settings_buffer
){

	// (X, Y, Z) mask for the idx
	const uchar idx_set_x_mask = 0x1;
	const uchar idx_set_y_mask = 0x2;
	const uchar idx_set_z_mask = 0x4;

	const uchar mask_8[8] = {
		0x1,  0x2,  0x4,  0x8,
		0x10, 0x20, 0x40, 0x80
	};

	// Mask for counting the previous valid bits
	const uchar count_mask_8[8] = {
		0x1,  0x3,  0x7,  0xF,
		0x1F, 0x3F, 0x7F, 0xFF
	};

		// uint64_t manipulation masks
	const ulong child_pointer_mask = 0x0000000000007fff;
	const ulong far_bit_mask = 0x8000;
	const ulong valid_mask = 0xFF0000;
	const ulong leaf_mask = 0xFF000000;
	const ulong contour_pointer_mask = 0xFFFFFF00000000;
	const ulong contour_mask = 0xFF00000000000000;

	// push the root node to the parent stack
	ulong current_index = *settings_buffer;
	ulong head = octree_descriptor_buffer[current_index];

	uint parent_stack_position = 0;
	ulong parent_stack[32];

	uchar scale = 0;
	uchar idx_stack[32];

	ulong current_descriptor = 0;

	bool found = false;

	parent_stack[parent_stack_position] = head;

	// Set our initial dimension and the position at the corner of the oct to keep track of our position
	int dimension = 128;
	int3 quad_position = (0, 0, 0);

	// While we are not at the required resolution
	//		Traverse down by setting the valid/leaf mask to the subvoxel
	//		Check to see if it is valid
	//			Yes?
	//				Check to see if it is a leaf
	//				No? Break
	//				Yes? Scale down to the next hierarchy, push the parent to the stack
	//
	//			No?
	//				Break
	while (dimension > 1) {

		// So we can be a little bit tricky here and increment our
		// array index that holds our masks as we build the idx.
		// Adding 1 for X, 2 for Y, and 4 for Z
		int mask_index = 0;

		// Do the logic steps to find which sub oct we step down into
		if (position.x >= (dimension / 2) + quad_position.x) {

			// Set our voxel position to the (0,0) of the correct oct
			quad_position.x += (dimension / 2);

			// increment the mask index and mentioned above
			mask_index += 1;

			// Set the idx to represent the move
			idx_stack[scale] |= idx_set_x_mask;

		}
		if (position.y >= (dimension / 2) + quad_position.y) {

			quad_position.y |= (dimension / 2);

			mask_index += 2;

			// TODO What is up with the binary operator on this one?
			idx_stack[scale] ^= idx_set_y_mask;

		}
		if (position.z >= (dimension / 2) + quad_position.z) {

			quad_position.z += (dimension / 2);

			mask_index += 4;

			idx_stack[scale] |= idx_set_z_mask;

		}

		// Check to see if we are on a valid oct
		if ((head >> 16) & mask_8[mask_index]) {

			// Check to see if it is a leaf
			if ((head >> 24) & mask_8[mask_index]) {

				// If it is, then we cannot traverse further as CP's won't have been generated
				found = true;
				return found;
			}

			// If all went well and we found a valid non-leaf oct then we will traverse further down the hierarchy
			scale++;
			dimension /= 2;

			// Count the number of valid octs that come before and add it to the index to get the position
			// Negate it by one as it counts itself
			int count = popcount((uchar)(head >> 16) & count_mask_8[mask_index]) - 1;

			// access the element at which head points to and then add the specified number of indices
			// to get to the correct child descriptor
			current_index = current_index + (head & child_pointer_mask) + count;
			head = octree_descriptor_buffer[current_index];

			// Increment the parent stack position and put the new oct node as the parent
			parent_stack_position++;
			parent_stack[parent_stack_position] = head;

		}
		else {
			// If the oct was not valid, then no CP's exists any further
			// This implicitly says that if it's non-valid then it must be a leaf!!

			// It appears that the traversal is now working but I need
			// to focus on how to now take care of the end condition.
			// Currently it adds the last parent on the second to lowest
			// oct CP. Not sure if thats correct
			found = 0;
			return found;
		}
	}

	found = 1;
	return found;
}

// =================================== Boolean ray intersection ============================
// =========================================================================================

bool cast_light_intersection_ray(
	global char* map,
	global int3* map_dim,
	float3 ray_dir,
    float3 ray_pos,
	global float* lights,
	global int* light_count

	){

	float distance_to_light = DistanceBetweenPoints(ray_pos, (float3)(lights[4], lights[5], lights[6]));
	//if (distance_to_light > 200.0f){
	//	return false;
	//}

	// Setup the voxel step based on what direction the ray is pointing
	int3 voxel_step = { 1, 1, 1 };
	voxel_step *= (ray_dir > 0) - (ray_dir < 0);

	// Setup the voxel coords from the camera origin
	int3 voxel = convert_int3(ray_pos);

	// Delta T is the units a ray must travel along an axis in order to traverse an integer split
	float3 delta_t = fabs(1.0f / ray_dir);

	// Compute intersection_t and add in the offset
	float3 intersection_t = delta_t * ((ray_pos)-floor(ray_pos)) * convert_float3(voxel_step);

	// for negative values, wrap around the delta_t
	intersection_t += delta_t * -convert_float3(isless(intersection_t, 0));

	int3 face_mask = { 0, 0, 0 };
	int length_cutoff = 0;

	// Andrew Woo's raycasting algo
	do {

		// Fancy no branch version of the logic step
		face_mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy);
		intersection_t += delta_t * fabs(convert_float3(face_mask.xyz));
		voxel.xyz += voxel_step.xyz * face_mask.xyz;

		if (any(voxel >= *map_dim) ||
			any(voxel < 0)) {
			return false;
		}

		// If we hit a voxel
		int voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))];

		if (voxel_data != 0)
			return true;

		if (++length_cutoff > 300)
			return false;

	} while (any(isless(intersection_t, (float3)(distance_to_light - 1))));

	return false;
}

// ====================================== Raycaster entry point =====================================
// ==================================================================================================

constant float4 fog_color = { 0.73f, 0.81f, 0.89f, 0.8f };
constant float4 overshoot_color = { 0.00f, 0.00f, 0.00f, 0.00f };
constant float4 overshoot_color_2 = { 0.00f, 0.00f, 0.00f, 0.00f };

__kernel void raycaster(
	global char* map,
	global int3* map_dim,
	global int2* resolution,
	global float3* projection_matrix,
	global float2* cam_dir,
	global float3* cam_pos,
	global float* lights,
	global int* light_count,
	__write_only image2d_t image,
	//global int* seed_memory,
	__read_only image2d_t texture_atlas,
	global int2 *atlas_dim,
	global int2 *tile_dim,
	global ulong *octree_descriptor_buffer,
	global uint *octree_attachment_lookup_buffer,
	global ulong *octree_attachment_buffer,
	global ulong *settings_buffer
){
	//	int global_id = x * y;

	// Get and set the random seed from seed memory
	//int seed = seed_memory[global_id];
	//int random_number = rand(&seed);
	//seed_memory[global_id] = seed;

	// Get the pixel on the viewport, and find the view matrix ray that matches it
	int2 pixel = (int2)(get_global_id(0), get_global_id(1));
    float3 ray_dir = projection_matrix[pixel.x + (*resolution).x * pixel.y];

	// Pitch
	ray_dir = (float3)(
		ray_dir.z * sin((*cam_dir).x) + ray_dir.x * cos((*cam_dir).x),
		ray_dir.y,
		ray_dir.z * cos((*cam_dir).x) - ray_dir.x * sin((*cam_dir).x)
		);

	// Yaw
    ray_dir = (float3)(
        ray_dir.x * cos((*cam_dir).y) - ray_dir.y * sin((*cam_dir).y),
        ray_dir.x * sin((*cam_dir).y) + ray_dir.y * cos((*cam_dir).y),
        ray_dir.z
    );

	// Setup the voxel step based on what direction the ray is pointing
    int3 voxel_step = {1, 1, 1};
	voxel_step *= (ray_dir > 0) - (ray_dir < 0);

    // Setup the voxel coords from the camera origin
	int3 voxel = convert_int3(*cam_pos);

    // Delta T is the units a ray must travel along an axis in order to
    // traverse an integer split
	float3 delta_t = fabs(1.0f / ray_dir);

	// Intersection T is the collection of the next intersection points
	// for all 3 axis XYZ. We take the full positive cardinality when
	// subtracting the floor, so we must transfer the sign over from
	// the voxel step
	float3 intersection_t = delta_t * ((*cam_pos) - ceil(*cam_pos)) * convert_float3(voxel_step);

	// When we transfer the sign over, we get the correct direction of
	// the offset, but we merely transposed over the value instead of mirroring
	// it over the axis like we want. So here, isless returns a boolean if intersection_t
	// is less than 0 which dictates whether or not we subtract the delta which in effect
	// mirrors the offset
	intersection_t -= delta_t * convert_float3(isless(intersection_t, 0));

	int dist = 0;
	int3 face_mask = { 0, 0, 0 };
	int voxel_data = 0;
	// Andrew Woo's raycasting algo
    do {

		// Fancy no branch version of the logic step
		face_mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy);
		intersection_t += delta_t * fabs(convert_float3(face_mask.xyz));
		voxel.xyz += voxel_step.xyz * face_mask.xyz;

		// Test for out of bounds contions, add fog
		if (any(voxel >= *map_dim)){
			write_imagef(image, pixel, white_light(mix(fog_color, overshoot_color, 1.0 - max(dist / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask));
			return;
		}
		if (any(voxel < 0)) {
			write_imagef(image, pixel, white_light(mix(fog_color, overshoot_color_2, 1.0 - max(dist / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask));
			return;
		}

        // If we hit a voxel
		if (voxel.x < 128 && voxel.y < 128 && voxel.z < 128){
			if (get_oct_vox(
				voxel,
				octree_descriptor_buffer,
				octree_attachment_lookup_buffer,
				octree_attachment_buffer,
				settings_buffer
				)){
					voxel_data = 1;
				} else {
					voxel_data = 0;
				}
		} else {
			voxel_data = map[voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * (voxel.z))];
		}

		if (voxel_data != 0) {

			float4 voxel_color = (float4)(0.0f, 0.0f, 0.0f, 0.001f);

			// Determine where on the 2d plane the ray intersected
			float3 face_position = (float3)(0);
			float2 tile_face_position = (float2)(0);
			float3 sign = (float3)(1.0f, 1.0f, 1.0f);

			// First determine the percent of the way the ray is towards the next intersection_t
			// in relation to the xyz position on the plane
			if (face_mask.x == -1) {

				sign.x *= -1.0;

				// the next intersection for this plane - the last intersection of the passed plane / delta of this plane
				// basically finds how far in on the other 2 axis we are when the ray traversed the plane
				float z_percent = (intersection_t.z - (intersection_t.x - delta_t.x)) / delta_t.z;
				float y_percent = (intersection_t.y - (intersection_t.x - delta_t.x)) / delta_t.y;

				// Since we intersected face x, we know that we are at the face (1.0)
				// I think the 1.001f rendering bug is the ray thinking it's within the voxel
				// even though it's sitting on the very edge
				face_position = (float3)(1.0001f, y_percent, z_percent);
				tile_face_position = (float2)(y_percent, z_percent);
			}
			else if (face_mask.y == -1) {

				sign.y *= -1.0;
				float x_percent = (intersection_t.x - (intersection_t.y - delta_t.y)) / delta_t.x;
				float z_percent = (intersection_t.z - (intersection_t.y - delta_t.y)) / delta_t.z;

				face_position = (float3)(x_percent, 1.0001f, z_percent);
				tile_face_position = (float2)(x_percent, z_percent);
			}

			else if (face_mask.z == -1) {

				sign.z *= -1.0;
				float x_percent = (intersection_t.x - (intersection_t.z - delta_t.z)) / delta_t.x;
				float y_percent = (intersection_t.y - (intersection_t.z - delta_t.z)) / delta_t.y;

				face_position = (float3)(x_percent, y_percent, 1.0001f);
				tile_face_position = (float2)(x_percent, y_percent);

			}

			// Because the raycasting process is agnostic to the quadrant
			// it's working in, we need to transpose the sign over to the face positions.
			// If we don't it will think that it is always working in the (1, 1, 1) quadrant
			// and will just "copy" the quadrant. This includes shadows as they use the face_position
			// in order to cast the intersection ray!!

			face_position.x = select((float)(face_position.x), (float)(-face_position.x + 1.0f), (int)(ray_dir.x > 0));
			tile_face_position.x = select((float)(tile_face_position.x), (float)(-tile_face_position.x + 1.0f), (int)(ray_dir.x < 0));

			if (ray_dir.y > 0){
				face_position.y =  - face_position.y + 1;
			} else {
				tile_face_position.x = 1.0 - tile_face_position.x;

				// We run into the Hairy ball problem, so we need to define
				// a special case for the zmask
				if (face_mask.z == -1) {
					tile_face_position.x = 1.0 - tile_face_position.x;
					tile_face_position.y = 1.0 - tile_face_position.y;
				}
			}

			face_position.z = select((float)(face_position.z), (float)(-face_position.z + 1.0f), (int)(ray_dir.z > 0));
			tile_face_position.y = select((float)(tile_face_position.y), (float)(-tile_face_position.y + 1.0f), (int)(ray_dir.z < 0));

			// Now either use the face position to retrieve a texture sample, or
			// just a plain color for the voxel color. Notice the JANK -1 after the
			// conditionals in the select statement. That's because select works on negs
			// and pos's. So a false equality will still eval as true as it is technically
			// a positive result (0)
			voxel_color = select(
				(float4)(0.25f, 0.64f, 0.87f, 0.0f),
				(float4)voxel_color,
			 	(int4)((voxel_data == 5) - 1)
			);

			voxel_color = select(
				(float4)(0.0f, 0.239f, 0.419f, 0.0f),
				(float4)read_imagef(
					 texture_atlas,
					 convert_int2(tile_face_position * convert_float2(*atlas_dim / *tile_dim)) +
					 convert_int2((float2)(3, 0) * convert_float2(*atlas_dim / *tile_dim))
				 ),
				 (int4)((voxel_data == 6) - 1)
			);

		 	voxel_color.w = 0.0f;

			// This has a very large performance hit, I assume CL doesn't really
			// like calling into other functions with lots of state.
			if (cast_light_intersection_ray(
				map,
				map_dim,
				normalize((float3)(lights[4], lights[5], lights[6]) - (convert_float3(voxel) + face_position)),
				(convert_float3(voxel) + face_position),
				lights,
				light_count
			)) {

				// If the light ray intersected an object on the way to the light point
				write_imagef(image, pixel, white_light(voxel_color, (float3)(1.0f, 1.0f, 1.0f), face_mask));
				return;
			}

			//  0  1  2  3  4  5  6  7   8   9
			// {r, g, b, i, x, y, z, x', y', z'}

			write_imagef(
				image,
				pixel,
				view_light(
					voxel_color,
					(convert_float3(voxel) + face_position) - (float3)(lights[4], lights[5], lights[6]),
					(float4)(lights[0], lights[1], lights[2], lights[3]),
					(convert_float3(voxel) + face_position) - (*cam_pos),
					face_mask * voxel_step
					)
				);

			return;
		}

    } while (++dist < 700.0f);

	//write_imagef(image, pixel, white_light(mix(fog_color, (float4)(0.40, 0.00, 0.40, 0.2), 1.0 - max(dist / 700.0f, (float)0)), (float3)(lights[7], lights[8], lights[9]), face_mask));
    return;
}


Revert r229409: "Hack to try deleting file from build bots"

git-svn-id: ffe668792ed300d6c2daa1f6eba2e0aa28d7ec6c@229411 91177308-0d34-0410-b5e6-96231b3b80d8

// RUN: rm %S/cl-strict-aliasing.ll
// RUN: %clang_cc1 -x cl -emit-llvm -cl-strict-aliasing < %s


Fix index in radix move kernel

Fix index in radix move kernel

/******************************************************************************
 * function: radix sort                                                       *
 * input:                                                                     *
 * output:                                                                    *
 ******************************************************************************/

 // Adapted from: https://github.com/Schw1ng/GPURadixSort

typedef struct radix_config {
  uint radices;  // number of radices
  uint blocks;   // number of blocks
  uint gpb;      // groups per block
  uint r_val;    // R
  uint tpg;      // threads per group
  uint epg;      // elements per group
  uint rpb;      // radices per block
  uint mask;     // bit mask
  uint l_val;    // L
  uint tpb;      // threads per block
  uint size;     // total elements
} configuration;

kernel void zeroes(global uint* counters) {
  counters[get_global_id(0)] = 0;
}

// If this is a prefix sum, can be optimize this, maybe, write a static
// version that assumes local size <= 1024?
inline void prefix_local(local uint* data, int len, int threads) {
  uint lid = get_local_id(0);
  int inc = 2;
  // reduce
  while (inc <= len) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + inc) < len; i += threads * inc)
      data[i + j] = data[i] + data[i + j];
    inc = inc << 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // downsweep
  data[len - 1] = 0;
  barrier(CLK_GLOBAL_MEM_FENCE);
  while (inc >= 2) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + j) <= len; i+= threads * inc) {
      uint tmp = data[i + j];
      data[i + j] = data[i] + data[i + j];
      data[i] = tmp;
    }
    inc = inc >> 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
}

kernel void count(global uint* cell_in,
                  global volatile uint* counters,
                  configuration conf,
                  uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  // Current thread group is based of the local id.
  uint thread_grp = lid / conf.r_val;
  // Startindex of the datablock that corresponds to the Threadblock.
  int active_block = grp * conf.gpb * conf.epg ;
  // Offset inside the block for the threadgroup of the current thread.
  int active_group = (lid / conf.r_val) * conf.epg;
  // Number of counters for each radix.
  uint groups = conf.blocks * conf.gpb;
  // Each Threadgroup has its own counter for each radix and is
  // calculating the offset based on current block / group.
  uint grp_offset = grp * conf.gpb;
  // Startindex for the current thread.
  uint start = active_block + active_group + lid % conf.r_val;
  uint end = min(active_block + active_group + conf.epg, conf.size);
  for (;start < end; start += conf.tpg) {
    uint radix = (cell_in[start] >> offset) & conf.mask;
    // The following code ensures that the counters of each Threadgroup
    // are sequentially incremented.
    for (uint i = 0; i < conf.r_val; i++) {
      if (lid % conf.r_val == i) {
        uint tmp = (radix * groups) + grp_offset + thread_grp;
        // TODO: Are atomics needed or is the fence enough?
        atomic_inc(&counters[tmp]);
        // ++counters[tmp];
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}

kernel void sum(global uint* cell_in,
                global volatile uint* counters,
                global uint* prefixes,
                local uint* l_counters,
                configuration conf,
                uint offset) {
  const uint lid = get_local_id(0);
  const uint grp = get_group_id(0);
  const uint groups = conf.blocks * conf.gpb;
  uint radix = conf.rpb * grp;
  for (uint i = 0; i < conf.rpb; ++i) {
    uint start = (radix * groups) + lid;
    uint end = (radix + 1) * groups;
    // Copy groups counters of the radix to local memory
    int k = 0;
    for (uint j = start; j < end; j+= conf.tpb) {
      l_counters[lid + conf.tpb * k] = counters[j];
      ++k;
      // barrier(CLK_LOCAL_MEM_FENCE); // TODO: ?
    }
    barrier(CLK_LOCAL_MEM_FENCE);
    // prefix sum on the counters in local memory
    prefix_local(l_counters, groups, conf.tpb);
    barrier(CLK_LOCAL_MEM_FENCE);
    // Gesamtprefix für den aktuellen radix berechnen
    if (lid == 1) {
      prefixes[radix]
        = l_counters[groups - 1] + counters[((radix + 1) * groups) - 1];
    }
    // Write prefix sum back to global memory
    barrier(CLK_GLOBAL_MEM_FENCE);
    k = 0;
    for (uint j = start; j < end; j += conf.tpb) {
      counters[j] = l_counters[lid + conf.tpb * k];
      ++k;
      // barrier(CLK_GLOBAL_MEM_FENCE); // TODO: ?
    }
    ++radix;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
}

kernel void values(global uint* cell_in,
                   global uint* cell_out,
                   global uint* counters,
                   global uint* prefixes,
                   local uint* l_counters,
                   local uint* l_prefixes,
                   configuration conf,
                   uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  uint thread_grp = lid / conf.r_val;
  uint radix = conf.rpb * grp;
  uint groups = conf.gpb * conf.blocks;
  uint rc_offset = radix * groups;
  // erst abschließen der radix summierung
  for (uint i = 0 ; i < conf.rpb ; i++) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      l_counters[i * groups + j] = counters[rc_offset + i * groups + j];
    }
    barrier(CLK_LOCAL_MEM_FENCE);
  }
  // Read radix prefixes to localMemory
  for (uint i = lid; i < conf.radices; i += conf.tpb) {
    l_prefixes[i] = prefixes[i];
  }
  // prefix sum on the prefixes in local memory
  barrier(CLK_LOCAL_MEM_FENCE);
  prefix_local(l_prefixes, conf.radices, conf.tpb);
  barrier(CLK_LOCAL_MEM_FENCE);
  // add prefix sum to the subcounters
  for (uint i = 0; i < conf.rpb; ++i) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      l_counters[i * groups + j] += l_prefixes[radix + i];
    }
    barrier(CLK_LOCAL_MEM_FENCE);
  }
  // Write back radices at the calculated offsets
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      counters[rc_offset + i * groups + j] = l_counters[i * groups + j];
    }
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
  barrier(CLK_GLOBAL_MEM_FENCE);
  uint active_block = grp * conf.gpb * conf.epg;
  uint active_counter = grp * conf.gpb;
  uint active_group = (lid / conf.r_val) * conf.epg;
  uint start = active_block + active_group + lid % conf.r_val;
  uint end = min(active_block + active_group + conf.epg, conf.size);
  for (uint i = start; i < end; i += conf.tpg) {
    uint bits = (cell_in[i] >> offset) & conf.mask;
    for (uint j = 0; j < conf.r_val; ++j) {
      if (lid % conf.r_val == j) {
        uint tmp = bits * groups + active_counter + thread_grp;
        cell_out[counters[tmp]] = cell_in[i];
        // TODO: do we need atomics or is the barrier enough?
        //++counters[tmp];
        atomic_inc(&counters[tmp]);
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}

kernel void values_by_keys(global uint* cell_in,
                           global uint* cell_out,
                           global uint* value_in,
                           global uint* value_out,
                           global uint* counters,
                           global uint* prefixes,
                           local uint* l_counters,
                           local uint* l_prefixes,
                           configuration conf,
                           uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  uint thread_grp = lid / conf.r_val;
  uint act_radix = conf.rpb * grp;
  uint groups = conf.gpb * conf.blocks;
  int rc_offset = act_radix * groups;
  // Finish the radix sum
  for (uint i = 0 ; i < conf.rpb ; i++) {
    for (uint blockidx = lid; blockidx < groups; blockidx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory to
      // shared memory. Jeder Thread liest die Counter basierend auf der groupId
      // aus.
      l_counters[i * groups + blockidx]
        = counters[rc_offset + i * groups + blockidx];
    }
  }
  // Read radix prefixes to localMemory
  for (uint i = lid; i < conf.radices; i+= conf.tpb) {
    l_prefixes[i] = prefixes[i];
  }
  // build prefix sum over radix counters?
  // (seems to be the prefixes, not counters)
  barrier(CLK_GLOBAL_MEM_FENCE);
  prefix_local(l_prefixes, conf.radices, conf.tpb);
  barrier(CLK_GLOBAL_MEM_FENCE);
  // Add the sum of the radix to all subounter of the radixes
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      l_counters[i * groups + j] += l_prefixes[act_radix + i];
    }
    // barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // Write back the radixes to their respectve offers
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint blockidx = lid; blockidx < groups; blockidx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory to
      // shared memory. Jeder Thread liest die Counter basierend auf der
      // groupId aus
      counters[rc_offset + i * groups + blockidx]
        = l_counters[i * groups + blockidx];
    }
  }
  barrier(CLK_GLOBAL_MEM_FENCE);
  int active_block = grp * conf.gpb * conf.epg ;
  int active_counter = grp * conf.gpb  ;
  int active_group = (lid / conf.r_val) * conf.epg;
  uint idx = active_block + active_group +  lid % conf.r_val;
  //uint boundary = active_block + active_group + conf.epg;
  //boundary = (boundary < conf.size) ? boundary : conf.size;
  uint boundary = min(active_block + active_group + conf.epg, conf.size);
  for (; idx < boundary; idx += conf.tpg) {
    uint tmpRdx = (cell_in[idx] >> offset) & conf.mask;
    for (uint tmpIdx = 0; tmpIdx < conf.r_val; tmpIdx++) {
      if (lid % conf.r_val == tmpIdx) {
        uint tmp = tmpRdx * groups + active_counter + thread_grp;
        cell_out[counters[tmp]] = cell_in[idx];
        value_out[counters[tmp]] = value_in[idx];
        // TODO: is the increment ok, or should we use atomics?
        ++counters[tmp];
        //atomic_inc(&counters[tmp]);
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}

/******************************************************************************
 * function: radix sort                                                       *
 * input:                                                                     *
 * output:                                                                    *
 ******************************************************************************/

 // Adapted from: https://github.com/Schw1ng/GPURadixSort

typedef struct radix_config {
  uint radices;  // number of radices
  uint blocks;   // number of blocks
  uint gpb;      // groups per block
  uint r_val;    // R
  uint tpg;      // threads per group
  uint epg;      // elements per group
  uint rpb;      // radices per block
  uint mask;     // bit mask
  uint l_val;    // L
  uint tpb;      // threads per block
  uint size;     // total elements
} configuration;

kernel void zeroes(global uint* counters) {
  counters[get_global_id(0)] = 0;
}

// If this is a prefix sum, can be optimize this, maybe, write a static
// version that assumes local size <= 1024?
inline void prefix_local(local uint* data, int len, int threads) {
  uint lid = get_local_id(0);
  int inc = 2;
  // reduce
  while (inc <= len) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + inc) < len; i += threads * inc)
      data[i + j] = data[i] + data[i + j];
    inc = inc << 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // downsweep
  data[len - 1] = 0;
  barrier(CLK_GLOBAL_MEM_FENCE);
  while (inc >= 2) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + j) <= len; i+= threads * inc) {
      uint tmp = data[i + j];
      data[i + j] = data[i] + data[i + j];
      data[i] = tmp;
    }
    inc = inc >> 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
}

// The `global` thing here is just the memory placement, right?
inline void prefix_global(global uint* data, int len, int threads) {
  uint lid = get_local_id(0);
  int inc = 2;
  // reduce
  while (inc <= len) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + inc) < len ; i += threads * inc)
      data[i + j] = data[i] + data[i + j];
    inc = inc << 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // Downsweep
  data[len-1] = 0;
  barrier(CLK_GLOBAL_MEM_FENCE);
  while (inc >= 2) {
    int j = inc >> 1;
    for (int i = (j - 1) + (lid * inc); (i + j) <= len; i += threads * inc) {
      uint tmp = data[i + j];
      data[i + j] = data[i] + data[i + j];
      data[i] = tmp;
    }
    inc = inc >> 1;
    barrier(CLK_GLOBAL_MEM_FENCE);
  }
}

kernel void count(global uint* cell_in,
                  global volatile uint* counters,
                  configuration conf,
                  uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  // Current thread group is based of the local id.
  uint thread_grp = lid / conf.r_val;
  // Startindex of the datablock that corresponds to the Threadblock.
  int active_block = grp * conf.gpb * conf.epg ;
  // Offset inside the block for the threadgroup of the current thread.
  int active_group = (lid / conf.r_val) * conf.epg;
  // Number of counters for each radix.
  uint groups = conf.blocks * conf.gpb;
  // Each Threadgroup has its own counter for each radix and is
  // calculating the offset based on current block / group.
  uint grp_offset = grp * conf.gpb;
  // Startindex for the current thread.
  uint start = active_block + active_group + lid % conf.r_val;
  //uint end = active_block + active_group + conf.epg;
  //end = (end > conf.size) ? conf.size : end;
  uint end = min(active_block + active_group + conf.epg, conf.size);
  for (;start < end; start += conf.tpg) {
    uint act_radix = (cell_in[start] >> offset) & conf.mask;
    // The following code ensures that the counters of each Threadgroup
    // are sequentially incremented.
    for (uint i = 0 ; i < conf.r_val; i++) {
      if (lid % conf.r_val == i) {
        uint start = (act_radix * groups) + grp_offset + thread_grp;
        // TODO: not a big fan of this, maybe use atomics?
        ++counters[start];
        //atomic_inc(&counters[start]);
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}

kernel void sum(global uint* cell_in,
                global volatile uint* counters,
                global uint* prefixes,
                local uint* counts,
                configuration conf,
                uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  uint groups = conf.blocks * conf.gpb;
  uint act_radix = conf.rpb * grp;
  for (uint i = 0; i < conf.rpb; i++) {
    // The Num_Groups counters of the radix are read from global memory to
    // shared memory. Jeder Thread liest die Counter basierend auf der localid aus
    int k = 0;
    uint boarder = ((act_radix + 1) * groups);
    // boarder = (boarder > conf.blocks * conf.gpb)
    //         ? conf.blocks * conf.gpb : boarder;
    for (uint j = (act_radix * groups) + lid; j < boarder; j+= conf.tpb) {
      counts[lid + conf.tpb * k] = counters[j];
      ++k;
    }
    barrier(CLK_GLOBAL_MEM_FENCE);
    // Die einzelnen RadixCounter sind nun in dem counts local Memory
    prefix_local(counts, groups, conf.tpb);
    barrier(CLK_GLOBAL_MEM_FENCE);
    // Gesamtprefix für den aktuellen radix berechnen
    if (lid == 1) {
      prefixes[act_radix]
        = counts[(groups) - 1] + counters[((act_radix + 1) * groups) - 1];
    }
    // Errechnete Prefixsumme zurück in den global memory schreiben
    barrier(CLK_GLOBAL_MEM_FENCE);
    k = 0;
    for (uint j = (act_radix * groups) + lid; j < ((act_radix + 1) * groups);
         j += conf.tpb) {
      counters[j] = counts[lid + conf.tpb * k];
      ++k;
    }
    ++act_radix;
  }
}

kernel void values(global uint* cell_in,
                   global uint* cell_out,
                   global uint* counters,
                   global uint* prefixes,
                   local uint* l_counters,
                   local uint* l_prefixes,
                   configuration conf,
                   uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  uint thread_grp = lid / conf.r_val;
  uint act_radix = conf.rpb * grp;
  uint groups = conf.gpb * conf.blocks;
  int rc_offset = act_radix * groups;
  // erst abschließen der radix summierung
  for (uint i = 0 ; i < conf.rpb ; i++) {
    for (uint idx = lid; idx < groups; idx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory
      // to shared memory. Jeder Thread liest die Counter basierend auf der
      // groupId aus
      l_counters[i * groups + idx] = counters[rc_offset + i * groups + idx];
    }
  }
  // Read radix prefixes to localMemory
  for (uint i = lid; i < conf.radices; i += conf.tpb) {
    l_prefixes[i] = prefixes[i];
  }
  // Präfixsumme über die RadixCounter bilden.
  barrier(CLK_GLOBAL_MEM_FENCE);
  prefix_local(l_prefixes, conf.radices, conf.tpb);
  barrier(CLK_GLOBAL_MEM_FENCE);
  // Die Präfixsumme des Radixe auf alle subcounter der radixes addieren
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      l_counters[i * groups + j] += l_prefixes[act_radix + i];
    }
    // barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // Zurückschreiben der Radixe mit entsprechedem offset.
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint idx = lid; idx < groups; idx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory
      // to shared memory. Jeder Thread liest die Counter basierend auf
      // der groupId aus
      counters[rc_offset + i * groups + idx] = l_counters[i * groups + idx];
    }
  }
  barrier(CLK_GLOBAL_MEM_FENCE);
  int active_block = grp * conf.gpb * conf.epg;
  int active_counter = grp * conf.gpb;
  int active_group = (lid / conf.r_val) * conf.epg;
  uint idx = active_block + active_group + lid % conf.r_val;
  //uint boundary = active_block + active_group + conf.epg;
  //boundary = (boundary < conf.size) ? boundary : conf.size;
  uint boundary = min(active_block + active_group + conf.epg, conf.size);
  for (; idx < boundary; idx += conf.tpg) {
    uint tmpRdx = (cell_in[idx] >> offset) & conf.mask;
    for (uint tmpIdx = 0; tmpIdx < conf.r_val; tmpIdx++) {
      if (lid % conf.r_val == tmpIdx) {
        uint idx = tmpRdx * groups + active_counter + thread_grp;
        cell_out[counters[idx]] = cell_in[idx];
        ++counters[idx];
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}

kernel void values_by_keys(global uint* cell_in,
                           global uint* cell_out,
                           global uint* value_in,
                           global uint* value_out,
                           global uint* counters,
                           global uint* prefixes,
                           local uint* l_counters,
                           local uint* l_prefixes,
                           configuration conf,
                           uint offset) {
  uint lid = get_local_id(0);
  uint grp = get_group_id(0);
  uint thread_grp = lid / conf.r_val;
  uint act_radix = conf.rpb * grp;
  uint groups = conf.gpb * conf.blocks;
  int rc_offset = act_radix * groups;
  // Finish the radix sum
  for (uint i = 0 ; i < conf.rpb ; i++) {
    for (uint blockidx = lid; blockidx < groups; blockidx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory to
      // shared memory. Jeder Thread liest die Counter basierend auf der groupId
      // aus.
      l_counters[i * groups + blockidx]
        = counters[rc_offset + i * groups + blockidx];
    }
  }
  // Read radix prefixes to localMemory
  for (uint i = lid; i < conf.radices; i+= conf.tpb) {
    l_prefixes[i] = prefixes[i];
  }
  // build prefix sum over radix counters?
  // (seems to be the prefixes, not counters)
  barrier(CLK_GLOBAL_MEM_FENCE);
  prefix_local(l_prefixes, conf.radices, conf.tpb);
  barrier(CLK_GLOBAL_MEM_FENCE);
  // Add the sum of the radix to all subounter of the radixes
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint j = lid; j < groups; j += conf.tpb) {
      l_counters[i * groups + j] += l_prefixes[act_radix + i];
    }
    // barrier(CLK_GLOBAL_MEM_FENCE);
  }
  // Write back the radixes to their respectve offers
  for (uint i = 0; i < conf.rpb; i++) {
    for (uint blockidx = lid; blockidx < groups; blockidx += conf.tpb) {
      // The Num_Groups counters of the radix are read from global memory to
      // shared memory. Jeder Thread liest die Counter basierend auf der
      // groupId aus
      counters[rc_offset + i * groups + blockidx]
        = l_counters[i * groups + blockidx];
    }
  }
  barrier(CLK_GLOBAL_MEM_FENCE);
  int active_block = grp * conf.gpb * conf.epg ;
  int active_counter = grp * conf.gpb  ;
  int active_group = (lid / conf.r_val) * conf.epg;
  uint idx = active_block + active_group +  lid % conf.r_val;
  //uint boundary = active_block + active_group + conf.epg;
  //boundary = (boundary < conf.size) ? boundary : conf.size;
  uint boundary = min(active_block + active_group + conf.epg, conf.size);
  for (; idx < boundary; idx += conf.tpg) {
    uint tmpRdx = (cell_in[idx] >> offset) & conf.mask;
    for (uint tmpIdx = 0; tmpIdx < conf.r_val; tmpIdx++) {
      if (lid % conf.r_val == tmpIdx) {
        uint tmp = tmpRdx * groups + active_counter + thread_grp;
        cell_out[counters[tmp]] = cell_in[idx];
        value_out[counters[tmp]] = value_in[idx];
        // TODO: is the increment ok, or should we use atomics?
        ++counters[tmp];
        //atomic_inc(&counters[tmp]);
      }
      barrier(CLK_GLOBAL_MEM_FENCE);
    }
  }
}


OpenCL kernel change

OpenCL kernel change

This makes it work on my AMD.

/*
* This is based on the John The Ripper SHA512 code, modified for double SHA512 and for use as a miner in Bitmessage.
* This software is originally Copyright (c) 2012 Myrice <qqlddg at gmail dot com>
* and it is hereby released to the general public under the following terms:
* Redistribution and use in source and binary forms, with or without modification, are permitted.
*/

#ifdef cl_khr_byte_addressable_store
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : disable
#endif

#define uint8_t  unsigned char
#define uint32_t unsigned int
#define uint64_t unsigned long
#define SALT_SIZE 0

#define BINARY_SIZE 8
#define FULL_BINARY_SIZE 64

#define PLAINTEXT_LENGTH 72

#define CIPHERTEXT_LENGTH 128

/// Warning: This version of SWAP64(n) is slow and avoid bugs on AMD GPUs(7970)
// #define SWAP64(n)       as_ulong(as_uchar8(n).s76543210)

#define SWAP64(n) \
  (((n) << 56)					\
   | (((n) & 0xff00) << 40)			\
   | (((n) & 0xff0000) << 24)			\
   | (((n) & 0xff000000) << 8)			\
   | (((n) >> 8) & 0xff000000)			\
   | (((n) >> 24) & 0xff0000)			\
   | (((n) >> 40) & 0xff00)			\
   | ((n) >> 56))

#define rol(x,n) ((x << n) | (x >> (64-n)))
#define ror(x,n) ((x >> n) | (x << (64-n)))
#define Ch(x,y,z) ((x & y) ^ ( (~x) & z))
#define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z))
#define Sigma0(x) ((ror(x,28))  ^ (ror(x,34)) ^ (ror(x,39)))
#define Sigma1(x) ((ror(x,14))  ^ (ror(x,18)) ^ (ror(x,41)))
#define sigma0(x) ((ror(x,1))  ^ (ror(x,8)) ^(x>>7))
#define sigma1(x) ((ror(x,19)) ^ (ror(x,61)) ^(x>>6))

typedef struct { // notice memory align problem
	uint64_t H[8];
	uint32_t buffer[32];	//1024 bits
	uint32_t buflen;
} sha512_ctx;

typedef struct {
    uint64_t target;
    char v[PLAINTEXT_LENGTH+1];
} sha512_key;

/* Macros for reading/writing chars from int32's */
#define PUTCHAR(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & ~(0xffU << (((index) & 3) << 3))) + ((val) << (((index) & 3) << 3))

__constant uint64_t k[] = {
	0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL,
	    0xe9b5dba58189dbbcUL,
	0x3956c25bf348b538UL, 0x59f111f1b605d019UL, 0x923f82a4af194f9bUL,
	    0xab1c5ed5da6d8118UL,
	0xd807aa98a3030242UL, 0x12835b0145706fbeUL, 0x243185be4ee4b28cUL,
	    0x550c7dc3d5ffb4e2UL,
	0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
	    0xc19bf174cf692694UL,
	0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL,
	    0x240ca1cc77ac9c65UL,
	0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL,
	    0x76f988da831153b5UL,
	0x983e5152ee66dfabUL, 0xa831c66d2db43210UL, 0xb00327c898fb213fUL,
	    0xbf597fc7beef0ee4UL,
	0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL, 0x06ca6351e003826fUL,
	    0x142929670a0e6e70UL,
	0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL,
	    0x53380d139d95b3dfUL,
	0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL,
	    0x92722c851482353bUL,
	0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL,
	    0xc76c51a30654be30UL,
	0xd192e819d6ef5218UL, 0xd69906245565a910UL, 0xf40e35855771202aUL,
	    0x106aa07032bbd1b8UL,
	0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL, 0x2748774cdf8eeb99UL,
	    0x34b0bcb5e19b48a8UL,
	0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL,
	    0x682e6ff3d6b2b8a3UL,
	0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL,
	    0x8cc702081a6439ecUL,
	0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL,
	    0xc67178f2e372532bUL,
	0xca273eceea26619cUL, 0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL,
	    0xf57d4f7fee6ed178UL,
	0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL, 0x113f9804bef90daeUL,
	    0x1b710b35131c471bUL,
	0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
	    0x431d67c49c100d4cUL,
	0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL,
	    0x6c44198c4a475817UL,
};

static void setup_ctx(sha512_ctx* ctx, const char * password, uint8_t pass_len)
{
	uint32_t* b32 = ctx->buffer;

	//set password to buffer
    for (uint32_t i = 0; i < pass_len; i++) {
		PUTCHAR(b32,i,password[i]);
	}
    ctx->buflen = pass_len;

	//append 1 to ctx buffer
	uint32_t length = ctx->buflen;
	PUTCHAR(b32, length, 0x80);
	while((++length & 3) != 0)  {
		PUTCHAR(b32, length, 0);
	}

	uint32_t* buffer32 = b32+(length>>2);
	for(uint32_t i = length; i < 128; i+=4) {// append 0 to 128
		*buffer32++=0;
	}

	//append length to buffer
	uint64_t *buffer64 = (uint64_t *)ctx->buffer;
	buffer64[15] = SWAP64(((uint64_t) ctx->buflen) * 8);
}

inline uint64_t sha512(char* password)
{
    __private sha512_ctx ctx;
	setup_ctx(&ctx, password, 72);
	// sha512 main`
	int i;

	uint64_t a = 0x6a09e667f3bcc908UL;
	uint64_t b = 0xbb67ae8584caa73bUL;
	uint64_t c = 0x3c6ef372fe94f82bUL;
	uint64_t d = 0xa54ff53a5f1d36f1UL;
	uint64_t e = 0x510e527fade682d1UL;
	uint64_t f = 0x9b05688c2b3e6c1fUL;
	uint64_t g = 0x1f83d9abfb41bd6bUL;
	uint64_t h = 0x5be0cd19137e2179UL;

	__private uint64_t w[16];

	uint64_t *data = (uint64_t *) ctx.buffer;

	for (i = 0; i < 16; i++)
		w[i] = SWAP64(data[i]);

	uint64_t t1, t2;
	for (i = 0; i < 16; i++) {
		t1 = k[i] + w[i] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	for (i = 16; i < 80; i++) {

		w[i & 15] =sigma1(w[(i - 2) & 15]) + sigma0(w[(i - 15) & 15]) + w[(i -16) & 15] + w[(i - 7) & 15];
		t1 = k[i] + w[i & 15] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	uint64_t finalhash[8];

	finalhash[0] = SWAP64(a + 0x6a09e667f3bcc908UL);
	finalhash[1] = SWAP64(b + 0xbb67ae8584caa73bUL);
	finalhash[2] = SWAP64(c + 0x3c6ef372fe94f82bUL);
	finalhash[3] = SWAP64(d + 0xa54ff53a5f1d36f1UL);
	finalhash[4] = SWAP64(e + 0x510e527fade682d1UL);
	finalhash[5] = SWAP64(f + 0x9b05688c2b3e6c1fUL);
	finalhash[6] = SWAP64(g + 0x1f83d9abfb41bd6bUL);
	finalhash[7] = SWAP64(h + 0x5be0cd19137e2179UL);

	setup_ctx(&ctx, (char*) finalhash, 64);

	a = 0x6a09e667f3bcc908UL;
	b = 0xbb67ae8584caa73bUL;
	c = 0x3c6ef372fe94f82bUL;
	d = 0xa54ff53a5f1d36f1UL;
	e = 0x510e527fade682d1UL;
	f = 0x9b05688c2b3e6c1fUL;
	g = 0x1f83d9abfb41bd6bUL;
	h = 0x5be0cd19137e2179UL;

	data = (uint64_t *) ctx.buffer;
	//((uint64_t*)ctx.buffer)[8] = SWAP64((uint64_t)0x80);

	for (i = 0; i < 16; i++)
		w[i] = SWAP64(data[i]);

	for (i = 0; i < 16; i++) {
		t1 = k[i] + w[i] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	for (i = 16; i < 80; i++) {

		w[i & 15] =sigma1(w[(i - 2) & 15]) + sigma0(w[(i - 15) & 15]) + w[(i -16) & 15] + w[(i - 7) & 15];
		t1 = k[i] + w[i & 15] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}
	return SWAP64(a + 0x6a09e667f3bcc908UL);
}

__kernel void kernel_sha512(__global const sha512_key *password,__global uint64_t *hash, uint64_t start)
{
    uint64_t idx = get_global_id(0);
	if (idx == 0 && start == 0) {
		*hash = 0;
	}
	uint64_t winval;

	uint64_t junk[9];

	__global uint64_t * source = (__global uint64_t*) password->v;
	for (int i = 1; i < 9; i++) {
		junk[i] = source[i];
	}

	junk[0] = SWAP64(idx + (start));

	winval = sha512((char*)junk);
	if (SWAP64(winval) < password->target) {
		*hash = SWAP64(junk[0]);
	}
}

/*
* This is based on the John The Ripper SHA512 code, modified for double SHA512 and for use as a miner in Bitmessage.
* This software is originally Copyright (c) 2012 Myrice <qqlddg at gmail dot com>
* and it is hereby released to the general public under the following terms:
* Redistribution and use in source and binary forms, with or without modification, are permitted.
*/

#ifdef cl_khr_byte_addressable_store
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : disable
#endif

#define uint8_t  unsigned char
#define uint32_t unsigned int
#define uint64_t unsigned long
#define SALT_SIZE 0

#define BINARY_SIZE 8
#define FULL_BINARY_SIZE 64

#define PLAINTEXT_LENGTH 72

#define CIPHERTEXT_LENGTH 128

/// Warning: This version of SWAP64(n) is slow and avoid bugs on AMD GPUs(7970)
#define SWAP64(n)       as_ulong(as_uchar8(n).s76543210)

/*
#define SWAP64(n) \
  (((n) << 56)					\
   | (((n) & 0xff00) << 40)			\
   | (((n) & 0xff0000) << 24)			\
   | (((n) & 0xff000000) << 8)			\
   | (((n) >> 8) & 0xff000000)			\
   | (((n) >> 24) & 0xff0000)			\
   | (((n) >> 40) & 0xff00)			\
   | ((n) >> 56))
*/

#define rol(x,n) ((x << n) | (x >> (64-n)))
#define ror(x,n) ((x >> n) | (x << (64-n)))
#define Ch(x,y,z) ((x & y) ^ ( (~x) & z))
#define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z))
#define Sigma0(x) ((ror(x,28))  ^ (ror(x,34)) ^ (ror(x,39)))
#define Sigma1(x) ((ror(x,14))  ^ (ror(x,18)) ^ (ror(x,41)))
#define sigma0(x) ((ror(x,1))  ^ (ror(x,8)) ^(x>>7))
#define sigma1(x) ((ror(x,19)) ^ (ror(x,61)) ^(x>>6))

typedef struct { // notice memory align problem
	uint64_t H[8];
	uint32_t buffer[32];	//1024 bits
	uint32_t buflen;
} sha512_ctx;

typedef struct {
    uint64_t target;
    char v[PLAINTEXT_LENGTH+1];
} sha512_key;

/* Macros for reading/writing chars from int32's */
#define PUTCHAR(buf, index, val) (buf)[(index)>>2] = ((buf)[(index)>>2] & ~(0xffU << (((index) & 3) << 3))) + ((val) << (((index) & 3) << 3))

__constant uint64_t k[] = {
	0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL,
	    0xe9b5dba58189dbbcUL,
	0x3956c25bf348b538UL, 0x59f111f1b605d019UL, 0x923f82a4af194f9bUL,
	    0xab1c5ed5da6d8118UL,
	0xd807aa98a3030242UL, 0x12835b0145706fbeUL, 0x243185be4ee4b28cUL,
	    0x550c7dc3d5ffb4e2UL,
	0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
	    0xc19bf174cf692694UL,
	0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL,
	    0x240ca1cc77ac9c65UL,
	0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL,
	    0x76f988da831153b5UL,
	0x983e5152ee66dfabUL, 0xa831c66d2db43210UL, 0xb00327c898fb213fUL,
	    0xbf597fc7beef0ee4UL,
	0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL, 0x06ca6351e003826fUL,
	    0x142929670a0e6e70UL,
	0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL,
	    0x53380d139d95b3dfUL,
	0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL,
	    0x92722c851482353bUL,
	0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL,
	    0xc76c51a30654be30UL,
	0xd192e819d6ef5218UL, 0xd69906245565a910UL, 0xf40e35855771202aUL,
	    0x106aa07032bbd1b8UL,
	0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL, 0x2748774cdf8eeb99UL,
	    0x34b0bcb5e19b48a8UL,
	0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL,
	    0x682e6ff3d6b2b8a3UL,
	0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL,
	    0x8cc702081a6439ecUL,
	0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL,
	    0xc67178f2e372532bUL,
	0xca273eceea26619cUL, 0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL,
	    0xf57d4f7fee6ed178UL,
	0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL, 0x113f9804bef90daeUL,
	    0x1b710b35131c471bUL,
	0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
	    0x431d67c49c100d4cUL,
	0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL,
	    0x6c44198c4a475817UL,
};

static void setup_ctx(sha512_ctx* ctx, const char * password, uint8_t pass_len)
{
	uint32_t* b32 = ctx->buffer;

	//set password to buffer
    for (uint32_t i = 0; i < pass_len; i++) {
		PUTCHAR(b32,i,password[i]);
	}
    ctx->buflen = pass_len;

	//append 1 to ctx buffer
	uint32_t length = ctx->buflen;
	PUTCHAR(b32, length, 0x80);
	while((++length & 3) != 0)  {
		PUTCHAR(b32, length, 0);
	}

	uint32_t* buffer32 = b32+(length>>2);
	for(uint32_t i = length; i < 128; i+=4) {// append 0 to 128
		*buffer32++=0;
	}

	//append length to buffer
	uint64_t *buffer64 = (uint64_t *)ctx->buffer;
	buffer64[15] = SWAP64(((uint64_t) ctx->buflen) * 8);
}

inline uint64_t sha512(char* password)
{
    __private sha512_ctx ctx;
	setup_ctx(&ctx, password, 72);
	// sha512 main`
	int i;

	uint64_t a = 0x6a09e667f3bcc908UL;
	uint64_t b = 0xbb67ae8584caa73bUL;
	uint64_t c = 0x3c6ef372fe94f82bUL;
	uint64_t d = 0xa54ff53a5f1d36f1UL;
	uint64_t e = 0x510e527fade682d1UL;
	uint64_t f = 0x9b05688c2b3e6c1fUL;
	uint64_t g = 0x1f83d9abfb41bd6bUL;
	uint64_t h = 0x5be0cd19137e2179UL;

	__private uint64_t w[16];

	uint64_t *data = (uint64_t *) ctx.buffer;

	for (i = 0; i < 16; i++)
		w[i] = SWAP64(data[i]);

	uint64_t t1, t2;
	for (i = 0; i < 16; i++) {
		t1 = k[i] + w[i] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	for (i = 16; i < 80; i++) {

		w[i & 15] =sigma1(w[(i - 2) & 15]) + sigma0(w[(i - 15) & 15]) + w[(i -16) & 15] + w[(i - 7) & 15];
		t1 = k[i] + w[i & 15] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	uint64_t finalhash[8];

	finalhash[0] = SWAP64(a + 0x6a09e667f3bcc908UL);
	finalhash[1] = SWAP64(b + 0xbb67ae8584caa73bUL);
	finalhash[2] = SWAP64(c + 0x3c6ef372fe94f82bUL);
	finalhash[3] = SWAP64(d + 0xa54ff53a5f1d36f1UL);
	finalhash[4] = SWAP64(e + 0x510e527fade682d1UL);
	finalhash[5] = SWAP64(f + 0x9b05688c2b3e6c1fUL);
	finalhash[6] = SWAP64(g + 0x1f83d9abfb41bd6bUL);
	finalhash[7] = SWAP64(h + 0x5be0cd19137e2179UL);

	setup_ctx(&ctx, (char*) finalhash, 64);

	a = 0x6a09e667f3bcc908UL;
	b = 0xbb67ae8584caa73bUL;
	c = 0x3c6ef372fe94f82bUL;
	d = 0xa54ff53a5f1d36f1UL;
	e = 0x510e527fade682d1UL;
	f = 0x9b05688c2b3e6c1fUL;
	g = 0x1f83d9abfb41bd6bUL;
	h = 0x5be0cd19137e2179UL;

	data = (uint64_t *) ctx.buffer;
	//((uint64_t*)ctx.buffer)[8] = SWAP64((uint64_t)0x80);

	for (i = 0; i < 16; i++)
		w[i] = SWAP64(data[i]);

	for (i = 0; i < 16; i++) {
		t1 = k[i] + w[i] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}

	for (i = 16; i < 80; i++) {

		w[i & 15] =sigma1(w[(i - 2) & 15]) + sigma0(w[(i - 15) & 15]) + w[(i -16) & 15] + w[(i - 7) & 15];
		t1 = k[i] + w[i & 15] + h + Sigma1(e) + Ch(e, f, g);
		t2 = Maj(a, b, c) + Sigma0(a);

		h = g;
		g = f;
		f = e;
		e = d + t1;
		d = c;
		c = b;
		b = a;
		a = t1 + t2;
	}
	return SWAP64(a + 0x6a09e667f3bcc908UL);
}

__kernel void kernel_sha512(__global const sha512_key *password,__global uint64_t *hash, uint64_t start)
{
    uint64_t idx = get_global_id(0);
	if (idx == 0 && start == 0) {
		*hash = 0;
	}
	uint64_t winval;

	uint64_t junk[9];

	__global uint64_t * source = (__global uint64_t*) password->v;
	for (int i = 1; i < 9; i++) {
		junk[i] = source[i];
	}

	junk[0] = SWAP64(idx + (start));

	winval = sha512((char*)junk);
	if (SWAP64(winval) < password->target) {
		*hash = SWAP64(junk[0]);
	}
}