Skip to content

Commit

Permalink
extracted rendering framework into a separate crate
Browse files Browse the repository at this point in the history
- first pass of support for other GAPIs
  • Loading branch information
mrDIMAS committed Sep 13, 2024
1 parent e64e86a commit 5de662f
Show file tree
Hide file tree
Showing 27 changed files with 378 additions and 279 deletions.
1 change: 1 addition & 0 deletions .idea/rg3d-core.iml

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

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ members = [
"fyrox-resource",
"fyrox-scripts",
"fyrox-animation",
"editor",
"editor",
"editor-standalone",
"template-core",
"template",
"template-core",
"template",
"fyrox-graph",
"fyrox-math",
"fyrox-dylib",
"fyrox",
"fyrox-impl"
, "project-manager"]
"fyrox-impl",
"project-manager",
"fyrox-gpu-hal"
]
resolver = "2"

[profile.dev]
Expand Down
1 change: 1 addition & 0 deletions editor/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::{
},
Editor,
};
use fyrox::renderer::framework::GeometryBufferExt;
use std::{any::TypeId, cell::RefCell, rc::Rc};

struct EdgeDetectShader {
Expand Down
1 change: 1 addition & 0 deletions editor/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::{
},
Editor,
};
use fyrox::renderer::framework::GeometryBufferExt;
use std::{any::TypeId, cell::RefCell, rc::Rc};

