From bbefc3ced3b23542050cd4ef6c5352c886609457 Mon Sep 17 00:00:00 2001 From: chyyran Date: Fri, 11 Nov 2022 02:26:57 -0500 Subject: [PATCH] gl: expose compiler in gl result --- Cargo.lock | 35 ++++++++++++++++++++- librashader-reflect/src/back/cross.rs | 12 +++++-- librashader-reflect/src/back/targets.rs | 19 +++++------ librashader-reflect/src/reflect/cross.rs | 26 ++++++++++++--- librashader-runtime-gl/Cargo.toml | 11 ++----- librashader-runtime-gl/src/lib.rs | 40 +++++++++++++++++++++--- 6 files changed, 111 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 737c951..3809a72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,6 +306,26 @@ dependencies = [ "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]] name = "half" version = "1.8.2" @@ -389,6 +409,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "khronos_api" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" + [[package]] name = "lazy_static" version = "1.4.0" @@ -454,12 +480,13 @@ dependencies = [ name = "librashader-runtime-gl" version = "0.1.0" dependencies = [ + "gl", "librashader", "librashader-preprocess", "librashader-presets", "librashader-reflect", "rustc-hash", - "windows", + "spirv_cross", ] [[package]] @@ -1075,6 +1102,12 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +[[package]] +name = "xml-rs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" + [[package]] name = "xmlparser" version = "0.13.5" diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index 1017b4c..5ec61bd 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -2,18 +2,23 @@ use crate::back::targets::{CompilerBackend, FromCompilation, GLSL, HLSL}; use crate::back::CompileShader; use crate::error::ShaderReflectError; use crate::front::shaderc::GlslangCompilation; -use crate::reflect::cross::{GlslReflect, HlslReflect}; +use crate::reflect::cross::{CompiledAst, CrossReflect, GlslReflect, HlslReflect}; use crate::reflect::ReflectShader; pub type GlVersion = spirv_cross::glsl::Version; +pub struct GlslangCompileContext { + pub texture_fixups: Vec, + pub compiler: CompiledAst +} impl FromCompilation for GLSL { type Target = GLSL; type Options = GlVersion; + type Context = GlslangCompileContext; fn from_compilation( compile: GlslangCompilation, ) -> Result< - CompilerBackend + ReflectShader>, + CompilerBackend + ReflectShader>, ShaderReflectError, > { Ok(CompilerBackend { @@ -25,11 +30,12 @@ impl FromCompilation for GLSL { impl FromCompilation for HLSL { type Target = HLSL; type Options = Option<()>; + type Context = (); fn from_compilation( compile: GlslangCompilation, ) -> Result< - CompilerBackend + ReflectShader>, + CompilerBackend + ReflectShader>, ShaderReflectError, > { Ok(CompilerBackend { diff --git a/librashader-reflect/src/back/targets.rs b/librashader-reflect/src/back/targets.rs index d3168cc..19eae2d 100644 --- a/librashader-reflect/src/back/targets.rs +++ b/librashader-reflect/src/back/targets.rs @@ -4,7 +4,6 @@ use crate::reflect::{ReflectSemantics, ReflectShader, ShaderReflection}; pub trait OutputTarget { type Output; - type AdditionalContext; } pub struct GLSL; @@ -14,15 +13,12 @@ pub struct MSL; impl OutputTarget for GLSL { type Output = String; - type AdditionalContext = Vec; } impl OutputTarget for HLSL { type Output = String; - type AdditionalContext = (); } impl OutputTarget for SpirV { type Output = Vec; - type AdditionalContext = (); } pub struct CompilerBackend { @@ -32,17 +28,21 @@ pub struct CompilerBackend { pub trait FromCompilation { type Target: OutputTarget; type Options; + type Context; + fn from_compilation( compile: T, - ) -> Result + ReflectShader>, ShaderReflectError>; + ) -> Result + ReflectShader>, ShaderReflectError>; } pub trait CompileShader { type Options; + type Context; + fn compile( - &mut self, + self, options: Self::Options, - ) -> Result, ShaderCompileError>; + ) -> Result, ShaderCompileError>; } impl ReflectShader for CompilerBackend @@ -64,11 +64,12 @@ where E: OutputTarget, { type Options = T::Options; + type Context = T::Context; fn compile( - &mut self, + self, options: Self::Options, - ) -> Result, ShaderCompileError> { + ) -> Result, ShaderCompileError> { self.backend.compile(options) } } diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index acb314c..83c72e0 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -15,6 +15,7 @@ use spirv_cross::{glsl, hlsl, ErrorCode}; use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompileShader, ShaderCompilerOutput}; +use crate::back::cross::GlslangCompileContext; pub struct CrossReflect where @@ -24,6 +25,14 @@ where fragment: Ast, } +pub struct CompiledAst +where +T: spirv_cross::spirv::Target { + pub vertex: Ast, + pub fragment: Ast, +} + + pub(crate) type HlslReflect = CrossReflect; pub(crate) type GlslReflect = CrossReflect; @@ -674,12 +683,12 @@ where impl CompileShader for CrossReflect { type Options = glsl::Version; + type Context = GlslangCompileContext; - // todo: compile should consume self fn compile( - &mut self, + mut self, version: Self::Options, - ) -> Result>, ShaderCompileError> { + ) -> Result, ShaderCompileError> { let mut options: glsl::CompilerOptions = Default::default(); options.version = version; options.fragment.default_float_precision = glsl::Precision::High; @@ -797,16 +806,23 @@ impl CompileShader for CrossReflect { Ok(ShaderCompilerOutput { vertex: self.vertex.compile()?, fragment: self.fragment.compile()?, - context: texture_fixups, + context: GlslangCompileContext { + texture_fixups, + compiler: CompiledAst { + vertex: self.vertex, + fragment: self.fragment + } + }, }) } } impl CompileShader for CrossReflect { type Options = Option<()>; + type Context = (); fn compile( - &mut self, + mut self, _options: Self::Options, ) -> Result, ShaderCompileError> { let mut options = hlsl::CompilerOptions::default(); diff --git a/librashader-runtime-gl/Cargo.toml b/librashader-runtime-gl/Cargo.toml index b1dd4ec..9c61238 100644 --- a/librashader-runtime-gl/Cargo.toml +++ b/librashader-runtime-gl/Cargo.toml @@ -10,13 +10,6 @@ edition = "2021" "librashader-presets" = { path = "../librashader-presets" } "librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-reflect" = { path = "../librashader-reflect" } +spirv_cross = "0.23.1" rustc-hash = "1.1.0" - -[dependencies.windows] -version = "0.43.0" -features = [ - "Win32_Foundation", - "Win32_Graphics_Dxgi_Common", - "Win32_Graphics_Direct3D", - "Win32_Graphics_Direct3D11", -] \ No newline at end of file +gl = "0.14.0" \ No newline at end of file diff --git a/librashader-runtime-gl/src/lib.rs b/librashader-runtime-gl/src/lib.rs index 386f7d2..f75d0f9 100644 --- a/librashader-runtime-gl/src/lib.rs +++ b/librashader-runtime-gl/src/lib.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::error::Error; use std::path::Path; +use gl::types::{GLenum, GLuint}; use rustc_hash::FxHashMap; use librashader::ShaderSource; @@ -49,6 +50,9 @@ pub fn load_pass_semantics(uniform_semantics: &mut FxHashMap) -> Result<(), Box>{ let preset = librashader_presets::ShaderPreset::try_parse(path)?; let mut passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset.shaders.iter() @@ -97,24 +101,36 @@ pub fn load(path: impl AsRef) -> Result<(), Box>{ let reflection = reflect.reflect(index as u32, &semantics)?; let glsl = reflect.compile(GlVersion::V4_60)?; + // shader_gl3: 1375 reflection.meta.texture_meta.get(&SemanticMap { semantics: TextureSemantics::Source, index: 0 }).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); reflections.push(reflection); } - - // todo: build gl semantics - - // shader_gl3:188 + let mut glprogram: Vec = Vec::new(); + for compilation in &compiled { + // compilation.context.compiler.vertex + } eprintln!("{:#?}", reflections); - eprintln!("{:#?}", compiled); + // eprintln!("{:#?}", compiled./); // eprintln!("{:?}", preset); // eprintln!("{:?}", reflect.reflect(&ReflectOptions { // pass_number: i as u32, @@ -125,6 +141,20 @@ pub fn load(path: impl AsRef) -> Result<(), Box>{ 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)] mod tests { use super::*;