Skip to content

Commit

Permalink
use a ref to abstraction instead of specific gl graphics server
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Oct 9, 2024
1 parent 4f2d505 commit 5f010cf
Show file tree
Hide file tree
Showing 44 changed files with 383 additions and 370 deletions.
11 changes: 5 additions & 6 deletions editor/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use crate::{
Attachment, AttachmentKind, FrameBuffer, ResourceBindGroup, ResourceBinding,
},
geometry_buffer::GeometryBuffer,
gl::server::GlGraphicsServer,
gpu_program::{GpuProgram, UniformLocation},
gpu_texture::{
Coordinate, GpuTextureKind, MagnificationFilter, MinificationFilter, PixelKind,
Expand All @@ -64,7 +63,7 @@ struct EdgeDetectShader {
}

impl EdgeDetectShader {
pub fn new(server: &GlGraphicsServer) -> Result<Self, FrameworkError> {
pub fn new(server: &dyn GraphicsServer) -> Result<Self, FrameworkError> {
let fragment_source = r#"
layout (location = 0) out vec4 outColor;
Expand Down Expand Up @@ -138,7 +137,7 @@ pub struct HighlightRenderPass {

impl HighlightRenderPass {
fn create_frame_buffer(
server: &GlGraphicsServer,
server: &dyn GraphicsServer,
width: usize,
height: usize,
) -> Box<dyn FrameBuffer> {
Expand Down Expand Up @@ -190,7 +189,7 @@ impl HighlightRenderPass {
.unwrap()
}

pub fn new_raw(server: &GlGraphicsServer, width: usize, height: usize) -> Self {
pub fn new_raw(server: &dyn GraphicsServer, width: usize, height: usize) -> Self {
Self {
framebuffer: Self::create_frame_buffer(server, width, height),
quad: <dyn GeometryBuffer>::from_surface_data(
Expand All @@ -205,11 +204,11 @@ impl HighlightRenderPass {
}
}

pub fn new(server: &GlGraphicsServer, width: usize, height: usize) -> Rc<RefCell<Self>> {
pub fn new(server: &dyn GraphicsServer, width: usize, height: usize) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::new_raw(server, width, height)))
}

pub fn resize(&mut self, server: &GlGraphicsServer, width: usize, height: usize) {
pub fn resize(&mut self, server: &dyn GraphicsServer, width: usize, height: usize) {
self.framebuffer = Self::create_frame_buffer(server, width, height);
}
}
Expand Down
4 changes: 2 additions & 2 deletions editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2705,14 +2705,14 @@ impl Editor {
graphics_context.window.scale_factor() as f32,
);

let overlay_pass = OverlayRenderPass::new(graphics_context.renderer.pipeline_state());
let overlay_pass = OverlayRenderPass::new(graphics_context.renderer.graphics_server());
graphics_context
.renderer
.add_render_pass(overlay_pass.clone());
self.overlay_pass = Some(overlay_pass);

let highlighter = HighlightRenderPass::new(
&graphics_context.renderer.state,
&*graphics_context.renderer.server,
self.settings.windows.window_size.x as usize,
self.settings.windows.window_size.y as usize,
);
Expand Down
7 changes: 3 additions & 4 deletions editor/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::{
error::FrameworkError,
framebuffer::{ResourceBindGroup, ResourceBinding},
geometry_buffer::GeometryBuffer,
gl::server::GlGraphicsServer,
gpu_program::{GpuProgram, UniformLocation},
server::GraphicsServer,
uniform::StaticUniformBuffer,
BlendFactor, BlendFunc, BlendParameters, CompareFunc, DrawParameters, ElementRange,
GeometryBufferExt,
Expand All @@ -43,7 +43,6 @@ use crate::{
},
Editor,
};
use fyrox::renderer::framework::server::GraphicsServer;
use std::{any::TypeId, cell::RefCell, rc::Rc};

struct OverlayShader {
Expand All @@ -53,7 +52,7 @@ struct OverlayShader {
}

impl OverlayShader {
pub fn new(server: &GlGraphicsServer) -> Result<Self, FrameworkError> {
pub fn new(server: &dyn GraphicsServer) -> Result<Self, FrameworkError> {
let fragment_source = include_str!("../resources/shaders/overlay_fs.glsl");
let vertex_source = include_str!("../resources/shaders/overlay_vs.glsl");
let program = server.create_program("OverlayShader", vertex_source, fragment_source)?;
Expand All @@ -75,7 +74,7 @@ pub struct OverlayRenderPass {
}

impl OverlayRenderPass {
pub fn new(server: &GlGraphicsServer) -> Rc<RefCell<Self>> {
pub fn new(server: &dyn GraphicsServer) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
quad: <dyn GeometryBuffer>::from_surface_data(
&SurfaceData::make_collapsed_xy_quad(),
Expand Down
2 changes: 1 addition & 1 deletion editor/src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl SceneController for GameScene {

if let Some(highlighter) = self.highlighter.as_ref() {
highlighter.borrow_mut().resize(
&gc.renderer.state,
&*gc.renderer.server,
frame_size.x as usize,
frame_size.y as usize,
);
Expand Down
15 changes: 15 additions & 0 deletions fyrox-graphics/src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ pub trait FrameBuffer: Any {
fn color_attachments(&self) -> &[Attachment];
fn depth_attachment(&self) -> Option<&Attachment>;
fn set_cubemap_face(&mut self, attachment_index: usize, face: CubeMapFace);
fn blit_to(
&self,
dest: &dyn FrameBuffer,
src_x0: i32,
src_y0: i32,
src_x1: i32,
src_y1: i32,
dst_x0: i32,
dst_y0: i32,
dst_x1: i32,
dst_y1: i32,
copy_color: bool,
copy_depth: bool,
copy_stencil: bool,
);
fn clear(
&mut self,
viewport: Rect<i32>,
Expand Down
53 changes: 53 additions & 0 deletions fyrox-graphics/src/gl/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,59 @@ impl FrameBuffer for GlFrameBuffer {
}
}

fn blit_to(
&self,
dest: &dyn FrameBuffer,
src_x0: i32,
src_y0: i32,
src_x1: i32,
src_y1: i32,
dst_x0: i32,
dst_y0: i32,
dst_x1: i32,
dst_y1: i32,
copy_color: bool,
copy_depth: bool,
copy_stencil: bool,
) {
let server = self.state.upgrade().unwrap();

let source = self;
let dest = dest.as_any().downcast_ref::<GlFrameBuffer>().unwrap();

let mut mask = 0;
if copy_color {
mask |= glow::COLOR_BUFFER_BIT;
}
if copy_depth {
mask |= glow::DEPTH_BUFFER_BIT;
}
if copy_stencil {
mask |= glow::STENCIL_BUFFER_BIT;
}

unsafe {
server
.gl
.bind_framebuffer(glow::READ_FRAMEBUFFER, source.id());
server
.gl
.bind_framebuffer(glow::DRAW_FRAMEBUFFER, dest.id());
server.gl.blit_framebuffer(
src_x0,
src_y0,
src_x1,
src_y1,
dst_x0,
dst_y0,
dst_x1,
dst_y1,
mask,
glow::NEAREST,
);
}
}

fn clear(
&mut self,
viewport: Rect<i32>,
Expand Down
116 changes: 31 additions & 85 deletions fyrox-graphics/src/gl/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::geometry_buffer::{GeometryBuffer, GeometryBufferDescriptor};
use crate::gl::geometry_buffer::GlGeometryBuffer;
use crate::gl::program::GlProgram;
use crate::gl::read_buffer::GlAsyncReadBuffer;
use crate::gl::ToGlConstant;
use crate::gpu_program::{GpuProgram, PropertyDefinition};
use crate::read_buffer::AsyncReadBuffer;
use crate::server::{GraphicsServer, ServerCapabilities};
use crate::{
buffer::{Buffer, BufferKind, BufferUsage},
core::{color::Color, log::Log, math::Rect},
error::FrameworkError,
framebuffer::{Attachment, FrameBuffer},
gl::{self, framebuffer::GlFrameBuffer, query::GlQuery, texture::GlTexture},
geometry_buffer::{GeometryBuffer, GeometryBufferDescriptor},
gl::{
self, framebuffer::GlFrameBuffer, geometry_buffer::GlGeometryBuffer, program::GlProgram,
query::GlQuery, read_buffer::GlAsyncReadBuffer, texture::GlTexture, ToGlConstant,
},
gpu_program::{GpuProgram, PropertyDefinition},
gpu_texture::{GpuTexture, GpuTextureKind, MagnificationFilter, MinificationFilter, PixelKind},
query::Query,
read_buffer::AsyncReadBuffer,
server::{GraphicsServer, ServerCapabilities, SharedGraphicsServer},
stats::PipelineStatistics,
BlendEquation, BlendFactor, BlendFunc, BlendMode, ColorMask, CompareFunc, CullFace,
DrawParameters, PolygonFace, PolygonFillMode, ScissorBox, StencilAction, StencilFunc,
Expand All @@ -43,11 +42,12 @@ use glow::HasContext;
#[cfg(not(target_arch = "wasm32"))]
use glutin::{
config::ConfigTemplateBuilder,
context::PossiblyCurrentContext,
context::{ContextApi, ContextAttributesBuilder, GlProfile, NotCurrentGlContext, Version},
context::{
ContextApi, ContextAttributesBuilder, GlProfile, NotCurrentGlContext,
PossiblyCurrentContext, Version,
},
display::{GetGlDisplay, GlDisplay},
surface::{GlSurface, SwapInterval},
surface::{Surface, WindowSurface},
surface::{GlSurface, Surface, SwapInterval, WindowSurface},
};
#[cfg(not(target_arch = "wasm32"))]
use glutin_winit::{DisplayBuilder, GlWindow};
Expand Down Expand Up @@ -257,8 +257,6 @@ impl InnerState {
}
}

pub type SharedPipelineState = Rc<GlGraphicsServer>;

pub struct GlGraphicsServer {
pub gl: glow::Context,
pub(crate) state: RefCell<InnerState>,
Expand Down Expand Up @@ -308,13 +306,14 @@ struct TextureUnitsStorage {
}

impl GlGraphicsServer {
#[allow(clippy::new_ret_no_self)]
#[allow(unused_mut)]
pub fn new(
#[allow(unused_variables)] vsync: bool,
#[allow(unused_variables)] msaa_sample_count: Option<u8>,
window_target: &EventLoopWindowTarget<()>,
window_builder: WindowBuilder,
) -> Result<(Window, SharedPipelineState), FrameworkError> {
) -> Result<(Window, SharedGraphicsServer), FrameworkError> {
#[cfg(not(target_arch = "wasm32"))]
let (window, gl_context, gl_surface, mut context, gl_kind) = {
let mut template = ConfigTemplateBuilder::new()
Expand Down Expand Up @@ -569,7 +568,7 @@ impl GlGraphicsServer {
this: Default::default(),
};

let shared = SharedPipelineState::new(state);
let shared = Rc::new(state);

*shared.this.borrow_mut() = Some(Rc::downgrade(&shared));

Expand Down Expand Up @@ -598,25 +597,6 @@ impl GlGraphicsServer {
None
}

pub fn set_polygon_fill_mode(
&self,
polygon_face: PolygonFace,
polygon_fill_mode: PolygonFillMode,
) {
let mut state = self.state.borrow_mut();
if state.polygon_fill_mode != polygon_fill_mode || state.polygon_face != polygon_face {
state.polygon_fill_mode = polygon_fill_mode;
state.polygon_face = polygon_face;

unsafe {
self.gl.polygon_mode(
state.polygon_face.into_gl(),
state.polygon_fill_mode.into_gl(),
)
}
}
}

pub(crate) fn set_framebuffer(&self, framebuffer: Option<glow::Framebuffer>) {
let mut state = self.state.borrow_mut();
if state.framebuffer != framebuffer {
Expand Down Expand Up @@ -941,55 +921,6 @@ impl GlGraphicsServer {
}
}

pub fn blit_framebuffer(
&self,
source: &dyn FrameBuffer,
dest: &dyn FrameBuffer,
src_x0: i32,
src_y0: i32,
src_x1: i32,
src_y1: i32,
dst_x0: i32,
dst_y0: i32,
dst_x1: i32,
dst_y1: i32,
copy_color: bool,
copy_depth: bool,
copy_stencil: bool,
) {
let source = source.as_any().downcast_ref::<GlFrameBuffer>().unwrap();
let dest = dest.as_any().downcast_ref::<GlFrameBuffer>().unwrap();

let mut mask = 0;
if copy_color {
mask |= glow::COLOR_BUFFER_BIT;
}
if copy_depth {
mask |= glow::DEPTH_BUFFER_BIT;
}
if copy_stencil {
mask |= glow::STENCIL_BUFFER_BIT;
}

unsafe {
self.gl
.bind_framebuffer(glow::READ_FRAMEBUFFER, source.id());
self.gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, dest.id());
self.gl.blit_framebuffer(
src_x0,
src_y0,
src_x1,
src_y1,
dst_x0,
dst_y0,
dst_x1,
dst_y1,
mask,
glow::NEAREST,
);
}
}

pub(crate) fn set_scissor_box(&self, scissor_box: &ScissorBox) {
unsafe {
self.gl.scissor(
Expand Down Expand Up @@ -1234,4 +1165,19 @@ impl GraphicsServer for GlGraphicsServer {
}
}
}

fn set_polygon_fill_mode(&self, polygon_face: PolygonFace, polygon_fill_mode: PolygonFillMode) {
let mut state = self.state.borrow_mut();
if state.polygon_fill_mode != polygon_fill_mode || state.polygon_face != polygon_face {
state.polygon_fill_mode = polygon_fill_mode;
state.polygon_face = polygon_face;

unsafe {
self.gl.polygon_mode(
state.polygon_face.into_gl(),
state.polygon_fill_mode.into_gl(),
)
}
}
}
}
4 changes: 4 additions & 0 deletions fyrox-graphics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
query::Query,
read_buffer::AsyncReadBuffer,
stats::PipelineStatistics,
PolygonFace, PolygonFillMode,
};
use std::{
any::Any,
Expand Down Expand Up @@ -57,6 +58,8 @@ impl Display for ServerCapabilities {
}
}

pub type SharedGraphicsServer = Rc<dyn GraphicsServer>;

pub trait GraphicsServer: Any {
fn create_buffer(
&self,
Expand Down Expand Up @@ -112,4 +115,5 @@ pub trait GraphicsServer: Any {
fn swap_buffers(&self) -> Result<(), FrameworkError>;
fn set_frame_size(&self, new_size: (u32, u32));
fn capabilities(&self) -> ServerCapabilities;
fn set_polygon_fill_mode(&self, polygon_face: PolygonFace, polygon_fill_mode: PolygonFillMode);
}
Loading

0 comments on commit 5f010cf

Please sign in to comment.