Skip to content

Commit c86e053

Browse files
committed
more refactoring
1 parent d1d6f8d commit c86e053

13 files changed

Lines changed: 267 additions & 149 deletions

File tree

editor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ use crate::{
183183
utils::doc::DocWindow,
184184
world::{graph::EditorSceneWrapper, menu::SceneNodeContextMenu, WorldViewer},
185185
};
186+
use fyrox::gui::texture::sampler;
186187
use fyrox_build_tools::{build::BuildWindow, CommandDescriptor};
187188
pub use message::Message;
188189
use plugins::inspector::InspectorPlugin;
@@ -721,6 +722,8 @@ impl Editor {
721722
}
722723

723724
pub fn new_with_settings(startup_data: Option<StartupData>, settings: Settings) -> Self {
725+
sampler::STANDARD.resource.data_ref().lod_bias = -1.0;
726+
724727
// Useful for debugging purposes when users don't bother to mention editor version
725728
// they're using.
726729
Log::info(format!("Editor version: {}", &*EDITOR_VERSION));

fyrox-build-tools/src/build.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,25 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21-
use fyrox_core::algebra::{Matrix3, Vector2};
22-
use fyrox_core::Uuid;
21+
use fyrox_core::{
22+
algebra::{Matrix3, Vector2},
23+
Uuid,
24+
};
2325
use fyrox_resource::untyped::ResourceKind;
24-
use fyrox_ui::button::Button;
25-
use fyrox_ui::image::Image;
26-
use fyrox_ui::scroll_viewer::ScrollViewer;
27-
use fyrox_ui::text::Text;
28-
use fyrox_ui::window::{Window, WindowAlignment};
2926
use fyrox_ui::{
3027
border::BorderBuilder,
31-
button::{ButtonBuilder, ButtonMessage},
28+
button::{Button, ButtonBuilder, ButtonMessage},
3229
core::{parking_lot::Mutex, pool::Handle, SafeLock},
3330
grid::{Column, GridBuilder, Row},
34-
image::ImageBuilder,
31+
image::{Image, ImageBuilder},
3532
message::UiMessage,
36-
scroll_viewer::{ScrollViewerBuilder, ScrollViewerMessage},
33+
scroll_viewer::{ScrollViewer, ScrollViewerBuilder, ScrollViewerMessage},
3734
stack_panel::StackPanelBuilder,
3835
style::{resource::StyleResourceExt, Style},
39-
text::{TextBuilder, TextMessage},
40-
texture::{
41-
TextureImportOptions, TextureMagnificationFilter, TextureMinificationFilter,
42-
TextureResource, TextureResourceExtension,
43-
},
36+
text::{Text, TextBuilder, TextMessage},
37+
texture::{TextureImportOptions, TextureResource, TextureResourceExtension},
4438
widget::{WidgetBuilder, WidgetMessage},
45-
window::{WindowBuilder, WindowMessage, WindowTitle},
39+
window::{Window, WindowAlignment, WindowBuilder, WindowMessage, WindowTitle},
4640
BuildContext, HorizontalAlignment, Orientation, Thickness, UserInterface,
4741
};
4842
use std::{
@@ -79,10 +73,7 @@ impl BuildWindow {
7973
Uuid::new_v4(),
8074
ResourceKind::Embedded,
8175
include_bytes!("resources/progress.png"),
82-
TextureImportOptions::default()
83-
.with_minification_filter(TextureMinificationFilter::LinearMipMapLinear)
84-
.with_magnification_filter(TextureMagnificationFilter::Linear)
85-
.with_lod_bias(-1.0),
76+
TextureImportOptions::default(),
8677
)
8778
.ok();
8879

fyrox-impl/src/engine/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ use crate::{
7979
gltf::material::GLTF_SHADER,
8080
model::{loader::ModelLoader, Model, ModelResource},
8181
texture::{
82-
self, loader::TextureLoader, CompressionOptions, Texture, TextureImportOptions,
83-
TextureKind, TextureMinificationFilter, TextureResource, TextureResourceExtension,
82+
self, loader::TextureLoader, Texture, TextureImportOptions, TextureKind,
83+
TextureResource, TextureResourceExtension,
8484
},
8585
},
8686
scene::{
@@ -210,9 +210,7 @@ impl InitializedGraphicsContext {
210210
Uuid::new_v4(),
211211
ResourceKind::Embedded,
212212
data,
213-
TextureImportOptions::default()
214-
.with_compression(CompressionOptions::NoCompression)
215-
.with_minification_filter(TextureMinificationFilter::Linear),
213+
TextureImportOptions::default().with_generate_mip_map(false),
216214
) {
217215
self.set_window_icon_from_texture(&texture);
218216
}

fyrox-impl/src/renderer/cache/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use std::{
3737
use uuid::Uuid;
3838

3939
pub mod geometry;
40+
pub mod sampler;
4041
pub mod shader;
4142
pub mod texture;
4243
pub mod uniform;
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
use crate::renderer::cache::TemporaryCache;
22+
use fyrox_core::err_once;
23+
use fyrox_graphics::{
24+
error::FrameworkError,
25+
sampler::{
26+
GpuSampler, GpuSamplerDescriptor, MagnificationFilter, MinificationFilter, WrapMode,
27+
},
28+
server::GraphicsServer,
29+
};
30+
use fyrox_texture::{
31+
sampler::{TextureSampler, TextureSamplerResource},
32+
TextureMagnificationFilter, TextureMinificationFilter, TextureWrapMode,
33+
};
34+
35+
pub fn convert_magnification_filter(v: TextureMagnificationFilter) -> MagnificationFilter {
36+
match v {
37+
TextureMagnificationFilter::Nearest => MagnificationFilter::Nearest,
38+
TextureMagnificationFilter::Linear => MagnificationFilter::Linear,
39+
}
40+
}
41+
42+
pub fn convert_minification_filter(v: TextureMinificationFilter) -> MinificationFilter {
43+
match v {
44+
TextureMinificationFilter::Nearest => MinificationFilter::Nearest,
45+
TextureMinificationFilter::NearestMipMapNearest => MinificationFilter::NearestMipMapNearest,
46+
TextureMinificationFilter::NearestMipMapLinear => MinificationFilter::NearestMipMapLinear,
47+
TextureMinificationFilter::Linear => MinificationFilter::Linear,
48+
TextureMinificationFilter::LinearMipMapNearest => MinificationFilter::LinearMipMapNearest,
49+
TextureMinificationFilter::LinearMipMapLinear => MinificationFilter::LinearMipMapLinear,
50+
}
51+
}
52+
53+
pub fn convert_wrap_mode(v: TextureWrapMode) -> WrapMode {
54+
match v {
55+
TextureWrapMode::Repeat => WrapMode::Repeat,
56+
TextureWrapMode::ClampToEdge => WrapMode::ClampToEdge,
57+
TextureWrapMode::ClampToBorder => WrapMode::ClampToBorder,
58+
TextureWrapMode::MirroredRepeat => WrapMode::MirroredRepeat,
59+
TextureWrapMode::MirrorClampToEdge => WrapMode::MirrorClampToEdge,
60+
}
61+
}
62+
63+
fn create_sampler(
64+
server: &dyn GraphicsServer,
65+
sampler: &TextureSampler,
66+
) -> Result<SamplerRenderData, FrameworkError> {
67+
Ok(SamplerRenderData {
68+
gpu_sampler: server.create_sampler(GpuSamplerDescriptor {
69+
mag_filter: convert_magnification_filter(sampler.magnification_filter()),
70+
min_filter: convert_minification_filter(sampler.minification_filter()),
71+
s_wrap_mode: convert_wrap_mode(sampler.s_wrap_mode()),
72+
t_wrap_mode: convert_wrap_mode(sampler.t_wrap_mode()),
73+
r_wrap_mode: convert_wrap_mode(sampler.r_wrap_mode()),
74+
anisotropy: sampler.anisotropy_level(),
75+
min_lod: sampler.min_lod(),
76+
max_lod: sampler.max_lod(),
77+
lod_bias: sampler.lod_bias(),
78+
})?,
79+
sampler_modifications_counter: sampler.modification_count,
80+
})
81+
}
82+
83+
#[derive(Clone)]
84+
pub struct SamplerRenderData {
85+
pub gpu_sampler: GpuSampler,
86+
sampler_modifications_counter: u64,
87+
}
88+
89+
#[derive(Default)]
90+
pub struct SamplerCache {
91+
cache: TemporaryCache<SamplerRenderData>,
92+
}
93+
94+
impl SamplerCache {
95+
/// Unconditionally uploads requested texture into GPU memory, previous GPU texture will be automatically
96+
/// destroyed.
97+
pub fn upload(
98+
&mut self,
99+
server: &dyn GraphicsServer,
100+
sampler_resource: &TextureSamplerResource,
101+
) -> Result<(), FrameworkError> {
102+
let sampler = sampler_resource.state();
103+
if let Some(sampler) = sampler.data_ref() {
104+
self.cache.get_entry_mut_or_insert_with(
105+
&sampler.cache_index,
106+
Default::default(),
107+
|| create_sampler(server, sampler),
108+
)?;
109+
Ok(())
110+
} else {
111+
Err(FrameworkError::Custom(
112+
"Sampler is not loaded yet!".to_string(),
113+
))
114+
}
115+
}
116+
117+
pub fn get(
118+
&mut self,
119+
server: &dyn GraphicsServer,
120+
sampler_resource: &TextureSamplerResource,
121+
) -> Option<&GpuSampler> {
122+
let sampler_data_guard = sampler_resource.state();
123+
if let Some(sampler) = sampler_data_guard.data_ref() {
124+
match self.cache.get_mut_or_insert_with(
125+
&sampler.cache_index,
126+
Default::default(),
127+
|| create_sampler(server, sampler),
128+
) {
129+
Ok(entry) => {
130+
// Check if some value has changed in resource.
131+
132+
if entry.sampler_modifications_counter != sampler.sampler_modifications_count()
133+
{
134+
entry.gpu_sampler = create_sampler(server, sampler).unwrap();
135+
}
136+
137+
return Some(entry);
138+
}
139+
Err(e) => {
140+
drop(sampler_data_guard);
141+
err_once!(
142+
sampler_resource.key() as usize,
143+
"Failed to create GPU sampler from sampler. Reason: {:?}",
144+
e,
145+
);
146+
}
147+
}
148+
}
149+
None
150+
}
151+
152+
pub fn update(&mut self, dt: f32) {
153+
self.cache.update(dt)
154+
}
155+
156+
pub fn clear(&mut self) {
157+
self.cache.clear();
158+
}
159+
160+
pub fn unload(&mut self, sampler: &TextureSamplerResource) {
161+
if let Some(sampler) = sampler.state().data() {
162+
self.cache.remove(&sampler.cache_index);
163+
}
164+
}
165+
166+
pub fn alive_count(&self) -> usize {
167+
self.cache.alive_count()
168+
}
169+
}

0 commit comments

Comments
 (0)