summaryrefslogtreecommitdiff
path: root/engine/src/rendering/shader.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/rendering/shader.rs')
-rw-r--r--engine/src/rendering/shader.rs159
1 files changed, 143 insertions, 16 deletions
diff --git a/engine/src/rendering/shader.rs b/engine/src/rendering/shader.rs
index 926302a..3fdc2c1 100644
--- a/engine/src/rendering/shader.rs
+++ b/engine/src/rendering/shader.rs
@@ -10,7 +10,6 @@ use ecs::phase::INIT as INIT_PHASE;
use shader_slang::{
Blob as SlangBlob,
ComponentType as SlangComponentType,
- DebugInfoLevel as SlangDebugInfoLevel,
EntryPoint as SlangEntryPoint,
GlobalSession as SlangGlobalSession,
Module as SlangModule,
@@ -236,7 +235,6 @@ pub struct Context
{
global_session: SlangGlobalSession,
session: Option<SlangSession>,
- modules: hashbrown::HashMap<AssetId, Module>,
programs: hashbrown::HashMap<AssetId, Program>,
}
@@ -245,6 +243,7 @@ impl Context
#[allow(clippy::missing_errors_doc)]
pub fn init_session(
&mut self,
+ opts: &ContextSessionInitOpts,
compile_targets: &[&CompileTarget],
) -> Result<Vec<CompileTargetId>, ContextSessionInitError>
{
@@ -257,10 +256,11 @@ impl Context
}
let session_options = shader_slang::CompilerOptions::default()
- .optimization(shader_slang::OptimizationLevel::None)
- .matrix_layout_row(true)
- .debug_information(SlangDebugInfoLevel::Maximal)
- .no_mangle(true);
+ .matrix_layout_row(true) // Matrices must be row major
+ .optimization(opts.optimization_level.into_slang())
+ .debug_information(opts.debug_info_level.into_slang())
+ .line_directive_mode(opts.line_directive_mode.into_slang())
+ .no_mangle(opts.no_mangle);
let (compile_target_ids, target_descs) = compile_targets
.iter()
@@ -271,7 +271,10 @@ impl Context
(
CompileTargetId { index: index as u32 },
shader_slang::TargetDesc::default()
- .format(compile_target.format.into_slang()),
+ .format(compile_target.format.into_slang())
+ // For some reason, the compiler options need to be specified
+ // for each target as well. Otherwise, they are not used
+ .options(&session_options),
)
})
.collect::<(Vec<_>, Vec<_>)>();
@@ -292,12 +295,6 @@ impl Context
}
#[must_use]
- pub fn get_module(&self, asset_id: &AssetId) -> Option<&Module>
- {
- self.modules.get(asset_id)
- }
-
- #[must_use]
pub fn get_program(&self, asset_id: &AssetId) -> Option<&Program>
{
self.programs.get(asset_id)
@@ -350,6 +347,139 @@ impl Context
}
}
+#[derive(Debug, Default, Clone)]
+#[non_exhaustive]
+pub struct ContextSessionInitOpts
+{
+ pub optimization_level: OptimizationLevel,
+ pub debug_info_level: DebugInfoLevel,
+ pub line_directive_mode: LineDirectiveMode,
+ pub no_mangle: bool,
+}
+
+impl ContextSessionInitOpts
+{
+ pub fn optimization_level(mut self, optimization_level: OptimizationLevel) -> Self
+ {
+ self.optimization_level = optimization_level;
+ self
+ }
+
+ pub fn debug_info_level(mut self, debug_info_level: DebugInfoLevel) -> Self
+ {
+ self.debug_info_level = debug_info_level;
+ self
+ }
+
+ pub fn line_directive_mode(mut self, line_directive_mode: LineDirectiveMode) -> Self
+ {
+ self.line_directive_mode = line_directive_mode;
+ self
+ }
+
+ pub fn no_mangle(mut self, no_mangle: bool) -> Self
+ {
+ self.no_mangle = no_mangle;
+ self
+ }
+}
+
+#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
+#[non_exhaustive]
+pub enum OptimizationLevel
+{
+ /// Don't optimize at all.
+ None,
+
+ /// Default optimization level: balance code quality and compilation time.
+ #[default]
+ Default,
+
+ /// Optimize aggressively.
+ High,
+
+ /// Include optimizations that may take a very long time, or may involve severe
+ /// space-vs-speed tradeoffs
+ Maximal,
+}
+
+impl OptimizationLevel
+{
+ fn into_slang(self) -> shader_slang::OptimizationLevel
+ {
+ match self {
+ Self::None => shader_slang::OptimizationLevel::None,
+ Self::Default => shader_slang::OptimizationLevel::Default,
+ Self::High => shader_slang::OptimizationLevel::High,
+ Self::Maximal => shader_slang::OptimizationLevel::Maximal,
+ }
+ }
+}
+
+#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
+#[non_exhaustive]
+pub enum DebugInfoLevel
+{
+ /// Don't emit debug information at all.
+ #[default]
+ None,
+
+ /// Emit as little debug information as possible, while still supporting stack
+ /// trackers.
+ Minimal,
+
+ /// Emit whatever is the standard level of debug information for each target.
+ Standard,
+
+ /// Emit as much debug information as possible for each target.
+ Maximal,
+}
+
+impl DebugInfoLevel
+{
+ fn into_slang(self) -> shader_slang::DebugInfoLevel
+ {
+ match self {
+ Self::None => shader_slang::DebugInfoLevel::None,
+ Self::Minimal => shader_slang::DebugInfoLevel::Minimal,
+ Self::Standard => shader_slang::DebugInfoLevel::Standard,
+ Self::Maximal => shader_slang::DebugInfoLevel::Maximal,
+ }
+ }
+}
+
+#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
+#[non_exhaustive]
+pub enum LineDirectiveMode
+{
+ /// Don't emit line directives at all.
+ #[default]
+ None,
+
+ /// Emit standard C-style `#line` directives.
+ Standard,
+
+ /// Emit GLSL-style directives with file *number* instead of name
+ Glsl,
+
+ /// Use a source map to track line mappings (ie no #line will appear in emitting
+ /// source)
+ SourceMap,
+}
+
+impl LineDirectiveMode
+{
+ fn into_slang(self) -> shader_slang::LineDirectiveMode
+ {
+ match self {
+ Self::None => shader_slang::LineDirectiveMode::None,
+ Self::Standard => shader_slang::LineDirectiveMode::Standard,
+ Self::Glsl => shader_slang::LineDirectiveMode::Glsl,
+ Self::SourceMap => shader_slang::LineDirectiveMode::SourceMap,
+ }
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CompileTargetId
{
@@ -757,7 +887,6 @@ pub(super) fn prepare(collector: &mut crate::ecs::extension::Collector<'_>)
.add_sole(Context {
global_session,
session: None,
- modules: hashbrown::HashMap::new(),
programs: hashbrown::HashMap::new(),
})
.ok();
@@ -816,8 +945,6 @@ fn load_modules(
}
};
- context.modules.insert(*asset_id, module.clone());
-
if !module_source.link_entrypoints.is_empty() {
assert!(!context.programs.contains_key(asset_id));