Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mattkleiny/surreal-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Jan 30, 2024
2 parents be96365 + 0a2f56c commit 1c34cbd
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 178 deletions.
2 changes: 0 additions & 2 deletions modules/graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ common = { package = "surreal-common", path = "../../common" }
macros = { package = "surreal-macros", path = "../../macros" }
serde = { workspace = true, optional = true }
bitflags = { workspace = true }

# local dependencies
image = { version = "0.24.8", default-features = false, features = ["png"] }
gl = "0.14.0"
18 changes: 3 additions & 15 deletions modules/graphics/src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,15 @@ impl GraphicsBackend for HeadlessGraphicsBackend {
Ok(())
}

fn target_blit(
&self,
from: TargetId,
to: TargetId,
source_rect: &Rectangle,
dest_rect: &Rectangle,
filter: TextureFilter,
) -> Result<(), TargetError> {
Ok(())
}

fn target_blit_to_display(
fn target_blit_to_active(
&self,
target: TargetId,
source_rect: &Rectangle,
dest_rect: &Rectangle,
source_rect: Option<common::Rectangle>,
dest_rect: Option<common::Rectangle>,
filter: TextureFilter,
) -> Result<(), TargetError> {
Ok(())
}

fn target_delete(&self, target: TargetId) -> Result<(), TargetError> {
Ok(())
}
Expand Down
5 changes: 1 addition & 4 deletions modules/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ pub enum TargetError {
FailedToBuildAttachments,
}

pub trait GraphicsVisitor {}

/// An abstraction on top of the underlying graphics API.
///
/// This is a mid-level abstraction that makes use of 'opaque' resource IDs to
Expand Down Expand Up @@ -150,7 +148,6 @@ pub trait GraphicsBackend {
fn target_create(&self, color_attachment: TextureId, depth_attachment: Option<TextureId>, stencil_attachment: Option<TextureId>) -> Result<TargetId, TargetError>;
fn target_activate(&self, target: TargetId) -> Result<(), TargetError>;
fn target_set_default(&self) -> Result<(), TargetError>;
fn target_blit(&self, from: TargetId, to: TargetId, source_rect: &common::Rectangle, dest_rect: &common::Rectangle, filter: TextureFilter) -> Result<(), TargetError>;
fn target_blit_to_display(&self, target: TargetId, source_rect: &common::Rectangle, dest_rect: &common::Rectangle, filter: TextureFilter) -> Result<(), TargetError>;
fn target_blit_to_active(&self, target: TargetId, source_rect: Option<common::Rectangle>, dest_rect: Option<common::Rectangle>, filter: TextureFilter) -> Result<(), TargetError>;
fn target_delete(&self, target: TargetId) -> Result<(), TargetError>;
}
34 changes: 34 additions & 0 deletions modules/graphics/src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ impl Material {
&self.shader
}

/// Gets the flags of the 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);
}

if self.culling_mode() != CullingMode::Disabled {
flags.insert(MaterialFlags::BACKFACE_CULLING);
}

if self.scissor_mode() != ScissorMode::Disabled {
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
}

/// Gets the underlying [`ShaderUniformSet`] of the material.
pub fn uniforms(&self) -> &ShaderUniformSet {
&self.uniforms
Expand Down
28 changes: 9 additions & 19 deletions modules/graphics/src/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,17 +800,19 @@ impl GraphicsBackend for OpenGLGraphicsBackend {
}
}

