Skip to content

Commit

Permalink
GraphicsServer::create_2d_render_target + reduced code bloat
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Oct 24, 2024
1 parent c592116 commit f5535b8
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 318 deletions.
35 changes: 5 additions & 30 deletions editor/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ use crate::{
buffer::BufferUsage,
error::FrameworkError,
framebuffer::{
Attachment, AttachmentKind, FrameBuffer, ResourceBindGroup, ResourceBinding,
Attachment, AttachmentKind, BufferLocation, FrameBuffer, ResourceBindGroup,
ResourceBinding,
},
geometry_buffer::GeometryBuffer,
gpu_program::{GpuProgram, UniformLocation},
gpu_texture::{
GpuTextureKind, MagnificationFilter, MinificationFilter, PixelKind, WrapMode,
},
gpu_texture::PixelKind,
server::GraphicsServer,
uniform::StaticUniformBuffer,
BlendFactor, BlendFunc, BlendParameters, CompareFunc, DrawParameters, ElementRange,
Expand All @@ -53,8 +52,6 @@ use crate::{
},
Editor,
};
use fyrox::renderer::framework::framebuffer::BufferLocation;
use fyrox::renderer::framework::gpu_texture::GpuTextureDescriptor;
use std::{any::TypeId, cell::RefCell, rc::Rc};

struct EdgeDetectShader {
Expand Down Expand Up @@ -143,33 +140,11 @@ impl HighlightRenderPass {
height: usize,
) -> Box<dyn FrameBuffer> {
let depth_stencil = server
.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind: PixelKind::D24S8,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})
.create_2d_render_target(PixelKind::D24S8, width, height)
.unwrap();

let frame_texture = server
.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind: PixelKind::RGBA8,
min_filter: MinificationFilter::Linear,
mag_filter: MagnificationFilter::Linear,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})
.create_2d_render_target(PixelKind::RGBA8, width, height)
.unwrap();

server
Expand Down
22 changes: 22 additions & 0 deletions fyrox-graphics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::gpu_texture::{
GpuTextureKind, MagnificationFilter, MinificationFilter, PixelKind, WrapMode,
};
use crate::{
buffer::{Buffer, BufferKind, BufferUsage},
error::FrameworkError,
Expand Down Expand Up @@ -111,4 +114,23 @@ pub trait GraphicsServer: Any {
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);
fn create_2d_render_target(
&self,
pixel_kind: PixelKind,
width: usize,
height: usize,
) -> Result<Rc<RefCell<dyn GpuTexture>>, FrameworkError> {
self.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})
}
}
18 changes: 2 additions & 16 deletions fyrox-impl/src/renderer/bloom/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ use crate::{
},
geometry_buffer::GeometryBuffer,
gpu_program::{GpuProgram, UniformLocation},
gpu_texture::{
GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind, WrapMode,
},
gpu_texture::{GpuTexture, PixelKind},
server::GraphicsServer,
uniform::StaticUniformBuffer,
DrawParameters, ElementRange,
Expand Down Expand Up @@ -79,18 +76,7 @@ fn create_framebuffer(
height: usize,
pixel_kind: PixelKind,
) -> Result<Box<dyn FrameBuffer>, FrameworkError> {
let frame = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})?;
let frame = server.create_2d_render_target(pixel_kind, width, height)?;

server.create_frame_buffer(
None,
Expand Down
18 changes: 2 additions & 16 deletions fyrox-impl/src/renderer/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use crate::{
},
geometry_buffer::GeometryBuffer,
gpu_program::{GpuProgram, UniformLocation},
gpu_texture::{
GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind, WrapMode,
},
gpu_texture::{GpuTexture, PixelKind},
server::GraphicsServer,
uniform::StaticUniformBuffer,
DrawParameters, ElementRange,
Expand Down Expand Up @@ -81,18 +78,7 @@ impl BloomRenderer {
width: usize,
height: usize,
) -> Result<Self, FrameworkError> {
let frame = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind: PixelKind::RGBA16F,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})?;
let frame = server.create_2d_render_target(PixelKind::RGBA16F, width, height)?;

