Skip to content

Commit

Permalink
store initial data and file extension (if any) of built-in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Sep 17, 2024
1 parent 79e1970 commit 3e30bab
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 193 deletions.
4 changes: 2 additions & 2 deletions editor/src/inspector/editors/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn make_name(font: &FontResource) -> String {
match font.kind() {
ResourceKind::Embedded => "Embedded - AaBbCcDd1234567890".to_string(),
ResourceKind::External(path) => {
if font == &BUILT_IN_FONT.clone() {
if font == &BUILT_IN_FONT.resource.clone() {
"BuiltIn - AaBbCcDd1234567890".to_string()
} else {
format!("{} - AaBbCcDd1234567890", path.display())
Expand All @@ -162,7 +162,7 @@ impl FontFieldBuilder {
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
font: BUILT_IN_FONT.clone(),
font: BUILT_IN_FONT.resource(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion editor/src/menu/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub struct CreateEntityMenu {

fn placeholder_material() -> MaterialResource {
let mut material = Material::standard();
let _ = material.set_property("diffuseTexture", PLACEHOLDER.clone());
let _ = material.set_property("diffuseTexture", PLACEHOLDER.resource());
MaterialResource::new_ok(ResourceKind::Embedded, material)
}

Expand Down
55 changes: 19 additions & 36 deletions fyrox-impl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,54 +1189,37 @@ pub(crate) fn initialize_resource_manager_loaders(
}

for shader in ShaderResource::standard_shaders() {
state
.built_in_resources
.insert(shader.kind().path_owned().unwrap(), shader.into_untyped());
state.built_in_resources.add((*shader).clone());
}

for texture in SkyBoxKind::built_in_skybox_textures() {
state.built_in_resources.insert(
texture.kind().path_owned().unwrap(),
texture.clone().into_untyped(),
);
state.built_in_resources.add(texture.clone());
}

state.built_in_resources.insert(
BUILT_IN_FONT.kind().path_owned().unwrap(),
BUILT_IN_FONT.clone().into_untyped(),
);
state.built_in_resources.add(BUILT_IN_FONT.clone());

state.built_in_resources.insert(
texture::PLACEHOLDER.kind().path_owned().unwrap(),
texture::PLACEHOLDER.clone().into_untyped(),
);
state.built_in_resources.add(texture::PLACEHOLDER.clone());

for material in [
material::STANDARD.clone(),
material::STANDARD_2D.clone(),
material::STANDARD_SPRITE.clone(),
material::STANDARD_TERRAIN.clone(),
material::STANDARD_TWOSIDES.clone(),
material::STANDARD_PARTICLE_SYSTEM.clone(),
&*material::STANDARD,
&*material::STANDARD_2D,
&*material::STANDARD_SPRITE,
&*material::STANDARD_TERRAIN,
&*material::STANDARD_TWOSIDES,
&*material::STANDARD_PARTICLE_SYSTEM,
] {
state.built_in_resources.insert(
material.kind().path_owned().unwrap(),
material.clone().into_untyped(),
);
state.built_in_resources.add(material.clone());
}

for material in [
surface::CUBE.clone(),
surface::QUAD.clone(),
surface::CYLINDER.clone(),
surface::SPHERE.clone(),
surface::CONE.clone(),
surface::TORUS.clone(),
for surface in [
&*surface::CUBE,
&*surface::QUAD,
&*surface::CYLINDER,
&*surface::SPHERE,
&*surface::CONE,
&*surface::TORUS,
] {
state.built_in_resources.insert(
material.kind().path_owned().unwrap(),
material.clone().into_untyped(),
);
state.built_in_resources.add(surface.clone());
}

state.constructors_container.add::<Texture>();
Expand Down
59 changes: 31 additions & 28 deletions fyrox-impl/src/material/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::{
resource::texture::{Texture, TextureResource},
};
use fxhash::FxHashMap;
use fyrox_resource::manager::BuiltInResource;
use fyrox_resource::state::ResourceState;
use fyrox_resource::untyped::ResourceKind;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -582,54 +583,56 @@ impl Display for MaterialError {
lazy_static! {
/// Standard PBR material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD: MaterialResource = MaterialResource::new_ok(
"__StandardMaterial".into(),
Material::from_shader(ShaderResource::standard(), None),
pub static ref STANDARD: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__StandardMaterial".into(),
Material::from_shader(ShaderResource::standard(), None),
)
);
}

lazy_static! {
/// Standard 2D material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD_2D: MaterialResource = MaterialResource::new_ok(
"__Standard2DMaterial".into(),
Material::from_shader(ShaderResource::standard_2d(), None),
pub static ref STANDARD_2D: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__Standard2DMaterial".into(),
Material::from_shader(ShaderResource::standard_2d(), None),
)
);
}

lazy_static! {
/// Standard particle system material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD_PARTICLE_SYSTEM: MaterialResource = MaterialResource::new_ok(
"__StandardParticleSystemMaterial".into(),
Material::from_shader(ShaderResource::standard_particle_system(), None),
pub static ref STANDARD_PARTICLE_SYSTEM: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__StandardParticleSystemMaterial".into(),
Material::from_shader(ShaderResource::standard_particle_system(), None),
)
);
}

lazy_static! {
/// Standard sprite material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD_SPRITE: MaterialResource = MaterialResource::new_ok(
"__StandardSpriteMaterial".into(),
Material::from_shader(ShaderResource::standard_sprite(), None),
pub static ref STANDARD_SPRITE: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__StandardSpriteMaterial".into(),
Material::from_shader(ShaderResource::standard_sprite(), None),
)
);
}

lazy_static! {
/// Standard terrain material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD_TERRAIN: MaterialResource = MaterialResource::new_ok(
"__StandardTerrainMaterial".into(),
Material::from_shader(ShaderResource::standard_terrain(), None),
pub static ref STANDARD_TERRAIN: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__StandardTerrainMaterial".into(),
Material::from_shader(ShaderResource::standard_terrain(), None),
)
);
}

