From 43ec5173c1b92d834ad8ef1bc40c17917c66fb4a Mon Sep 17 00:00:00 2001 From: chyyran Date: Sun, 27 Nov 2022 23:47:41 -0500 Subject: [PATCH] reflect: seal ast output mutability after compilation --- librashader-reflect/src/back/cross.rs | 6 ++--- librashader-reflect/src/reflect/cross.rs | 33 +++++++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index 1f52cc5..1ed1764 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -2,13 +2,13 @@ use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompilerBackend, CompileShader, FromCompilation}; use crate::error::ShaderReflectError; use crate::front::shaderc::GlslangCompilation; -use crate::reflect::cross::{CompiledAst, GlslReflect, HlslReflect}; +use crate::reflect::cross::{CompiledProgram, GlslReflect, HlslReflect}; use crate::reflect::ReflectShader; pub type GlVersion = spirv_cross::glsl::Version; pub struct GlslangGlslContext { pub sampler_bindings: Vec<(String, u32)>, - pub compiler: CompiledAst, + pub compiler: CompiledProgram, } impl FromCompilation for GLSL { @@ -30,7 +30,7 @@ impl FromCompilation for GLSL { } pub struct GlslangHlslContext { - pub compiler: CompiledAst, + pub compiler: CompiledProgram, } diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 1be6282..7b6001d 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError}; use crate::front::shaderc::GlslangCompilation; use crate::reflect::semantics::{BindingStage, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, MemberOffset, PushReflection, ReflectSemantics, ShaderReflection, TextureBinding, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, UboReflection, ValidateTypeSemantics, VariableMeta, VariableSemanticMap, VariableSemantics}; @@ -11,6 +12,7 @@ use crate::back::cross::{GlslangGlslContext, GlslangHlslContext}; use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompileShader, ShaderCompilerOutput}; + pub struct CrossReflect where T: spirv_cross::spirv::Target, @@ -19,12 +21,25 @@ where fragment: Ast, } -pub struct CompiledAst +///! The output of the SPIR-V AST after compilation. +///! +///! This output is immutable and can not be recompiled later. +pub struct CompiledAst(Ast); +impl Deref for CompiledAst { + type Target = Ast; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +///! The compiled SPIR-V program after compilation. +pub struct CompiledProgram where T: spirv_cross::spirv::Target, { - pub vertex: Ast, - pub fragment: Ast, + pub vertex: CompiledAst, + pub fragment: CompiledAst, } pub(crate) type HlslReflect = CrossReflect; @@ -803,9 +818,9 @@ impl CompileShader for CrossReflect { fragment: self.fragment.compile()?, context: GlslangGlslContext { sampler_bindings: texture_fixups, - compiler: CompiledAst { - vertex: self.vertex, - fragment: self.fragment, + compiler: CompiledProgram { + vertex: CompiledAst(self.vertex), + fragment: CompiledAst(self.fragment), }, }, }) @@ -830,9 +845,9 @@ impl CompileShader for CrossReflect { vertex: self.vertex.compile()?, fragment: self.fragment.compile()?, context: GlslangHlslContext { - compiler: CompiledAst { - vertex: self.vertex, - fragment: self.fragment + compiler: CompiledProgram { + vertex: CompiledAst(self.vertex), + fragment: CompiledAst(self.fragment) } }, })