Skip to content

Commit

Permalink
Add fork of glsl-to-spirv. Fixed asset loader for android
Browse files Browse the repository at this point in the history
  • Loading branch information
enfipy committed Oct 13, 2020
1 parent ff2cbb5 commit 2162426
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 81 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["Request", "Window", "Response"]}
wasm-bindgen-futures = "0.4"
js-sys = "0.3"

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = { version = "0.2" }
11 changes: 10 additions & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ impl AssetServer {

// TODO: add type checking here. people shouldn't be able to request a Handle<Texture> for a Mesh asset
pub fn load<T, P: AsRef<Path>>(&self, path: P) -> Result<Handle<T>, AssetServerError> {
self.load_untyped(self.get_root_path()?.join(path))
// TODO: Make it work in other loader functions.
#[cfg(not(target_os = "android"))]
let path = self.get_root_path()?.join(path);
#[cfg(target_os = "android")]
let path = path.as_ref().to_path_buf();
self.load_untyped(path)
.map(Handle::from)
}

Expand All @@ -205,7 +210,11 @@ impl AssetServer {
where
T: 'static,
{
// TODO: Make it work in other loader functions.
#[cfg(not(target_os = "android"))]
let path = self.get_root_path()?.join(path);
#[cfg(target_os = "android")]
let path = path.as_ref().to_path_buf();
if let Some(ref extension) = path.extension() {
if let Some(index) = self.extension_to_loader_index.get(
extension
Expand Down
14 changes: 1 addition & 13 deletions crates/bevy_asset/src/load_request/platform_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{AssetLoadError, AssetLoader, AssetResult, Handle};
use anyhow::Result;
use async_trait::async_trait;
use crossbeam_channel::Sender;
use std::{fs::File, io::Read};

/// Handles load requests from an AssetServer
Expand All @@ -22,18 +21,7 @@ where
}

fn load_asset(&self, load_request: &LoadRequest) -> Result<TAsset, AssetLoadError> {
match File::open(&load_request.path) {
Ok(mut file) => {
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let asset = self.loader.from_bytes(&load_request.path, bytes)?;
Ok(asset)
}
Err(e) => Err(AssetLoadError::Io(std::io::Error::new(
e.kind(),
format!("{}", load_request.path.display()),
))),
}
self.loader.load_from_file(&load_request.path)
}
}

Expand Down
35 changes: 23 additions & 12 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ use crate::{AssetServer, AssetVersion, Assets, Handle, LoadState};
use anyhow::Result;
use bevy_ecs::{Res, ResMut, Resource};
use crossbeam_channel::{Receiver, Sender, TryRecvError};
use fs::File;
use io::Read;
use std::{
fs, io,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Errors that occur while loading assets
#[derive(Error, Debug)]
pub enum AssetLoadError {
#[error("Encountered an io error while loading asset.")]
Io(#[from] io::Error),
Io(#[from] std::io::Error),
#[error("This asset's loader encountered an error while loading.")]
LoaderError(#[from] anyhow::Error),
}
Expand All @@ -24,11 +19,27 @@ pub trait AssetLoader<T>: Send + Sync + 'static {
fn from_bytes(&self, asset_path: &Path, bytes: Vec<u8>) -> Result<T, anyhow::Error>;
fn extensions(&self) -> &[&str];
fn load_from_file(&self, asset_path: &Path) -> Result<T, AssetLoadError> {
let mut file = File::open(asset_path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let asset = self.from_bytes(asset_path, bytes)?;
Ok(asset)
#[cfg(not(target_os = "android"))]
{
use std::io::Read;
use std::fs::File;
let mut file = File::open(asset_path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let asset = self.from_bytes(asset_path, bytes)?;
Ok(asset)
}
#[cfg(target_os = "android")]
{
use std::ffi::CString;
let asset_manager = ndk_glue::native_activity().asset_manager();
let mut opened_asset = asset_manager
.open(&CString::new(asset_path.to_str().unwrap()).unwrap())
.ok_or(AssetLoadError::LoaderError(anyhow::Error::msg("Failed to open asset")))?;
let bytes = opened_asset.get_buffer()?;
let asset = self.from_bytes(asset_path, bytes.to_vec())?;
Ok(asset)
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDe
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
#[cfg(target_os = "android")]
vertex: shaders.add(Shader::from_spirv(
ShaderStage::Vertex,
include_bytes!("forward.vert.spv"),
)),
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("forward.vert"),
)),
#[cfg(target_os = "android")]
fragment: Some(shaders.add(Shader::from_spirv(
ShaderStage::Fragment,
include_bytes!("forward.frag.spv"),
))),
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("forward.frag"),
))),
// #[cfg(target_os = "android")]
// vertex: shaders.add(Shader::from_spirv(
// ShaderStage::Vertex,
// include_bytes!("forward.vert.spv"),
// )),
// #[cfg(target_os = "android")]
// fragment: Some(shaders.add(Shader::from_spirv(
// ShaderStage::Fragment,
// include_bytes!("forward.frag.spv"),
// ))),
})
}
}
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ hexasphere = "1.0.0"
parking_lot = "0.11.0"

[target.'cfg(not(target_os = "ios"))'.dependencies]
bevy-glsl-to-spirv = "0.1.7"
bevy-glsl-to-spirv = { git = "https://github.com/PrototypeNM1/glsl-to-spirv.git", branch = "static_lib" }

[target.'cfg(target_os = "ios")'.dependencies]
shaderc = "0.6.2"
Expand Down
7 changes: 1 addition & 6 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ fn glsl_to_spirv(
stage: ShaderStage,
shader_defs: Option<&[String]>,
) -> Vec<u32> {
use std::io::Read;

let mut output = bevy_glsl_to_spirv::compile(glsl_source, stage.into(), shader_defs).unwrap();
let mut spv_bytes = Vec::new();
output.read_to_end(&mut spv_bytes).unwrap();
bytes_to_words(&spv_bytes)
bevy_glsl_to_spirv::compile(glsl_source, stage.into(), shader_defs).unwrap()
}

#[cfg(target_os = "ios")]
Expand Down
48 changes: 24 additions & 24 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ pub fn build_sprite_sheet_pipeline(shaders: &mut Assets<Shader>) -> PipelineDesc
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("sprite_sheet.vert"),
)),
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("sprite_sheet.frag"),
))),
#[cfg(target_os = "android")]
vertex: shaders.add(Shader::from_spirv(
ShaderStage::Vertex,
include_bytes!("sprite_sheet.vert.spv"),
)),
#[cfg(target_os = "android")]
fragment: Some(shaders.add(Shader::from_spirv(
ShaderStage::Fragment,
include_bytes!("sprite_sheet.frag.spv"),
))),
// #[cfg(target_os = "android")]
// vertex: shaders.add(Shader::from_spirv(
// ShaderStage::Vertex,
// include_bytes!("sprite_sheet.vert.spv"),
// )),
// #[cfg(target_os = "android")]
// fragment: Some(shaders.add(Shader::from_spirv(
// ShaderStage::Fragment,
// include_bytes!("sprite_sheet.frag.spv"),
// ))),
})
}
}
Expand Down Expand Up @@ -114,26 +114,26 @@ pub fn build_sprite_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("sprite.vert"),
)),
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("sprite.frag"),
))),
#[cfg(target_os = "android")]
vertex: shaders.add(Shader::from_spirv(
ShaderStage::Vertex,
include_bytes!("sprite.vert.spv"),
)),
#[cfg(target_os = "android")]
fragment: Some(shaders.add(Shader::from_spirv(
ShaderStage::Fragment,
include_bytes!("sprite.frag.spv"),
))),
// #[cfg(target_os = "android")]
// vertex: shaders.add(Shader::from_spirv(
// ShaderStage::Vertex,
// include_bytes!("sprite.vert.spv"),
// )),
// #[cfg(target_os = "android")]
// fragment: Some(shaders.add(Shader::from_spirv(
// ShaderStage::Fragment,
// include_bytes!("sprite.frag.spv"),
// ))),
})
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ pub fn build_ui_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("ui.vert"),
)),
#[cfg(not(target_os = "android"))]
// #[cfg(not(target_os = "android"))]
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("ui.frag"),
))),
#[cfg(target_os = "android")]
vertex: shaders.add(Shader::from_spirv(
ShaderStage::Vertex,
include_bytes!("ui.vert.spv"),
)),
#[cfg(target_os = "android")]
fragment: Some(shaders.add(Shader::from_spirv(
ShaderStage::Fragment,
include_bytes!("ui.frag.spv"),
))),
// #[cfg(target_os = "android")]
// vertex: shaders.add(Shader::from_spirv(
// ShaderStage::Vertex,
// include_bytes!("ui.vert.spv"),
// )),
// #[cfg(target_os = "android")]
// fragment: Some(shaders.add(Shader::from_spirv(
// ShaderStage::Fragment,
// include_bytes!("ui.frag.spv"),
// ))),
})
}
}
Expand Down

0 comments on commit 2162426

Please sign in to comment.