Path: blob/master/modules/raycast/lightmap_raycaster_embree.cpp
10277 views
/**************************************************************************/1/* lightmap_raycaster_embree.cpp */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#ifdef TOOLS_ENABLED3132#include "lightmap_raycaster_embree.h"3334#ifdef __SSE2__35#include <pmmintrin.h>36#endif3738LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() {39return memnew(LightmapRaycasterEmbree);40}4142void LightmapRaycasterEmbree::make_default_raycaster() {43create_function = create_embree_raycaster;44}4546void LightmapRaycasterEmbree::filter_function(const struct RTCFilterFunctionNArguments *p_args) {47RTCHit *hit = (RTCHit *)p_args->hit;4849unsigned int geomID = hit->geomID;50float u = hit->u;51float v = hit->v;5253LightmapRaycasterEmbree *scene = (LightmapRaycasterEmbree *)p_args->geometryUserPtr;54RTCGeometry geom = rtcGetGeometry(scene->embree_scene, geomID);5556rtcInterpolate0(geom, hit->primID, hit->u, hit->v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &hit->u, 2);5758if (scene->alpha_textures.has(geomID)) {59const AlphaTextureData &alpha_texture = scene->alpha_textures[geomID];6061if (alpha_texture.sample(hit->u, hit->v) < 128) {62p_args->valid[0] = 0;63return;64}65}6667rtcInterpolate0(geom, hit->primID, u, v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &hit->Ng_x, 3);68}6970bool LightmapRaycasterEmbree::intersect(Ray &r_ray) {71RTCRayQueryContext context;72rtcInitRayQueryContext(&context);73RTCIntersectArguments args;74rtcInitIntersectArguments(&args);75args.context = &context;76rtcIntersect1(embree_scene, (RTCRayHit *)&r_ray, &args);77return r_ray.geomID != RTC_INVALID_GEOMETRY_ID;78}7980void LightmapRaycasterEmbree::intersect(Vector<Ray> &r_rays) {81Ray *rays = r_rays.ptrw();82for (int i = 0; i < r_rays.size(); ++i) {83intersect(rays[i]);84}85}8687void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) {88if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) {89AlphaTextureData tex;90tex.size = p_alpha_texture->get_size();91tex.data = p_alpha_texture->get_data();92alpha_textures.insert(p_id, tex);93}94}9596float blerp(float c00, float c10, float c01, float c11, float tx, float ty) {97return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty);98}99100uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const {101float x = u * size.x;102float y = v * size.y;103int xi = (int)x;104int yi = (int)y;105106uint8_t texels[4];107108for (int i = 0; i < 4; ++i) {109int sample_x = CLAMP(xi + i % 2, 0, size.x - 1);110int sample_y = CLAMP(yi + i / 2, 0, size.y - 1);111texels[i] = data[sample_y * size.x + sample_x];112}113114return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi));115}116117void LightmapRaycasterEmbree::add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) {118RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE);119120rtcSetGeometryVertexAttributeCount(embree_mesh, 2);121122int vertex_count = p_vertices.size();123124ERR_FAIL_COND(vertex_count % 3 != 0);125ERR_FAIL_COND(vertex_count != p_uv2s.size());126ERR_FAIL_COND(!p_normals.is_empty() && vertex_count != p_normals.size());127128Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);129memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count);130131Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count);132memcpy(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count);133134uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);135for (int i = 0; i < vertex_count; i++) {136embree_triangles[i] = i;137}138139if (!p_normals.is_empty()) {140Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);141memcpy(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count);142}143144rtcCommitGeometry(embree_mesh);145rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function);146rtcSetGeometryUserData(embree_mesh, this);147rtcAttachGeometryByID(embree_scene, embree_mesh, p_id);148rtcReleaseGeometry(embree_mesh);149}150151void LightmapRaycasterEmbree::commit() {152rtcCommitScene(embree_scene);153}154155void LightmapRaycasterEmbree::set_mesh_filter(const HashSet<int> &p_mesh_ids) {156for (const int &E : p_mesh_ids) {157rtcDisableGeometry(rtcGetGeometry(embree_scene, E));158}159rtcCommitScene(embree_scene);160filter_meshes = p_mesh_ids;161}162163void LightmapRaycasterEmbree::clear_mesh_filter() {164for (const int &E : filter_meshes) {165rtcEnableGeometry(rtcGetGeometry(embree_scene, E));166}167rtcCommitScene(embree_scene);168filter_meshes.clear();169}170171void embree_lm_error_handler(void *p_user_data, RTCError p_code, const char *p_str) {172print_error("Embree error: " + String(p_str));173}174175LightmapRaycasterEmbree::LightmapRaycasterEmbree() {176#ifdef __SSE2__177_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);178_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);179#endif180181embree_device = rtcNewDevice(nullptr);182rtcSetDeviceErrorFunction(embree_device, &embree_lm_error_handler, nullptr);183embree_scene = rtcNewScene(embree_device);184}185186LightmapRaycasterEmbree::~LightmapRaycasterEmbree() {187if (embree_scene != nullptr) {188rtcReleaseScene(embree_scene);189}190191if (embree_device != nullptr) {192rtcReleaseDevice(embree_device);193}194}195196#endif // TOOLS_ENABLED197198199