Module: WGPU::Native::Distribution

Defined in:
lib/wgpu/native/distribution.rb

Constant Summary collapse

VERSION =
"v27.0.4.0"
RELEASE_BASE_URL =
"https://github.com/gfx-rs/wgpu-native/releases/download/#{VERSION}"
VERSION_COMPONENTS =
VERSION.delete_prefix("v").split(".").map { |part| Integer(part, 10) }.freeze
ENCODED_VERSION =

wgpuGetVersion packs vMAJOR.MINOR.PATCH.BUILD into four big-endian bytes.

VERSION_COMPONENTS.reduce(0) { |encoded, part| (encoded << 8) | part }
UNIMPLEMENTED_CAPABILITIES =
{
  compilation_info: [VERSION].freeze,
  buffer_map_state: [VERSION].freeze,
  pipeline_async: [VERSION].freeze
}.freeze
ARTIFACTS =
[
  {
    pattern: /x86_64-linux/,
    archive: "wgpu-linux-x86_64-release.zip",
    library: "libwgpu_native.so",
    sha256: "271481ef76fbf3ea09631a6079e9493636ecf813cd9c92306c44a1a452991ba1"
  },
  {
    pattern: /aarch64-linux/,
    archive: "wgpu-linux-aarch64-release.zip",
    library: "libwgpu_native.so",
    sha256: "a2f22248200997b69373273b10d50a58164f6ed840877289f3e46bff317b134e"
  },
  {
    pattern: /x86_64-darwin/,
    archive: "wgpu-macos-x86_64-release.zip",
    library: "libwgpu_native.dylib",
    sha256: "660fe9be59b555ec1d7c839e5cf8b6c71762938af61ab444a7a58dd87970dba2"
  },
  {
    pattern: /arm64-darwin/,
    archive: "wgpu-macos-aarch64-release.zip",
    library: "libwgpu_native.dylib",
    sha256: "15367c26fdbe6892db35007d39f3883593384e777360b70e6bd704cb5dedde53"
  },
  {
    pattern: /(?:x64|x86_64)-(?:mingw|mswin)/,
    archive: "wgpu-windows-x86_64-msvc-release.zip",
    library: "wgpu_native.dll",
    sha256: "f14ca334b4d253881bde2605bd147f332178d705f56fbd74f81458797c77fce1"
  }
].map(&:freeze).freeze

Class Method Summary collapse

Class Method Details

.artifact_for(platform = RUBY_PLATFORM) ⇒ Hash

Selects the native release artifact for a Ruby platform.

Parameters:

  • platform (String) (defaults to: RUBY_PLATFORM)

    Ruby platform identifier

Returns:

  • (Hash)

    artifact metadata

Raises:

  • (LoadError)

    if the platform is unsupported



61
62
63
64
65
66
67
68
# File 'lib/wgpu/native/distribution.rb', line 61

def artifact_for(platform = RUBY_PLATFORM)
  raise LoadError, unsupported_platform_message(platform) if platform.include?("musl")

  artifact = ARTIFACTS.find { |candidate| candidate[:pattern].match?(platform) }
  return artifact if artifact

  raise LoadError, unsupported_platform_message(platform)
end

.cache_directories(env: ENV, home: Dir.home, host_os: ) ⇒ Array<String>

Returns cache directories in lookup order.

Parameters:

  • env (Hash) (defaults to: ENV)

    environment used for cache overrides

  • home (String) (defaults to: Dir.home)

    user home directory

  • host_os (String) (defaults to: )

    host operating-system identifier

Returns:

  • (Array<String>)

    unique cache directories



100
101
102
# File 'lib/wgpu/native/distribution.rb', line 100

def cache_directories(env: ENV, home: Dir.home, host_os: RbConfig::CONFIG["host_os"])
  [primary_cache_dir(env:, home:, host_os:), legacy_cache_dir(home:)].uniq
end

.capability_implemented?(name) ⇒ Boolean

Reports whether a native capability is implemented by the pinned release.

Parameters:

  • name (Symbol)

    capability name

Returns:

  • (Boolean)


138
139
140
# File 'lib/wgpu/native/distribution.rb', line 138

