CPU lightmapper environment energy fixes.

* Better handling of the scene's environment energy in the lightmapper
  bakes.
* Fixed a bug where ProceduralSky::get_panorama() returned a reference
  instead of a copy.
* Removed includes to Embree's internal header files.
This commit is contained in:
JFonS 2021-04-22 15:01:25 +02:00
parent 8e68f2e5f4
commit 2db2d1153d
5 changed files with 32 additions and 27 deletions

View file

@ -30,12 +30,7 @@
#include "lightmap_raycaster.h"
// From Embree.
#include <math/vec2.h>
#include <math/vec3.h>
#include <xmmintrin.h>
using namespace embree;
#include <pmmintrin.h>
LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() {
return memnew(LightmapRaycasterEmbree);
@ -135,23 +130,22 @@ void LightmapRaycasterEmbree::add_mesh(const Vector<Vector3> &p_vertices, const
ERR_FAIL_COND(vertex_count % 3 != 0);
ERR_FAIL_COND(vertex_count != p_uv2s.size());
ERR_FAIL_COND(!p_normals.empty() && vertex_count != p_normals.size());
Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
copymem(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count);
Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count);
copymem(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count);
Vec3fa *embree_vertices = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count);
Vec2fa *embree_light_uvs = (Vec2fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vec2fa), vertex_count);
uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);
Vec3fa *embree_normals = nullptr;
if (!p_normals.empty()) {
embree_normals = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count);
for (int i = 0; i < vertex_count; i++) {
embree_triangles[i] = i;
}
for (int i = 0; i < vertex_count; i++) {
embree_vertices[i] = Vec3fa(p_vertices[i].x, p_vertices[i].y, p_vertices[i].z);
embree_light_uvs[i] = Vec2fa(p_uv2s[i].x, p_uv2s[i].y);
if (embree_normals != nullptr) {
embree_normals[i] = Vec3fa(p_normals[i].x, p_normals[i].y, p_normals[i].z);
}
embree_triangles[i] = i;
if (!p_normals.empty()) {
Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
copymem(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count);
}
rtcCommitGeometry(embree_mesh);

View file

@ -810,7 +810,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa
} break;
case ENVIRONMENT_MODE_CUSTOM_SKY: {
if (environment_custom_sky.is_valid()) {
environment_image = _get_irradiance_from_sky(environment_custom_sky, Vector2i(128, 64));
environment_image = _get_irradiance_from_sky(environment_custom_sky, environment_custom_energy, Vector2i(128, 64));
environment_xform.set_euler(environment_custom_sky_rotation_degrees * Math_PI / 180.0);
}
@ -1233,7 +1233,7 @@ void BakedLightmap::_clear_lightmaps() {
}
}
Ref<Image> BakedLightmap::_get_irradiance_from_sky(Ref<Sky> p_sky, Vector2i p_size) {
Ref<Image> BakedLightmap::_get_irradiance_from_sky(Ref<Sky> p_sky, float p_energy, Vector2i p_size) {
if (p_sky.is_null()) {
return Ref<Image>();
}
@ -1245,7 +1245,7 @@ Ref<Image> BakedLightmap::_get_irradiance_from_sky(Ref<Sky> p_sky, Vector2i p_si
}
Ref<ProceduralSky> procedural = p_sky;
if (procedural.is_valid()) {
sky_image = procedural->get_panorama();
sky_image = procedural->get_data();
}
if (sky_image.is_null()) {
@ -1254,6 +1254,17 @@ Ref<Image> BakedLightmap::_get_irradiance_from_sky(Ref<Sky> p_sky, Vector2i p_si
sky_image->convert(Image::FORMAT_RGBF);
sky_image->resize(p_size.x, p_size.y, Image::INTERPOLATE_CUBIC);
if (p_energy != 1.0) {
sky_image->lock();
for (int i = 0; i < p_size.y; i++) {
for (int j = 0; j < p_size.x; j++) {
sky_image->set_pixel(j, i, sky_image->get_pixel(j, i) * p_energy);
}
}
sky_image->unlock();
}
return sky_image;
}
@ -1261,7 +1272,7 @@ Ref<Image> BakedLightmap::_get_irradiance_map(Ref<Environment> p_env, Vector2i p
Environment::BGMode bg_mode = p_env->get_background();
switch (bg_mode) {
case Environment::BG_SKY: {
return _get_irradiance_from_sky(p_env->get_sky(), Vector2i(128, 64));
return _get_irradiance_from_sky(p_env->get_sky(), p_env->get_bg_energy(), Vector2i(128, 64));
}
case Environment::BG_CLEAR_COLOR:
case Environment::BG_COLOR: {

View file

@ -187,7 +187,7 @@ private:
void _clear_lightmaps();
void _get_material_images(const MeshesFound &p_found_mesh, Lightmapper::MeshData &r_mesh_data, Vector<Ref<Texture> > &r_albedo_textures, Vector<Ref<Texture> > &r_emission_textures);
Ref<Image> _get_irradiance_from_sky(Ref<Sky> p_sky, Vector2i p_size);
Ref<Image> _get_irradiance_from_sky(Ref<Sky> p_sky, float p_energy, Vector2i p_size);
Ref<Image> _get_irradiance_map(Ref<Environment> p_env, Vector2i p_size);
void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights);
Vector2i _compute_lightmap_size(const MeshesFound &p_mesh);

View file

@ -390,8 +390,8 @@ ProceduralSky::TextureSize ProceduralSky::get_texture_size() const {
return texture_size;
}
Ref<Image> ProceduralSky::get_panorama() const {
return panorama;
Ref<Image> ProceduralSky::get_data() const {
return panorama->duplicate();
}
RID ProceduralSky::get_rid() const {

View file

@ -190,7 +190,7 @@ public:
void set_texture_size(TextureSize p_size);
TextureSize get_texture_size() const;
Ref<Image> get_panorama() const;
Ref<Image> get_data() const;
virtual RID get_rid() const;