gl: expose compiler in gl result

This commit is contained in:
chyyran 2022-11-11 02:26:57 -05:00
parent f3d0b2acae
commit bbefc3ced3
6 changed files with 111 additions and 32 deletions

35
Cargo.lock generated
View file

@ -306,6 +306,26 @@ dependencies = [
"byteorder", "byteorder",
] ]
[[package]]
name = "gl"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a94edab108827d67608095e269cf862e60d920f144a5026d3dbcfd8b877fb404"
dependencies = [
"gl_generator",
]
[[package]]
name = "gl_generator"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d"
dependencies = [
"khronos_api",
"log",
"xml-rs",
]
[[package]] [[package]]
name = "half" name = "half"
version = "1.8.2" version = "1.8.2"
@ -389,6 +409,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "khronos_api"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -454,12 +480,13 @@ dependencies = [
name = "librashader-runtime-gl" name = "librashader-runtime-gl"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"gl",
"librashader", "librashader",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets", "librashader-presets",
"librashader-reflect", "librashader-reflect",
"rustc-hash", "rustc-hash",
"windows", "spirv_cross",
] ]
[[package]] [[package]]
@ -1075,6 +1102,12 @@ version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
[[package]]
name = "xml-rs"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
[[package]] [[package]]
name = "xmlparser" name = "xmlparser"
version = "0.13.5" version = "0.13.5"

View file

