MetalCompute 1.0
An API to make GPU compute calls easier
Loading...
Searching...
No Matches
MTLComputeKernel.hpp
Go to the documentation of this file.
2
3#pragma once
4
5namespace MTLCompute {
6
7 class Kernel {
8
9 private:
10 MTL::Device *gpu;
11 MTL::Library *library;
12 MTL::Function *function;
13 MTL::ComputePipelineState *pipeline;
14
15 public:
16
27 Kernel(MTL::Device *gpu, const std::string &filename) {
28 this->gpu = gpu;
29
30 this->library = this->gpu->newLibrary(NS::URL::fileURLWithPath(NS::String::string(filename.c_str(), \
31 NS::ASCIIStringEncoding)), nullptr);
32
33 if (this->library == nullptr) {
34 std::cerr << "Error: Could not load library " << filename << std::endl;
35 }
36
37 }
38
50 Kernel(MTL::Device *gpu, const std::string &filename, const std::string &funcname) {
51 this->gpu = gpu;
52
53 this->library = this->gpu->newLibrary(NS::String::string(filename.c_str(), NS::ASCIIStringEncoding), nullptr);
54
55 if (this->library == nullptr) {
56 std::cerr << "Error: Could not load library " << filename << std::endl;
57 }
58
59 useFunction(funcname);
60
61 }
62
67 Kernel() = default;
68
76 this->library->autorelease();
77 }
78
85 std::vector<std::string> getFunctionNames() {
86 std::vector<std::string> names;
87 for (int i = 0; i < this->library->functionNames()->count(); i++) {
88 names.push_back(this->library->functionNames()->object(i)->description()->cString(NS::ASCIIStringEncoding));
89 }
90 return names;
91 }
92
101 void useFunction(const std::string &funcname) {
102 this->function = this->library->newFunction(NS::String::string(funcname.c_str(), NS::ASCIIStringEncoding));
103 if (this->function == nullptr) {
104 throw std::runtime_error("Error: Could not load function " + funcname);
105 }
106
107 NS::Error *error = nullptr;
108 this->pipeline = gpu->newComputePipelineState(this->function, &error);
109 }
110
117 MTL::ComputePipelineState *getPLS() {
118 return this->pipeline;
119 }
120
121 };
122
123}
Definition MTLComputeKernel.hpp:7
MTL::ComputePipelineState * getPLS()
Get the MTL::ComputePipelineState object.
Definition MTLComputeKernel.hpp:117
~Kernel()
Destructor for the Kernel class.
Definition MTLComputeKernel.hpp:75
Kernel()=default
Default constructor for the Kernel class.
MTL::Library * library
The Metal library object.
Definition MTLComputeKernel.hpp:11
void useFunction(const std::string &funcname)
Use a function in the library.
Definition MTLComputeKernel.hpp:101
std::vector< std::string > getFunctionNames()
Get the names of the functions in the library.
Definition MTLComputeKernel.hpp:85
MTL::ComputePipelineState * pipeline
The Metal compute pipeline state object.
Definition MTLComputeKernel.hpp:13
Kernel(MTL::Device *gpu, const std::string &filename)
Constructor for the Kernel class.
Definition MTLComputeKernel.hpp:27
MTL::Device * gpu
The Metal device object.
Definition MTLComputeKernel.hpp:10
MTL::Function * function
The Metal function object.
Definition MTLComputeKernel.hpp:12
Kernel(MTL::Device *gpu, const std::string &filename, const std::string &funcname)
Constructor for the Kernel class with function name.
Definition MTLComputeKernel.hpp:50
Definition MTLComputeBuffer.hpp:7