Skip to content

Commit

Permalink
Windows上ではGPUの情報を出すようにする (VOICEVOX#353)
Browse files Browse the repository at this point in the history
* Windows上ではGPUの情報を出すようにする

* リファクタ

* メッセージを変更

* メッセージを変更

Co-authored-by: Hiroshiba <[email protected]>

Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
qryxip and Hiroshiba authored Jan 3, 2023
1 parent 5312271 commit 84b4414
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
85 changes: 80 additions & 5 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ once_cell = "1.15.0"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85"
thiserror = "1.0.37"
tracing = { version = "0.1.37", features = ["log"] }
voicevox_core = { path = "crates/voicevox_core" }

# min-sized-rustを元にrelease buildのサイズが小さくなるようにした
Expand Down
5 changes: 5 additions & 0 deletions crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ surf = "2.3.2"
flate2 = "1.0.24"
tar = "0.4.38"
heck = "0.4.0"

[target."cfg(windows)".dependencies]
humansize = "2.1.2"
tracing.workspace = true
windows = { version = "0.43.0", features = ["Win32_Foundation", "Win32_Graphics_Dxgi"] }
41 changes: 41 additions & 0 deletions crates/voicevox_core/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ impl VoicevoxCore {
}

fn new() -> Self {
#[cfg(windows)]
list_windows_video_cards();

Self {
synthesis_engine: SynthesisEngine::new(
InferenceCore::new(false, None),
Expand Down Expand Up @@ -607,6 +610,44 @@ pub const fn error_result_to_message(result_code: VoicevoxResultCode) -> &'stati
}
}

#[cfg(windows)]
fn list_windows_video_cards() {
use std::{ffi::OsString, os::windows::ffi::OsStringExt as _};

use humansize::BINARY;
use tracing::{error, info};
use windows::Win32::Graphics::Dxgi::{
CreateDXGIFactory, IDXGIFactory, DXGI_ADAPTER_DESC, DXGI_ERROR_NOT_FOUND,
};

info!("検出されたGPU (DirectMLには1番目のGPUが使われます):");
match list_windows_video_cards() {
Ok(descs) => {
for desc in descs {
let description = OsString::from_wide(trim_nul(&desc.Description));
let vram = humansize::format_size(desc.DedicatedVideoMemory, BINARY);
info!(" - {description:?} ({vram})");
}
}
Err(err) => error!("{err}"),
}

fn list_windows_video_cards() -> windows::core::Result<Vec<DXGI_ADAPTER_DESC>> {
#[allow(unsafe_code)]
unsafe {
let factory = CreateDXGIFactory::<IDXGIFactory>()?;
(0..)
.map(|i| factory.EnumAdapters(i)?.GetDesc())
.take_while(|r| !matches!(r, Err(e) if e.code() == DXGI_ERROR_NOT_FOUND))
.collect()
}
}

fn trim_nul(s: &[u16]) -> &[u16] {
&s[..s.iter().position(|&c| c == 0x0000).unwrap_or(s.len())]
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core_python_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ pyo3 = { version = "0.17.2", features = ["abi3-py38", "extension-module"] }
pyo3-log = "0.7.0"
serde.workspace = true
serde_json.workspace = true
tracing = { version = "0.1.37", features = ["log"] }
tracing.workspace = true
voicevox_core.workspace = true

0 comments on commit 84b4414

Please sign in to comment.