Skip to content

Commit

Permalink
Render target
Browse files Browse the repository at this point in the history
  • Loading branch information
Nercury committed Dec 2, 2018
1 parent 3c5bffa commit 703fc95
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 2 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lesson-25-x-terrain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2018"

[dependencies]
resources = { path = "../lib/resources", features = ["backend_filesystem_watch"] }
winput = { path = "../lib/winput" }
lesson_25_x_render_gl = { path = "render_gl" }
lesson_25_x_render_gl_derive = { path = "render_gl_derive" }
gl = { path = "../lib/gl" }
failure = "0.1.3"
env_logger = "*"
Expand Down
24 changes: 24 additions & 0 deletions lesson-25-x-terrain/render_gl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "lesson_25_x_render_gl"
version = "0.1.0"
authors = []
edition = "2018"

[dependencies]
gl = { path = "../../lib/gl" }
resources = { path = "../../lib/resources" }
lesson_25_x_render_gl_derive = { path = "../render_gl_derive" }
half = "1.1.1"
vec-2-10-10-10 = "0.1.2"
nalgebra = "0.16"
ncollide3d = "0.17"
failure = "0.1.3"
font-kit = { version = "0.1.0" }
euclid = "0.18.2"
image = "0.20.1"
lyon_tessellation = "0.11.0"
lyon_path = "0.11.0"
int_hash = "0.1.1"
slotmap = "0.3"
log = "0.4.6"
floating-duration = "0.1.2"
3 changes: 3 additions & 0 deletions lesson-25-x-terrain/render_gl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[macro_use] extern crate failure;

pub mod render_target;
90 changes: 90 additions & 0 deletions lesson-25-x-terrain/render_gl/src/render_target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use nalgebra as na;
use failure;
use gl;
use gl::types::*;

#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "Render target creation failed. The framebuffer was not complete.")]
TheFramebufferWasNotComplete,
}

pub struct DefaultRenderTarget {
size: na::Vector2<i32>,
gl: gl::Gl,
}

impl DefaultRenderTarget {
pub fn new(gl: &gl::Gl, size: na::Vector2<i32>) -> DefaultRenderTarget {
DefaultRenderTarget {
size,
gl: gl.clone(),
}
}

pub fn bind(&self) {
unsafe {
self.gl.BindFramebuffer(gl::FRAMEBUFFER, 0);
}
}
}

pub struct FramebufferTarget {
size: na::Vector2<i32>,
fb: GLuint,
tex: GLuint,
gl: gl::Gl,
}

impl FramebufferTarget {
pub fn new(gl: &gl::Gl, size: na::Vector2<i32>) -> Result<FramebufferTarget, Error> {
let mut tex = 0;
let mut fb = 0;

unsafe {
gl.GenFramebuffers(1, &mut fb);
gl.BindFramebuffer(gl::FRAMEBUFFER, fb);

gl.GenTextures(1, &mut tex);
gl.BindTexture(gl::TEXTURE_2D, tex);
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32);
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32);

gl.FramebufferTexture(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, tex, 0);

gl.TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32, size.x, size.y, 0, gl::RGBA, gl::UNSIGNED_BYTE, std::ptr::null());

let complete = gl.CheckFramebufferStatus(gl::FRAMEBUFFER);

gl.BindFramebuffer(gl::FRAMEBUFFER, 0);
gl.BindTexture(gl::TEXTURE_2D, 0);

if complete != gl::FRAMEBUFFER_COMPLETE {
return Err(Error::TheFramebufferWasNotComplete);
}
}

Ok(FramebufferTarget {
size,
fb,
tex,
gl: gl.clone(),
})
}

pub fn bind(&self) {
unsafe {
self.gl.BindFramebuffer(gl::FRAMEBUFFER, self.fb);
}
}
}

impl Drop for FramebufferTarget
{
fn drop(&mut self){
unsafe{
self.gl.DeleteFramebuffers(1, &self.fb);
self.gl.DeleteTextures(1, &self.tex);
}
}
}
11 changes: 11 additions & 0 deletions lesson-25-x-terrain/render_gl_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "lesson_25_x_render_gl_derive"
version = "0.1.0"
authors = []

[dependencies]
quote = "0.3.15"
syn = "0.11.11"