Ok(Self {
shader: Shader::new(server)?,
Expand Down
37 changes: 7 additions & 30 deletions fyrox-impl/src/renderer/gbuffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ use crate::{
ResourceBinding,
},
geometry_buffer::GeometryBuffer,
gpu_texture::{
GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind, WrapMode,
},
gpu_texture::{GpuTexture, PixelKind},
server::GraphicsServer,
uniform::StaticUniformBuffer,
BlendFactor, BlendFunc, BlendParameters, DrawParameters, ElementRange,
Expand Down Expand Up @@ -110,32 +107,12 @@ impl GBuffer {
width: usize,
height: usize,
) -> Result<Self, FrameworkError> {
fn create_rt(
server: &dyn GraphicsServer,
pixel_kind: PixelKind,
width: usize,
height: usize,
) -> Result<Rc<RefCell<dyn GpuTexture>>, FrameworkError> {
server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})
}

let diffuse_texture = create_rt(server, PixelKind::RGBA8, width, height)?;
let normal_texture = create_rt(server, PixelKind::RGBA8, width, height)?;
let diffuse_texture = server.create_2d_render_target(PixelKind::RGBA8, width, height)?;
let normal_texture = server.create_2d_render_target(PixelKind::RGBA8, width, height)?;
let framebuffer = server.create_frame_buffer(
Some(Attachment {
kind: AttachmentKind::DepthStencil,
texture: create_rt(server, PixelKind::D24S8, width, height)?,
texture: server.create_2d_render_target(PixelKind::D24S8, width, height)?,
}),
vec![
Attachment {
Expand All @@ -148,15 +125,15 @@ impl GBuffer {
},
Attachment {
kind: AttachmentKind::Color,
texture: create_rt(server, PixelKind::RGBA16F, width, height)?,
texture: server.create_2d_render_target(PixelKind::RGBA16F, width, height)?,
},
Attachment {
kind: AttachmentKind::Color,
texture: create_rt(server, PixelKind::RGBA8, width, height)?,
texture: server.create_2d_render_target(PixelKind::RGBA8, width, height)?,
},
Attachment {
kind: AttachmentKind::Color,
texture: create_rt(server, PixelKind::R8UI, width, height)?,
texture: server.create_2d_render_target(PixelKind::R8UI, width, height)?,
},
],
)?;
Expand Down
16 changes: 1 addition & 15 deletions fyrox-impl/src/renderer/hdr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,7 @@ pub struct LumBuffer {

impl LumBuffer {
fn new(server: &dyn GraphicsServer, size: usize) -> Result<Self, FrameworkError> {
let texture = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle {
width: size,
height: size,
},
pixel_kind: PixelKind::R32F,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})?;
let texture = server.create_2d_render_target(PixelKind::R32F, size, size)?;
Ok(Self {
framebuffer: server.create_frame_buffer(
None,
Expand Down
69 changes: 17 additions & 52 deletions fyrox-impl/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ mod skybox_shader;
mod ssao;
mod stats;

use crate::material::shader::ShaderDefinition;
use crate::{
asset::{event::ResourceEvent, manager::ResourceManager},
core::{
Expand All @@ -69,7 +68,7 @@ use crate::{
engine::{error::EngineError, GraphicsContextParams},
graph::SceneGraph,
gui::draw::DrawingContext,
material::shader::{Shader, ShaderResource, ShaderResourceExtension},
material::shader::{Shader, ShaderDefinition, ShaderResource, ShaderResourceExtension},
renderer::{
bloom::BloomRenderer,
bundle::{ObserverInfo, RenderDataBundleStorage, RenderDataBundleStorageOptions},
Expand All @@ -84,14 +83,17 @@ use crate::{
buffer::{Buffer, BufferKind, BufferUsage},
error::FrameworkError,
framebuffer::{
Attachment, AttachmentKind, FrameBuffer, ResourceBindGroup, ResourceBinding,
Attachment, AttachmentKind, BufferLocation, FrameBuffer, ResourceBindGroup,
ResourceBinding,
},
geometry_buffer::{DrawCallStatistics, GeometryBuffer},
gl::server::GlGraphicsServer,
gpu_program::SamplerFallback,
gpu_texture::{
GpuTexture, GpuTextureKind, MagnificationFilter, MinificationFilter, PixelKind,
WrapMode,
GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind,
},
server::GraphicsServer,
server::{GraphicsServer, SharedGraphicsServer},
uniform::StaticUniformBuffer,
DrawParameters, ElementRange, GeometryBufferExt, PolygonFace, PolygonFillMode,
},
Expand All @@ -106,11 +108,6 @@ use crate::{
scene::{camera::Camera, mesh::surface::SurfaceData, Scene, SceneContainer},
};
use fxhash::FxHashMap;
use fyrox_graphics::gpu_texture::GpuTextureDescriptor;
use fyrox_graphics::{
framebuffer::BufferLocation, gl::server::GlGraphicsServer, gpu_program::SamplerFallback,
server::SharedGraphicsServer,
};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
pub use stats::*;
Expand Down Expand Up @@ -504,32 +501,10 @@ impl AssociatedSceneData {
width: usize,
height: usize,
) -> Result<Self, FrameworkError> {
let depth_stencil = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
pixel_kind: PixelKind::D24S8,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
})?;

let hdr_frame_texture = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle { width, height },
// Intermediate scene frame will be rendered in HDR render target.
pixel_kind: PixelKind::RGBA16F,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: Default::default(),
t_wrap_mode: Default::default(),
r_wrap_mode: Default::default(),
anisotropy: 1.0,
data: None,
})?;
let depth_stencil = server.create_2d_render_target(PixelKind::D24S8, width, height)?;
// Intermediate scene frame will be rendered in HDR render target.
let hdr_frame_texture =
server.create_2d_render_target(PixelKind::RGBA16F, width, height)?;

let hdr_scene_framebuffer = server.create_frame_buffer(
Some(Attachment {
Expand Down Expand Up @@ -758,21 +733,11 @@ fn make_ui_frame_buffer(
data: None,
})?;

let depth_stencil = server.create_texture(GpuTextureDescriptor {
kind: GpuTextureKind::Rectangle {
width: frame_size.x as usize,
height: frame_size.y as usize,
},
pixel_kind: PixelKind::D24S8,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: Default::default(),
t_wrap_mode: Default::default(),
r_wrap_mode: Default::default(),
anisotropy: 1.0,
data: None,
})?;
let depth_stencil = server.create_2d_render_target(
PixelKind::D24S8,
frame_size.x as usize,
frame_size.y as usize,
)?;

server.create_frame_buffer(
Some(Attachment {
Expand Down
Loading

0 comments on commit f5535b8

Please sign in to comment.