fn target_blit(
fn target_blit_to_active(
&self,
from: TargetId,
to: TargetId,
source_rect: &Rectangle,
dest_rect: &Rectangle,
target: TargetId,
source_rect: Option<Rectangle>,
dest_rect: Option<Rectangle>,
filter: TextureFilter,
) -> Result<(), TargetError> {
unsafe {
gl::BindFramebuffer(gl::READ_FRAMEBUFFER, from.into());
gl::BindFramebuffer(gl::DRAW_FRAMEBUFFER, to.into());
gl::BindFramebuffer(gl::READ_FRAMEBUFFER, target.into());
gl::BindFramebuffer(gl::DRAW_FRAMEBUFFER, TargetId::NONE.into());

let source_rect = source_rect.unwrap_or(Rectangle::from_corner_points(0., 0., 1., 1.));
let dest_rect = dest_rect.unwrap_or(Rectangle::from_corner_points(0., 0., 1., 1.));

gl::BlitFramebuffer(
source_rect.left() as i32,
Expand All @@ -835,18 +837,6 @@ impl GraphicsBackend for OpenGLGraphicsBackend {
}
}

fn target_blit_to_display(
&self,
handle: TargetId,
source_rect: &Rectangle,
dest_rect: &Rectangle,
filter: TextureFilter,
) -> Result<(), TargetError> {
self.target_blit(handle, TargetId::NONE, source_rect, dest_rect, filter)?;

Ok(())
}

fn target_delete(&self, target: TargetId) -> Result<(), TargetError> {
unsafe {
gl::DeleteFramebuffers(1, &target.into());
Expand Down
79 changes: 37 additions & 42 deletions modules/graphics/src/rendering/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! Command queueing for common operations against the renderer.
use std::sync::Mutex;

use super::*;
Expand All @@ -17,32 +15,6 @@ pub struct RenderQueue {
commands: Mutex<Vec<RenderCommand>>,
}

#[derive(Debug)]
pub enum RenderError {
BufferError(BufferError),
TextureError(TextureError),
ShaderError(ShaderError),
MeshError(MeshError),
TargetError(TargetError),
}

macro_rules! impl_from_error {
($error:tt) => {
impl From<$error> for RenderError {
#[inline(always)]
fn from(error: $error) -> Self {
Self::$error(error)
}
}
};
}

impl_from_error!(BufferError);
impl_from_error!(TextureError);
impl_from_error!(ShaderError);
impl_from_error!(MeshError);
impl_from_error!(TargetError);

/// A single command for a [`RenderQueue`] to execute.
#[allow(dead_code)]
enum RenderCommand {
Expand Down Expand Up @@ -81,8 +53,37 @@ enum RenderCommand {
vertex_count: usize,
index_count: usize,
},
/// Blits the given render target to the active render target.
BlitRenderTargetToActive { target_id: TargetId, filter: TextureFilter },
}

/// Represents an error that occurred while using the render queue.
#[derive(Debug)]
pub enum RenderQueueError {
BufferError(BufferError),
TextureError(TextureError),
ShaderError(ShaderError),
MeshError(MeshError),
TargetError(TargetError),
}

macro_rules! impl_from_error {
($error:tt) => {
impl From<$error> for RenderQueueError {
#[inline(always)]
fn from(error: $error) -> Self {
Self::$error(error)
}
}
};
}

impl_from_error!(BufferError);
impl_from_error!(TextureError);
impl_from_error!(ShaderError);
impl_from_error!(MeshError);
impl_from_error!(TargetError);

impl RenderQueue {
/// Creates a new [`RenderQueue`].
pub fn new() -> Self {
Expand Down Expand Up @@ -138,19 +139,10 @@ impl RenderQueue {
self.enqueue(RenderCommand::ClearColorBuffer { color });
}

// self.enqueue(RenderCommand::SetShader {
// shader_id: target.blit_shader().id(),
// uniforms: Box::new(target.blit_uniforms().clone()),
// blend_state: BlendState::Disabled,
// culling_mode: CullingMode::Disabled,
// scissor_mode: ScissorMode::Disabled,
// });
// self.enqueue(RenderCommand::DrawMesh {
// mesh_id: target.blit_mesh().id(),
// topology: PrimitiveTopology::TriangleList,
// vertex_count: target.blit_mesh().vertices(),
// index_count: target.blit_mesh().indices(),
// });
self.enqueue(RenderCommand::BlitRenderTargetToActive {
target_id: target.id(),
filter: TextureFilter::Linear,
});
}

/// Clears all [`RenderCommand`] from the queue.
Expand All @@ -161,7 +153,7 @@ impl RenderQueue {
}

/// Flushes all [`RenderCommand`]s in the queue to the given renderer.
pub fn flush(&mut self, graphics: &GraphicsEngine) -> Result<(), RenderError> {
pub fn flush(&mut self, graphics: &GraphicsEngine) -> Result<(), RenderQueueError> {
let mut commands = self.commands.lock().unwrap();

for command in commands.drain(..) {
Expand Down Expand Up @@ -225,6 +217,9 @@ impl RenderQueue {
} => {
graphics.mesh_draw(mesh_id, topology, vertex_count, index_count)?;
}
RenderCommand::BlitRenderTargetToActive { target_id, filter } => {
graphics.target_blit_to_active(target_id, None, None, filter)?;
}
}
}

Expand Down
Loading

0 comments on commit 1c34cbd

Please sign in to comment.