/*************************************************************************/
/*  raycast_occlusion_cull.h                                             */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef OCCLUSION_CULL_RAYCASTER_H
#define OCCLUSION_CULL_RAYCASTER_H

#include "core/io/image.h"
#include "core/math/camera_matrix.h"
#include "core/object/object.h"
#include "core/object/ref_counted.h"
#include "core/templates/local_vector.h"
#include "core/templates/rid_owner.h"
#include "scene/resources/mesh.h"
#include "servers/rendering/renderer_scene_occlusion_cull.h"

#include <embree3/rtcore.h>

class RaycastOcclusionCull : public RendererSceneOcclusionCull {
	typedef RTCRayHit16 RayPacket;

public:
	class RaycastHZBuffer : public HZBuffer {
	private:
		Size2i packs_size;

		struct CameraRayThreadData {
			CameraMatrix camera_matrix;
			Transform3D camera_transform;
			bool camera_orthogonal;
			int thread_count;
			Size2i buffer_size;
		};

		void _camera_rays_threaded(uint32_t p_thread, CameraRayThreadData *p_data);
		void _generate_camera_rays(const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, int p_from, int p_to);

	public:
		LocalVector<RayPacket> camera_rays;
		LocalVector<uint32_t> camera_ray_masks;
		RID scenario_rid;

		virtual void clear() override;
		virtual void resize(const Size2i &p_size) override;
		void sort_rays();
		void update_camera_rays(const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_work_pool);
	};

private:
	struct InstanceID {
		RID scenario;
		RID instance;

		bool operator<(const InstanceID &rhs) const {
			if (instance == rhs.instance) {
				return rhs.scenario < scenario;
			}
			return instance < rhs.instance;
		}

		InstanceID() {}
		InstanceID(RID s, RID i) :
				scenario(s), instance(i) {}
	};

	struct Occluder {
		PackedVector3Array vertices;
		PackedInt32Array indices;
		Set<InstanceID> users;
	};

	struct OccluderInstance {
		RID occluder;
		LocalVector<uint32_t> indices;
		LocalVector<Vector3> xformed_vertices;
		Transform3D xform;
		bool enabled = true;
		bool removed = false;
	};

	struct Scenario {
		struct RaycastThreadData {
			RayPacket *rays;
			const uint32_t *masks;
		};

		struct TransformThreadData {
			uint32_t thread_count;
			uint32_t vertex_count;
			Transform3D xform;
			const Vector3 *read;
			Vector3 *write;
		};

		Thread *commit_thread = nullptr;
		bool commit_done = true;
		bool dirty = false;
		bool removed = false;

		RTCScene ebr_scene[2] = { nullptr, nullptr };
		int current_scene_idx = 0;

		HashMap<RID, OccluderInstance> instances;
		Set<RID> dirty_instances; // To avoid duplicates
		LocalVector<RID> dirty_instances_array; // To iterate and split into threads
		LocalVector<RID> removed_instances;

		void _update_dirty_instance_thread(int p_idx, RID *p_instances);
		void _update_dirty_instance(int p_idx, RID *p_instances, ThreadWorkPool *p_thread_pool);
		void _transform_vertices_thread(uint32_t p_thread, TransformThreadData *p_data);
		void _transform_vertices_range(const Vector3 *p_read, Vector3 *p_write, const Transform3D &p_xform, int p_from, int p_to);
		static void _commit_scene(void *p_ud);
		bool update(ThreadWorkPool &p_thread_pool);

		void _raycast(uint32_t p_thread, const RaycastThreadData *p_raycast_data) const;
		void raycast(LocalVector<RayPacket> &r_rays, const LocalVector<uint32_t> p_valid_masks, ThreadWorkPool &p_thread_pool) const;
	};

	static RaycastOcclusionCull *raycast_singleton;

	static const int TILE_SIZE = 4;
	static const int TILE_RAYS = TILE_SIZE * TILE_SIZE;

	RTCDevice ebr_device = nullptr;
	RID_PtrOwner<Occluder> occluder_owner;
	HashMap<RID, Scenario> scenarios;
	HashMap<RID, RaycastHZBuffer> buffers;
	RS::ViewportOcclusionCullingBuildQuality build_quality;

	void _init_embree();

public:
	virtual bool is_occluder(RID p_rid) override;
	virtual RID occluder_allocate() override;
	virtual void occluder_initialize(RID p_occluder) override;
	virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) override;
	virtual void free_occluder(RID p_occluder) override;

	virtual void add_scenario(RID p_scenario) override;
	virtual void remove_scenario(RID p_scenario) override;
	virtual void scenario_set_instance(RID p_scenario, RID p_instance, RID p_occluder, const Transform3D &p_xform, bool p_enabled) override;
	virtual void scenario_remove_instance(RID p_scenario, RID p_instance) override;

	virtual void add_buffer(RID p_buffer) override;
	virtual void remove_buffer(RID p_buffer) override;
	virtual HZBuffer *buffer_get_ptr(RID p_buffer) override;
	virtual void buffer_set_scenario(RID p_buffer, RID p_scenario) override;
	virtual void buffer_set_size(RID p_buffer, const Vector2i &p_size) override;
	virtual void buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, ThreadWorkPool &p_thread_pool) override;
	virtual RID buffer_get_debug_texture(RID p_buffer) override;

	virtual void set_build_quality(RS::ViewportOcclusionCullingBuildQuality p_quality) override;

	RaycastOcclusionCull();
	~RaycastOcclusionCull();
};

#endif // OCCLUSION_CULL_RAYCASTER_H