Skip to content

Commit

Permalink
Add basic sprite example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Jul 13, 2024
1 parent 8f802e3 commit 3593ef6
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 327 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bitflags = "2.1.0"
common = { package = "surreal-common", path = "./crates/common" }
editor = { package = "surreal-editor", path = "./editor", optional = true }

# framework
# backends
sdl = { package = "surreal-backend-sdl", path = "./backends/sdl", optional = true }

# crates
Expand All @@ -46,6 +46,10 @@ graphics = { package = "surreal-graphics", path = "./crates/graphics", optional
input = { package = "surreal-input", path = "./crates/input", optional = true }
scripting = { package = "surreal-scripting", path = "./crates/scripting", optional = true }

[[example]]
name = "sprites"
required-features = ["sdl", "graphics"]

[[example]]
name = "hello-world"
required-features = ["sdl", "graphics"]
2 changes: 2 additions & 0 deletions crates/common/src/abstractions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
//! scene.
pub use os::*;
pub use variant::*;

mod os;
mod variant;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common::{Quat, StringName, Vec2, Vec3, Vec4};
use crate::{Quat, StringName, Vec2, Vec3, Vec4};

/// A type that can hold varying different values.
///
Expand Down
4 changes: 0 additions & 4 deletions crates/graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ serde = { workspace = true, optional = true }
bitflags = { workspace = true }
image = { version = "0.25.1", default-features = false, features = ["png"] }
gl = "0.14.0"

[features]
default = ["shaderlib"]
shaderlib = []
4 changes: 0 additions & 4 deletions crates/graphics/src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ impl GraphicsBackend for HeadlessGraphicsBackend {
Ok(())
}

fn shader_metadata(&self, shader: ShaderId) -> Result<ShaderFlags, ShaderError> {
Ok(ShaderFlags::empty())
}

fn shader_uniform_location(&self, shader: ShaderId, name: &str) -> Option<usize> {
None
}
Expand Down
1 change: 0 additions & 1 deletion crates/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ pub trait GraphicsBackend {
// shaders
fn shader_create(&self) -> Result<ShaderId, ShaderError>;
fn shader_link(&self, shader: ShaderId, kernels: &[ShaderKernel]) -> Result<(), ShaderError>;
fn shader_metadata(&self, shader: ShaderId) -> Result<ShaderFlags, ShaderError>;
fn shader_uniform_location(&self, shader: ShaderId, name: &str) -> Option<usize>;
fn shader_set_uniform(&self, shader: ShaderId, location: usize, value: &ShaderUniform) -> Result<(), ShaderError>;
fn shader_activate(&self, shader: ShaderId) -> Result<(), ShaderError>;
Expand Down
33 changes: 9 additions & 24 deletions crates/graphics/src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub enum BlendState {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BlendFactor {
One,
SrcAlpha,
SrcColor,
DstAlpha,
DstColor,
OneMinusSrcAlpha,
OneMinusSrcColor,
OneMinusDstAlpha,
OneMinusDstColor,
SourceAlpha,
SourceColor,
DestinationAlpha,
DestinationColor,
OneMinusSourceAlpha,
OneMinusSourceColor,
OneMinusDestinationAlpha,
OneMinusDestinationColor,
}

/// Culling modes for materials.
Expand Down Expand Up @@ -63,7 +63,7 @@ pub struct Material {

impl Material {
/// Constructs a new material for the given [`ShaderProgram`].
pub fn new(shader: &ShaderProgram) -> Self {
pub fn from_program(shader: &ShaderProgram) -> Self {
Self {
shader: shader.clone(),
uniforms: ShaderUniformSet::default(),
Expand All @@ -82,9 +82,6 @@ impl Material {
pub fn flags(&self) -> MaterialFlags {
let mut flags = MaterialFlags::empty();

let shader = &self.shader;
let metadata = shader.flags();

if self.blend_state() != BlendState::Disabled {
flags.insert(MaterialFlags::ALPHA_BLENDING);
}
Expand All @@ -97,18 +94,6 @@ impl Material {
flags.insert(MaterialFlags::SCISSOR_TESTING);
}

if metadata.contains(ShaderFlags::ALPHA_TESTING) {
flags.insert(MaterialFlags::ALPHA_TESTING);
}

if metadata.contains(ShaderFlags::DEPTH_TESTING) {
flags.insert(MaterialFlags::DEPTH_TESTING);
}

if metadata.contains(ShaderFlags::DEPTH_WRITING) {
flags.insert(MaterialFlags::DEPTH_WRITING);
}

flags
}

Expand Down
33 changes: 8 additions & 25 deletions crates/graphics/src/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ pub struct OpenGLGraphicsBackend {
#[derive(Default)]
struct BackendState {
sampler_cache: FastHashMap<TextureSampler, u32>,
shader_metadata: FastHashMap<ShaderId, ShaderMetadata>,
}

/// Internal metadata for a shader.
struct ShaderMetadata {
flags: ShaderFlags,
}

impl OpenGLGraphicsBackend {
Expand Down Expand Up @@ -95,14 +89,14 @@ impl GraphicsBackend for OpenGLGraphicsBackend {
fn convert_blend_factor(factor: BlendFactor) -> u32 {
match factor {
BlendFactor::One => gl::ONE,
BlendFactor::SrcAlpha => gl::SRC_ALPHA,
BlendFactor::SrcColor => gl::SRC_COLOR,
BlendFactor::DstAlpha => gl::DST_ALPHA,
BlendFactor::DstColor => gl::DST_COLOR,
BlendFactor::OneMinusSrcAlpha => gl::ONE_MINUS_SRC_ALPHA,
BlendFactor::OneMinusSrcColor => gl::ONE_MINUS_SRC_COLOR,
BlendFactor::OneMinusDstAlpha => gl::ONE_MINUS_DST_ALPHA,
BlendFactor::OneMinusDstColor => gl::ONE_MINUS_DST_COLOR,
BlendFactor::SourceAlpha => gl::SRC_ALPHA,
BlendFactor::SourceColor => gl::SRC_COLOR,
BlendFactor::DestinationAlpha => gl::DST_ALPHA,
BlendFactor::DestinationColor => gl::DST_COLOR,
BlendFactor::OneMinusSourceAlpha => gl::ONE_MINUS_SRC_ALPHA,
BlendFactor::OneMinusSourceColor => gl::ONE_MINUS_SRC_COLOR,
BlendFactor::OneMinusDestinationAlpha => gl::ONE_MINUS_DST_ALPHA,
BlendFactor::OneMinusDestinationColor => gl::ONE_MINUS_DST_COLOR,
}
}

Expand Down Expand Up @@ -502,17 +496,6 @@ impl GraphicsBackend for OpenGLGraphicsBackend {
Ok(())
}

#[profiling]
fn shader_metadata(&self, shader: ShaderId) -> Result<ShaderFlags, ShaderError> {
let state = self.state.borrow();

state
.shader_metadata
.get(&shader)
.map(|metadata| metadata.flags)
.ok_or(ShaderError::InvalidId(shader))
}

#[profiling]
fn shader_uniform_location(&self, shader: ShaderId, name: &str) -> Option<usize> {
unsafe {
Expand Down
20 changes: 3 additions & 17 deletions crates/graphics/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod lang {
type Environment: Default = ShaderEnvironment;

/// Parses the given raw source code into one or more [`ShaderKernel`]s.
fn parse_kernels(source_code: &str, environment: &Self::Environment) -> Result<Vec<ShaderKernel>, ShaderError>;
fn parse_kernels(source_code: &str) -> Result<Vec<ShaderKernel>, ShaderError>;
}
}

Expand Down Expand Up @@ -94,7 +94,6 @@ pub struct ShaderProgram {
struct ShaderProgramState {
id: ShaderId,
location_cache: FastHashMap<String, Option<usize>>,
flags: ShaderFlags,
}

impl ShaderProgram {
Expand All @@ -104,7 +103,6 @@ impl ShaderProgram {
state: Rc::new(RefCell::new(ShaderProgramState {
id: graphics().shader_create()?,
location_cache: FastHashMap::default(),
flags: ShaderFlags::empty(),
})),
})
}
Expand Down Expand Up @@ -147,11 +145,6 @@ impl ShaderProgram {
self.state.borrow().id
}

/// Returns the [`ShaderFlags`] for the underlying program.
pub fn flags(&self) -> ShaderFlags {
self.state.borrow().flags
}

/// Retrieves the binding location of the given shader uniform in the
/// underlying program.
pub fn get_uniform_location(&self, name: &str) -> Option<usize> {
Expand Down Expand Up @@ -207,21 +200,14 @@ impl ShaderProgram {

/// Reloads the [`ShaderProgram`] from the given shader code.
pub fn load_code<S: ShaderLanguage>(&self, text: &str) -> Result<(), ShaderError> {
// TODO: allow for shader environment to be passed in
let shaders = S::parse_kernels(text, &S::Environment::default())?;

self.load_kernels(&shaders)?;
self.load_kernels(&S::parse_kernels(text)?)?;

Ok(())
}

/// Reloads the [`ShaderProgram`] from the given shader code.
pub fn load_kernels(&self, kernels: &[ShaderKernel]) -> Result<(), ShaderError> {
let mut state = self.state.borrow_mut();
let server = graphics();

server.shader_link(state.id, kernels)?;
state.flags = server.shader_metadata(state.id)?;
graphics().shader_link(self.id(), kernels)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 3593ef6

Please sign in to comment.