-
-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathmvUtilities_apple.mm
More file actions
155 lines (117 loc) · 5.59 KB
/
Copy pathmvUtilities_apple.mm
File metadata and controls
155 lines (117 loc) · 5.59 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "mvUtilities.h"
#include "mvViewport.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <Metal/Metal.h>
#include <MetalKit/MetalKit.h>
#include <Quartz/Quartz.h>
#include <simd/simd.h>
#include <vector>
#include "mvAppleSpecifics.h"
#include "mvPyUtils.h"
// this is necessary to keep objective-c's reference counts
// from reaching 0.
static std::vector<std::pair<id<MTLTexture>, id<MTLTexture>>> g_textures;
void
OutputFrameBufferArray(PymvBuffer* out)
{
mvSubmitCallback([]() {mvThrowPythonError(mvErrorCode::mvNone, "`output_frame_buffer(...)` has not been implemented for this platform yet."); });
}
void
OutputFrameBuffer(const char* filepath)
{
mvSubmitCallback([](){mvThrowPythonError(mvErrorCode::mvNone, "`output_frame_buffer(...)` has not been implemented for this platform yet.");});
}
void*
LoadTextureFromArray(unsigned width, unsigned height, float* data)
{
mvGraphics& graphics = GContext->graphics;
auto graphicsData = (mvGraphics_Metal*)graphics.backendSpecifics;
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:width height:height mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead;
textureDescriptor.storageMode = MTLStorageModeManaged;
id <MTLTexture> texture = [graphicsData->device newTextureWithDescriptor:textureDescriptor];
[texture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:data bytesPerRow:width * 4 * 4];
g_textures.push_back({texture, texture});
return (__bridge void*)g_textures.back().second;
}
void*
LoadTextureFromArrayDynamic(unsigned width, unsigned height, float* data)
{
mvGraphics& graphics = GContext->graphics;
auto graphicsData = (mvGraphics_Metal*)graphics.backendSpecifics;
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:width height:height mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead;
textureDescriptor.storageMode = MTLStorageModeManaged;
id <MTLTexture> texture = [graphicsData->device newTextureWithDescriptor:textureDescriptor];
[texture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:data bytesPerRow:width * 4 * 4];
g_textures.push_back({texture, texture});
return (__bridge void*)g_textures.back().second;
}
void*
LoadTextureFromArrayRaw(unsigned width, unsigned height, float* data, int components)
{
mvGraphics& graphics = GContext->graphics;
auto graphicsData = (mvGraphics_Metal*)graphics.backendSpecifics;
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:width height:height mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead;
textureDescriptor.storageMode = MTLStorageModeManaged;
id <MTLTexture> texture = [graphicsData->device newTextureWithDescriptor:textureDescriptor];
[texture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:data bytesPerRow:width * 4 * 4];
g_textures.push_back({texture, texture});
return (__bridge void*)g_textures.back().second;
}
void*
LoadTextureFromFile(const char* filename, int& width, int& height)
{
mvGraphics& graphics = GContext->graphics;
auto graphicsData = (mvGraphics_Metal*)graphics.backendSpecifics;
unsigned char* image_data = stbi_load(filename, &width, &height, nullptr, 4);
if (image_data == nullptr)
return nullptr;
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
width:width
height:height
mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead;
textureDescriptor.storageMode = MTLStorageModeManaged;
id <MTLTexture> texture = [graphicsData->device newTextureWithDescriptor:textureDescriptor];
[texture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:image_data bytesPerRow:width * 4];
g_textures.push_back({texture, texture});
return (__bridge void*)g_textures.back().second;
}
bool
UnloadTexture(const std::string& filename)
{
if (texture == nullptr)
return;
auto out_srv = (GLuint)(size_t)texture;
if (out_srv == 0)
return;
return true;
}
void
FreeTexture(void* texture)
{
id <MTLTexture> out_srv = (__bridge id <MTLTexture>)texture;
std::vector<std::pair<id<MTLTexture>, id<MTLTexture>>> oldtextures = g_textures;
g_textures.clear();
for(auto& texturepair : oldtextures)
{
if(texturepair.first != out_srv)
g_textures.push_back(texturepair);
}
}
void
UpdateTexture(void* texture, unsigned width, unsigned height, std::vector<float>& data)
{
id <MTLTexture> out_srv = (__bridge id <MTLTexture>)texture;
[out_srv replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:data.data() bytesPerRow:width * 4 * 4];
}
void UpdateRawTexture(void* texture, unsigned width, unsigned height, float* data, int components)
{
id <MTLTexture> out_srv = (__bridge id <MTLTexture>)texture;
[out_srv replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:data bytesPerRow:width * components * 4];
}