1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::back::msl::{MslVersion, NagaMslContext, NagaMslModule};
use crate::back::targets::MSL;
use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::error::ShaderCompileError;
use crate::reflect::naga::{NagaLoweringOptions, NagaReflect};
use naga::back::msl::{
BindSamplerTarget, BindTarget, EntryPointResources, Options, PipelineOptions, TranslationInfo,
};
use naga::valid::{Capabilities, ValidationFlags};
use naga::{Module, TypeInner};
fn msl_version_to_naga_msl(version: MslVersion) -> (u8, u8) {
(version.major as u8, version.minor as u8)
}
impl CompileShader<MSL> for NagaReflect {
type Options = Option<crate::back::msl::MslVersion>;
type Context = NagaMslContext;
fn compile(
mut self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
// https://github.com/libretro/RetroArch/blob/434e94c782af2e4d4277a24b7ed8e5fc54870088/gfx/drivers_shader/slang_process.cpp#L524
let lang_version = msl_version_to_naga_msl(options.unwrap_or(MslVersion::new(2, 0, 0)));
let mut vert_options = Options {
lang_version,
per_entry_point_map: Default::default(),
inline_samplers: vec![],
spirv_cross_compatibility: true,
fake_missing_bindings: false,
bounds_check_policies: Default::default(),
zero_initialize_workgroup_memory: false,
force_loop_bounding: false,
task_dispatch_limits: None,
mesh_shader_primitive_indices_clamp: false,
emit_int_div_checks: true,
ray_query_initialization_tracking: false,
};
let mut frag_options = vert_options.clone();
fn write_msl(
module: &Module,
options: Options,
) -> Result<(String, TranslationInfo), ShaderCompileError> {
let mut valid =
naga::valid::Validator::new(ValidationFlags::all(), Capabilities::empty());
let info = valid.validate(&module)?;
let pipeline_options = PipelineOptions {
entry_point: None,
allow_and_force_point_size: false,
vertex_pulling_transform: false,
vertex_buffer_mappings: vec![],
binding_array_length_map: Default::default(),
};
let msl = naga::back::msl::write_string(&module, &info, &options, &pipeline_options)?;
Ok(msl)
}
fn generate_bindings(module: &Module) -> EntryPointResources {
let mut resources = EntryPointResources::default();
let binding_map = &mut resources.resources;
// Don't set PCB because they'll be gone after lowering..
// resources.push_constant_buffer = Some(1u8);
for (_, variable) in module.global_variables.iter() {
let Some(binding) = &variable.binding else {
continue;
};
let Ok(ty) = module.types.get_handle(variable.ty) else {
continue;
};
match ty.inner {
TypeInner::Sampler { .. } => {
binding_map.insert(
binding.clone(),
BindTarget {
buffer: None,
texture: None,
sampler: Some(BindSamplerTarget::Resource(binding.binding as u8)),
external_texture: None,
mutable: false,
},
);
}
TypeInner::Struct { .. } => {
binding_map.insert(
binding.clone(),
BindTarget {
buffer: Some(binding.binding as u8),
texture: None,
sampler: None,
external_texture: None,
mutable: false,
},
);
}
TypeInner::Image { .. } => {
binding_map.insert(
binding.clone(),
BindTarget {
buffer: None,
texture: Some(binding.binding as u8),
sampler: None,
external_texture: None,
mutable: false,
},
);
}
_ => continue,
}
}
resources
}
self.do_lowering(&NagaLoweringOptions {
write_pcb_as_ubo: true,
sampler_bind_group: 1,
suppress_derivative_uniformity: false,
});
frag_options
.per_entry_point_map
.insert(String::from("main"), generate_bindings(&self.fragment));
vert_options
.per_entry_point_map
.insert(String::from("main"), generate_bindings(&self.vertex));
let fragment = write_msl(&self.fragment, frag_options)?;
let vertex = write_msl(&self.vertex, vert_options)?;
let vertex_binding = self.get_next_binding(0);
Ok(ShaderCompilerOutput {
vertex: vertex.0,
fragment: fragment.0,
context: NagaMslContext {
fragment: NagaMslModule {
translation_info: fragment.1,
module: self.fragment,
},
vertex: NagaMslModule {
translation_info: vertex.1,
module: self.vertex,
},
next_free_binding: vertex_binding,
},
})
}
fn compile_boxed(
self: Box<Self>,
options: Self::Options,
) -> Result<ShaderCompilerOutput<String, Self::Context>, ShaderCompileError> {
<NagaReflect as CompileShader<MSL>>::compile(*self, options)
}
}
#[cfg(test)]
mod test {
use crate::back::targets::MSL;
use crate::back::{CompileShader, FromCompilation};
use crate::reflect::naga::{Naga, NagaLoweringOptions};
use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics};
use crate::reflect::ReflectShader;
use bitflags::Flags;
use librashader_common::map::{FastHashMap, ShortString};
use librashader_preprocess::ShaderSource;
// use spirv_cross::msl;
// #[test]
// pub fn test_into() {
// let result = ShaderSource::load("../test/basic.slang").unwrap();
//
// let mut uniform_semantics: FastHashMap<ShortString, UniformSemantic> = Default::default();
//
// for (_index, param) in result.parameters.iter().enumerate() {
// uniform_semantics.insert(
// param.1.id.clone(),
// UniformSemantic::Unique(Semantic {
// semantics: UniqueSemantics::FloatParameter,
// index: (),
// }),
// );
// }
//
// let compilation = crate::front::SpirvCompilation::try_from(&result).unwrap();
//
// let mut msl = <MSL as FromCompilation<_, Naga>>::from_compilation(compilation).unwrap();
//
// msl.reflect(
// 0,
// &ShaderSemantics {
// uniform_semantics,
// texture_semantics: Default::default(),
// },
// )
// .expect("");
//
// let compiled = msl.compile(Some(msl::Version::V2_0)).unwrap();
//
// println!(
// "{:?}",
// compiled.context.fragment.translation_info.entry_point_names
// );
// }
}