moved opengl-specific code to fyrox-graphics-gl

This commit is contained in:
Dmitry Stepanov
2025-05-25 16:32:36 +03:00
parent 8cab60fbbc
commit 82e53ba5d7
21 changed files with 133 additions and 111 deletions
+1
View File
@@ -49,6 +49,7 @@
<sourceFolder url="file://$MODULE_DIR$/fyrox-resource/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/fyrox-material/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/fyrox-shader/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/fyrox-graphics-gl/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
<excludeFolder url="file://$MODULE_DIR$/examples/wasm/target" />
</content>
+1 -1
View File
@@ -22,7 +22,7 @@ members = [
"fyrox-texture",
"fyrox-autotile",
"fyrox-material"
]
, "fyrox-graphics-gl"]
resolver = "2"
[profile.dev]
+29
View File
@@ -0,0 +1,29 @@
[package]
name = "fyrox-graphics-gl"
version = "0.36.0"
edition = "2021"
license = "MIT"
description = "OpenGL-based graphics server for Fyrox Game Engine"
keywords = ["graphics", "gapi"]
categories = ["graphics", "rendering::graphics-api"]
include = ["/src/**/*", "/Cargo.toml", "/LICENSE", "/README.md"]
homepage = "https://fyrox.rs"
documentation = "https://docs.rs/fyrox-graphics-gl"
repository = "https://github.com/FyroxEngine/Fyrox"
rust-version = "1.86"
[dependencies]
winit = { version = "0.29.2", features = ["serde"] }
fyrox-graphics = { version = "0.36", path = "../fyrox-graphics" }
fyrox-core = { version = "0.36", path = "../fyrox-core" }
fxhash = "0.2.1"
regex = "1"
glow = "0.16"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
glutin = "0.31"
glutin-winit = "0.4.2"
raw-window-handle = "0.5.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
serde-wasm-bindgen = "0.6.3"
@@ -18,9 +18,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::gl::server::GlGraphicsServer;
use crate::gl::ToGlConstant;
use crate::{
use crate::ToGlConstant;
use crate::server::GlGraphicsServer;
use fyrox_graphics::{
buffer::{BufferKind, BufferUsage, GpuBufferTrait},
error::FrameworkError,
};
@@ -18,7 +18,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::{
use crate::texture::PixelDescriptor;
pub use crate::{
ToGlConstant, buffer::GlBuffer, geometry_buffer::GlGeometryBuffer, program::GlProgram,
sampler::GlSampler, server::GlGraphicsServer, texture::GlTexture,
};
use fyrox_graphics::{
ColorMask, DrawParameters, ElementRange,
buffer::GpuBufferTrait,
core::{color::Color, math::Rect},
error::FrameworkError,
@@ -28,15 +34,9 @@ use crate::{
GpuFrameBufferTrait, ResourceBindGroup, ResourceBinding,
},
geometry_buffer::GpuGeometryBuffer,
gl::sampler::GlSampler,
gl::{
buffer::GlBuffer, geometry_buffer::GlGeometryBuffer, program::GlProgram,
server::GlGraphicsServer, texture::GlTexture, ToGlConstant,
},
gpu_program::GpuProgram,
gpu_texture::image_2d_size_bytes,
gpu_texture::{CubeMapFace, GpuTextureKind, GpuTextureTrait, PixelElementKind},
ColorMask, DrawParameters, ElementRange,
};
use glow::{HasContext, PixelPackData};
use std::rc::Weak;
@@ -274,7 +274,7 @@ impl GpuFrameBufferTrait for GlFrameBuffer {
if let GpuTextureKind::Rectangle { width, height } = texture.kind() {
let pixel_kind = texture.pixel_kind();
let pixel_info = pixel_kind.pixel_descriptor();
let pixel_info = PixelDescriptor::from(pixel_kind);
let mut buffer = vec![0; image_2d_size_bytes(pixel_kind, width, height)];
unsafe {
server.gl.read_pixels(
@@ -18,21 +18,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::{
use crate::{ToGlConstant, buffer::GlBuffer, server::GlGraphicsServer};
use fyrox_graphics::{
ElementKind,
buffer::{BufferKind, GpuBufferTrait},
core::{array_as_u8_slice, math::TriangleDefinition},
error::FrameworkError,
geometry_buffer::{
AttributeKind, ElementsDescriptor, GeometryBufferDescriptor, GpuGeometryBufferTrait,
},
gl::{buffer::GlBuffer, server::GlGraphicsServer, ToGlConstant},
ElementKind,
};
use glow::HasContext;
use std::{cell::Cell, marker::PhantomData, rc::Weak};
impl AttributeKind {
fn gl_type(self) -> u32 {
impl ToGlConstant for AttributeKind {
fn into_gl(self) -> u32 {
match self {
AttributeKind::Float => glow::FLOAT,
AttributeKind::UnsignedByte => glow::UNSIGNED_BYTE,
@@ -94,7 +94,7 @@ impl GlGeometryBuffer {
server.gl.vertex_attrib_pointer_f32(
definition.location,
definition.component_count as i32,
definition.kind.gl_type(),
definition.kind.into_gl(),
definition.normalized,
buffer.data.element_size as i32,
offset as i32,
@@ -19,12 +19,12 @@
// SOFTWARE.
use crate::{
ToGlConstant,
server::{GlGraphicsServer, GlKind},
};
use fyrox_graphics::{
core::log::{Log, MessageKind},
error::FrameworkError,
gl::{
server::{GlGraphicsServer, GlKind},
ToGlConstant,
},
gpu_program::{
GpuProgramTrait, GpuShaderTrait, SamplerKind, ShaderKind, ShaderPropertyKind,
ShaderResourceDefinition, ShaderResourceKind,
@@ -33,18 +33,16 @@ use crate::{
use glow::HasContext;
use std::{marker::PhantomData, ops::Range, rc::Weak};
impl SamplerKind {
pub fn glsl_name(&self) -> &str {
match self {
SamplerKind::Sampler1D => "sampler1D",
SamplerKind::Sampler2D => "sampler2D",
SamplerKind::Sampler3D => "sampler3D",
SamplerKind::SamplerCube => "samplerCube",
SamplerKind::USampler1D => "usampler1D",
SamplerKind::USampler2D => "usampler2D",
SamplerKind::USampler3D => "usampler3D",
SamplerKind::USamplerCube => "usamplerCube",
}
fn glsl_sampler_name(sampler: SamplerKind) -> &'static str {
match sampler {
SamplerKind::Sampler1D => "sampler1D",
SamplerKind::Sampler2D => "sampler2D",
SamplerKind::Sampler3D => "sampler3D",
SamplerKind::SamplerCube => "samplerCube",
SamplerKind::USampler1D => "usampler1D",
SamplerKind::USampler2D => "usampler2D",
SamplerKind::USampler3D => "usampler3D",
SamplerKind::USamplerCube => "usamplerCube",
}
}
@@ -187,7 +185,7 @@ impl GlShader {
let resource_name = &property.name;
match property.kind {
ShaderResourceKind::Texture { kind, .. } => {
let glsl_name = kind.glsl_name();
let glsl_name = glsl_sampler_name(kind);
texture_bindings += &format!("uniform {glsl_name} {resource_name};\n");
}
ShaderResourceKind::PropertyGroup(ref fields) => {
@@ -264,7 +262,9 @@ impl GlShader {
}
}
block += "};\n";
block += &format!("layout(std140) uniform U{resource_name} {{ T{resource_name} {resource_name}; }};\n");
block += &format!(
"layout(std140) uniform U{resource_name} {{ T{resource_name} {resource_name}; }};\n"
);
source.insert_str(0, &block);
line_offset -= count_lines(&block);
}
@@ -304,7 +304,9 @@ impl GlShader {
{
format!("Shader {name} compiled successfully!")
} else {
format!("Shader {name} compiled successfully!\nAdditional info: {compilation_message}")
format!(
"Shader {name} compiled successfully!\nAdditional info: {compilation_message}"
)
};
Log::writeln(MessageKind::Information, msg);
@@ -497,7 +499,7 @@ impl Drop for GlProgram {
#[cfg(test)]
mod test {
use crate::gl::program::{patch_error_message, Vendor};
use crate::program::{Vendor, patch_error_message};
#[test]
fn test_line_correction() {
@@ -18,9 +18,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::{
use crate::server::GlGraphicsServer;
use fyrox_graphics::{
error::FrameworkError,
gl::server::GlGraphicsServer,
query::{GpuQueryTrait, QueryKind, QueryResult},
};
use glow::HasContext;
@@ -18,13 +18,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::{
use crate::texture::PixelDescriptor;
use crate::{ToGlConstant, buffer::GlBuffer, framebuffer::GlFrameBuffer, server::GlGraphicsServer};
use fyrox_graphics::{
buffer::{BufferKind, BufferUsage, GpuBufferTrait},
core::{algebra::Vector2, math::Rect},
error::FrameworkError,
framebuffer::GpuFrameBufferTrait,
gl::{buffer::GlBuffer, framebuffer::GlFrameBuffer, server::GlGraphicsServer, ToGlConstant},
gpu_texture::{image_2d_size_bytes, GpuTextureKind},
gpu_texture::{GpuTextureKind, image_2d_size_bytes},
read_buffer::GpuAsyncReadBufferTrait,
};
use glow::{HasContext, PixelPackData};
@@ -97,7 +98,7 @@ impl GpuAsyncReadBufferTrait for GlAsyncReadBuffer {
})?
.texture;
let attachment_pixel_descriptor = color_attachment.pixel_kind().pixel_descriptor();
let attachment_pixel_descriptor = PixelDescriptor::from(color_attachment.pixel_kind());
let color_attachment_size =
if let GpuTextureKind::Rectangle { width, height } = color_attachment.kind() {
@@ -18,11 +18,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::sampler::{Coordinate, MagnificationFilter, MinificationFilter, WrapMode};
use crate::{
use crate::{ToGlConstant, server::GlGraphicsServer};
use fyrox_graphics::{
error::FrameworkError,
gl::{server::GlGraphicsServer, ToGlConstant},
sampler::{GpuSamplerDescriptor, GpuSamplerTrait},
sampler::{
Coordinate, GpuSamplerDescriptor, GpuSamplerTrait, MagnificationFilter, MinificationFilter,
WrapMode,
},
};
use glow::HasContext;
use std::rc::Weak;
@@ -18,23 +18,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::buffer::GlBuffer;
use crate::{
framebuffer::GlFrameBuffer,
geometry_buffer::GlGeometryBuffer,
program::{GlProgram, GlShader},
query::GlQuery,
read_buffer::GlAsyncReadBuffer,
sampler::GlSampler,
texture::GlTexture,
ToGlConstant,
};
use fyrox_graphics::{
buffer::{BufferKind, BufferUsage, GpuBuffer},
core::{color::Color, log::Log, math::Rect},
error::FrameworkError,
framebuffer::{Attachment, GpuFrameBuffer},
geometry_buffer::{GeometryBufferDescriptor, GpuGeometryBuffer},
gl::{
self,
framebuffer::GlFrameBuffer,
geometry_buffer::GlGeometryBuffer,
program::{GlProgram, GlShader},
query::GlQuery,
read_buffer::GlAsyncReadBuffer,
sampler::GlSampler,
texture::GlTexture,
ToGlConstant,
},
gpu_program::{GpuProgram, GpuShader, ShaderKind, ShaderResourceDefinition},
gpu_texture::{GpuTexture, GpuTextureDescriptor},
query::GpuQuery,
@@ -367,7 +367,8 @@ impl GlGraphicsServer {
let gl_surface = gl_config
.display()
.create_window_surface(&gl_config, &attrs)?;
.create_window_surface(&gl_config, &attrs)
.map_err(|err| FrameworkError::Custom(format!("{err:?}")))?;
let (non_current_gl_context, gl_kind) = if let Ok(gl3_3_core_context) =
gl_display.create_context(&gl_config, &gl3_3_core_context_attributes)
@@ -375,12 +376,16 @@ impl GlGraphicsServer {
(gl3_3_core_context, GlKind::OpenGL)
} else {
(
gl_display.create_context(&gl_config, &gles3_context_attributes)?,
gl_display
.create_context(&gl_config, &gles3_context_attributes)
.map_err(|err| FrameworkError::Custom(format!("{err:?}")))?,
GlKind::OpenGLES,
)
};
let gl_context = non_current_gl_context.make_current(&gl_surface)?;
let gl_context = non_current_gl_context
.make_current(&gl_surface)
.map_err(|err| FrameworkError::Custom(format!("{err:?}")))?;
if vsync {
Log::verify(gl_surface.set_swap_interval(
@@ -499,7 +504,7 @@ impl GlGraphicsServer {
#[cfg(debug_assertions)]
{
use crate::core::log::{Log, MessageKind};
use fyrox_core::log::{Log, MessageKind};
if context.supported_extensions().contains("GL_KHR_debug") {
context.debug_message_callback(|source, msg_type, id, severity, message| {
@@ -1001,7 +1006,7 @@ impl GraphicsServer for GlGraphicsServer {
buffer_kind: BufferKind,
buffer_usage: BufferUsage,
) -> Result<GpuBuffer, FrameworkError> {
Ok(GpuBuffer(Rc::new(gl::buffer::GlBuffer::new(
Ok(GpuBuffer(Rc::new(GlBuffer::new(
self,
size,
buffer_kind,
@@ -1138,7 +1143,10 @@ impl GraphicsServer for GlGraphicsServer {
#[cfg(not(target_arch = "wasm32"))]
{
let state = self.state.borrow();
Ok(state.gl_surface.swap_buffers(&state.gl_context)?)
state
.gl_surface
.swap_buffers(&state.gl_context)
.map_err(|err| FrameworkError::Custom(format!("{err:?}")))
}
#[cfg(target_arch = "wasm32")]
@@ -18,23 +18,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::{
use crate::{ToGlConstant, server::GlGraphicsServer};
use fyrox_graphics::{
error::FrameworkError,
gl::{server::GlGraphicsServer, ToGlConstant},
gpu_texture::{
image_1d_size_bytes, image_2d_size_bytes, image_3d_size_bytes, CubeMapFace,
GpuTextureDescriptor, GpuTextureKind, GpuTextureTrait, PixelKind,
CubeMapFace, GpuTextureDescriptor, GpuTextureKind, GpuTextureTrait, PixelKind,
image_1d_size_bytes, image_2d_size_bytes, image_3d_size_bytes,
},
};
use glow::{HasContext, PixelUnpackData, COMPRESSED_RED_RGTC1, COMPRESSED_RG_RGTC2};
use glow::{COMPRESSED_RED_RGTC1, COMPRESSED_RG_RGTC2, HasContext, PixelUnpackData};
use std::cell::Cell;
use std::{
marker::PhantomData,
rc::{Rc, Weak},
};
impl GpuTextureKind {
pub fn gl_texture_target(&self) -> u32 {
impl ToGlConstant for GpuTextureKind {
fn into_gl(self) -> u32 {
match self {
Self::Line { .. } => glow::TEXTURE_1D,
Self::Rectangle { .. } => glow::TEXTURE_2D,
@@ -78,9 +78,9 @@ pub struct PixelDescriptor {
pub swizzle_mask: Option<[i32; 4]>,
}
impl PixelKind {
pub(crate) fn pixel_descriptor(self) -> PixelDescriptor {
let (data_type, format, internal_format, swizzle_mask) = match self {
impl From<PixelKind> for PixelDescriptor {
fn from(value: PixelKind) -> Self {
let (data_type, format, internal_format, swizzle_mask) = match value {
PixelKind::R32F => (glow::FLOAT, glow::RED, glow::R32F, None),
PixelKind::R32UI => (glow::UNSIGNED_INT, glow::RED_INTEGER, glow::R32UI, None),
PixelKind::R16F => (glow::FLOAT, glow::RED, glow::R16F, None),
@@ -198,7 +198,7 @@ impl TempBinding {
let unit = server
.free_texture_unit()
.expect("Texture units limit exceeded!");
let target = texture.kind.get().gl_texture_target();
let target = texture.kind.get().into_gl();
server.set_texture(unit, target, Some(texture.texture));
Self {
server,
@@ -284,11 +284,7 @@ impl GlTexture {
}
pub fn bind(&self, server: &GlGraphicsServer, sampler_index: u32) {
server.set_texture(
sampler_index,
self.kind.get().gl_texture_target(),
Some(self.texture),
);
server.set_texture(sampler_index, self.kind.get().into_gl(), Some(self.texture));
}
fn make_temp_binding(&self) -> TempBinding {
@@ -385,7 +381,7 @@ impl GpuTextureTrait for GlTexture {
let mut temp_binding = self.make_temp_binding();
temp_binding.set_max_level(mip_count.saturating_sub(1));
let target = kind.gl_texture_target();
let target = kind.into_gl();
unsafe {
let PixelDescriptor {
@@ -393,7 +389,7 @@ impl GpuTextureTrait for GlTexture {
format,
internal_format,
swizzle_mask,
} = pixel_kind.pixel_descriptor();
} = PixelDescriptor::from(pixel_kind);
let is_compressed = pixel_kind.is_compressed();
+3 -14
View File
@@ -8,25 +8,14 @@ keywords = ["graphics", "gapi"]
categories = ["graphics", "rendering::graphics-api"]
include = ["/src/**/*", "/Cargo.toml", "/LICENSE", "/README.md"]
homepage = "https://fyrox.rs"
documentation = "https://docs.rs/fyrox-ui"
documentation = "https://docs.rs/fyrox-graphics"
repository = "https://github.com/FyroxEngine/Fyrox"
rust-version = "1.86"
[dependencies]
fyrox-core = { path = "../fyrox-core", version = "0.36.2", features = ["serde"] }
glow = "0.16"
serde = { version = "1", features = ["derive"] }
strum = "0.26.1"
strum_macros = "0.26.1"
winit = { version = "0.29.2", features = ["serde"] }
bytemuck = { version = "1.16.1", features = ["derive"] }
fxhash = "0.2.1"
regex = "1"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
glutin = "0.31"
glutin-winit = "0.4.2"
raw-window-handle = "0.5.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
serde-wasm-bindgen = "0.6.3"
bytemuck = { version = "1.16.1", features = ["derive"] }
-7
View File
@@ -167,13 +167,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)
+2 -1
View File
@@ -147,7 +147,8 @@ pub enum PixelElementKind {
}
impl PixelKind {
pub(crate) fn unpack_alignment(self) -> Option<i32> {
#[doc(hidden)]
pub fn unpack_alignment(self) -> Option<i32> {
match self {
Self::RGBA16
| Self::RGBA16F
+1 -2
View File
@@ -31,7 +31,6 @@ pub mod buffer;
pub mod error;
pub mod framebuffer;
pub mod geometry_buffer;
pub mod gl;
pub mod gpu_program;
pub mod gpu_texture;
pub mod query;
@@ -569,7 +568,7 @@ pub enum ElementKind {
}
impl ElementKind {
fn index_per_element(self) -> usize {
pub fn index_per_element(self) -> usize {
match self {
ElementKind::Triangle => 3,
ElementKind::Line => 2,
+2 -3
View File
@@ -8,14 +8,13 @@ use fyrox_core::define_as_any_trait;
use std::fmt::Debug;
/// Kind of a GPU query.
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum QueryKind {
/// Queries a number of rendered pixels; any pixel that passed all pipeline tests counts.
SamplesPassed = glow::SAMPLES_PASSED,
SamplesPassed,
/// Queries a flag that defines whether the rendering operation produced any pixels or not.
AnySamplesPassed = glow::ANY_SAMPLES_PASSED,
AnySamplesPassed,
}
/// Result of a query.
+1
View File
@@ -23,6 +23,7 @@ fyrox-resource = { path = "../fyrox-resource", version = "0.36.2" }
fyrox-animation = { path = "../fyrox-animation", version = "0.36.2" }
fyrox-graph = { path = "../fyrox-graph", version = "0.36.2" }
fyrox-graphics = { path = "../fyrox-graphics", version = "0.36.2" }
fyrox-graphics-gl = { path = "../fyrox-graphics-gl", version = "0.36" }
fyrox-texture = { path = "../fyrox-texture", version = "0.36.2" }
fyrox-autotile = { path = "../fyrox-autotile", version = "0.36.0" }
fyrox-material = { path = "../fyrox-material", version = "0.36" }
+2 -1
View File
@@ -104,7 +104,8 @@ use crate::{
use fxhash::{FxHashMap, FxHashSet};
use fyrox_animation::AnimationTracksData;
use fyrox_core::visitor::error::VisitError;
use fyrox_graphics::{gl::server::GlGraphicsServer, server::SharedGraphicsServer};
use fyrox_graphics::server::SharedGraphicsServer;
use fyrox_graphics_gl::server::GlGraphicsServer;
use fyrox_sound::{
buffer::{loader::SoundBufferLoader, SoundBuffer},
renderer::hrtf::{HrirSphereLoader, HrirSphereResourceData},