mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-20 07:34:34 +00:00
Initial wgpu abstraction
This commit is contained in:
46
Source/Shader/Shader.rs
Normal file
46
Source/Shader/Shader.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use super::{ShaderSource, ShaderStage};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Shader {
|
||||
pub label: Cow<'static, str>,
|
||||
pub source: ShaderSource,
|
||||
pub stage: ShaderStage,
|
||||
}
|
||||
|
||||
impl Shader {
|
||||
pub fn New(stage: ShaderStage, source: ShaderSource) -> Self {
|
||||
Self {
|
||||
label: "".into(),
|
||||
source,
|
||||
stage,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn FromSpirv(spirv: &[u8]) -> Self {
|
||||
Self {
|
||||
label: "".into(),
|
||||
source: ShaderSource::Spirv(spirv),
|
||||
stage,
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn FromGlsl(stage: ShaderStage, glsl: &str) -> Self {
|
||||
Self {
|
||||
label: "".into(),
|
||||
source: ShaderSource::Glsl(glsl.to_string()),
|
||||
stage,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn FromWgsl(wgsl: &str) -> Self {
|
||||
Self {
|
||||
label: "".into(),
|
||||
source: ShaderSource::Wgsl(wgsl.to_string()),
|
||||
stage: ShaderStage::Multiple,
|
||||
}
|
||||
}
|
||||
}
|
22
Source/Shader/ShaderSource.rs
Normal file
22
Source/Shader/ShaderSource.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ShaderSource {
|
||||
Glsl(String),
|
||||
Spirv(Vec<u8>),
|
||||
Wgsl(String),
|
||||
}
|
||||
|
||||
impl ShaderSource {
|
||||
pub fn SpirvFromBytes(bytes: &[u8]) -> ShaderSource {
|
||||
ShaderSource::Spirv(Vec::from(bytes))
|
||||
}
|
||||
|
||||
pub fn WgslToString(&self) -> Option<&String> {
|
||||
if let ShaderSource::Wgsl(s) = &self {
|
||||
Some(s)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
17
Source/Shader/ShaderStage.rs
Normal file
17
Source/Shader/ShaderStage.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum ShaderStage {
|
||||
/// Handles the processing of individual vertices
|
||||
Vertex = 0,
|
||||
|
||||
/// Process a fragment generated by the rasterization into a set of colors and a single depth value.
|
||||
Fragment = 1,
|
||||
|
||||
/// Used entirely for computing arbitrary information
|
||||
Compute = 2,
|
||||
|
||||
/// Multi-stage shader, used with wgsl sources.
|
||||
Multiple = 3,
|
||||
}
|
11
Source/Shader/mod.rs
Normal file
11
Source/Shader/mod.rs
Normal file
@ -0,0 +1,11 @@
|
||||
#[path = "Shader.rs"]
|
||||
mod _Shader;
|
||||
pub use self::_Shader::*;
|
||||
|
||||
#[path = "ShaderSource.rs"]
|
||||
mod _ShaderSource;
|
||||
pub use self::_ShaderSource::*;
|
||||
|
||||
#[path = "ShaderStage.rs"]
|
||||
mod _ShaderStage;
|
||||
pub use self::_ShaderStage::*;
|
Reference in New Issue
Block a user