Path: blob/master/modules/lightmapper_rd/lightmapper_rd.h
10277 views
/**************************************************************************/1/* lightmapper_rd.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/templates/local_vector.h"33#include "scene/3d/lightmapper.h"34#include "scene/resources/mesh.h"35#include "servers/rendering/rendering_device.h"3637class RDShaderFile;38class LightmapperRD : public Lightmapper {39GDCLASS(LightmapperRD, Lightmapper)4041struct BakeParameters {42float world_size[3] = {};43float bias = 0.0;4445float to_cell_offset[3] = {};46int32_t grid_size = 0;4748float to_cell_size[3] = {};49uint32_t light_count = 0;5051float env_transform[12] = {};5253int32_t atlas_size[2] = {};54float exposure_normalization = 0.0f;55uint32_t bounces = 0;5657float bounce_indirect_energy = 0.0f;58uint32_t shadowmask_light_idx = 0;59uint32_t transparency_rays = 0;60float supersampling_factor = 0.0f;61};6263struct MeshInstance {64MeshData data;65int slice = 0;66Vector2i offset;67};6869struct Light {70float position[3] = {};71uint32_t type = LIGHT_TYPE_DIRECTIONAL;72float direction[3] = {};73float energy = 0.0;74float color[3] = {};75float size = 0.0;76float range = 0.0;77float attenuation = 0.0;78float cos_spot_angle = 0.0;79float inv_spot_attenuation = 0.0;80float indirect_energy = 0.0;81float shadow_blur = 0.0;82uint32_t static_bake = 0;83uint32_t pad = 0;8485bool operator<(const Light &p_light) const {86return type < p_light.type;87}88};8990struct LightMetadata {91String name;92uint32_t type = LIGHT_TYPE_DIRECTIONAL;9394bool operator<(const LightMetadata &p_light) const {95return type < p_light.type;96}97};9899struct Vertex {100float position[3] = {};101float normal_z = 0.0;102float uv[2] = {};103float normal_xy[2] = {};104105bool operator==(const Vertex &p_vtx) const {106return (position[0] == p_vtx.position[0]) &&107(position[1] == p_vtx.position[1]) &&108(position[2] == p_vtx.position[2]) &&109(uv[0] == p_vtx.uv[0]) &&110(uv[1] == p_vtx.uv[1]) &&111(normal_xy[0] == p_vtx.normal_xy[0]) &&112(normal_xy[1] == p_vtx.normal_xy[1]) &&113(normal_z == p_vtx.normal_z);114}115};116117struct Edge {118Vector3 a;119Vector3 b;120Vector3 na;121Vector3 nb;122bool operator==(const Edge &p_seam) const {123return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;124}125Edge() {126}127128Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {129a = p_a;130b = p_b;131na = p_na;132nb = p_nb;133}134};135136struct Probe {137float position[4] = {};138};139140Vector<Probe> probe_positions;141142struct EdgeHash {143_FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {144uint32_t h = hash_murmur3_one_float(p_edge.a.x);145h = hash_murmur3_one_float(p_edge.a.y, h);146h = hash_murmur3_one_float(p_edge.a.z, h);147h = hash_murmur3_one_float(p_edge.b.x, h);148h = hash_murmur3_one_float(p_edge.b.y, h);149h = hash_murmur3_one_float(p_edge.b.z, h);150return h;151}152};153struct EdgeUV2 {154Vector2 a;155Vector2 b;156Vector2i indices;157bool operator==(const EdgeUV2 &p_uv2) const {158return a == p_uv2.a && b == p_uv2.b;159}160bool seam_found = false;161EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {162a = p_a;163b = p_b;164indices = p_indices;165}166EdgeUV2() {}167};168169struct Seam {170Vector2i a;171Vector2i b;172uint32_t slice;173bool operator<(const Seam &p_seam) const {174return slice < p_seam.slice;175}176};177178struct VertexHash {179_FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {180uint32_t h = hash_murmur3_one_float(p_vtx.position[0]);181h = hash_murmur3_one_float(p_vtx.position[1], h);182h = hash_murmur3_one_float(p_vtx.position[2], h);183h = hash_murmur3_one_float(p_vtx.uv[0], h);184h = hash_murmur3_one_float(p_vtx.uv[1], h);185h = hash_murmur3_one_float(p_vtx.normal_xy[0], h);186h = hash_murmur3_one_float(p_vtx.normal_xy[1], h);187h = hash_murmur3_one_float(p_vtx.normal_z, h);188return hash_fmix32(h);189}190};191192struct Triangle {193uint32_t indices[3] = {};194uint32_t slice = 0;195float min_bounds[3] = {};196uint32_t cull_mode = 0;197float max_bounds[3] = {};198float pad1 = 0.0;199bool operator<(const Triangle &p_triangle) const {200return slice < p_triangle.slice;201}202};203204struct ClusterAABB {205float min_bounds[3];206float pad0 = 0.0f;207float max_bounds[3];208float pad1 = 0.0f;209};210211Vector<MeshInstance> mesh_instances;212213Vector<Light> lights;214Vector<LightMetadata> light_metadata;215216struct TriangleSort {217uint32_t cell_index = 0;218uint32_t triangle_index = 0;219AABB triangle_aabb;220221bool operator<(const TriangleSort &p_triangle_sort) const {222return cell_index < p_triangle_sort.cell_index; //sorting by triangle index in this case makes no sense223}224};225226template <int T>227struct TriangleSortAxis {228bool operator()(const TriangleSort &p_a, const TriangleSort &p_b) const {229return p_a.triangle_aabb.get_center()[T] < p_b.triangle_aabb.get_center()[T];230}231};232233void _plot_triangle_into_triangle_index_list(int p_size, const Vector3i &p_ofs, const AABB &p_bounds, const Vector3 p_points[3], uint32_t p_triangle_index, LocalVector<TriangleSort> &triangles, uint32_t p_grid_size);234void _sort_triangle_clusters(uint32_t p_cluster_size, uint32_t p_cluster_index, uint32_t p_index_start, uint32_t p_count, LocalVector<TriangleSort> &p_triangle_sort, LocalVector<ClusterAABB> &p_cluster_aabb);235236struct RasterPushConstant {237float atlas_size[2] = {};238float uv_offset[2] = {};239float to_cell_size[3] = {};240uint32_t base_triangle = 0;241float to_cell_offset[3] = {};242float bias = 0.0;243int32_t grid_size[3] = {};244uint32_t pad2 = 0;245};246247struct RasterSeamsPushConstant {248uint32_t base_index = 0;249uint32_t slice = 0;250float uv_offset[2] = {};251uint32_t debug = 0;252float blend = 0.0;253uint32_t pad[2] = {};254};255256struct PushConstant {257uint32_t atlas_slice = 0;258uint32_t ray_count = 0;259uint32_t ray_from = 0;260uint32_t ray_to = 0;261uint32_t region_ofs[2] = {};262uint32_t probe_count = 0;263uint32_t denoiser_range = 0;264};265266Vector<Ref<Image>> lightmap_textures;267Vector<Ref<Image>> shadowmask_textures;268Vector<Color> probe_values;269270struct DilateParams {271uint32_t radius;272uint32_t pad[3];273};274275struct DenoiseParams {276float spatial_bandwidth;277float light_bandwidth;278float albedo_bandwidth;279float normal_bandwidth;280281int half_search_window;282float filter_strength;283float pad[2];284};285286BakeError _blit_meshes_into_atlas(int p_max_texture_size, int p_denoiser_range, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, float p_supersampling_factor, BakeStepFunc p_step_function, void *p_bake_userdata);287void _create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_size, uint32_t p_cluster_size, Vector<Probe> &probe_positions, GenerateProbes p_generate_probes, Vector<int> &slice_triangle_count, Vector<int> &slice_seam_count, RID &vertex_buffer, RID &triangle_buffer, RID &lights_buffer, RID &r_triangle_indices_buffer, RID &r_cluster_indices_buffer, RID &r_cluster_aabbs_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata);288void _raster_geometry(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, int grid_size, AABB bounds, float p_bias, Vector<int> slice_triangle_count, RID position_tex, RID unocclude_tex, RID normal_tex, RID raster_depth_buffer, RID rasterize_shader, RID raster_base_uniform);289290BakeError _dilate(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices);291BakeError _denoise(RenderingDevice *p_rd, Ref<RDShaderFile> &p_compute_shader, const RID &p_compute_base_uniform_set, PushConstant &p_push_constant, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, RID p_unocclude_tex, float p_denoiser_strength, int p_denoiser_range, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function, void *p_bake_userdata);292BakeError _pack_l1(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices);293294Error _store_pfm(RenderingDevice *p_rd, RID p_atlas_tex, int p_index, const Size2i &p_atlas_size, const String &p_name, bool p_shadowmask);295Ref<Image> _read_pfm(const String &p_name, bool p_shadowmask);296BakeError _denoise_oidn(RenderingDevice *p_rd, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, bool p_shadowmask, const String &p_exe);297298public:299virtual void add_mesh(const MeshData &p_mesh) override;300virtual void add_directional_light(const String &p_name, bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) override;301virtual void add_omni_light(const String &p_name, bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) override;302virtual void add_spot_light(const String &p_name, bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) override;303virtual void add_probe(const Vector3 &p_position) override;304virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_bake_shadowmask, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_bake_userdata = nullptr, float p_exposure_normalization = 1.0, float p_supersampling_factor = 1.0f) override;305306int get_bake_texture_count() const override;307Ref<Image> get_bake_texture(int p_index) const override;308int get_shadowmask_texture_count() const override;309Ref<Image> get_shadowmask_texture(int p_index) const override;310int get_bake_mesh_count() const override;311Variant get_bake_mesh_userdata(int p_index) const override;312Rect2 get_bake_mesh_uv_scale(int p_index) const override;313int get_bake_mesh_texture_slice(int p_index) const override;314int get_bake_probe_count() const override;315Vector3 get_bake_probe_point(int p_probe) const override;316Vector<Color> get_bake_probe_sh(int p_probe) const override;317318LightmapperRD();319};320321322