-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
62 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,68 @@ | ||
export MtlLibrary, MtlLibraryFromFile, MtlLibraryFromData | ||
export MTLLibrary, MTLLibraryFromFile, MTLLibraryFromData | ||
|
||
const MTLLibrary = Ptr{MtLibrary} | ||
@objcwrapper immutable=false MTLLibrary <: NSObject | ||
|
||
""" | ||
MTLDevice(i::Integer) | ||
# compatibility with cmt | ||
Base.unsafe_convert(T::Type{Ptr{MtLibrary}}, obj::MTLLibrary) = | ||
reinterpret(T, Base.unsafe_convert(id, obj)) | ||
MTLLibrary(ptr::Ptr{MtLibrary}) = MTLLibrary(reinterpret(id{MTLLibrary}, ptr)) | ||
|
||
Get a handle to a compute device. | ||
""" | ||
mutable struct MtlLibrary | ||
handle::MTLLibrary | ||
device::MTLDevice | ||
end | ||
|
||
Base.unsafe_convert(::Type{MTLLibrary}, lib::MtlLibrary) = lib.handle | ||
|
||
Base.:(==)(a::MtlLibrary, b::MtlLibrary) = a.handle == b.handle | ||
Base.hash(lib::MtlLibrary, h::UInt) = hash(lib.handle, h) | ||
function MTLLibrary(device::MTLDevice, src::String, | ||
opts::MTLCompileOptions=MTLCompileOptions()) | ||
err = Ref{id{NSError}}(nil) | ||
handle = @objc [device::id{MTLDevice} newLibraryWithSource:src::id{NSString} | ||
options:opts::id{MTLCompileOptions} | ||
error:err::Ptr{id{NSError}}]::id{MTLLibrary} | ||
err[] == nil || throw(NSError(err[])) | ||
|
||
function MtlLibrary(device::MTLDevice, src::String, opts::MTLCompileOptions=MTLCompileOptions()) | ||
handle = @mtlthrows _errptr mtNewLibraryWithSource(device, src, opts, _errptr) | ||
|
||
obj = MtlLibrary(handle, device) | ||
obj = MTLLibrary(handle) | ||
finalizer(unsafe_destroy!, obj) | ||
|
||
return obj | ||
end | ||
|
||
function MtlLibraryFromFile(device::MTLDevice, path::String) | ||
function MTLLibraryFromFile(device::MTLDevice, path::String) | ||
err = Ref{id{NSError}}(nil) | ||
handle = if macos_version() >= v"13" | ||
@mtlthrows _errptr mtNewLibraryWithURL(device, path, _errptr) | ||
url = NSFileURL(path) | ||
@objc [device::id{MTLDevice} newLibraryWithURL:url::id{NSURL} | ||
error:err::Ptr{id{NSError}}]::id{MTLLibrary} | ||
else | ||
@mtlthrows _errptr mtNewLibraryWithFile(device, path, _errptr) | ||
@objc [device::id{MTLDevice} newLibraryWithFile:path::id{NSString} | ||
error:err::Ptr{id{NSError}}]::id{MTLLibrary} | ||
end | ||
err[] == nil || throw(NSError(err[])) | ||
|
||
obj = MtlLibrary(handle, device) | ||
obj = MTLLibrary(handle) | ||
finalizer(unsafe_destroy!, obj) | ||
|
||
return obj | ||
end | ||
|
||
function MtlLibraryFromData(device::MTLDevice, data) | ||
GC.@preserve data begin | ||
handle = @mtlthrows _errptr mtNewLibraryWithData(device, pointer(data), sizeof(data), _errptr) | ||
function MTLLibraryFromData(device::MTLDevice, input_data) | ||
err = Ref{id{NSError}}(nil) | ||
GC.@preserve input_data begin | ||
data = dispatch_data(pointer(input_data), sizeof(input_data)) | ||
handle = @objc [device::id{MTLDevice} newLibraryWithData:data::dispatch_data_t | ||
error:err::Ptr{id{NSError}}]::id{MTLLibrary} | ||
end | ||
err[] == nil || throw(NSError(err[])) | ||
|
||
obj = MtlLibrary(handle, device) | ||
obj = MTLLibrary(handle) | ||
finalizer(unsafe_destroy!, obj) | ||
|
||
return obj | ||
end | ||
|
||
function unsafe_destroy!(lib::MtlLibrary) | ||
mtRelease(lib.handle) | ||
function unsafe_destroy!(lib::MTLLibrary) | ||
@objc [lib::id{MTLLibrary} release]::Nothing | ||
end | ||
|
||
|
||
## properties | ||
|
||
Base.propertynames(::MtlLibrary) = (:device, :label, :functionNames) | ||
|
||
function Base.getproperty(lib::MtlLibrary, f::Symbol) | ||
if f === :label | ||
ptr = mtLibraryLabel(lib) | ||
ptr == C_NULL ? nothing : unsafe_string(ptr) | ||
elseif f === :functionNames | ||
count = Ref{Csize_t}(0) | ||
mtLibraryFunctionNames(lib, count, C_NULL) | ||
names = Vector{Cstring}(undef, count[]) | ||
mtLibraryFunctionNames(lib, count, names) | ||
unsafe_string.(names) | ||
else | ||
getfield(lib, f) | ||
end | ||
end | ||
|
||
function Base.setproperty!(lib::MtlLibrary, f::Symbol, val) | ||
if f === :label | ||
mtLibraryLabelSet(lib, val) | ||
else | ||
setfield!(lib, f, val) | ||
end | ||
end | ||
|
||
|
||
## display | ||
|
||
function Base.show(io::IO, lib::MtlLibrary) | ||
print(io, "MtlLibrary($(lib.device))") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", lib::MtlLibrary) | ||
println(io, "MtlLibrary:") | ||
println(io, " device: ", lib.device) | ||
print(io, " label: ", lib.label) | ||
@objcproperties MTLLibrary begin | ||
@autoproperty device::id{MTLDevice} | ||
@autoproperty label::id{NSString} setter=setLabel | ||
@autoproperty functionNames::id{NSArray} type=Vector{NSString} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters