mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Add Utf16SourceLocation and support having it in CompilationMessage (#10294)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Co-authored-by: Andy Leiserson <aleiserson@mozilla.com>
This commit is contained in:
1 parent
aed54364fe
commit
237037257f
4 files changed
+113
-47
No files matched your search
@@ -118,6 +118,8 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109).
|
||||
|
||||
By @jinleili in [#10308](https://github.com/gfx-rs/wgpu/pull/10308).
|
||||
|
||||
- Added `Utf16SourceLocation` which is analogue to `SourceLocation` but using UTF-16 code units. Added `Utf16SourceLocation::to_utf8` and `SourceLocation::to_utf16` to convert between them. By @sagudev in [#10294](https://github.com/gfx-rs/wgpu/pull/10294).
|
||||
|
||||
#### Naga
|
||||
|
||||
- Add `@builtin(hit_barycentrics)`, a `vec2<f32>` readable in `@any_hit` and `@closest_hit` ray tracing pipeline shaders, holding two of the barycentric coordinates of the hit point on the triangle (the third is `1.0 - x - y`). Currently only supported with the SPIR-V backend. By @JMS55 in [#10193](https://github.com/gfx-rs/wgpu/pull/10193).
|
||||
|
||||
+4
-11
@@ -129,22 +129,15 @@ impl GPUCompilationMessage {
|
||||
|
||||
match loc {
|
||||
Some(loc) => {
|
||||
let len_utf16 = |s: &str| s.chars().map(|c| c.len_utf16() as u64).sum();
|
||||
|
||||
let start = loc.offset as usize;
|
||||
|
||||
// Naga reports a `line_pos` using UTF-8 bytes, so we cannot use it.
|
||||
let line_start =
|
||||
source[0..start].rfind('\n').map(|pos| pos + 1).unwrap_or(0);
|
||||
let line_pos = len_utf16(&source[line_start..start]) + 1;
|
||||
let loc = loc.to_utf16(source);
|
||||
|
||||
Self {
|
||||
message,
|
||||
r#type: GPUCompilationMessageType::Error,
|
||||
line_num: loc.line_number.into(),
|
||||
line_pos,
|
||||
offset: len_utf16(&source[0..start]),
|
||||
length: len_utf16(&source[start..start + loc.length as usize]),
|
||||
line_pos: loc.line_position.into(),
|
||||
offset: loc.offset.into(),
|
||||
length: loc.length.into(),
|
||||
}
|
||||
}
|
||||
_ => Self {
|
||||
|
||||
@@ -7,27 +7,35 @@ use serde::{Deserialize, Serialize};
|
||||
/// Compilation information for a shader module.
|
||||
///
|
||||
/// Corresponds to [WebGPU `GPUCompilationInfo`](https://gpuweb.github.io/gpuweb/#gpucompilationinfo).
|
||||
/// The source locations use bytes, and index a UTF-8 encoded string.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
/// The source locations use bytes, and index a UTF-8 or UTF-16 encoded string.
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct CompilationInfo {
|
||||
pub struct CompilationInfo<SL = SourceLocation> {
|
||||
/// The messages from the shader compilation process.
|
||||
pub messages: Vec<CompilationMessage>,
|
||||
pub messages: Vec<CompilationMessage<SL>>,
|
||||
}
|
||||
|
||||
impl<SL> Default for CompilationInfo<SL> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
messages: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A single message from the shader compilation process.
|
||||
///
|
||||
/// Roughly corresponds to [`GPUCompilationMessage`](https://www.w3.org/TR/webgpu/#gpucompilationmessage),
|
||||
/// except that the location uses UTF-8 for all positions.
|
||||
/// except that the location may use UTF-8 for all positions.
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct CompilationMessage {
|
||||
pub struct CompilationMessage<SL = SourceLocation> {
|
||||
/// The text of the message.
|
||||
pub message: String,
|
||||
/// The type of the message.
|
||||
pub message_type: CompilationMessageType,
|
||||
/// Where in the source code the message points at.
|
||||
pub location: Option<SourceLocation>,
|
||||
pub location: Option<SL>,
|
||||
}
|
||||
|
||||
/// The type of a compilation message.
|
||||
@@ -50,6 +58,8 @@ pub enum CompilationMessageType {
|
||||
/// - `offset` and `length` are in bytes (UTF-8 code units), instead of UTF-16 code units.
|
||||
/// - `line_position` is in bytes (UTF-8 code units), and is usually not directly intended for humans.
|
||||
///
|
||||
/// For UTF-16 code units, see [`Utf16SourceLocation`].
|
||||
///
|
||||
/// [gcm]: https://www.w3.org/TR/webgpu/#gpucompilationmessage
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
@@ -59,8 +69,87 @@ pub struct SourceLocation {
|
||||
/// 1-based column in code units (in bytes) of the start of the span.
|
||||
/// Remember to convert accordingly when displaying to the user.
|
||||
pub line_position: u32,
|
||||
/// 0-based Offset in code units (in bytes) of the start of the span.
|
||||
/// 0-based offset in code units (in bytes) of the start of the span.
|
||||
pub offset: u32,
|
||||
/// Length in code units (in bytes) of the span.
|
||||
pub length: u32,
|
||||
}
|
||||
|
||||
/// A human-readable representation for a span, tailored for text source.
|
||||
///
|
||||
/// Corresponds to the positional members of [`GPUCompilationMessage`][gcm] from
|
||||
/// the WebGPU specification.
|
||||
///
|
||||
/// Unlike [`SourceLocation`], which uses UTF-8 units as a convenience for the
|
||||
/// `wgpu` Rust API, this struct locates the span using UTF-16 code units, just
|
||||
/// like the WebGPU specification.
|
||||
///
|
||||
/// [gcm]: https://www.w3.org/TR/webgpu/#gpucompilationmessage
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct Utf16SourceLocation {
|
||||
/// 1-based line number.
|
||||
pub line_number: u32,
|
||||
/// 1-based column in UTF-16 code units of the start of the span.
|
||||
/// Remember to convert accordingly when displaying to the user.
|
||||
pub line_position: u32,
|
||||
/// 0-based offset in UTF-16 code units of the start of the span.
|
||||
pub offset: u32,
|
||||
/// Length in UTF-16 code units of the span.
|
||||
pub length: u32,
|
||||
}
|
||||
|
||||
impl SourceLocation {
|
||||
/// Converts a `SourceLocation` in UTF-8 code units to UTF-16 code units.
|
||||
pub fn to_utf16(&self, source: &str) -> Utf16SourceLocation {
|
||||
let len_utf16 = |s: &str| s.chars().map(|c| c.len_utf16() as u32).sum::<u32>();
|
||||
let start = self.offset as usize;
|
||||
let end = start + self.length as usize;
|
||||
let utf16_offset = len_utf16(&source[..start]);
|
||||
let utf16_length = len_utf16(&source[start..end]);
|
||||
|
||||
let line_start = source[..start].rfind('\n').map_or(0, |pos| pos + 1);
|
||||
let utf16_line_position = len_utf16(&source[line_start..start]) + 1;
|
||||
|
||||
Utf16SourceLocation {
|
||||
line_number: self.line_number,
|
||||
line_position: utf16_line_position,
|
||||
offset: utf16_offset,
|
||||
length: utf16_length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Utf16SourceLocation {
|
||||
/// Converts a `SourceLocation` in UTF-16 code units to UTF-8 code units.
|
||||
pub fn to_utf8(&self, source: &str) -> SourceLocation {
|
||||
fn map_utf16_to_utf8_offset(utf16_offset: u32, text: &str) -> u32 {
|
||||
let mut utf16_i = 0;
|
||||
for (utf8_index, c) in text.char_indices() {
|
||||
if utf16_i >= utf16_offset {
|
||||
return utf8_index as u32;
|
||||
}
|
||||
utf16_i += c.len_utf16() as u32;
|
||||
}
|
||||
if utf16_i >= utf16_offset {
|
||||
text.len() as u32
|
||||
} else {
|
||||
log::error!("UTF16 offset {utf16_offset} is out of bounds for string {text}");
|
||||
u32::MAX
|
||||
}
|
||||
}
|
||||
let utf8_offset = map_utf16_to_utf8_offset(self.offset, source);
|
||||
let utf8_length = map_utf16_to_utf8_offset(self.length, &source[utf8_offset as usize..]);
|
||||
|
||||
let prefix = &source[..utf8_offset as usize];
|
||||
let line_start = prefix.rfind('\n').map(|pos| pos + 1).unwrap_or(0) as u32;
|
||||
let utf8_line_position = utf8_offset - line_start + 1; // Counting UTF-8 bytes
|
||||
|
||||
SourceLocation {
|
||||
line_number: self.line_number,
|
||||
line_position: utf8_line_position,
|
||||
offset: utf8_offset,
|
||||
length: utf8_length,
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-28
@@ -147,22 +147,6 @@ enum WebShaderCompilationInfo {
|
||||
},
|
||||
}
|
||||
|
||||
fn map_utf16_to_utf8_offset(utf16_offset: u32, text: &str) -> u32 {
|
||||
let mut utf16_i = 0;
|
||||
for (utf8_index, c) in text.char_indices() {
|
||||
if utf16_i >= utf16_offset {
|
||||
return utf8_index as u32;
|
||||
}
|
||||
utf16_i += c.len_utf16() as u32;
|
||||
}
|
||||
if utf16_i >= utf16_offset {
|
||||
text.len() as u32
|
||||
} else {
|
||||
log::error!("UTF16 offset {utf16_offset} is out of bounds for string {text}");
|
||||
u32::MAX
|
||||
}
|
||||
}
|
||||
|
||||
fn compilation_message_from_js(
|
||||
js_message: webgpu_sys::GpuCompilationMessage,
|
||||
compilation_info: &WebShaderCompilationInfo,
|
||||
@@ -178,20 +162,18 @@ fn compilation_message_from_js(
|
||||
let span = match compilation_info {
|
||||
WebShaderCompilationInfo::Wgsl { .. } if utf16_offset == 0 && utf16_length == 0 => None,
|
||||
WebShaderCompilationInfo::Wgsl { source } => {
|
||||
let offset = map_utf16_to_utf8_offset(utf16_offset, source);
|
||||
let length = map_utf16_to_utf8_offset(utf16_length, &source[offset as usize..]);
|
||||
let line_number = js_message.line_num() as u32; // That's legal, because we're counting lines the same way
|
||||
let utf16_line_position = js_message.line_pos() as u32;
|
||||
|
||||
let prefix = &source[..offset as usize];
|
||||
let line_start = prefix.rfind('\n').map(|pos| pos + 1).unwrap_or(0) as u32;
|
||||
let line_position = offset - line_start + 1; // Counting UTF-8 byte indices
|
||||
|
||||
Some(crate::SourceLocation {
|
||||
offset,
|
||||
length,
|
||||
line_number,
|
||||
line_position,
|
||||
})
|
||||
Some(
|
||||
wgt::Utf16SourceLocation {
|
||||
offset: utf16_offset,
|
||||
length: utf16_length,
|
||||
line_number,
|
||||
line_position: utf16_line_position,
|
||||
}
|
||||
.to_utf8(source),
|
||||
)
|
||||
}
|
||||
WebShaderCompilationInfo::Transformed { .. } => None,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user