Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile in shaders to webrender instead of requiring a res directory #563

Merged
merged 3 commits into from Nov 17, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Some generated files are not rendered by default. Learn more.

@@ -12,7 +12,7 @@ use euclid::Size2D;
use gleam::gl;
use std::io::Read;
use std::fs::File;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::env;
use webrender_traits::{RenderApi, PipelineId};
use glutin::{Event, ElementState, VirtualKeyCode as Key};
@@ -44,8 +44,9 @@ impl webrender_traits::RenderNotifier for Notifier {
}
}

fn read_file(dir: &str, frame: i32, api: &RenderApi) -> bool {
let filename = format!("{}/frame_{}.bin", dir, frame);
fn read_file(dir: &Path, frame: i32, api: &RenderApi) -> bool {
let mut filename = PathBuf::from(dir);
filename.push(format!("frame_{}.bin", frame));
let mut file = match File::open(&filename) {
Ok(file) => file,
Err(_e) => {
@@ -72,12 +73,17 @@ fn read_file(dir: &str, frame: i32, api: &RenderApi) -> bool {

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
println!("{} <resources_path> <directory>", args[0]);
if args.len() != 2 && args.len() != 3 {
println!("{} [<resources_path>] <directory>", args[0]);
return;
}
let resource_path = &args[1];
let ref dir = args[2];

let (resource_path, dir) = if args.len() == 2 {
(Some(PathBuf::from(&args[1])), PathBuf::from(&args[2]))
} else {
(None, PathBuf::from(&args[1]))
};

let window = glutin::WindowBuilder::new()
.with_title("WebRender Replay")
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3,2)))
@@ -91,7 +97,7 @@ fn main() {

let opts = webrender::RendererOptions {
device_pixel_ratio: window.hidpi_factor(),
resource_path: PathBuf::from(resource_path),
resource_override_path: resource_path,
enable_aa: false,
enable_msaa: false,
enable_profiler: false,
@@ -111,7 +117,7 @@ fn main() {

//read and send the resources file
let mut frame_num = 0;
read_file(dir, frame_num, &api);
read_file(&dir, frame_num, &api);

for event in window.wait_events() {
match event {
@@ -131,14 +137,14 @@ fn main() {
}
Event::KeyboardInput(ElementState::Pressed, _, Some(Key::Right)) =>{
frame_num += 1;
if !read_file(dir, frame_num, &api) {
if !read_file(&dir, frame_num, &api) {
frame_num -= 1;
println!("At last frame.");
}
}
Event::KeyboardInput(ElementState::Pressed, _, Some(Key::Left)) => {
frame_num -= 1;
if frame_num < 0 || !read_file(dir, frame_num, &api) {
if frame_num < 0 || !read_file(&dir, frame_num, &api) {
frame_num +=1;
println!("At first frame.");
}
@@ -55,12 +55,11 @@ impl webrender_traits::RenderNotifier for Notifier {

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("{} <shader path>", args[0]);
return;
}

let res_path = &args[1];
let res_path = if args.len() > 1 {
Some(PathBuf::from(&args[1]))
} else {
None
};

let window = glutin::WindowBuilder::new()
.with_title("WebRender Sample")
@@ -80,13 +79,13 @@ fn main() {
};

println!("OpenGL version {}", version);
println!("Shader resource path: {}", res_path);
println!("Shader resource path: {:?}", res_path);

let (width, height) = window.get_inner_size().unwrap();

let opts = webrender::RendererOptions {
device_pixel_ratio: 1.0,
resource_path: PathBuf::from(res_path),
resource_override_path: res_path,
enable_aa: false,
enable_msaa: false,
enable_profiler: false,
@@ -1,9 +1,10 @@
[package]
name = "webrender"
version = "0.9.0"
version = "0.10.0"
authors = ["Glenn Watson <gw@intuitionlibrary.com>"]
license = "MPL-2.0"
repository = "https://github.com/servo/webrender"
build = "build.rs"
workspace = ".."

[features]
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::env;
use std::path::{Path, PathBuf};
use std::io::prelude::*;
use std::fs::{canonicalize, read_dir, File};

fn write_shaders(glsl_files: Vec<PathBuf>, shader_file_path: &Path) {
let mut shader_file = File::create(shader_file_path).unwrap();

write!(shader_file, "/// AUTO GENERATED BY build.rs\n\n").unwrap();
write!(shader_file, "use std::collections::HashMap;\n").unwrap();
write!(shader_file, "lazy_static! {{\n").unwrap();
write!(shader_file, " pub static ref SHADERS: HashMap<&'static str, &'static str> = {{\n").unwrap();
write!(shader_file, " let mut h = HashMap::with_capacity({});\n", glsl_files.len()).unwrap();
for glsl in glsl_files {
let shader_name = glsl.file_name().unwrap().to_str().unwrap();
// strip .glsl
let shader_name = shader_name.replace(".glsl", "");
let full_path = canonicalize(&glsl).unwrap();
let full_name = full_path.as_os_str().to_str().unwrap();
// if someone is building on a network share, I'm sorry.
let full_name = full_name.replace("\\\\?\\", "");
let full_name = full_name.replace("\\", "/");
write!(shader_file, " h.insert(\"{}\", include_str!(\"{}\"));\n",
shader_name, full_name).unwrap();
}
write!(shader_file, " h\n").unwrap();
write!(shader_file, " }};\n").unwrap();
write!(shader_file, "}}\n").unwrap();
}

fn main() {
let out_dir = env::var("OUT_DIR").unwrap_or("out".to_owned());

let shaders_file = Path::new(&out_dir).join("shaders.rs");
let mut glsl_files = vec![];

let res_dir = Path::new("res");
for entry in read_dir(res_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if entry.file_name().to_str().unwrap().ends_with(".glsl") {
glsl_files.push(path.to_owned());
}
}

write_shaders(glsl_files, &shaders_file);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.