@ -2,18 +2,23 @@ use crate::back::targets::{CompilerBackend, FromCompilation, GLSL, HLSL};
use crate::back::CompileShader; use crate::back::CompileShader;
use crate::error::ShaderReflectError; use crate::error::ShaderReflectError;
use crate::front::shaderc::GlslangCompilation; use crate::front::shaderc::GlslangCompilation;
use crate::reflect::cross::{GlslReflect, HlslReflect}; use crate::reflect::cross::{CompiledAst, CrossReflect, GlslReflect, HlslReflect};
use crate::reflect::ReflectShader; use crate::reflect::ReflectShader;
pub type GlVersion = spirv_cross::glsl::Version; pub type GlVersion = spirv_cross::glsl::Version;
pub struct GlslangCompileContext {
pub texture_fixups: Vec<u32>,
pub compiler: CompiledAst<spirv_cross::glsl::Target>
}
impl FromCompilation<GlslangCompilation> for GLSL { impl FromCompilation<GlslangCompilation> for GLSL {
type Target = GLSL; type Target = GLSL;
type Options = GlVersion; type Options = GlVersion;
type Context = GlslangCompileContext;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: GlslangCompilation,
) -> Result< ) -> Result<
CompilerBackend<impl CompileShader<Self::Target, Options = Self::Options> + ReflectShader>, CompilerBackend<impl CompileShader<Self::Target, Options = Self::Options, Context = Self::Context> + ReflectShader>,
ShaderReflectError, ShaderReflectError,
> { > {
Ok(CompilerBackend { Ok(CompilerBackend {
@ -25,11 +30,12 @@ impl FromCompilation<GlslangCompilation> for GLSL {
impl FromCompilation<GlslangCompilation> for HLSL { impl FromCompilation<GlslangCompilation> for HLSL {
type Target = HLSL; type Target = HLSL;
type Options = Option<()>; type Options = Option<()>;
type Context = ();
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: GlslangCompilation,
) -> Result< ) -> Result<
CompilerBackend<impl CompileShader<Self::Target, Options = Self::Options> + ReflectShader>, CompilerBackend<impl CompileShader<Self::Target, Options = Self::Options, Context = Self::Context> + ReflectShader>,
ShaderReflectError, ShaderReflectError,
> { > {
Ok(CompilerBackend { Ok(CompilerBackend {

View file

@ -4,7 +4,6 @@ use crate::reflect::{ReflectSemantics, ReflectShader, ShaderReflection};
pub trait OutputTarget { pub trait OutputTarget {
type Output; type Output;
type AdditionalContext;
} }
pub struct GLSL; pub struct GLSL;
@ -14,15 +13,12 @@ pub struct MSL;
impl OutputTarget for GLSL { impl OutputTarget for GLSL {
type Output = String; type Output = String;
type AdditionalContext = Vec<u32>;
} }
impl OutputTarget for HLSL { impl OutputTarget for HLSL {
type Output = String; type Output = String;
type AdditionalContext = ();
} }
impl OutputTarget for SpirV { impl OutputTarget for SpirV {
type Output = Vec<u32>; type Output = Vec<u32>;
type AdditionalContext = ();
} }
pub struct CompilerBackend<T> { pub struct CompilerBackend<T> {
@ -32,17 +28,21 @@ pub struct CompilerBackend<T> {
pub trait FromCompilation<T> { pub trait FromCompilation<T> {
type Target: OutputTarget; type Target: OutputTarget;
type Options; type Options;
type Context;
fn from_compilation( fn from_compilation(
compile: T, compile: T,
) -> Result<CompilerBackend<impl CompileShader<Self::Target> + ReflectShader>, ShaderReflectError>; ) -> Result<CompilerBackend<impl CompileShader<Self::Target, Context=Self::Context> + ReflectShader>, ShaderReflectError>;
} }
pub trait CompileShader<T: OutputTarget> { pub trait CompileShader<T: OutputTarget> {
type Options; type Options;
type Context;
fn compile( fn compile(
&mut self, self,
options: Self::Options, options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, T::AdditionalContext>, ShaderCompileError>; ) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
} }
impl<T> ReflectShader for CompilerBackend<T> impl<T> ReflectShader for CompilerBackend<T>
@ -64,11 +64,12 @@ where
E: OutputTarget, E: OutputTarget,
{ {
type Options = T::Options; type Options = T::Options;
type Context = T::Context;
fn compile( fn compile(
&mut self, self,
options: Self::Options, options: Self::Options,
) -> Result<ShaderCompilerOutput<E::Output, E::AdditionalContext>, ShaderCompileError> { ) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options) self.backend.compile(options)
} }
} }

View file

@ -15,6 +15,7 @@ use spirv_cross::{glsl, hlsl, ErrorCode};
use crate::back::targets::{GLSL, HLSL}; use crate::back::targets::{GLSL, HLSL};
use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::back::cross::GlslangCompileContext;
pub struct CrossReflect<T> pub struct CrossReflect<T>
where where
@ -24,6 +25,14 @@ where
fragment: Ast<T>, fragment: Ast<T>,
} }
pub struct CompiledAst<T>
where
T: spirv_cross::spirv::Target {
pub vertex: Ast<T>,
pub fragment: Ast<T>,
}
pub(crate) type HlslReflect = CrossReflect<hlsl::Target>; pub(crate) type HlslReflect = CrossReflect<hlsl::Target>;
pub(crate) type GlslReflect = CrossReflect<glsl::Target>; pub(crate) type GlslReflect = CrossReflect<glsl::Target>;
@ -674,12 +683,12 @@ where
impl CompileShader<GLSL> for CrossReflect<glsl::Target> { impl CompileShader<GLSL> for CrossReflect<glsl::Target> {
type Options = glsl::Version; type Options = glsl::Version;
type Context = GlslangCompileContext;
// todo: compile should consume self
fn compile( fn compile(
&mut self, mut self,
version: Self::Options, version: Self::Options,
) -> Result<ShaderCompilerOutput<String, Vec<u32>>, ShaderCompileError> { ) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
let mut options: glsl::CompilerOptions = Default::default(); let mut options: glsl::CompilerOptions = Default::default();
options.version = version; options.version = version;
options.fragment.default_float_precision = glsl::Precision::High; options.fragment.default_float_precision = glsl::Precision::High;
@ -797,16 +806,23 @@ impl CompileShader<GLSL> for CrossReflect<glsl::Target> {
Ok(ShaderCompilerOutput { Ok(ShaderCompilerOutput {
vertex: self.vertex.compile()?, vertex: self.vertex.compile()?,
fragment: self.fragment.compile()?, fragment: self.fragment.compile()?,
context: texture_fixups, context: GlslangCompileContext {
texture_fixups,
compiler: CompiledAst {
vertex: self.vertex,
fragment: self.fragment
}
},
}) })
} }
} }
impl CompileShader<HLSL> for CrossReflect<hlsl::Target> { impl CompileShader<HLSL> for CrossReflect<hlsl::Target> {
type Options = Option<()>; type Options = Option<()>;
type Context = ();
fn compile( fn compile(
&mut self, mut self,
_options: Self::Options, _options: Self::Options,
) -> Result<ShaderCompilerOutput<String>, ShaderCompileError> { ) -> Result<ShaderCompilerOutput<String>, ShaderCompileError> {
let mut options = hlsl::CompilerOptions::default(); let mut options = hlsl::CompilerOptions::default();

View file

@ -10,13 +10,6 @@ edition = "2021"
"librashader-presets" = { path = "../librashader-presets" } "librashader-presets" = { path = "../librashader-presets" }
"librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-preprocess" = { path = "../librashader-preprocess" }
"librashader-reflect" = { path = "../librashader-reflect" } "librashader-reflect" = { path = "../librashader-reflect" }
spirv_cross = "0.23.1"
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
gl = "0.14.0"
[dependencies.windows]
version = "0.43.0"
features = [
"Win32_Foundation",
"Win32_Graphics_Dxgi_Common",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D11",
]

View file

@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
use gl::types::{GLenum, GLuint};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use librashader::ShaderSource; use librashader::ShaderSource;
@ -49,6 +50,9 @@ pub fn load_pass_semantics(uniform_semantics: &mut FxHashMap<String, UniformSema
} }
// todo: init gl
pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>>{ pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>>{
let preset = librashader_presets::ShaderPreset::try_parse(path)?; let preset = librashader_presets::ShaderPreset::try_parse(path)?;
let mut passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset.shaders.iter() let mut passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset.shaders.iter()
@ -97,24 +101,36 @@ pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>>{
let reflection = reflect.reflect(index as u32, &semantics)?; let reflection = reflect.reflect(index as u32, &semantics)?;
let glsl = reflect.compile(GlVersion::V4_60)?; let glsl = reflect.compile(GlVersion::V4_60)?;
// shader_gl3: 1375 // shader_gl3: 1375
reflection.meta.texture_meta.get(&SemanticMap { reflection.meta.texture_meta.get(&SemanticMap {
semantics: TextureSemantics::Source, semantics: TextureSemantics::Source,
index: 0 index: 0
}).unwrap().binding; }).unwrap().binding;
// unsafe {
// let vertex = gl_compile_shader(gl::VERTEX_SHADER, glsl.vertex.as_str());
// let fragment = gl_compile_shader(gl::FRAGMENT_SHADER, glsl.fragment.as_str());
//
// let program = gl::CreateProgram();
// gl::AttachShader(program, vertex);
// gl::AttachShader(program, fragment);
//
// }
compiled.push(glsl); compiled.push(glsl);
reflections.push(reflection); reflections.push(reflection);
} }
let mut glprogram: Vec<GLuint> = Vec::new();
// todo: build gl semantics for compilation in &compiled {
// compilation.context.compiler.vertex
// shader_gl3:188 }
eprintln!("{:#?}", reflections); eprintln!("{:#?}", reflections);
eprintln!("{:#?}", compiled); // eprintln!("{:#?}", compiled./);
// eprintln!("{:?}", preset); // eprintln!("{:?}", preset);
// eprintln!("{:?}", reflect.reflect(&ReflectOptions { // eprintln!("{:?}", reflect.reflect(&ReflectOptions {
// pass_number: i as u32, // pass_number: i as u32,
@ -125,6 +141,20 @@ pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>>{
Ok(()) Ok(())
} }
unsafe fn gl_compile_shader(stage: GLenum, source: &str) -> GLuint {
let shader = gl::CreateShader(stage);
gl::ShaderSource(shader, 1, &source.as_bytes().as_ptr().cast(), std::ptr::null());
gl::CompileShader(shader);
let mut compile_status = 0;
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut compile_status);
if compile_status == 0 {
panic!("failed to compile")
}
shader
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;