[lib]
proc-macro = true
121 changes: 121 additions & 0 deletions lesson-25-x-terrain/render_gl_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#![recursion_limit = "128"]

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

#[proc_macro_derive(VertexAttribPointers, attributes(location, divisor))]
pub fn vertex_attrib_pointers_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let s = input.to_string();
let ast = syn::parse_derive_input(&s).unwrap();
let gen = generate_impl(&ast);
gen.parse().unwrap()
}

fn generate_impl(ast: &syn::DeriveInput) -> quote::Tokens {
let ident = &ast.ident;
let generics = &ast.generics;
let where_clause = &ast.generics.where_clause;
let fields_vertex_attrib_pointer = generate_vertex_attrib_pointer_calls(&ast.body);

quote!{
impl #ident #generics #where_clause {
#[allow(unused_variables)]
pub fn vertex_attrib_pointers(gl: &::gl::Gl) {
let stride = ::std::mem::size_of::<Self>();
let offset = 0;

#(#fields_vertex_attrib_pointer)*
}
}
}
}

fn generate_vertex_attrib_pointer_calls(body: &syn::Body) -> Vec<quote::Tokens> {
match body {
&syn::Body::Enum(_) => panic!("VertexAttribPointers can not be implemented for enums"),
&syn::Body::Struct(syn::VariantData::Unit) => {
panic!("VertexAttribPointers can not be implemented for Unit structs")
}
&syn::Body::Struct(syn::VariantData::Tuple(_)) => {
panic!("VertexAttribPointers can not be implemented for Tuple structs")
}
&syn::Body::Struct(syn::VariantData::Struct(ref s)) => s
.iter()
.map(generate_struct_field_vertex_attrib_pointer_call)
.collect(),
}
}

fn generate_struct_field_vertex_attrib_pointer_call(field: &syn::Field) -> quote::Tokens {
let field_name = match field.ident {
Some(ref i) => format!("{}", i),
None => String::from(""),
};
let field_ty = &field.ty;

if let Some(location_attr) = field
.attrs
.iter()
.filter(|a| a.value.name() == "location")
.next()
{
let location_value: usize = match location_attr.value {
syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => {
s.parse().unwrap_or_else(|_| {
panic!(
"Field {} location attribute value must contain an integer",
field_name
)
})
}
_ => panic!(
"Field {} location attribute value must be a string literal",
field_name
),
};

let divisor_call = match field
.attrs
.iter()
.filter(|a| a.value.name() == "divisor")
.next()
{
Some(attr) => {
let divisor_value: u32 = match attr.value {
syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => {
s.parse().unwrap_or_else(|_| {
panic!(
"Field {} divisor attribute value must contain an integer",
field_name
)
})
}
_ => panic!(
"Field {} divisor attribute value must be a string literal",
field_name
),
};

quote! {
gl.VertexAttribDivisor(#location_value as u32, #divisor_value);
}
}
None => quote!{},
};

quote! {
let location = #location_value;
unsafe {
#field_ty::vertex_attrib_pointer(gl, stride, location, offset);
#divisor_call
}
let offset = offset + ::std::mem::size_of::<#field_ty>();
}
} else {
quote! {
let offset = offset + ::std::mem::size_of::<#field_ty>();
}
}
}
1 change: 1 addition & 0 deletions lesson-25-x-terrain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use resources::{
Resources,
backend::FileSystem
};
use winput;

mod debug;
mod onion;
Expand Down
3 changes: 1 addition & 2 deletions lib/resources/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ mod test {
}

#[test]
fn writing_resource_should_produce_change_sync_point_and_other_resource_proxies_should_see_it_as_modified(
) {
fn writing_resource_should_produce_change_sync_point_and_other_resource_proxies_should_see_it_as_modified() {
let res =
Resources::new().loaded_from("a", 0, backend::InMemory::new().with("name", b"hello"));

Expand Down
7 changes: 7 additions & 0 deletions lib/winput/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "winput"
version = "0.1.0"
authors = []
edition = "2018"

[dependencies]
12 changes: 12 additions & 0 deletions lib/winput/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(Debug, Copy, Clone)]
pub struct WindowDimensions {
pub size: WindowSize,
pub hdpi_size: WindowSize,
pub high_dpi: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct WindowSize {
pub width: i32,
pub height: i32,
}

0 comments on commit 703fc95

Please sign in to comment.