remove static linking of ags

This commit is contained in:
Junkui Zhang 2025-07-28 17:28:58 +08:00
parent ac1ea0f96d
commit a9058346bf
5 changed files with 43 additions and 54 deletions

View file

@ -251,9 +251,6 @@ mod windows {
};
pub(super) fn build() {
// Link the AMD AGS library
link_amd_ags();
// Compile HLSL shaders
#[cfg(not(debug_assertions))]
compile_shaders();
@ -263,24 +260,6 @@ mod windows {
embed_resource();
}
fn link_amd_ags() {
// We can not use relative paths in `cargo:rustc-link-search`, so we need to use the absolute path.
// See: https://stackoverflow.com/questions/41917096/how-do-i-make-rustc-link-search-relative-to-the-project-location
let lib_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("libs");
#[cfg(target_pointer_width = "64")]
let lib_name = "amd_ags_x64_2022_MT";
#[cfg(target_pointer_width = "32")]
let lib_name = "amd_ags_x86_2022_MT";
println!("cargo:rustc-link-lib=static={}", lib_name);
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!(
"cargo:rerun-if-changed={}/{}.lib",
lib_dir.display(),
lib_name
);
}
#[cfg(feature = "windows-manifest")]
fn embed_resource() {
let manifest = std::path::Path::new("resources/windows/gpui.manifest.xml");
@ -331,6 +310,13 @@ mod windows {
/// You can set the `GPUI_FXC_PATH` environment variable to specify the path to the fxc.exe compiler.
fn find_fxc_compiler() -> String {
// Check environment variable
if let Ok(path) = std::env::var("GPUI_FXC_PATH") {
if Path::new(&path).exists() {
return path;
}
}
// Try to find in PATH
if let Ok(output) = std::process::Command::new("where").arg("fxc.exe").output() {
if output.status.success() {
@ -347,13 +333,6 @@ mod windows {
.to_string();
}
// Check environment variable
if let Ok(path) = std::env::var("GPUI_FXC_PATH") {
if Path::new(&path).exists() {
return path;
}
}
panic!("Failed to find fxc.exe");
}

View file

@ -1,10 +0,0 @@
The files in this folder are required for the Windows platform support in the gpui library.
#### amd_ags_x64.lib & amd_ags_x86.lib
These libraries are used for AMD GPU support, currently only used on Windows, mainly for retrieving GPU information and capabilities. They are linked against the AMD AGS (AMD GPU Services) library.
The official AMD AGS documentation can be found [here](https://gpuopen.com/amd-gpu-services-ags-library). And these two files are grabbed from the [official AMD AGS repository](https://github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK), currently at version 6.3.0.
If you want to update these files, don't forget to update the value of `AGS_CURRENT_VERSION` in `gpui/src/platform/windows/directx_renderer.rs` as well.

View file

@ -1607,6 +1607,12 @@ mod nvidia {
mod amd {
use std::os::raw::{c_char, c_int, c_void};
use anyhow::{Context, Result};
use windows::{
Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA},
core::s,
};
// https://github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK/blob/5d8812d703d0335741b6f7ffc37838eeb8b967f7/ags_lib/inc/amd_ags.h#L145
const AGS_CURRENT_VERSION: i32 = (6 << 22) | (3 << 12) | 0;
@ -1625,19 +1631,36 @@ mod amd {
pub devices: *mut c_void,
}
unsafe extern "C" {
fn agsInitialize(
version: c_int,
config: *const c_void,
context: *mut *mut AGSContext,
gpu_info: *mut AGSGPUInfo,
) -> c_int;
// https://github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK/blob/5d8812d703d0335741b6f7ffc37838eeb8b967f7/ags_lib/inc/amd_ags.h#L429
#[allow(non_camel_case_types)]
type agsInitialize_t = unsafe extern "C" fn(
version: c_int,
config: *const c_void,
context: *mut *mut AGSContext,
gpu_info: *mut AGSGPUInfo,
) -> c_int;
fn agsDeInitialize(context: *mut AGSContext) -> c_int;
}
// https://github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK/blob/5d8812d703d0335741b6f7ffc37838eeb8b967f7/ags_lib/inc/amd_ags.h#L436
#[allow(non_camel_case_types)]
type agsDeInitialize_t = unsafe extern "C" fn(context: *mut AGSContext) -> c_int;
pub(super) fn get_driver_version() -> anyhow::Result<String> {
pub(super) fn get_driver_version() -> Result<String> {
unsafe {
#[cfg(target_pointer_width = "64")]
let amd_dll =
LoadLibraryA(s!("amd_ags_x64.dll")).context("Failed to load AMD AGS library")?;
#[cfg(target_pointer_width = "32")]
let amd_dll =
LoadLibraryA(s!("amd_ags_x86.dll")).context("Failed to load AMD AGS library")?;
let ags_initialize_addr = GetProcAddress(amd_dll, s!("agsInitialize"))
.ok_or_else(|| anyhow::anyhow!("Failed to get agsInitialize address"))?;
let ags_deinitialize_addr = GetProcAddress(amd_dll, s!("agsDeInitialize"))
.ok_or_else(|| anyhow::anyhow!("Failed to get agsDeInitialize address"))?;
let ags_initialize: agsInitialize_t = std::mem::transmute(ags_initialize_addr);
let ags_deinitialize: agsDeInitialize_t = std::mem::transmute(ags_deinitialize_addr);
let mut context: *mut AGSContext = std::ptr::null_mut();
let mut gpu_info: AGSGPUInfo = AGSGPUInfo {
driver_version: std::ptr::null(),
@ -1646,17 +1669,14 @@ mod amd {
devices: std::ptr::null_mut(),
};
let result = agsInitialize(
let result = ags_initialize(
AGS_CURRENT_VERSION,
std::ptr::null(),
&mut context,
&mut gpu_info,
);
if result != 0 {
return Err(anyhow::anyhow!(
"Failed to initialize AGS, error code: {}",
result
));
anyhow::bail!("Failed to initialize AMD AGS, error code: {}", result);
}
// Vulkan acctually returns this as the driver version
@ -1676,7 +1696,7 @@ mod amd {
"Unknown Radeon Driver Version".to_string()
};
agsDeInitialize(context);
ags_deinitialize(context);
Ok(format!("{} ({})", software_version, driver_version))
}
}