-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxToEquirectKernel.mm
More file actions
110 lines (92 loc) · 4.77 KB
/
MaxToEquirectKernel.mm
File metadata and controls
110 lines (92 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2021 Ronan LE MEILLAT
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#import <Metal/Metal.h>
#include <unordered_map>
#include <mutex>
#include "MaxToEquirectKernel.h"
std::mutex s_PipelineQueueMutex;
typedef std::unordered_map<id<MTLCommandQueue>, id<MTLComputePipelineState>> PipelineQueueMap;
PipelineQueueMap s_PipelineQueueMap;
void RunMetalKernel(void* p_CmdQ, int p_in_Width, int p_in_Height, int p_out_Width, int p_out_Height, const float* p_Input, float* p_Output)
{
const char* kernelName = "gopromax_equirectangular";
id<MTLCommandQueue> queue = static_cast<id<MTLCommandQueue> >(p_CmdQ);
id<MTLDevice> device = queue.device;
id<MTLLibrary> metalLibrary; // Metal library
id<MTLFunction> kernelFunction; // Compute kernel
id<MTLComputePipelineState> pipelineState; // Metal pipeline
NSError* err;
std::unique_lock<std::mutex> lock(s_PipelineQueueMutex);
const auto it = s_PipelineQueueMap.find(queue);
if (it == s_PipelineQueueMap.end())
{
id<MTLLibrary> metalLibrary; // Metal library
id<MTLFunction> kernelFunction; // Compute kernel
NSError* err;
MTLCompileOptions* options = [MTLCompileOptions new];
options.fastMathEnabled = YES;
if (!(metalLibrary = [device newLibraryWithSource:@(metal_src_MaxToEquirectKernel) options:options error:&err]))
{
fprintf(stdout, "Failed to load metal library, %s\n", err.localizedDescription.UTF8String);
return;
}
[options release];
if (!(kernelFunction = [metalLibrary newFunctionWithName:[NSString stringWithUTF8String:kernelName]/* constantValues : constantValues */]))
{
fprintf(stdout, "Failed to retrieve kernel\n");
[metalLibrary release];
return;
}
if (!(pipelineState = [device newComputePipelineStateWithFunction:kernelFunction error:&err]))
{
fprintf(stdout, "Unable to compile, %s\n", err.localizedDescription.UTF8String);
[metalLibrary release];
[kernelFunction release];
return;
}
s_PipelineQueueMap[queue] = pipelineState;
//Release resources
[metalLibrary release];
[kernelFunction release];
}
else
{
pipelineState = it->second;
}
id<MTLBuffer> srcDeviceBuf = reinterpret_cast<id<MTLBuffer> >(const_cast<float *>(p_Input));
id<MTLBuffer> dstDeviceBuf = reinterpret_cast<id<MTLBuffer> >(p_Output);
id<MTLCommandBuffer> commandBuffer = [queue commandBuffer];
commandBuffer.label = [NSString stringWithFormat:@"gopromax_equirectangular"];
id<MTLComputeCommandEncoder> computeEncoder = [commandBuffer computeCommandEncoder];
[computeEncoder setComputePipelineState:pipelineState];
int exeWidth = [pipelineState threadExecutionWidth];
MTLSize threadGroupCount = MTLSizeMake(exeWidth, 1, 1);
MTLSize threadGroups = MTLSizeMake((p_out_Width + exeWidth - 1)/exeWidth, p_out_Height, 1);
[computeEncoder setBuffer:srcDeviceBuf offset: 0 atIndex: 0];
[computeEncoder setBuffer:dstDeviceBuf offset: 0 atIndex: 8];
[computeEncoder setBytes:&p_in_Width length:sizeof(int) atIndex:11];//prepare for p_in_width
[computeEncoder setBytes:&p_in_Height length:sizeof(int) atIndex:12];//prepare for p_in_height
[computeEncoder setBytes:&p_out_Width length:sizeof(int) atIndex:13]; //prepare for p_out_width
[computeEncoder setBytes:&p_out_Height length:sizeof(int) atIndex:14];//prepare for p_out_height
[computeEncoder dispatchThreadgroups:threadGroups threadsPerThreadgroup: threadGroupCount];
[computeEncoder endEncoding];
[commandBuffer commit];
}