lazy_static! {
/// Standard two-sided material. Keep in mind that this material is global, any modification
/// of it will reflect on every other usage of it.
pub static ref STANDARD_TWOSIDES: MaterialResource = MaterialResource::new_ok(
"__StandardTwoSidesMaterial".into(),
Material::from_shader(ShaderResource::standard_twosides(), None),
pub static ref STANDARD_TWOSIDES: BuiltInResource<Material> = BuiltInResource::new_no_source(
MaterialResource::new_ok(
"__StandardTwoSidesMaterial".into(),
Material::from_shader(ShaderResource::standard_twosides(), None),
)
);
}

Expand Down
122 changes: 72 additions & 50 deletions fyrox-impl/src/material/shader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ use crate::{
lazy_static::lazy_static,
renderer::framework::framebuffer::DrawParameters,
};
use fyrox_resource::embedded_data_source;
use fyrox_resource::manager::BuiltInResource;
use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand Down Expand Up @@ -543,6 +545,17 @@ impl Shader {
cache_index: Default::default(),
})
}

/// Creates a shader from string represented as raw bytes. This function will fail if the `bytes`
/// does not contain Utf8-encoded string.
pub fn from_string_bytes(bytes: &[u8]) -> Result<Self, ShaderError> {
Ok(Self {
definition: ShaderDefinition::from_str(
std::str::from_utf8(bytes).map_err(|_| ShaderError::NotUtf8Source)?,
)?,
cache_index: Default::default(),
})
}
}

