naleksiev / mtlpp Goto Github PK
View Code? Open in Web Editor NEWC++ Metal wrapper
License: MIT License
C++ Metal wrapper
License: MIT License
Assuming the intended output is something more interesting?
randallr-mac:macos_10.12 randallr$ ./02_triangle
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Do you plan to add support for Metal 2 to this library?
Hi, thank you so much for your brilliant library. I’m just trying to get up and running with a very basic example. My ultimate goal will be using Metal through your library to move some computationally expensive (and “embarrassingly parallel”) operations in C++ from the CPU to the GPU.
Here is the function I have attempted to put together using your library.
void SampleMetal(){
//SET UP METAL - device, library, command queue, command buffer, command encoder
mtlpp::Library device = mtlpp::Device::CreateSystemDefaultDevice();
mtlpp::Device library = device.NewDefaultLibrary();
mtlpp::CommandQueue commandQueue = device.NewCommandQueue();
mtlpp::CommandBuffer commandBuffer = commandQueue.CommandBuffer();
assert(device);
assert(library);
assert(commandQueue);
assert(commandBuffer);
//SET UP SHADER - create function, make compute pipeline
ns::String functionName = "doubleInputValues";
mtlpp::Function kernelFunction = library.NewFunction(functionName);
assert(kernelFunction);
ns::Error error;
mtlpp::ComputePipelineState pipelineState = device.NewComputePipelineState(kernelFunction, &error);
assert(pipelineState);
// CREATE INPUT & OUTPUT
int i = 50;
std::vector<int> inputVector(i);
mtlpp::Buffer inputBuffer = device.NewBuffer(&inputVector, sizeof(inputVector), mtlpp::ResourceOptions::CpuCacheModeDefaultCache);
std::vector<int> outputVector(i, 0);
mtlpp::Buffer outputBuffer = device.NewBuffer(&outputVector, sizeof(outputVector), mtlpp::ResourceOptions::CpuCacheModeDefaultCache);
assert(inputBuffer);
assert(outputBuffer);
// Create COMPUTE COMMAND CODER
mtlpp::ComputeCommandEncoder commandEncoder = commandBuffer.ComputeCommandEncoder();
commandEncoder.SetBuffer(inputBuffer,0,0);
commandEncoder.SetBuffer(outputBuffer,0,1);
assert(commandEncoder);
commandEncoder.SetComputePipelineState(pipelineState);
// Find max number of parallel threads & set up on GPU
int threadExecutionWidth = pipelineState.GetThreadExecutionWidth();
mtlpp::Size threadsPerGroup = mtlpp::Size(threadExecutionWidth,1,1);
mtlpp::Size numThreadGroups = mtlpp::Size(((uint32_t)inputVector.size()+threadExecutionWidth)/threadExecutionWidth,1,1);
commandEncoder.DispatchThreadgroups(numThreadGroups, threadsPerGroup);
//finalise
commandEncoder.EndEncoding();
commandBuffer.Commit();
commandBuffer.WaitUntilCompleted();
//output??
std::string outputString((char *) outputBuffer.GetContents());
std::cout << "output string: “ << outputString;
}
And here is the shader function:
kernel void doubleInputValues(const device float *inVector [[ buffer(0) ]], device float *outVector [[ buffer(1) ]], uint id [[ thread_position_in_grid ]]) {
outVector[id] = 2*inVector[id];
}
I have some questions I was hoping you’d be able to help with:
I can’t seem to get the right output as I couldn’t find how to parse the bytes data from your examples (as they’re all for textures as opposed to computation) - would you mind pointing me in the right direction? It currently outputs some garbage values like \360\323
and I’m not entirely sure what I’m doing wrong although I’m sure the issue is probably in the GetContents()
line at the end of the SampleMetal()
function.
I’d be using the function as part of processing video in real time, am I supposed to set up Metal, set up the Shader etc for every frame or are there parts that only need to be created once like setting up Metal and the Shader and then just assigning input and output vectors for each video frame? I did try doing this but got a crash of EXC_BAD_ACCESS
on the computeCommandEncoder
pointer so wasn’t sure if it’s possible. (I know this is more of a Metal API question but I wasn’t sure if the issue was related to my incorrect implementation of your library).
The entire function runs in about 4 milliseconds on an iPhone 6 plus, is that supposed to be correct or is there something I’m doing horribly inefficiently as I would have thought simply running a doubling operation on 50 values should be much less time. I’m trying to get some CPU work offloaded onto the GPU to get video processing nearer to 60fps so would like some insight on how to make this as fast as possible before I start building more complicated shaders.
Thank you very much for your help and for building this library!
Is metal 2 supported by this library ? Thanks
Hi,
I am using Clion, with Cmake. I can build and execute your examples from the command line, and I added the required dependency as follows:
arget_link_libraries(DeepSleepUITarget ${OPENGL_LIBRARIES} ${CMAKE_CURRENT_SOURCE_DIR}/libs/mtlpp.o ${ARMADILLO_LIBRARIES} ${GLFW_LIB} ${Boost_LIBRARIES}
However, during runtime, I get lots of errors such as:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Volumes/1tb-ext/2019/db/stan/Dropbox/dev2/cpp/DeepSleepGUI --target DeepSleepUITarget -- -j 8 [ 25%] Linking CXX executable DeepSleepUITarget Undefined symbols for architecture x86_64: "_CFRelease", referenced from: __ZN2ns6ObjectD2Ev in mtlpp.o __ZN2ns6ObjectaSERKS0_ in mtlpp.o __ZN2ns6ObjectaSEOS0_ in mtlpp.o "_CFRetain", referenced from: __ZN2ns6ObjectC2ERKNS_6HandleE in mtlpp.o __ZN2ns6ObjectC2ERKS0_ in mtlpp.o __ZN2ns6ObjectaSERKS0_ in mtlpp.o "_MTLCopyAllDevices", referenced from: __ZN5mtlpp6Device14CopyAllDevicesEv in mtlpp.o "_MTLCreateSystemDefaultDevice", referenced from:
I understand that I have missing flags, but not sure how/what to add them in Cmake.
Thanks,
Running scripts/build_osx.sh for macOS 10.13 and 10.14 raises deprecation warnings. Deprecated functions are insertDebugCaptureBoundary and textureBarrier. Full text of warnings is at https://pastebin.com/sdqNAEme. Only modifications to any files were adding macOS 10.13 and 10.14 to build_osx.sh, as seen at https://pastebin.com/AcxmPCih. Code still works, but deprecated stuff should be replaced.
Specifically: https://github.com/google/skia/blob/acb4b0f474c10c1553bef45292a26cac2b6e556e/include/gpu/GrContext.h#L83
Curious how one might go about using it here.
Also - why the fuck did apple deprecate OpenGL and not provide a C/C++ api for Metal?
Please see #6 for more details. Looking forward to your thoughts on this subject. Thank you.
Do you have any plan to support metal binary archive(https://developer.apple.com/documentation/metal/mtlbinaryarchive) which is bring up with WWDC2020 (https://developer.apple.com/videos/play/wwdc2020/10615)
Thanks
Hi,
Love the project. I was trying to wrap your code so I could use it in Python with my ML projects. Here's what I have so far: https://github.com/mattpaletta/PyMetal
At the moment I believe the issue is it fails to link the metal library.
Any suggestions or improvements?
Thanks.
I'm getting the message
[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.
whenever I create an ns::Error and the application will crash upon calling an error member function
If you enable arc via -fobjc-arc you get a bunch of the following errors:
../mtlpp.mm:1349:17: error: pointer to non-const type 'NSError ' with no explicit ownership
NSError* nsErrorPtr = error ? &nsError : nullptr;
I was just wondering if there is GLFW support? If so is there any example code to use with glfw?
Not sure I understand the comments about toll-free bridging? What's the best way to create a fairly minimal windowed Metal app with a render loop? The 99_view_controller.mm example seems to be missing some header files.
Hi,
seeing commits in Unreal master branch I see:
Initial extensions to mtlpp:
- Fixed over retention of alloc-init'd objects.
- Added 10_13 & 11_0 availablity macros.
- Started, but have not yet finished adding new Metal API function wrappers.
so just saying if these will be avaiable in your repo as could be brought from Unreal mtlpp source code..
Hey,
I think that ParallelRenderCommandEncoder is missing EndEncoding method. According to Metal guide it has to be called after all render command encoders have called their EndEncoding.
Looks like easy fix. Anyways, mtlpp is awesome! Keep up the good work!
Cheers!
I've been trying to build mtlpp with CMake and i've gotten close, but unfortunately i keep getting those weird errors :
error: expected '(' for function-style cast or type construction
return Handle{ GetItem(index) };
There are plenty of them everywhere, any idea of why this is happening?
it's amazing that you did this project with metal c++ so early. now i also need to coding with metal c++, but now i get in trouble beacause there is few example(metal c++), my project is get MTLTexture from video decode by ffmpeg haedware, but i didn't find
CVPixelBufferRef in metal C++,can you give some advice?
needs new MacOS 10.13 and Xcode 9..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.