struct OverlayShader {
Expand Down
13 changes: 13 additions & 0 deletions fyrox-gpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "fyrox-gpu-hal"
version = "0.1.0"
edition = "2021"

[dependencies]
fyrox-core = { path = "../fyrox-core", version = "0.28.1" }
fxhash = "0.2.1"
glow = "0.14.0"
bytemuck = "1.16.1"
serde = "1"
strum = "0.26.3"
strum_macros = "0.26.4"
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ impl From<NulError> for FrameworkError {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<glutin::error::Error> for FrameworkError {
fn from(err: glutin::error::Error) -> Self {
Self::Custom(format!("{:?}", err))
}
}

impl From<String> for FrameworkError {
fn from(v: String) -> Self {
Self::Custom(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

use crate::{
core::{color::Color, math::Rect, reflect::prelude::*, scope_profile, visitor::prelude::*},
renderer::framework::{
error::FrameworkError,
geometry_buffer::{DrawCallStatistics, ElementRange, GeometryBuffer},
gpu_program::{GpuProgram, GpuProgramBinding},
gpu_texture::{CubeMapFace, GpuTexture, GpuTextureKind, PixelElementKind},
state::{BlendEquation, BlendFunc, ColorMask, PipelineState, StencilFunc, StencilOp},
},
error::FrameworkError,
geometry_buffer::{DrawCallStatistics, ElementRange, GeometryBuffer},
gpu_program::{GpuProgram, GpuProgramBinding},
gpu_texture::{CubeMapFace, GpuTexture, GpuTextureKind, PixelElementKind},
state::{BlendEquation, BlendFunc, ColorMask, PipelineState, StencilFunc, StencilOp},
};
use glow::HasContext;
use serde::{Deserialize, Serialize};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::scene::mesh::surface::SurfaceData;
use crate::{
core::array_as_u8_slice,
core::{math::TriangleDefinition, scope_profile},
renderer::framework::{error::FrameworkError, state::PipelineState},
scene::mesh::buffer::{VertexAttributeDataType, VertexBuffer},
error::FrameworkError,
state::PipelineState,
};
use glow::HasContext;
use std::rc::Weak;
use std::{cell::Cell, marker::PhantomData, mem::size_of};
use std::{cell::Cell, marker::PhantomData, mem::size_of, rc::Weak};

struct NativeBuffer {
state: Weak<PipelineState>,
Expand Down Expand Up @@ -313,22 +311,6 @@ impl<'a> GeometryBufferBinding<'a> {
}

impl GeometryBuffer {
pub fn from_surface_data(
data: &SurfaceData,
kind: GeometryBufferKind,
state: &PipelineState,
) -> Result<Self, FrameworkError> {
let geometry_buffer = GeometryBufferBuilder::new(ElementKind::Triangle)
.with_buffer_builder(BufferBuilder::from_vertex_buffer(&data.vertex_buffer, kind))
.build(state)?;

geometry_buffer
.bind(state)
.set_triangles(data.geometry_buffer.triangles_ref());

Ok(geometry_buffer)
}

pub fn set_buffer_data<T: bytemuck::Pod>(
&mut self,
state: &PipelineState,
Expand Down Expand Up @@ -399,11 +381,11 @@ impl Drop for GeometryBuffer {
}

pub struct BufferBuilder {
element_size: usize,
kind: GeometryBufferKind,
attributes: Vec<AttributeDefinition>,
data: *const u8,
data_size: usize,
pub element_size: usize,
pub kind: GeometryBufferKind,
pub attributes: Vec<AttributeDefinition>,
pub data: *const u8,
pub data_size: usize,
}

impl BufferBuilder {
Expand All @@ -423,43 +405,6 @@ impl BufferBuilder {
}
}

pub fn from_vertex_buffer(buffer: &VertexBuffer, kind: GeometryBufferKind) -> Self {
Self {
element_size: buffer.vertex_size() as usize,
kind,
attributes: buffer
.layout()
.iter()
.map(|a| AttributeDefinition {
location: a.shader_location as u32,
kind: match (a.data_type, a.size) {
(VertexAttributeDataType::F32, 1) => AttributeKind::Float,
(VertexAttributeDataType::F32, 2) => AttributeKind::Float2,
(VertexAttributeDataType::F32, 3) => AttributeKind::Float3,
(VertexAttributeDataType::F32, 4) => AttributeKind::Float4,
(VertexAttributeDataType::U32, 1) => AttributeKind::UnsignedInt,
(VertexAttributeDataType::U32, 2) => AttributeKind::UnsignedInt2,
(VertexAttributeDataType::U32, 3) => AttributeKind::UnsignedInt3,
(VertexAttributeDataType::U32, 4) => AttributeKind::UnsignedInt4,
(VertexAttributeDataType::U16, 1) => AttributeKind::UnsignedShort,
(VertexAttributeDataType::U16, 2) => AttributeKind::UnsignedShort2,
(VertexAttributeDataType::U16, 3) => AttributeKind::UnsignedShort3,
(VertexAttributeDataType::U16, 4) => AttributeKind::UnsignedShort4,
(VertexAttributeDataType::U8, 1) => AttributeKind::UnsignedByte,
(VertexAttributeDataType::U8, 2) => AttributeKind::UnsignedByte2,
(VertexAttributeDataType::U8, 3) => AttributeKind::UnsignedByte3,
(VertexAttributeDataType::U8, 4) => AttributeKind::UnsignedByte4,
_ => unreachable!(),
},
normalized: a.normalized,
divisor: a.divisor as u32,
})
.collect(),
data: buffer.raw_data().as_ptr(),
data_size: buffer.raw_data().len(),
}
}

pub fn with_attribute(mut self, attribute: AttributeDefinition) -> Self {
self.attributes.push(attribute);
self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::renderer::framework::state::GlKind;
use crate::state::GlKind;
use crate::{
core::{
algebra::{Matrix2, Matrix3, Matrix4, Vector2, Vector3, Vector4},
color::Color,
log::{Log, MessageKind},
sstorage::ImmutableString,
},
renderer::framework::{error::FrameworkError, gpu_texture::GpuTexture, state::PipelineState},
error::FrameworkError,
gpu_texture::GpuTexture,
state::PipelineState,
};
use fxhash::FxHashMap;
use glow::HasContext;
Expand All @@ -39,8 +41,7 @@ pub struct GpuProgram {
// Force compiler to not implement Send and Sync, because OpenGL is not thread-safe.
thread_mark: PhantomData<*const u8>,
uniform_locations: RefCell<FxHashMap<ImmutableString, Option<UniformLocation>>>,
pub(crate) built_in_uniform_locations:
[Option<UniformLocation>; BuiltInUniform::Count as usize],
pub built_in_uniform_locations: [Option<UniformLocation>; BuiltInUniform::Count as usize],
}

#[repr(usize)]
Expand Down Expand Up @@ -146,7 +147,7 @@ fn prepare_source_code(code: &str, gl_kind: GlKind) -> String {
pub struct GpuProgramBinding<'a, 'b> {
pub state: &'a PipelineState,
active_sampler: u32,
pub(crate) program: &'b GpuProgram,
pub program: &'b GpuProgram,
}

impl<'a, 'b> GpuProgramBinding<'a, 'b> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{
core::color::Color,
renderer::framework::{error::FrameworkError, state::PipelineState},
resource::texture::{
TextureKind, TextureMagnificationFilter, TextureMinificationFilter, TexturePixelKind,
TextureWrapMode,
},
};
use crate::{core::color::Color, error::FrameworkError, state::PipelineState};
use bytemuck::Pod;
use glow::{HasContext, PixelPackData, COMPRESSED_RED_RGTC1, COMPRESSED_RG_RGTC2};
use std::marker::PhantomData;
Expand All @@ -51,33 +44,6 @@ pub enum GpuTextureKind {
},
}

impl From<TextureKind> for GpuTextureKind {
fn from(v: TextureKind) -> Self {
match v {
TextureKind::Line { length } => GpuTextureKind::Line {
length: length as usize,
},
TextureKind::Rectangle { width, height } => GpuTextureKind::Rectangle {
width: width as usize,
height: height as usize,
},
TextureKind::Cube { width, height } => GpuTextureKind::Cube {
width: width as usize,
height: height as usize,
},
TextureKind::Volume {
width,
height,
depth,
} => GpuTextureKind::Volume {
width: width as usize,
height: height as usize,
depth: depth as usize,
},
}
}
}

impl GpuTextureKind {
fn gl_texture_target(&self) -> u32 {
match self {
Expand Down Expand Up @@ -128,38 +94,6 @@ pub enum PixelKind {
RGB10A2,
}

impl From<TexturePixelKind> for PixelKind {
fn from(texture_kind: TexturePixelKind) -> Self {
match texture_kind {
TexturePixelKind::R8 => Self::R8,
TexturePixelKind::RGB8 => Self::RGB8,
TexturePixelKind::RGBA8 => Self::RGBA8,
TexturePixelKind::RG8 => Self::RG8,
TexturePixelKind::R16 => Self::R16,
TexturePixelKind::RG16 => Self::RG16,
TexturePixelKind::BGR8 => Self::BGR8,
TexturePixelKind::BGRA8 => Self::BGRA8,
TexturePixelKind::RGB16 => Self::RGB16,
TexturePixelKind::RGBA16 => Self::RGBA16,
TexturePixelKind::RGB16F => Self::RGB16F,
TexturePixelKind::DXT1RGB => Self::DXT1RGB,
TexturePixelKind::DXT1RGBA => Self::DXT1RGBA,
TexturePixelKind::DXT3RGBA => Self::DXT3RGBA,
TexturePixelKind::DXT5RGBA => Self::DXT5RGBA,
TexturePixelKind::R8RGTC => Self::R8RGTC,
TexturePixelKind::RG8RGTC => Self::RG8RGTC,
TexturePixelKind::RGB32F => Self::RGB32F,
TexturePixelKind::RGBA32F => Self::RGBA32F,
TexturePixelKind::Luminance8 => Self::L8,
TexturePixelKind::LuminanceAlpha8 => Self::LA8,
TexturePixelKind::Luminance16 => Self::L16,
TexturePixelKind::LuminanceAlpha16 => Self::LA16,
TexturePixelKind::R32F => Self::R32F,
TexturePixelKind::R16F => Self::R16F,
}
}
}

pub enum PixelElementKind {
Float,
NormalizedUnsignedInteger,
Expand Down Expand Up @@ -549,15 +483,6 @@ impl MagnificationFilter {
}
}

impl From<TextureMagnificationFilter> for MagnificationFilter {
fn from(v: TextureMagnificationFilter) -> Self {
match v {
TextureMagnificationFilter::Nearest => Self::Nearest,
TextureMagnificationFilter::Linear => Self::Linear,
}
}
}

#[derive(Copy, Clone, PartialOrd, PartialEq, Eq, Hash, Debug)]
#[repr(u32)]
pub enum MinificationFilter {
Expand All @@ -569,18 +494,6 @@ pub enum MinificationFilter {
LinearMipMapLinear = glow::LINEAR_MIPMAP_LINEAR,
}

impl From<TextureMinificationFilter> for MinificationFilter {
fn from(v: TextureMinificationFilter) -> Self {
match v {
TextureMinificationFilter::Nearest => Self::Nearest,
TextureMinificationFilter::NearestMipMapNearest => Self::NearestMipMapNearest,
TextureMinificationFilter::NearestMipMapLinear => Self::NearestMipMapLinear,
TextureMinificationFilter::Linear => Self::Linear,
TextureMinificationFilter::LinearMipMapNearest => Self::LinearMipMapNearest,
TextureMinificationFilter::LinearMipMapLinear => Self::LinearMipMapLinear,
}
}
}
impl MinificationFilter {
pub fn into_gl_value(self) -> i32 {
self as i32
Expand All @@ -603,18 +516,6 @@ impl WrapMode {
}
}

impl From<TextureWrapMode> for WrapMode {
fn from(v: TextureWrapMode) -> Self {
match v {
TextureWrapMode::Repeat => WrapMode::Repeat,
TextureWrapMode::ClampToEdge => WrapMode::ClampToEdge,
TextureWrapMode::ClampToBorder => WrapMode::ClampToBorder,
TextureWrapMode::MirroredRepeat => WrapMode::MirroredRepeat,
TextureWrapMode::MirrorClampToEdge => WrapMode::MirrorClampToEdge,
}
}
}

#[derive(Copy, Clone)]
#[repr(u32)]
pub enum Coordinate {
Expand Down
Loading

0 comments on commit 5de662f

Please sign in to comment.