Path: blob/master/servers/rendering/rendering_light_culler.h
10277 views
/**************************************************************************/1/* rendering_light_culler.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/math/plane.h"33#include "core/math/vector3.h"34#include "renderer_scene_cull.h"3536struct Projection;37struct Transform3D;3839// For testing performance improvements from the LightCuller:40// Uncomment LIGHT_CULLER_DEBUG_FLASH and it will turn the culler41// on and off every LIGHT_CULLER_DEBUG_FLASH_FREQUENCY camera prepares.42// Uncomment LIGHT_CULLER_DEBUG_LOGGING to get periodic print of the number of casters culled before / after.43// Uncomment LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT to get periodic print of the number of casters culled for the directional light..4445// #define LIGHT_CULLER_DEBUG_LOGGING46// #define LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT47// #define LIGHT_CULLER_DEBUG_REGULAR_LIGHT48// #define LIGHT_CULLER_DEBUG_FLASH49#define LIGHT_CULLER_DEBUG_FLASH_FREQUENCY 102450////////////////////////////////////////////////////////////////////////////////////////////////5152// The code to generate the lookup table is included but commented out.53// This may be useful for debugging / regenerating the LUT in the future,54// especially if the order of planes changes.55// When this define is set, the generated lookup table will be printed to debug output.56// The generated lookup table can be copy pasted57// straight to LUT_entry_sizes and LUT_entries.58// See the referenced article for explanation.59// #define RENDERING_LIGHT_CULLER_CALCULATE_LUT6061////////////////////////////////////////////////////////////////////////////////////////////////62// This define will be set automatically depending on earlier defines, you can leave this as is.63#if defined(LIGHT_CULLER_DEBUG_LOGGING) || defined(RENDERING_LIGHT_CULLER_CALCULATE_LUT)64#define RENDERING_LIGHT_CULLER_DEBUG_STRINGS65#endif6667// Culls shadow casters that can't cast shadows into the camera frustum.68class RenderingLightCuller {69public:70RenderingLightCuller();7172private:73class LightSource {74public:75enum SourceType {76ST_UNKNOWN,77ST_DIRECTIONAL,78ST_SPOTLIGHT,79ST_OMNI,80};8182LightSource() {83type = ST_UNKNOWN;84angle = 0.0f;85range = FLT_MAX;86}8788// All in world space, culling done in world space.89Vector3 pos;90Vector3 dir;91SourceType type;9293float angle; // For spotlight.94float range;95};9697// Same order as godot.98enum PlaneOrder {99PLANE_NEAR,100PLANE_FAR,101PLANE_LEFT,102PLANE_TOP,103PLANE_RIGHT,104PLANE_BOTTOM,105PLANE_TOTAL,106};107108// Same order as godot.109enum PointOrder {110PT_FAR_LEFT_TOP,111PT_FAR_LEFT_BOTTOM,112PT_FAR_RIGHT_TOP,113PT_FAR_RIGHT_BOTTOM,114PT_NEAR_LEFT_TOP,115PT_NEAR_LEFT_BOTTOM,116PT_NEAR_RIGHT_TOP,117PT_NEAR_RIGHT_BOTTOM,118};119120// 6 bits, 6 planes.121enum {122NUM_CAM_PLANES = 6,123NUM_CAM_POINTS = 8,124MAX_CULL_PLANES = 17,125LUT_SIZE = 64,126};127128public:129// Before each pass with a different camera, you must call this so the culler can pre-create130// the camera frustum planes and corner points in world space which are used for the culling.131bool prepare_camera(const Transform3D &p_cam_transform, const Projection &p_cam_matrix);132133// REGULAR LIGHTS (SPOT, OMNI).134// These are prepared then used for culling one by one, single threaded.135// prepare_regular_light() returns false if the entire light is culled (i.e. there is no intersection between the light and the view frustum).136bool prepare_regular_light(const RendererSceneCull::Instance &p_instance) { return _prepare_light(p_instance, -1); }137138// Cull according to the regular light planes that were setup in the previous call to prepare_regular_light.139void cull_regular_light(PagedArray<RendererSceneCull::Instance *> &r_instance_shadow_cull_result);140141// Directional lights are prepared in advance, and can be culled multithreaded chopping and changing between142// different directional_light_id.143void prepare_directional_light(const RendererSceneCull::Instance *p_instance, int32_t p_directional_light_id);144145// Return false if the instance is to be culled.146bool cull_directional_light(const RendererSceneCull::InstanceBounds &p_bound, int32_t p_directional_light_id);147148// Can turn on and off from the engine if desired.149void set_caster_culling_active(bool p_active) { data.caster_culling_active = p_active; }150void set_light_culling_active(bool p_active) { data.light_culling_active = p_active; }151152private:153struct LightCullPlanes {154void add_cull_plane(const Plane &p);155Plane cull_planes[MAX_CULL_PLANES];156int num_cull_planes = 0;157#ifdef LIGHT_CULLER_DEBUG_DIRECTIONAL_LIGHT158uint32_t rejected_count = 0;159#endif160};161162bool _prepare_light(const RendererSceneCull::Instance &p_instance, int32_t p_directional_light_id = -1);163164// Avoid adding extra culling planes derived from near colinear triangles.165// The normals derived from these will be inaccurate, and can lead to false166// culling of objects that should be within the light volume.167bool _is_colinear_tri(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c) const {168// Lengths of sides a, b and c.169float la = (p_b - p_a).length();170float lb = (p_c - p_b).length();171float lc = (p_c - p_a).length();172173// Get longest side into lc.174if (lb < la) {175SWAP(la, lb);176}177if (lc < lb) {178SWAP(lb, lc);179}180181// Prevent divide by zero.182if (lc > 0.001f) {183// If the summed length of the smaller two184// sides is close to the length of the longest side,185// the points are colinear, and the triangle is near degenerate.186float ld = ((la + lb) - lc) / lc;187188// ld will be close to zero for colinear tris.189return ld < 0.001f;190}191192// Don't create planes from tiny triangles,193// they won't be accurate.194return true;195}196197// Internal version uses LightSource.198bool _add_light_camera_planes(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);199200// Directional light gives parallel culling planes (as opposed to point lights).201bool add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source);202203// Is the light culler active? maybe not in the editor...204bool is_caster_culling_active() const { return data.caster_culling_active; }205bool is_light_culling_active() const { return data.light_culling_active; }206207// Do we want to log some debug output?208bool is_logging() const { return data.debug_count == 0; }209210struct Data {211// Camera frustum planes (world space) - order ePlane.212Vector<Plane> frustum_planes;213214// Camera frustum corners (world space) - order ePoint.215Vector3 frustum_points[NUM_CAM_POINTS];216217// Master can have multiple directional lights.218// These need to store their own cull planes individually, as master219// chops and changes between culling different lights220// instead of doing one by one, and we don't want to prepare221// lights multiple times per frame.222LocalVector<LightCullPlanes> directional_cull_planes;223224// Single threaded cull planes for regular lights225// (OMNI, SPOT). These lights reuse the same set of cull plane data.226LightCullPlanes regular_cull_planes;227228#ifdef LIGHT_CULLER_DEBUG_REGULAR_LIGHT229uint32_t regular_rejected_count = 0;230#endif231// The whole regular light can be out of range of the view frustum, in which case all casters should be culled.232bool out_of_range = false;233234#ifdef RENDERING_LIGHT_CULLER_DEBUG_STRINGS235static String plane_bitfield_to_string(unsigned int BF);236// Names of the plane and point enums, useful for debugging.237static const char *string_planes[];238static const char *string_points[];239#endif240241// Precalculated look up table.242static uint8_t LUT_entry_sizes[LUT_SIZE];243static uint8_t LUT_entries[LUT_SIZE][8];244245bool caster_culling_active = true;246bool light_culling_active = true;247248// Light culling is a basic on / off switch.249// Caster culling only works if light culling is also on.250bool is_active() const { return light_culling_active; }251252// Ideally a frame counter, but for ease of implementation253// this is just incremented on each prepare_camera.254// used to turn on and off debugging features.255int debug_count = -1;256} data;257258// This functionality is not required in general use (and is compiled out),259// as the lookup table can normally be hard coded260// (provided order of planes etc does not change).261// It is provided for debugging / future maintenance.262#ifdef RENDERING_LIGHT_CULLER_CALCULATE_LUT263void get_neighbouring_planes(PlaneOrder p_plane, PlaneOrder r_neigh_planes[4]) const;264void get_corners_of_planes(PlaneOrder p_plane_a, PlaneOrder p_plane_b, PointOrder r_points[2]) const;265void create_LUT();266void compact_LUT_entry(uint32_t p_entry_id);267void debug_print_LUT();268void debug_print_LUT_as_table();269void add_LUT(int p_plane_0, int p_plane_1, PointOrder p_pts[2]);270void add_LUT_entry(uint32_t p_entry_id, PointOrder p_pts[2]);271String debug_string_LUT_entry(const LocalVector<uint8_t> &p_entry, bool p_pair = false);272String string_LUT_entry(const LocalVector<uint8_t> &p_entry);273274// Contains a list of points for each combination of plane facing directions.275LocalVector<uint8_t> _calculated_LUT[LUT_SIZE];276#endif277};278279280