-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
346 lines (274 loc) · 10.4 KB
/
Copy pathmain.cpp
File metadata and controls
346 lines (274 loc) · 10.4 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#define CA_PRIVATE_IMPLEMENTATION
#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#define MTK_PRIVATE_IMPLEMENTATION
#include <iostream>
#include <execinfo.h>
#include <signal.h>
#include <Metal/Metal.hpp>
#include <AppKit/AppKit.hpp>
#include <MetalKit/MetalKit.hpp>
//
// @Note(impcuong): Abbrev-list
// + mtl := Metal
// + NS := NeXTSTEP
// + CG := CoreGraphic
// + res := resource
// + enc := encode
// + txt := texture
// + desc := descriptor
//
const char *mtl_shader = R"(
#include <metal_stdlib>
using namespace metal;
kernel void add_same_sz_arr_pair(
device const float *f [[buffer(0)]],
device const float *s [[buffer(1)]],
device float *r [[buffer(2)]],
uint id [[thread_position_in_grid]])
{
r[id] = f[id] + s[id];
}
struct Visible_Vert
{
float4 pos [[position]];
float2 coord;
};
vertex Visible_Vert draw_vert_shader(uint vert_id [[vertex_id]])
{
float2 pos[6] = {
float2(-1, -1), float2(1, -1), float2(-1, 1),
float2(-1, 1), float2(1, -1), float2(1, 1)
};
float2 coords[6] = {
float2(0, 1), float2(1, 1), float2(0, 0),
float2(0, 0), float2(1, 1), float2(1, 0)
};
Visible_Vert vert;
vert.pos = float4(pos[vert_id], 0, 1);
vert.coord = coords[vert_id];
return vert;
}
fragment float4 draw_frag_shader(Visible_Vert in [[stage_in]],
texture2d<float> txt [[texture(0)]])
{
constexpr sampler screen_obj(address::clamp_to_edge, filter::linear);
return txt.sample(screen_obj, in.coord);
}
)";
const float _W = 512.0;
const float _H = 512.0;
class Renderer : public MTK::ViewDelegate
{
public:
MTL::CommandQueue *queue;
MTL::RenderPipelineState *pstate;
MTL::Texture *texture;
Renderer(MTL::CommandQueue *q, MTL::RenderPipelineState *p, MTL::Texture *t)
: queue(q), pstate(p), texture(t) {}
void drawInMTKView(MTK::View *view) override
{
MTL::CommandBuffer *cmd = queue->commandBuffer();
MTL::RenderPassDescriptor *rpd = view->currentRenderPassDescriptor();
if (rpd)
{
MTL::RenderCommandEncoder *enc = cmd->renderCommandEncoder(rpd);
enc->setRenderPipelineState(pstate);
enc->setFragmentTexture(texture, 0 /*index=*/);
enc->drawPrimitives(MTL::PrimitiveTypeTriangle,
NS::UInteger(0) /*vertexStart=*/, NS::UInteger(6) /*vertexCount=*/);
enc->endEncoding();
cmd->presentDrawable(view->currentDrawable());
}
cmd->commit();
}
void drawableSizeWillChange(MTK::View *view, CGSize size) override {}
};
void capture_signal_then_report(int sig_no, siginfo_t *info, void *_ctx)
{
fprintf(stderr, "INFO: Emitted signal: %d\n", sig_no);
fprintf(stderr, "INFO: Violated address (si_addr): %p\n", info->si_addr);
const int _depth = 20;
void *report[_depth];
size_t size = backtrace(report, _depth);
fprintf(stderr, "INFO: Stack-trace:\n");
// Using the literal '2' for STDERR_FILENO since you noted the constant issue.
backtrace_symbols_fd(report, size, 2 /*fd=*/);
signal(sig_no, SIG_DFL);
raise(sig_no);
}
int main()
{
#define ENABLE_STACKTRACE
#ifdef ENABLE_STACKTRACE
struct sigaction sig_reporter = {0};
// union __sigaction_u
// {
// void (*__sa_handler)(int);
// void (*__sa_sigaction)(int, siginfo_t *, void *);
// };
// #define sa_sigaction __sigaction_u.__sa_sigaction
sig_reporter.sa_sigaction = capture_signal_then_report;
sigemptyset(&sig_reporter.sa_mask);
sig_reporter.sa_flags = SA_SIGINFO;
if (sigaction(SIGSEGV, &sig_reporter, NULL) == -1)
perror("ERROR: Segmentation fault has been found!");
#endif
// ----- //
NS::AutoreleasePool *res_pool = NS::AutoreleasePool::alloc()->init();
NS::SharedPtr<MTL::Device> mtl_dev = NS::TransferPtr(MTL::CreateSystemDefaultDevice());
NS::UInteger alloc_sz = mtl_dev.get()->currentAllocatedSize();
std::cout << "alloc_sz = " << alloc_sz << "\n";
MTL::CommandQueue *mtl_cmd_queue = mtl_dev.get()->newCommandQueue();
// ----- //
using MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB;
NS::Error *mtl_err = nullptr;
NS::String *lib_name = NS::String::string(mtl_shader, NS::UTF8StringEncoding);
MTL::Library *mtl_lib = mtl_dev->newLibrary(lib_name, nullptr /*options=*/, &mtl_err);
NS::String *compute_func_name = NS::String::string("add_same_sz_arr_pair", NS::UTF8StringEncoding);
MTL::Function *mtl_compute_func = mtl_lib->newFunction(compute_func_name);
MTL::ComputePipelineState *mtl_compute_pstate = mtl_dev->newComputePipelineState(mtl_compute_func, &mtl_err);
NS::String *draw_vert_func_name = NS::String::string("draw_vert_shader", NS::UTF8StringEncoding);
MTL::Function *mtl_draw_vert_func = mtl_lib->newFunction(draw_vert_func_name);
NS::String *draw_frag_func_name = NS::String::string("draw_frag_shader", NS::UTF8StringEncoding);
MTL::Function *mtl_draw_frag_func = mtl_lib->newFunction(draw_frag_func_name);
MTL::RenderPipelineDescriptor *mtl_render_pdesc = MTL::RenderPipelineDescriptor::alloc()->init();
mtl_render_pdesc->setVertexFunction(mtl_draw_vert_func);
mtl_render_pdesc->setFragmentFunction(mtl_draw_frag_func);
mtl_render_pdesc->colorAttachments()->object(0 /*attachmentIndex=*/)->setPixelFormat(PixelFormatBGRA8Unorm_sRGB);
MTL::RenderPipelineState *mtl_render_pstate = mtl_dev->newRenderPipelineState(mtl_render_pdesc, &mtl_err);
// ----- //
const size_t arr_sz = static_cast<size_t>(_W * _H);
const float real_sz = sizeof(float) * arr_sz;
MTL::Buffer *buf_f = mtl_dev->newBuffer(real_sz, MTL::ResourceStorageModeShared);
MTL::Buffer *buf_s = mtl_dev->newBuffer(real_sz, MTL::ResourceStorageModeShared);
MTL::Buffer *buf_r = mtl_dev->newBuffer(real_sz, MTL::ResourceStorageModeShared);
float *f = static_cast<float *>(buf_f->contents()); // typeof(buf_f->contents()) := <void *>
float *s = static_cast<float *>(buf_s->contents()); // typeof(buf_s->contents()) := <void *>
for (int i = 0; i < arr_sz; i++)
{
f[i] = 2;
s[i] = i;
}
// ----- //
MTL::CommandBuffer *mtl_cmd_buf = mtl_cmd_queue->commandBuffer();
MTL::ComputeCommandEncoder *mtl_cmd_enc = mtl_cmd_buf->computeCommandEncoder();
mtl_cmd_enc->setComputePipelineState(mtl_compute_pstate);
mtl_cmd_enc->setBuffer(buf_f, 0 /*offset=*/, 0 /*index=*/);
mtl_cmd_enc->setBuffer(buf_s, 0 /*offset=*/, 1 /*index=*/);
mtl_cmd_enc->setBuffer(buf_r, 0 /*offset=*/, 2 /*index=*/);
mtl_cmd_enc->dispatchThreads(MTL::Size::Make(arr_sz /*width=*/, 1 /*height=*/, 1 /*depth=*/) /*threadsPerGrid=*/,
MTL::Size::Make(mtl_compute_pstate->maxTotalThreadsPerThreadgroup(), 1, 1) /*threadsPerThreadgroup=*/);
mtl_cmd_enc->endEncoding();
mtl_cmd_buf->commit();
mtl_cmd_buf->waitUntilCompleted();
// ----- //
auto res = static_cast<float *>(std::malloc(real_sz));
std::memcpy(res, buf_r->contents(), real_sz);
// auto res = static_cast<float *>(buf_r->contents());
for (int i = 0; i < arr_sz / 1000; i++)
{
std::cout << "f: " << f[i] << ", s: " << s[i] << " | ";
std::cout << f[i] << " + " << s[i] << " = " << res[i] << "\n";
}
buf_f->release();
buf_s->release();
// @Note: Uncomment this one will collapse the whole render-pipeline.
// Which can be rationalized as SEGV error in term of read-access violation.
// @Hack: Get rid of the abundance `buf_r` object by uplifting the
// manual memory allocation, an old-schooled mechanism.
buf_r->release();
mtl_compute_pstate->release();
mtl_compute_func->release();
// ------ //
//
// Creates texture to display the precomputed data.
//
MTL::TextureDescriptor *mtl_txt_desc = MTL::TextureDescriptor::alloc()->init();
mtl_txt_desc->setWidth(_W);
mtl_txt_desc->setHeight(_H);
mtl_txt_desc->setPixelFormat(PixelFormatBGRA8Unorm_sRGB);
mtl_txt_desc->setUsage(MTL::TextureUsageShaderRead);
MTL::Texture *mtl_txt = mtl_dev->newTexture(mtl_txt_desc /*descriptor=*/);
//
// Converts our float-array to RGBA pixels
//
float min_val = *std::min_element(res, res + arr_sz);
float max_val = *std::max_element(res, res + arr_sz);
std::vector<uint8_t> pixels(arr_sz * 4);
for (int i = 0; i < arr_sz; i++)
{
float norm = (res[i] - min_val) / (max_val - min_val);
// @Note: Normalize to 0-255 range.
uint8_t color = static_cast<uint8_t>(norm * 255.0f);
pixels[i * 4 + 0] = color; // R
pixels[i * 4 + 1] = color; // G
pixels[i * 4 + 2] = color; // B
pixels[i * 4 + 3] = 255; // A
}
//
// Uploads to this texture.
//
MTL::Region mtl_region(0, 0, _W, _H);
mtl_txt->replaceRegion(mtl_region, 0 /*level=*/, pixels.data() /*pixelBytes=*/, _W * 4 /*bytesPerRow=*/);
// ----- //
//
// Setups phase.
//
// @Docs: https://en.wikipedia.org/wiki/NeXTSTEP
NS::Application *app = NS::Application::sharedApplication();
using NS::StringEncoding::UTF8StringEncoding;
CGRect content_rect = {
.origin = {.x = 100.0, .y = 100.0},
.size = {.width = _W, .height = _H}
};
NS::Window *app_window = NS::Window::alloc()->init(
content_rect,
NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled /*styleMask=*/,
NS::BackingStoreBuffered /*backing=*/,
false /*defer=*/
);
assert(mtl_dev.get() != nullptr);
// From: <Foundation/NSObject.hpp>
// namespace NS
// {
// template <class _Class, class _Base = class Object>
// class _NS_EXPORT Referencing : public _Base
// {
// public:
// _Class *retain();
// void release();
// _Class *autorelease();
// UInteger retainCount() const;
// };
// }
//
// From: <MetalKit/MTKView.hpp>
// class View : public NS::Referencing<MTK::View, NS::View> { ... }
MTK::View *app_view = MTK::View::alloc()->init(content_rect /*frame=*/, mtl_dev.get() /*pDevice=*/);
assert(app_view != nullptr);
app_view->setColorPixelFormat(PixelFormatBGRA8Unorm_sRGB);
app_view->setClearColor(MTL::ClearColor::Make(0.5, 0.5, 0.5, 1.0));
app_window->setContentView(app_view /*pContentView=*/);
app_window->setTitle(NS::String::string("Welcome to the other side", UTF8StringEncoding));
app_window->makeKeyAndOrderFront(nullptr /*pSender=*/);
app->activateIgnoringOtherApps(true);
//
// Renders phase.
//
Renderer *renderer = new Renderer(mtl_cmd_queue, mtl_render_pstate, mtl_txt);
app_view->setDelegate(renderer);
app_view->setPaused(false);
app_view->setEnableSetNeedsDisplay(true);
app->run(); // @Note: Triggering signal.
delete renderer;
mtl_draw_vert_func->release();
mtl_draw_frag_func->release();
app_view->release();
app_window->release();
app->release();
// ----- //
mtl_lib->release();
mtl_dev->release();
res_pool->release();
}