def capability_implemented?(name)
  !UNIMPLEMENTED_CAPABILITIES.fetch(name, []).include?(VERSION)
end

.legacy_cache_dir(home: Dir.home) ⇒ String

Returns the legacy versioned cache directory.

Parameters:

  • home (String) (defaults to: Dir.home)

    user home directory

Returns:

  • (String)

    absolute legacy cache directory



90
91
92
# File 'lib/wgpu/native/distribution.rb', line 90

def legacy_cache_dir(home: Dir.home)
  File.join(File.expand_path(home), ".cache", "wgpu-ruby", VERSION)
end

.library_paths(platform: RUBY_PLATFORM, env: ENV, home: Dir.home, host_os: ) ⇒ Array<String>

Returns candidate cached library paths for a platform.

Parameters:

  • platform (String) (defaults to: RUBY_PLATFORM)

    Ruby platform identifier

  • env (Hash) (defaults to: ENV)

    environment used for cache overrides

  • home (String) (defaults to: Dir.home)

    user home directory

  • host_os (String) (defaults to: )

    host operating-system identifier

Returns:

  • (Array<String>)

    candidate shared-library paths

Raises:

  • (LoadError)

    if the platform is unsupported



112
113
114
115
116
# File 'lib/wgpu/native/distribution.rb', line 112

def library_paths(platform: RUBY_PLATFORM, env: ENV, home: Dir.home,
                  host_os: RbConfig::CONFIG["host_os"])
  library = artifact_for(platform)[:library]
  cache_directories(env:, home:, host_os:).map { |directory| File.join(directory, "lib", library) }
end

.primary_cache_dir(env: ENV, home: Dir.home, host_os: ) ⇒ String

Returns the preferred versioned native-library cache directory.

Parameters:

  • env (Hash) (defaults to: ENV)

    environment used for cache overrides

  • home (String) (defaults to: Dir.home)

    user home directory

  • host_os (String) (defaults to: )

    host operating-system identifier

Returns:

  • (String)

    absolute cache directory



76
77
78
79
80
81
82
83
84
# File 'lib/wgpu/native/distribution.rb', line 76

def primary_cache_dir(env: ENV, home: Dir.home, host_os: RbConfig::CONFIG["host_os"])
  override = present_value(env["WGPU_CACHE_DIR"])
  return File.join(File.expand_path(override), VERSION) if override

  xdg_cache = present_value(env["XDG_CACHE_HOME"])
  return File.join(File.expand_path(xdg_cache), "wgpu-ruby", VERSION) if xdg_cache

  File.join(default_cache_base(env:, home:, host_os:), "wgpu-ruby", VERSION)
end

.release_url(artifact) ⇒ String

Builds the download URL for an artifact entry.

Parameters:

Returns:

  • (String)

    release archive URL



122
123
124
# File 'lib/wgpu/native/distribution.rb', line 122

def release_url(artifact)
  "#{RELEASE_BASE_URL}/#{artifact.fetch(:archive)}"
end

.unsupported_platform_message(platform) ⇒ String

Builds an actionable unsupported-platform message.

Parameters:

  • platform (String)

    Ruby platform identifier

Returns:

  • (String)

    diagnostic message



146
147
148
149
150
151
152
153
# File 'lib/wgpu/native/distribution.rb', line 146

def unsupported_platform_message(platform)
  supported = ARTIFACTS.map { |artifact| artifact[:pattern].inspect }.join(", ")
  <<~MESSAGE.chomp
    Unsupported platform: #{platform}
    Supported 64-bit platforms: #{supported}
    Build wgpu-native for this host and set WGPU_LIB_PATH to the shared library.
  MESSAGE
end

.version_string(encoded_version) ⇒ String

Decodes the packed native version into a release tag.

Parameters:

  • encoded_version (Integer)

    four-byte packed version

Returns:

  • (String)

    version in vMAJOR.MINOR.PATCH.BUILD form



130
131
132
133
# File 'lib/wgpu/native/distribution.rb', line 130

def version_string(encoded_version)
  components = [24, 16, 8, 0].map { |shift| (encoded_version >> shift) & 0xFF }
  "v#{components.join(".")}"
end