impl ResourceData for Shader {
Expand Down Expand Up @@ -579,6 +592,9 @@ pub enum ShaderError {

/// A parsing error has occurred.
ParseError(ron::error::SpannedError),

/// Bytes does not represent Utf8-encoded string.
NotUtf8Source,
}

impl Display for ShaderError {
Expand All @@ -590,6 +606,9 @@ impl Display for ShaderError {
ShaderError::ParseError(v) => {
write!(f, "A parsing error has occurred {v:?}")
}
ShaderError::NotUtf8Source => {
write!(f, "Bytes does not represent Utf8-encoded string.")
}
}
}
}
Expand Down Expand Up @@ -634,7 +653,7 @@ pub trait ShaderResourceExtension: Sized {
fn standard_twosides() -> Self;

/// Returns a list of standard shader.
fn standard_shaders() -> Vec<ShaderResource>;
fn standard_shaders() -> [&'static BuiltInResource<Shader>; 6];
}

impl ShaderResourceExtension for ShaderResource {
Expand All @@ -643,80 +662,83 @@ impl ShaderResourceExtension for ShaderResource {
}

fn standard() -> Self {
STANDARD.clone()
STANDARD.resource()
}

fn standard_2d() -> Self {
STANDARD_2D.clone()
STANDARD_2D.resource()
}

fn standard_particle_system() -> Self {
STANDARD_PARTICLE_SYSTEM.clone()
STANDARD_PARTICLE_SYSTEM.resource()
}

fn standard_sprite() -> Self {
STANDARD_SPRITE.clone()
STANDARD_SPRITE.resource()
}

fn standard_terrain() -> Self {
STANDARD_TERRAIN.clone()
STANDARD_TERRAIN.resource()
}

fn standard_twosides() -> Self {
STANDARD_TWOSIDES.clone()
STANDARD_TWOSIDES.resource()
}

fn standard_shaders() -> Vec<ShaderResource> {
vec![
Self::standard(),
Self::standard_2d(),
Self::standard_particle_system(),
Self::standard_sprite(),
Self::standard_terrain(),
Self::standard_twosides(),
fn standard_shaders() -> [&'static BuiltInResource<Shader>; 6] {
[
&STANDARD,
&STANDARD_2D,
&STANDARD_PARTICLE_SYSTEM,
&STANDARD_SPRITE,
&STANDARD_TERRAIN,
&STANDARD_TWOSIDES,
]
}
}

lazy_static! {
static ref STANDARD: ShaderResource = ShaderResource::new_ok(
STANDARD_SHADER_NAME.into(),
Shader::from_string(STANDARD_SHADER_SRC).unwrap(),
);
}

lazy_static! {
static ref STANDARD_2D: ShaderResource = ShaderResource::new_ok(
STANDARD_2D_SHADER_NAME.into(),
Shader::from_string(STANDARD_2D_SHADER_SRC).unwrap(),
);
}

lazy_static! {
static ref STANDARD_PARTICLE_SYSTEM: ShaderResource = ShaderResource::new_ok(
STANDARD_PARTICLE_SYSTEM_SHADER_NAME.into(),
Shader::from_string(STANDARD_PARTICLE_SYSTEM_SHADER_SRC).unwrap(),
static ref STANDARD: BuiltInResource<Shader> =
BuiltInResource::new(embedded_data_source!("standard/standard.shader"), |data| {
ShaderResource::new_ok(
STANDARD_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
});
static ref STANDARD_2D: BuiltInResource<Shader> = BuiltInResource::new(
embedded_data_source!("standard/standard2d.shader"),
|data| ShaderResource::new_ok(
STANDARD_2D_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
);
}

lazy_static! {
static ref STANDARD_SPRITE: ShaderResource = ShaderResource::new_ok(
STANDARD_SPRITE_SHADER_NAME.into(),
Shader::from_string(STANDARD_SPRITE_SHADER_SRC).unwrap(),
static ref STANDARD_PARTICLE_SYSTEM: BuiltInResource<Shader> = BuiltInResource::new(
embedded_data_source!("standard/standard_particle_system.shader"),
|data| ShaderResource::new_ok(
STANDARD_PARTICLE_SYSTEM_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
);
}

lazy_static! {
static ref STANDARD_TERRAIN: ShaderResource = ShaderResource::new_ok(
STANDARD_TERRAIN_SHADER_NAME.into(),
Shader::from_string(STANDARD_TERRAIN_SHADER_SRC).unwrap(),
static ref STANDARD_SPRITE: BuiltInResource<Shader> = BuiltInResource::new(
embedded_data_source!("standard/standard_sprite.shader"),
|data| ShaderResource::new_ok(
STANDARD_SPRITE_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
);
}

lazy_static! {
static ref STANDARD_TWOSIDES: ShaderResource = ShaderResource::new_ok(
STANDARD_TWOSIDES_SHADER_NAME.into(),
Shader::from_string(STANDARD_TWOSIDES_SHADER_SRC).unwrap(),
static ref STANDARD_TERRAIN: BuiltInResource<Shader> =
BuiltInResource::new(embedded_data_source!("standard/terrain.shader"), |data| {
ShaderResource::new_ok(
STANDARD_TERRAIN_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
});
static ref STANDARD_TWOSIDES: BuiltInResource<Shader> = BuiltInResource::new(
embedded_data_source!("standard/standard-two-sides.shader"),
|data| ShaderResource::new_ok(
STANDARD_TWOSIDES_SHADER_NAME.into(),
Shader::from_string_bytes(data).unwrap(),
)
);
}

Expand Down
2 changes: 1 addition & 1 deletion fyrox-impl/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ impl Renderer {
let mut shader_cache = ShaderCache::default();

for shader in ShaderResource::standard_shaders() {
shader_cache.get(&state, &shader);
shader_cache.get(&state, &shader.resource);
}

Ok(Self {
Expand Down
Loading

0 comments on commit 3e30bab

Please sign in to comment.