Skip to content

Commit 1bdadaa

Browse files
committed
rt(gl): port to OpenGL runtime to glow
1 parent 77b957b commit 1bdadaa

36 files changed

+1395
-1380
lines changed

Cargo.lock

+14-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ash = "0.38"
2323
spirv-cross2 = { version = "0.4", default-features = false }
2424
objc2-metal = { version = "0.2" }
2525
objc2 = { version = "0.5.0" }
26-
26+
glow = { version = "0.14", git = "https://github.com/grovesNL/glow/" }
2727
wgpu = { version = "22", default-features = false }
2828
wgpu-types = { version = "22" }
2929

librashader-common/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description = "RetroArch shaders for all."
1313

1414
[features]
1515
default = []
16-
opengl = ["gl"]
16+
opengl = ["glow"]
1717
d3d9 = ["windows"]
1818
d3d11 = ["windows", "dxgi"]
1919
d3d12 = ["windows", "dxgi"]
@@ -28,7 +28,7 @@ rustc-hash = "2.0.0"
2828
halfbrown = "0.2.4"
2929
smartstring = "1.0"
3030

31-
gl = { version = "0.14", optional = true }
31+
glow = { workspace = true, optional = true }
3232
ash = { workspace = true, optional = true }
3333
wgpu-types = { workspace = true, optional = true }
3434

librashader-common/src/gl.rs

+45-45
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
use crate::{FilterMode, ImageFormat, WrapMode};
22

