Skip to content

Commit

Permalink
preprocess: support latin-1 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Jan 28, 2023
1 parent b5ce7ce commit 43b7d6f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions librashader-preprocess/Cargo.toml
Expand Up @@ -16,6 +16,7 @@ thiserror = "1.0.37"
nom = "7.1.1"
librashader-common = { path = "../librashader-common", version = "0.1.0-beta.9" }
rustc-hash = "1.1.0"
encoding_rs = "0.8.31"

[features]
default = [ "line_directives" ]
Expand Down
3 changes: 3 additions & 0 deletions librashader-preprocess/src/error.rs
Expand Up @@ -11,6 +11,9 @@ pub enum PreprocessError {
/// An IO error occurred when reading the source file.
#[error("the file was not found during resolution")]
IOError(PathBuf, std::io::Error),
/// A known encoding was not found for the file.
#[error("a known encoding was not found for the file. supported encodings are UTF-8 and Latin-1")]
EncodingError(PathBuf),
/// Unexpected EOF when reading the source file.
#[error("unexpected end of file")]
UnexpectedEof,
Expand Down
30 changes: 27 additions & 3 deletions librashader-preprocess/src/include.rs
Expand Up @@ -3,18 +3,42 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::str::Lines;
use encoding_rs::{DecoderResult, WINDOWS_1252};

#[cfg(feature = "line_directives")]
const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &str =
"#extension GL_GOOGLE_cpp_style_line_directive : require";

fn read_file(path: impl AsRef<Path>) -> Result<String, PreprocessError> {
let path = path.as_ref();
let mut source = String::new();
let mut buf = Vec::new();
File::open(path)
.and_then(|mut f| f.read_to_string(&mut source))
.and_then(|mut f| {
f.read_to_end(&mut buf)?;
Ok(())
})
.map_err(|e| PreprocessError::IOError(path.to_path_buf(), e))?;
Ok(source)

match String::from_utf8(buf) {
Ok(s) => Ok(s),
Err(e) => {
let buf = e.into_bytes();
let decoder = WINDOWS_1252.new_decoder();
let Some(len) = decoder.max_utf8_buffer_length_without_replacement(buf.len()) else {
return Err(PreprocessError::EncodingError(path.to_path_buf()))
};

let mut latin1_string = String::with_capacity(len);

let (result, _) = WINDOWS_1252.new_decoder()
.decode_to_string_without_replacement(&buf, &mut latin1_string, true);
if result == DecoderResult::InputEmpty {
Ok(latin1_string)
} else {
Err(PreprocessError::EncodingError(path.to_path_buf()))
}
}
}
}

pub fn read_source(path: impl AsRef<Path>) -> Result<String, PreprocessError> {
Expand Down
2 changes: 1 addition & 1 deletion librashader-reflect/src/reflect/cross.rs
Expand Up @@ -261,7 +261,7 @@ where
blame: SemanticErrorBlame,
) -> Result<u32, ShaderReflectError> {
let size = ast.get_declared_struct_size(push.base_type_id)?;
if size >= MAX_PUSH_BUFFER_SIZE {
if size > MAX_PUSH_BUFFER_SIZE {
return Err(blame.error(SemanticsErrorKind::InvalidPushBufferSize(size)));
}
Ok(size)
Expand Down
2 changes: 1 addition & 1 deletion librashader-runtime-d3d11/src/lib.rs
Expand Up @@ -33,7 +33,7 @@ mod tests {
#[test]
fn triangle_d3d11() {
let sample = hello_triangle::d3d11_hello_triangle::Sample::new(
"../test/slang-shaders/crt/crt-lottes.slangp",
"../test/slang-shaders/presets/crt-royale-kurozumi.slangp",
// "../test/basic.slangp",
Some(&FilterChainOptionsD3D11 {
use_deferred_context: false,
Expand Down

0 comments on commit 43b7d6f

Please sign in to comment.