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

Add skeleton of GL rendering #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ travis-ci = { repository = "MarkMcCaskey/rusty-boy", branch = "master" }
maintainence = {status = "experimental"}

[features]
default = []
default = ["opengl"]
asm = ["nom"]
debugger = ["ncurses", "nom"]
vulkan = []
opengl = ["gl", "rusty-boy-derive"]
development = ["debugger"]

[dependencies]
app_dirs = "^1.1.1"
bmp = "0.4"
clap = "^2.31"
gameboy-rom = "0.2"
lazy_static = "^1.0"
Expand All @@ -29,11 +31,14 @@ serde = "^1.0.0"
serde_derive = "^1.0.0"
time = "^0.1"

rusty-boy-derive = { path = "lib/rusty-boy-derive", optional = true }
vulkano = {version = "0.6.2", optional = true}
vulkano-shader-derive = {version = "0.6.2", optional = true}
winit = {version = "0.7.6", optional = true}
vulkano-win = {version = "0.6.2", optional = true}

gl = {version = "^0.12", optional = true}

nom = {version = "^2.2", optional = true}
ncurses = {version = "^5.85.0", optional = true}

Expand Down
13 changes: 13 additions & 0 deletions lib/rusty-boy-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rusty-boy-derive"
version = "0.1.1"
authors = ["Mark McCaskey"]
edition = "2018"
license = "MIT"

[dependencies]
quote = "0.6"
syn = "0.15"

[lib]
proc-macro = true
85 changes: 85 additions & 0 deletions lib/rusty-boy-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#![recursion_limit = "128"]

// Code from and/or inspired by http://nercury.github.io/rust/opengl/tutorial/2018/07/11/opengl-in-rust-from-scratch-10-procedural-macros.html

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

use syn::Token;

#[proc_macro_derive(VertexAttribPointers, attributes(location))]
pub fn vertex_attrib_pointers_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse_macro_input!(input as syn::DeriveInput);
let gen = generate_impl(ast);
gen
}

fn generate_impl(ast: syn::DeriveInput) -> proc_macro::TokenStream {
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.data);
let tok_strm2 = quote! {
impl #ident #generics #where_clause {
fn vertex_attrib_pointers() {
let stride = std::mem::size_of::<Self>();
let offset = 0;

#(#fields_vertex_attrib_pointer)*
}
}
};
//panic!("generate impl quote: {:#?}", tok_strm2);
tok_strm2.into()
}

fn generate_vertex_attrib_pointer_calls(body: syn::Data) -> Vec<Box<dyn quote::ToTokens>> {
match body {
syn::Data::Enum(_) => panic!("VertexAttribPointers can not be implemented for enums"),
syn::Data::Union(_) => panic!("VertexAttribPointers can not be implemented for unions"),
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Unit,
..
}) => panic!("VertexAttribPointers can not be implemented for Unit structs"),
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Unnamed(_),
..
}) => panic!("VertexAttribPointers can not be implemented for Tuple structs"),
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Named(syn::FieldsNamed { named, .. }),
..
}) => named
.into_iter()
.map(generate_struct_field_vertex_attrib_pointer_call)
.collect(),
}
}

fn generate_struct_field_vertex_attrib_pointer_call(field: syn::Field) -> Box<dyn quote::ToTokens> {
let field_name = field.ident.map(|id| format!("{}", &id)).unwrap_or_default();
let location_attr = field
.attrs
.into_iter()
.filter(|a| a.path.segments.iter().next().unwrap().ident.to_string() == "location")
.next()
.unwrap_or_else(|| {
panic!(
"Field {:?} is missing #[location = ?] attribute",
field_name
)
});

let location_value_literal = location_attr.tts.into_iter().skip(1).next();

let field_ty = &field.ty;
Box::new(quote! {
let location = #location_value_literal;
unsafe {
#field_ty::vertex_attrib_pointer(stride, location, offset);
}
let offset = offset + std::mem::size_of::<#field_ty>();
})
}
5 changes: 4 additions & 1 deletion src/io/applicationsettings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Stores all settings related to the application from a user perspective

use crate::io::constants::{APP_INFO, SCALE};
use app_dirs::*;
use clap::ArgMatches;
use crate::io::constants::{APP_INFO, SCALE};
use std::path::PathBuf;

use log::LevelFilter;
Expand All @@ -19,6 +19,7 @@ pub struct ApplicationSettings {
pub memvis_mode: bool,
pub debugger_on: bool,
pub vulkan_mode: bool,
pub gl_mode: bool,
config_path: Option<PathBuf>,
pub data_path: Option<PathBuf>,
pub ui_scale: f32,
Expand All @@ -35,6 +36,7 @@ impl ApplicationSettings {
let trace_mode = arguments.is_present("trace");
let memvis_mode = arguments.is_present("visualize");
let vulkan_mode = arguments.is_present("vulkan");
let gl_mode = arguments.is_present("opengl");

// Set up logging
let stdout = ConsoleAppender::builder()
Expand Down Expand Up @@ -91,6 +93,7 @@ impl ApplicationSettings {
trace_mode,
memvis_mode,
vulkan_mode,
gl_mode,
config_path,
data_path,
debugger_on: should_debugger,
Expand Down
9 changes: 8 additions & 1 deletion src/io/applicationstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ impl ApplicationState {
Box::new(graphics::sdl2::Sdl2Renderer::new(&app_settings)?)
};

#[cfg(not(feature = "vulkan"))]
#[cfg(feature = "opengl")]
let renderer: Box<Renderer> = if dbg!(app_settings.gl_mode) {
Box::new(graphics::gl::GlRenderer::new(&app_settings)?)
} else {
Box::new(graphics::sdl2::Sdl2Renderer::new(&app_settings)?)
};

#[cfg(not(any(feature = "vulkan", feature = "opengl")))]
let renderer: Box<Renderer> = Box::new(graphics::sdl2::Sdl2Renderer::new(&app_settings)?);

let gbcopy = gameboy.clone();
Expand Down
11 changes: 11 additions & 0 deletions src/io/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,16 @@ pub fn read_arguments<'input>() -> ArgMatches<'input> {
);
}

#[cfg(feature = "opengl")]
{
app_builder = app_builder.arg(
Arg::with_name("opengl")
.short("g")
.long("opengl")
.help("Runs graphics on the GPU with OpenGL")
.takes_value(false),
);
}

app_builder.get_matches()
}
1 change: 0 additions & 1 deletion src/io/graphics/.#vulkan.rs

This file was deleted.

Loading