LWSClient::LWSClient() { _in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10; _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1); _out_buf_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_BUF) - 1) + 10; _out_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_PKT) - 1); context = NULL; _lws_ref = NULL; _peer = Ref<LWSPeer>(memnew(LWSPeer)); };
VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() { file = NULL; theora_p = 0; vorbis_p = 0; videobuf_ready = 0; playing = false; frames_pending = 0; videobuf_time = 0; paused = false; buffering = false; texture = Ref<ImageTexture>(memnew(ImageTexture)); mix_callback = NULL; mix_udata = NULL; audio_track = 0; delay_compensation = 0; audio_frames_wrote = 0; #ifdef THEORA_USE_THREAD_STREAMING int rb_power = nearest_shift(RB_SIZE_KB * 1024); ring_buffer.resize(rb_power); read_buffer.resize(RB_SIZE_KB * 1024); thread_sem = Semaphore::create(); thread = NULL; thread_exit = false; thread_eof = false; #endif };
Error PacketPeerUDPWinsock::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) { ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); sock_type = IP::TYPE_ANY; if (p_bind_address.is_valid()) sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; int sock = _get_socket(); if (sock == -1) return ERR_CANT_CREATE; struct sockaddr_storage addr = { 0 }; size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address()); if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) { close(); return ERR_UNAVAILABLE; } blocking = true; printf("UDP Connection listening on port %i\n", p_port); rb.resize(nearest_shift(p_recv_buffer_size)); return OK; }
void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { //warning may lose packets ERR_EXPLAIN("Buffer in use, resizing would cause loss of data"); ERR_FAIL_COND(ring_buffer.data_left()); ring_buffer.resize(nearest_shift(p_max_size + 4)); temp_buffer.resize(nearest_power_of_2(p_max_size + 4)); }
Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size){ close(); int sock = _get_socket(); if (sock == -1 ) return ERR_CANT_CREATE; sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_port = htons(p_port); addr.sin_addr.s_addr = INADDR_ANY; if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_in)) == -1 ) { close(); return ERR_UNAVAILABLE; } printf("UDP Connection listening on port %i bufsize %i \n", p_port,p_recv_buffer_size); rb.resize(nearest_shift(p_recv_buffer_size)); return OK; }
Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_address_type, int p_recv_buffer_size) { ERR_FAIL_COND_V(p_address_type != IP_Address::TYPE_IPV4 && p_address_type != IP_Address::TYPE_IPV6, ERR_INVALID_PARAMETER); close(); int sock = _get_socket(p_address_type); if (sock == -1 ) return ERR_CANT_CREATE; sockaddr_storage addr = {0}; size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_address_type, NULL); if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) { close(); return ERR_UNAVAILABLE; } rb.resize(nearest_shift(p_recv_buffer_size)); return OK; }
Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) { close(); int sock = _get_socket(); if (sock == -1 ) return ERR_CANT_CREATE; struct sockaddr_storage addr = {0}; size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL); if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) { close(); return ERR_UNAVAILABLE; } blocking=true; printf("UDP Connection listening on port %i\n", p_port); rb.resize(nearest_shift(p_recv_buffer_size)); return OK; }
Error AudioRBResampler::setup(int p_channels, int p_src_mix_rate, int p_target_mix_rate, int p_buffer_msec, int p_minbuff_needed) { ERR_FAIL_COND_V(p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6, ERR_INVALID_PARAMETER); int desired_rb_bits = nearest_shift(MAX((p_buffer_msec / 1000.0) * p_src_mix_rate, p_minbuff_needed)); bool recreate = !rb; if (rb && (uint32_t(desired_rb_bits) != rb_bits || channels != uint32_t(p_channels))) { memdelete_arr(rb); memdelete_arr(read_buf); recreate = true; } if (recreate) { channels = p_channels; rb_bits = desired_rb_bits; rb_len = (1 << rb_bits); rb_mask = rb_len - 1; rb = memnew_arr(float, rb_len *p_channels); read_buf = memnew_arr(float, rb_len *p_channels); } src_mix_rate = p_src_mix_rate; target_mix_rate = p_target_mix_rate; offset = 0; rb_read_pos = 0; rb_write_pos = 0; //avoid maybe strange noises upon load for (unsigned int i = 0; i < (rb_len * channels); i++) { rb[i] = 0; read_buf[i] = 0; } return OK; }
BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, bool p_create_visual_debug) { String save_path; if (image_path.begins_with("res://")) { save_path = image_path; } else { if (get_filename() != "") { save_path = get_filename().get_base_dir(); } else if (get_owner() && get_owner()->get_filename() != "") { save_path = get_owner()->get_filename().get_base_dir(); } if (save_path == "") { return BAKE_ERROR_NO_SAVE_PATH; } if (image_path != "") { save_path.plus_file(image_path); } } { //check for valid save path DirAccessRef d = DirAccess::open(save_path); if (!d) { ERR_PRINTS("Invalid Save Path: " + save_path); return BAKE_ERROR_NO_SAVE_PATH; } } Ref<BakedLightmapData> new_light_data; new_light_data.instance(); VoxelLightBaker baker; int bake_subdiv; int capture_subdiv; AABB bake_bounds; { bake_bounds = AABB(-extents, extents * 2.0); int subdiv = nearest_power_of_2_templated(int(bake_bounds.get_longest_axis_size() / bake_cell_size)); bake_bounds.size[bake_bounds.get_longest_axis_size()] = subdiv * bake_cell_size; bake_subdiv = nearest_shift(subdiv) + 1; capture_subdiv = bake_subdiv; float css = bake_cell_size; while (css < capture_cell_size && capture_subdiv > 2) { capture_subdiv--; css *= 2.0; } } baker.begin_bake(bake_subdiv, bake_bounds); List<PlotMesh> mesh_list; List<PlotLight> light_list; _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), mesh_list, light_list); if (bake_begin_function) { bake_begin_function(mesh_list.size() + light_list.size() + 1 + mesh_list.size() * 100); } int step = 0; int pmc = 0; for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) { if (bake_step_function) { bake_step_function(step++, RTR("Plotting Meshes: ") + " (" + itos(pmc + 1) + "/" + itos(mesh_list.size()) + ")"); } pmc++; baker.plot_mesh(E->get().local_xform, E->get().mesh, E->get().instance_materials, E->get().override_material); } pmc = 0; baker.begin_bake_light(VoxelLightBaker::BakeQuality(bake_quality), VoxelLightBaker::BakeMode(bake_mode), propagation, energy); for (List<PlotLight>::Element *E = light_list.front(); E; E = E->next()) { if (bake_step_function) { bake_step_function(step++, RTR("Plotting Lights:") + " (" + itos(pmc + 1) + "/" + itos(light_list.size()) + ")"); } pmc++; PlotLight pl = E->get(); switch (pl.light->get_light_type()) { case VS::LIGHT_DIRECTIONAL: { baker.plot_light_directional(-pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_bake_mode() == Light::BAKE_ALL); } break; case VS::LIGHT_OMNI: { baker.plot_light_omni(pl.local_xform.origin, pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL); } break; case VS::LIGHT_SPOT: { baker.plot_light_spot(pl.local_xform.origin, pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_param(Light::PARAM_SPOT_ANGLE), pl.light->get_param(Light::PARAM_SPOT_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL); } break; } } /*if (bake_step_function) { bake_step_function(pmc++, RTR("Finishing Plot")); }*/ baker.end_bake(); Set<String> used_mesh_names; pmc = 0; for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) { String mesh_name = E->get().mesh->get_name(); if (mesh_name == "" || mesh_name.find(":") != -1 || mesh_name.find("/") != -1) { mesh_name = "LightMap"; } if (used_mesh_names.has(mesh_name)) { int idx = 2; String base = mesh_name; while (true) { mesh_name = base + itos(idx); if (!used_mesh_names.has(mesh_name)) break; idx++; } } used_mesh_names.insert(mesh_name); pmc++; VoxelLightBaker::LightMapData lm; Error err; if (bake_step_function) { BakeTimeData btd; btd.text = RTR("Lighting Meshes: ") + mesh_name + " (" + itos(pmc) + "/" + itos(mesh_list.size()) + ")"; btd.pass = step; btd.last_step = 0; err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm, _bake_time, &btd); if (err != OK) { bake_end_function(); if (err == ERR_SKIP) return BAKE_ERROR_USER_ABORTED; return BAKE_ERROR_CANT_CREATE_IMAGE; } step += 100; } else { err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm); } if (err == OK) { Ref<Image> image; image.instance(); uint32_t tex_flags = Texture::FLAGS_DEFAULT; if (hdr) { //just save a regular image PoolVector<uint8_t> data; int s = lm.light.size(); data.resize(lm.light.size() * 2); { PoolVector<uint8_t>::Write w = data.write(); PoolVector<float>::Read r = lm.light.read(); uint16_t *hfw = (uint16_t *)w.ptr(); for (int i = 0; i < s; i++) { hfw[i] = Math::make_half_float(r[i]); } } image->create(lm.width, lm.height, false, Image::FORMAT_RGBH, data); } else { //just save a regular image PoolVector<uint8_t> data; int s = lm.light.size(); data.resize(lm.light.size()); { PoolVector<uint8_t>::Write w = data.write(); PoolVector<float>::Read r = lm.light.read(); for (int i = 0; i < s; i += 3) { Color c(r[i + 0], r[i + 1], r[i + 2]); c = c.to_srgb(); w[i + 0] = CLAMP(c.r * 255, 0, 255); w[i + 1] = CLAMP(c.g * 255, 0, 255); w[i + 2] = CLAMP(c.b * 255, 0, 255); } } image->create(lm.width, lm.height, false, Image::FORMAT_RGB8, data); //This texture is saved to SRGB for two reasons: // 1) first is so it looks better when doing the LINEAR->SRGB conversion (more accurate) // 2) So it can be used in the GLES2 backend, which does not support linkear workflow tex_flags |= Texture::FLAG_CONVERT_TO_LINEAR; } Ref<ImageTexture> tex; String image_path = save_path.plus_file(mesh_name + ".tex"); bool set_path = true; if (ResourceCache::has(image_path)) { tex = Ref<Resource>((Resource *)ResourceCache::get(image_path)); set_path = false; } if (!tex.is_valid()) { tex.instance(); } tex->create_from_image(image, tex_flags); err = ResourceSaver::save(image_path, tex, ResourceSaver::FLAG_CHANGE_PATH); if (err != OK) { if (bake_end_function) { bake_end_function(); } ERR_FAIL_COND_V(err != OK, BAKE_ERROR_CANT_CREATE_IMAGE); } if (set_path) { tex->set_path(image_path); } new_light_data->add_user(E->get().path, tex, E->get().instance_idx); } } AABB bounds = AABB(-extents, extents * 2); new_light_data->set_cell_subdiv(capture_subdiv); new_light_data->set_bounds(bounds); new_light_data->set_octree(baker.create_capture_octree(capture_subdiv)); { float bake_bound_size = bake_bounds.get_longest_axis_size(); Transform to_bounds; to_bounds.basis.scale(Vector3(bake_bound_size, bake_bound_size, bake_bound_size)); to_bounds.origin = bounds.position; Transform to_grid; to_grid.basis.scale(Vector3(1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1))); Transform to_cell_space = to_grid * to_bounds.affine_inverse(); new_light_data->set_cell_space_transform(to_cell_space); } if (bake_end_function) { bake_end_function(); } //create the data for visual server if (p_create_visual_debug) { MultiMeshInstance *mmi = memnew(MultiMeshInstance); mmi->set_multimesh(baker.create_debug_multimesh(VoxelLightBaker::DEBUG_LIGHT)); add_child(mmi); #ifdef TOOLS_ENABLED if (get_tree()->get_edited_scene_root() == this) { mmi->set_owner(this); } else { mmi->set_owner(get_owner()); } #else mmi->set_owner(get_owner()); #endif } set_light_data(new_light_data); return BAKE_ERROR_OK; }