3-
impl From<ImageFormat> for gl::types::GLenum {
3+
impl From<ImageFormat> for u32 {
44
fn from(format: ImageFormat) -> Self {
55
match format {
6-
ImageFormat::Unknown => 0 as gl::types::GLenum,
7-
ImageFormat::R8Unorm => gl::R8,
8-
ImageFormat::R8Uint => gl::R8UI,
9-
ImageFormat::R8Sint => gl::R8I,
10-
ImageFormat::R8G8Unorm => gl::RG8,
11-
ImageFormat::R8G8Uint => gl::RG8UI,
12-
ImageFormat::R8G8Sint => gl::RG8I,
13-
ImageFormat::R8G8B8A8Unorm => gl::RGBA8,
14-
ImageFormat::R8G8B8A8Uint => gl::RGBA8UI,
15-
ImageFormat::R8G8B8A8Sint => gl::RGBA8I,
16-
ImageFormat::R8G8B8A8Srgb => gl::SRGB8_ALPHA8,
17-
ImageFormat::A2B10G10R10UnormPack32 => gl::RGB10_A2,
18-
ImageFormat::A2B10G10R10UintPack32 => gl::RGB10_A2UI,
19-
ImageFormat::R16Uint => gl::R16UI,
20-
ImageFormat::R16Sint => gl::R16I,
21-
ImageFormat::R16Sfloat => gl::R16F,
22-
ImageFormat::R16G16Uint => gl::RG16UI,
23-
ImageFormat::R16G16Sint => gl::RG16I,
24-
ImageFormat::R16G16Sfloat => gl::RG16F,
25-
ImageFormat::R16G16B16A16Uint => gl::RGBA16UI,
26-
ImageFormat::R16G16B16A16Sint => gl::RGBA16I,
27-
ImageFormat::R16G16B16A16Sfloat => gl::RGBA16F,
28-
ImageFormat::R32Uint => gl::R32UI,
29-
ImageFormat::R32Sint => gl::R32I,
30-
ImageFormat::R32Sfloat => gl::R32F,
31-
ImageFormat::R32G32Uint => gl::RG32UI,
32-
ImageFormat::R32G32Sint => gl::RG32I,
33-
ImageFormat::R32G32Sfloat => gl::RG32F,
34-
ImageFormat::R32G32B32A32Uint => gl::RGBA32UI,
35-
ImageFormat::R32G32B32A32Sint => gl::RGBA32I,
36-
ImageFormat::R32G32B32A32Sfloat => gl::RGBA32F,
6+
ImageFormat::Unknown => 0,
7+
ImageFormat::R8Unorm => glow::R8,
8+
ImageFormat::R8Uint => glow::R8UI,
9+
ImageFormat::R8Sint => glow::R8I,
10+
ImageFormat::R8G8Unorm => glow::RG8,
11+
ImageFormat::R8G8Uint => glow::RG8UI,
12+
ImageFormat::R8G8Sint => glow::RG8I,
13+
ImageFormat::R8G8B8A8Unorm => glow::RGBA8,
14+
ImageFormat::R8G8B8A8Uint => glow::RGBA8UI,
15+
ImageFormat::R8G8B8A8Sint => glow::RGBA8I,
16+
ImageFormat::R8G8B8A8Srgb => glow::SRGB8_ALPHA8,
17+
ImageFormat::A2B10G10R10UnormPack32 => glow::RGB10_A2,
18+
ImageFormat::A2B10G10R10UintPack32 => glow::RGB10_A2UI,
19+
ImageFormat::R16Uint => glow::R16UI,
20+
ImageFormat::R16Sint => glow::R16I,
21+
ImageFormat::R16Sfloat => glow::R16F,
22+
ImageFormat::R16G16Uint => glow::RG16UI,
23+
ImageFormat::R16G16Sint => glow::RG16I,
24+
ImageFormat::R16G16Sfloat => glow::RG16F,
25+
ImageFormat::R16G16B16A16Uint => glow::RGBA16UI,
26+
ImageFormat::R16G16B16A16Sint => glow::RGBA16I,
27+
ImageFormat::R16G16B16A16Sfloat => glow::RGBA16F,
28+
ImageFormat::R32Uint => glow::R32UI,
29+
ImageFormat::R32Sint => glow::R32I,
30+
ImageFormat::R32Sfloat => glow::R32F,
31+
ImageFormat::R32G32Uint => glow::RG32UI,
32+
ImageFormat::R32G32Sint => glow::RG32I,
33+
ImageFormat::R32G32Sfloat => glow::RG32F,
34+
ImageFormat::R32G32B32A32Uint => glow::RGBA32UI,
35+
ImageFormat::R32G32B32A32Sint => glow::RGBA32I,
36+
ImageFormat::R32G32B32A32Sfloat => glow::RGBA32F,
3737
}
3838
}
3939
}
4040

41-
impl From<WrapMode> for gl::types::GLenum {
41+
impl From<WrapMode> for i32 {
4242
fn from(value: WrapMode) -> Self {
4343
match value {
44-
WrapMode::ClampToBorder => gl::CLAMP_TO_BORDER,
45-
WrapMode::ClampToEdge => gl::CLAMP_TO_EDGE,
46-
WrapMode::Repeat => gl::REPEAT,
47-
WrapMode::MirroredRepeat => gl::MIRRORED_REPEAT,
44+
WrapMode::ClampToBorder => glow::CLAMP_TO_BORDER as i32,
45+
WrapMode::ClampToEdge => glow::CLAMP_TO_EDGE as i32,
46+
WrapMode::Repeat => glow::REPEAT as i32,
47+
WrapMode::MirroredRepeat => glow::MIRRORED_REPEAT as i32,
4848
}
4949
}
5050
}
5151

52-
impl From<FilterMode> for gl::types::GLenum {
52+
impl From<FilterMode> for i32 {
5353
fn from(value: FilterMode) -> Self {
5454
match value {
55-
FilterMode::Linear => gl::LINEAR,
56-
_ => gl::NEAREST,
55+
FilterMode::Linear => glow::LINEAR as i32,
56+
_ => glow::NEAREST as i32,
5757
}
5858
}
5959
}
6060

6161
impl FilterMode {
6262
/// Get the mipmap filtering mode for the given combination.
63-
pub fn gl_mip(&self, mip: FilterMode) -> gl::types::GLenum {
63+
pub fn gl_mip(&self, mip: FilterMode) -> u32 {
6464
match (self, mip) {
65-
(FilterMode::Linear, FilterMode::Linear) => gl::LINEAR_MIPMAP_LINEAR,
66-
(FilterMode::Linear, FilterMode::Nearest) => gl::LINEAR_MIPMAP_NEAREST,
67-
(FilterMode::Nearest, FilterMode::Linear) => gl::NEAREST_MIPMAP_LINEAR,
68-
_ => gl::NEAREST_MIPMAP_NEAREST,
65+
(FilterMode::Linear, FilterMode::Linear) => glow::LINEAR_MIPMAP_LINEAR,
66+
(FilterMode::Linear, FilterMode::Nearest) => glow::LINEAR_MIPMAP_NEAREST,
67+
(FilterMode::Nearest, FilterMode::Linear) => glow::NEAREST_MIPMAP_LINEAR,
68+
_ => glow::NEAREST_MIPMAP_NEAREST,
6969
}
7070
}
7171
}

librashader-reflect/src/reflect/cross/glsl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl CompileShader<GLSL> for CrossReflect<targets::Glsl> {
145145
)?;
146146
self.fragment
147147
.set_decoration(res.id, Decoration::Binding, DecorationValue::unset())?;
148-
let mut name = res.name.to_string();
148+
let name = res.name.to_string();
149149
texture_fixups.push((name, binding));
150150
}
151151

librashader-runtime-gl/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ librashader-runtime = { path = "../librashader-runtime" , version = "0.4.5" }
2020
librashader-cache = { path = "../librashader-cache", version = "0.4.5" }
2121

2222
spirv-cross2 = { workspace = true, features = ["glsl"] }
23-
gl = "0.14.0"
23+
glow = { workspace = true}
2424
bytemuck = { version = "1.12.3", features = ["derive"] }
2525
thiserror = "1.0.37"
2626
rayon = "1.6.1"
2727

28-
sptr = "0.3"
29-
3028
[features]
3129
stable = ["librashader-reflect/stable"]
3230

librashader-runtime-gl/src/binding.rs

+36-31
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
use gl::types::GLint;
1+
use glow::HasContext;
22
use librashader_reflect::reflect::semantics::{BindingStage, UniformMemberBlock};
33
use librashader_runtime::uniforms::{BindUniform, UniformScalar, UniformStorage};
4+
use std::fmt::Display;
45

56
#[derive(Debug, Copy, Clone)]
67
pub struct VariableLocation {
7-
pub(crate) ubo: Option<UniformLocation<GLint>>,
8-
pub(crate) push: Option<UniformLocation<GLint>>,
8+
pub(crate) ubo: Option<UniformLocation<Option<glow::UniformLocation>>>,
9+
pub(crate) push: Option<UniformLocation<Option<glow::UniformLocation>>>,
910
}
1011

1112
impl VariableLocation {
12-
pub fn location(&self, offset_type: UniformMemberBlock) -> Option<UniformLocation<GLint>> {
13+
pub fn location(
14+
&self,
15+
offset_type: UniformMemberBlock,
16+
) -> Option<UniformLocation<Option<glow::UniformLocation>>> {
1317
match offset_type {
1418
UniformMemberBlock::Ubo => self.ubo,
1519
UniformMemberBlock::PushConstant => self.push,
@@ -23,64 +27,65 @@ pub struct UniformLocation<T> {
2327
pub fragment: T,
2428
}
2529

26-
impl UniformLocation<GLint> {
30+
impl UniformLocation<Option<glow::UniformLocation>> {
31+
#[allow(unused_comparisons)]
2732
pub fn is_valid(&self, stage: BindingStage) -> bool {
28-
let mut validity = false;
29-
if stage.contains(BindingStage::FRAGMENT) {
30-
validity = validity || self.fragment >= 0;
31-
}
32-
if stage.contains(BindingStage::VERTEX) {
33-
validity = validity || self.vertex >= 0;
34-
}
35-
validity
33+
// since glow::UniformLocation is None or NonZeroU32,
34+
// is_some is sufficient for validity
35+
(stage.contains(BindingStage::FRAGMENT) && self.fragment.is_some())
36+
|| (stage.contains(BindingStage::VERTEX) && self.vertex.is_some())
3637
}
3738

3839
pub fn bindable(&self) -> bool {
3940
self.is_valid(BindingStage::VERTEX | BindingStage::FRAGMENT)
4041
}
4142
}
4243

43-
pub(crate) type GlUniformStorage = UniformStorage<GlUniformBinder, VariableLocation>;
44+
pub(crate) type GlUniformStorage =
45+
UniformStorage<GlUniformBinder, VariableLocation, Box<[u8]>, Box<[u8]>, glow::Context>;
4446

45-
pub trait GlUniformScalar: UniformScalar {
46-
const FACTORY: unsafe fn(GLint, Self) -> ();
47+
pub trait GlUniformScalar: UniformScalar + Display {
48+
const FACTORY: unsafe fn(&glow::Context, Option<&glow::UniformLocation>, Self) -> ();
4749
}
4850

4951
impl GlUniformScalar for f32 {
50-
const FACTORY: unsafe fn(GLint, Self) -> () = gl::Uniform1f;
52+
const FACTORY: unsafe fn(&glow::Context, Option<&glow::UniformLocation>, Self) -> () =
53+
glow::Context::uniform_1_f32;
5154
}
5255

5356
impl GlUniformScalar for i32 {
54-
const FACTORY: unsafe fn(GLint, Self) -> () = gl::Uniform1i;
57+
const FACTORY: unsafe fn(&glow::Context, Option<&glow::UniformLocation>, Self) -> () =
58+
glow::Context::uniform_1_i32;
5559
}
5660

5761
impl GlUniformScalar for u32 {
58-
const FACTORY: unsafe fn(GLint, Self) -> () = gl::Uniform1ui;
62+
const FACTORY: unsafe fn(&glow::Context, Option<&glow::UniformLocation>, Self) -> () =
63+
glow::Context::uniform_1_u32;
5964
}
6065

6166
pub(crate) struct GlUniformBinder;
62-
impl<T> BindUniform<VariableLocation, T, ()> for GlUniformBinder
67+
impl<T> BindUniform<VariableLocation, T, glow::Context> for GlUniformBinder
6368
where
6469
T: GlUniformScalar,
6570
{
6671
fn bind_uniform(
6772
block: UniformMemberBlock,
6873
value: T,
6974
location: VariableLocation,
70-
_: &(),
75+
device: &glow::Context,
7176
) -> Option<()> {
7277
if let Some(location) = location
7378
.location(block)
7479
.filter(|location| location.bindable())
7580
{
7681
if location.is_valid(BindingStage::VERTEX) {
7782
unsafe {
78-
T::FACTORY(location.vertex, value);
83+
T::FACTORY(device, location.vertex.as_ref(), value);
7984
}
8085
}
8186
if location.is_valid(BindingStage::FRAGMENT) {
8287
unsafe {
83-
T::FACTORY(location.fragment, value);
88+
T::FACTORY(device, location.fragment.as_ref(), value);
8489
}
8590
}
8691
Some(())
@@ -90,23 +95,23 @@ where
9095
}
9196
}
9297

93-
impl BindUniform<VariableLocation, &[f32; 4], ()> for GlUniformBinder {
98+
impl BindUniform<VariableLocation, &[f32; 4], glow::Context> for GlUniformBinder {
9499
fn bind_uniform(
95100
block: UniformMemberBlock,
96101
vec4: &[f32; 4],
97102
location: VariableLocation,
98-
_: &(),
103+
device: &glow::Context,
99104
) -> Option<()> {
100105
if let Some(location) = location
101106
.location(block)
102107
.filter(|location| location.bindable())
103108
{
104109
unsafe {
105110
if location.is_valid(BindingStage::VERTEX) {
106-
gl::Uniform4fv(location.vertex, 1, vec4.as_ptr());
111+
device.uniform_4_f32_slice(location.vertex.as_ref(), vec4);
107112
}
108113
if location.is_valid(BindingStage::FRAGMENT) {
109-
gl::Uniform4fv(location.fragment, 1, vec4.as_ptr());
114+
device.uniform_4_f32_slice(location.fragment.as_ref(), vec4);
110115
}
111116
}
112117
Some(())
@@ -116,23 +121,23 @@ impl BindUniform<VariableLocation, &[f32; 4], ()> for GlUniformBinder {
116121
}
117122
}
118123

119-
impl BindUniform<VariableLocation, &[f32; 16], ()> for GlUniformBinder {
124+
impl BindUniform<VariableLocation, &[f32; 16], glow::Context> for GlUniformBinder {
120125
fn bind_uniform(
121126
block: UniformMemberBlock,
122127
mat4: &[f32; 16],
123128
location: VariableLocation,
124-
_: &(),
129+
device: &glow::Context,
125130
) -> Option<()> {
126131
if let Some(location) = location
127132
.location(block)
128133
.filter(|location| location.bindable())
129134
{
130135
unsafe {
131136
if location.is_valid(BindingStage::VERTEX) {
132-
gl::UniformMatrix4fv(location.vertex, 1, gl::FALSE, mat4.as_ptr());
137+
device.uniform_matrix_4_f32_slice(location.vertex.as_ref(), false, mat4);
133138
}
134139
if location.is_valid(BindingStage::FRAGMENT) {
135-
gl::UniformMatrix4fv(location.fragment, 1, gl::FALSE, mat4.as_ptr());
140+
device.uniform_matrix_4_f32_slice(location.fragment.as_ref(), false, mat4);
136141
}
137142
}
138143
Some(())

0 commit comments

Comments
 (0)