Skip to content

Commit

Permalink
GLES backend as a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Aug 4, 2024
1 parent 9bf7ca3 commit d20f5bd
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 246 deletions.
17 changes: 10 additions & 7 deletions blade-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ repository = "https://github.com/kvark/blade"

[lib]

[features]
default = ["gles"]
gles = ["dep:glow", "dep:egl", "dep:libloading", "naga/glsl-out"]

[dependencies]
bitflags = { workspace = true }
bytemuck = { workspace = true }
Expand All @@ -18,6 +22,7 @@ log = { workspace = true }
mint = { workspace = true }
naga = { workspace = true }
raw-window-handle = "0.6"
glow = { version = "0.13", optional = true }

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
block = "0.1"
Expand All @@ -34,13 +39,11 @@ gpu-alloc-ash = "0.7"
naga = { workspace = true, features = ["spv-out"] }
slab = { workspace = true }

[target.'cfg(any(gles, target_arch = "wasm32"))'.dependencies]
glow = "0.13"
naga = { workspace = true, features = ["glsl-out"] }

[target.'cfg(all(gles, not(target_arch = "wasm32")))'.dependencies]
egl = { package = "khronos-egl", version = "5.0", features = ["dynamic"] }
libloading = { version = "0.8" }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
egl = { package = "khronos-egl", version = "5.0", features = [
"dynamic",
], optional = true }
libloading = { version = "0.8", optional = true }

[target.'cfg(all(target_arch = "wasm32"))'.dependencies]
wasm-bindgen = "0.2.83"
Expand Down
20 changes: 20 additions & 0 deletions blade-graphics/src/common/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::derive::HasShaderBinding;

impl HasShaderBinding for super::TextureView {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::Texture;
}
impl HasShaderBinding for super::Sampler {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::Sampler;
}
impl HasShaderBinding for super::BufferPiece {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::Buffer;
}
impl<'a, const N: crate::ResourceIndex> HasShaderBinding for &'a super::BufferArray<N> {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::BufferArray { count: N };
}
impl<'a, const N: crate::ResourceIndex> HasShaderBinding for &'a super::TextureArray<N> {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::TextureArray { count: N };
}
impl HasShaderBinding for super::AccelerationStructure {
const TYPE: crate::ShaderBinding = crate::ShaderBinding::AccelerationStructure;
}
107 changes: 107 additions & 0 deletions blade-graphics/src/common/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
pub type BufferPiece = crate::GenericBufferPiece<Buffer>;

#[derive(Clone, Copy, Debug)]
pub struct TexturePiece {
pub texture: Texture,
pub mip_level: u32,
pub array_layer: u32,
pub origin: [u32; 3],
}

impl From<Texture> for TexturePiece {
fn from(texture: Texture) -> Self {
Self {
texture,
mip_level: 0,
array_layer: 0,
origin: [0; 3],
}
}
}

pub type BufferArray<const N: crate::ResourceIndex> = crate::ResourceArray<BufferPiece, N>;
pub type TextureArray<const N: crate::ResourceIndex> = crate::ResourceArray<TextureView, N>;

#[derive(Clone, Debug)]
pub struct AccelerationStructureMesh {
pub vertex_data: BufferPiece,
pub vertex_format: crate::VertexFormat,
pub vertex_stride: u32,
pub vertex_count: u32,
pub index_data: BufferPiece,
pub index_type: Option<crate::IndexType>,
pub triangle_count: u32,
pub transform_data: BufferPiece,
pub is_opaque: bool,
}

pub trait ShaderBindable: Clone + Copy + crate::derive::HasShaderBinding {
fn bind_to(&self, context: &mut PipelineContext, index: u32);
}

#[derive(Clone, Copy, Debug)]
pub enum FinishOp {
Store,
Discard,
ResolveTo(TextureView),
Ignore,
}

#[derive(Debug)]
pub struct RenderTarget {
pub view: TextureView,
pub init_op: crate::InitOp,
pub finish_op: FinishOp,
}

#[derive(Debug)]
pub struct RenderTargetSet<'a> {
pub colors: &'a [RenderTarget],
pub depth_stencil: Option<RenderTarget>,
}

impl Context {
pub fn try_create_shader(
&self,
desc: super::ShaderDesc,
) -> Result<super::Shader, &'static str> {
let module = naga::front::wgsl::parse_str(desc.source).map_err(|e| {
e.emit_to_stderr_with_path(desc.source, "");
"compilation failed"
})?;

let device_caps = self.capabilities();

// Bindings are set up at pipeline creation, ignore here
let flags = naga::valid::ValidationFlags::all() ^ naga::valid::ValidationFlags::BINDINGS;
let mut caps = naga::valid::Capabilities::empty();
caps.set(
naga::valid::Capabilities::RAY_QUERY | naga::valid::Capabilities::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
!device_caps.ray_query.is_empty(),
);
let info = naga::valid::Validator::new(flags, caps)
.validate(&module)
.map_err(|e| {
crate::util::emit_annotated_error(&e, "", desc.source);
crate::util::print_err(&e);
"validation failed"
})?;

Ok(super::Shader { module, info })
}

pub fn create_shader(&self, desc: super::ShaderDesc) -> super::Shader {
self.try_create_shader(desc).unwrap()
}
}

pub trait ShaderData {
fn layout() -> crate::ShaderDataLayout;
fn fill(&self, context: PipelineContext);
}

mod traits {
pub trait PipelineEncoder {
fn bind<D: super::ShaderData>(&mut self, group: u32, data: &D);
}
}
11 changes: 11 additions & 0 deletions blade-graphics/src/common/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl super::ComputePipeline {
/// Return the dispatch group counts sufficient to cover the given extent.
pub fn get_dispatch_for(&self, extent: crate::Extent) -> [u32; 3] {
let wg_size = self.get_workgroup_size();
[
(extent.width + wg_size[0] - 1) / wg_size[0],
(extent.height + wg_size[1] - 1) / wg_size[1],
(extent.depth + wg_size[2] - 1) / wg_size[2],
]
}
}
18 changes: 0 additions & 18 deletions blade-graphics/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ impl<T: bytemuck::Pod> HasShaderBinding for T {
size: mem::size_of::<T>() as u32,
};
}
impl HasShaderBinding for super::TextureView {
const TYPE: ShaderBinding = ShaderBinding::Texture;
}
impl HasShaderBinding for super::Sampler {
const TYPE: ShaderBinding = ShaderBinding::Sampler;
}
impl HasShaderBinding for super::BufferPiece {
const TYPE: ShaderBinding = ShaderBinding::Buffer;
}
impl<'a, const N: ResourceIndex> HasShaderBinding for &'a super::BufferArray<N> {
const TYPE: ShaderBinding = ShaderBinding::BufferArray { count: N };
}
impl<'a, const N: ResourceIndex> HasShaderBinding for &'a super::TextureArray<N> {
const TYPE: ShaderBinding = ShaderBinding::TextureArray { count: N };
}
impl HasShaderBinding for super::AccelerationStructure {
const TYPE: ShaderBinding = ShaderBinding::AccelerationStructure;
}

pub trait HasVertexAttribute {
const FORMAT: VertexFormat;
Expand Down
Loading

0 comments on commit d20f5bd

Please sign in to comment.