metal-cpp

RSS for tag

C++ games and apps can tap into the power of Metal by bridging with metal-cpp.

metal-cpp Documentation

Posts under metal-cpp tag

18 Posts
Sort by:
Post not yet marked as solved
0 Replies
125 Views
I'm trying to follow the metal-cpp tutorials I've found at https://developer.apple.com/metal/sample-code/?q=learn The program seems to be launching correctly (I can see the menu bar and interact with it), but nothing is rendered inside the window. I suppose the culprit is somewhere in the following function (I see it binds the device, the view and the window with the object in charge of drawing stuff in the view) void core::Application::applicationDidFinishLaunching(NS::Notification *pNotification) { CGRect frame = (CGRect){{100.0, 100.0}, {512.0, 512.0}}; m_Window->init(frame, NS::WindowStyleMaskClosable | NS::WindowStyleMaskTitled, NS::BackingStoreBuffered, false); m_Device = MTL::CreateSystemDefaultDevice(); m_View = MTK::View::alloc()->init(frame, m_Device); m_View->setColorPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm); m_View->setClearColor(MTL::ClearColor::Make(1.0, 0.0, 0.0, 1.0)); m_ViewDelegate = new graphics::ViewDelegate(m_Device); m_View->setDelegate(m_ViewDelegate); m_Window->setContentView(m_View); m_Window->setTitle(NS::String::string("Template 1", NS::StringEncoding::UTF8StringEncoding)); m_Window->makeKeyAndOrderFront(nullptr); NS::Application* nsApp = reinterpret_cast<NS::Application*>(pNotification->object()); nsApp->activateIgnoringOtherApps(true); } but, as you can infer from the fact that I'm failing at the very first tutorial of the bunch, I'm quite lost. I've tried debugging the app with the Xcode debugger and I saw that it never enters in this function. void ViewDelegate::drawInMTKView(MTK::View *pView) { m_Renderer->Draw(pView); } Can it be a symptom of some call missing from my code? Thank you in advance for your help
Posted
by p_Each.
Last updated
.
Post marked as solved
1 Replies
201 Views
I've been trying to find a C/C++ framework for apps on macos. I couldn't find good docs on metal. Is there a way to write C++ apps without any other library?
Posted Last updated
.
Post marked as solved
6 Replies
313 Views
environment: Apple clang version 14.0.3 (clang-1403.0.22.14.1) I am trying to override c++ symbols at linking time for the purpose of inserting measurement code into an existing library. Perhaps typical linkers would search for symbols in specifing order of library and adopt the first hit. Xcode's ld apparently does this in units of a object file. If you create one object file to override one function and link it first, but call another function in the same object file contained in the original library, that one will be adopted. As a result, you cannot override function. To override a function, the function to be overridden must be moved to a new cpp file and separated from the object file. Here is a sample program to test this. https://onedrive.live.com/redir?resid=DD46698E2D493F32!395&authkey=!AJqfiva7CXIDI_Y&e=OaFlSr As you can see in main.cpp, func() and bar() are called from main(). func() and bar() are implemented in func.cpp and bar.cpp, respectively. They are linked to libTest.lib. To override func(), link libTest2.lib implementing func2() in func2.cpp, before libTest.lib. If you run make and look at the code with objdump -d a.out, you can see the override. Because the content of func2() is return i+1, __Z4funci contains leal 1(%rdi), %eax. Then, build with make clean and make CONCAT=1, func() and bar() are concatenated into one cpp file and compiled. The generated a.out is checked in the same way, you will see movl %edi, %eax at same position which means return i; which is the content of func(). This sample is small in scale, but in a larger project, it can be quite a hassle to isolate the functions you want to override. Is there any easier way to override the function? Since the original function name cannot be changed, it is our policy not to use -alias symbol_name alternate_symbol_name. Thanks.
Posted
by mkawasaki.
Last updated
.
Post not yet marked as solved
3 Replies
645 Views
Is it possible to use the Metal API on vision Pro? I noticed that using MTKView in my visionOS app is not recognized, and also noticed other forum posts from months ago saying that MTKView is not yet supported. If it is still not an option, if and when will it be supported? Also wondering about metal-cpp support as well, since my app involves integrating an existing C++ library with visionOS (see here: https://github.com/MinVR/MinVR). Is this possible?
Posted Last updated
.
Post not yet marked as solved
0 Replies
272 Views
We are trying to read disk sectors raw Data. Sample code can be found at the bottom. For external drive, it is working fine. We are able to see raw data which is not zero. For internal ssd APFS drive, We only get data filled with zeroes. We have tried with System Integrity Protection enabling and disabling. Please let us know Why same API failing for internal APFS drive? Is there any specific API available for reading raw data of internal APFS drive? #include <fcntl.h> #include <unistd.h> #include <iomanip> int main() { // Adjust this to your disk device on macOS const char* diskPath = "/dev/rdisk1"; //internal SSD device on macOS // Size of a sector (usually 4096 bytes for most disks on macOS) const int sectorSize = 4096; // Number of sectors you want to read const int numSectors = 8; // Starting sector number off_t startSector = 0; // Open the disk device using low-level file I/O int diskFile = open(diskPath, O_RDONLY); if (diskFile == -1) { std::cerr << "Error opening disk file." << std::endl; return 1; } // Read multiple sectors into a buffer char buffer[numSectors * sectorSize]; ssize_t bytesRead = pread(diskFile, buffer, numSectors * sectorSize, startSector * sectorSize); // Close the disk file close(diskFile); if (bytesRead != numSectors * sectorSize) { std::cerr << "Error reading sectors." << std::endl; return 1; } // Display the contents of the sectors in hex for (int i = 0; i < numSectors * sectorSize; ++i) { std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)buffer[i] << " "; if ((i + 1) % 16 == 0) { std::cout << std::endl; } } return 0; }
Posted Last updated
.
Post not yet marked as solved
1 Replies
475 Views
Hi, I'm using Instrument to profile my metal app, found a metric called display ( average frame time ) shows much longer than what expected. Is this truly the frame time ? Or did I misunderstand something ?
Posted Last updated
.
Post marked as solved
4 Replies
626 Views
Hello Apple Developer Forum community, I'm exploring the possibilities of mixing Swift and C++ for a project that involves VisionOS. The Swift official documentation on Mixing Swift and C++ mentions that C++ interoperability is supported for development and deployment on all platforms that Swift supports. I would like to confirm whether this support extends to VisionOS. Does VisionOS allow the use of C++ APIs, and how can various C++ APIs be effectively imported and used in Swift for VisionOS development? If anyone has experience or insights related to mixing Swift and C++ specifically for VisionOS, I would greatly appreciate your guidance and advice. Thank you for your assistance. Best regards,
Posted
by Saad_CB.
Last updated
.
Post marked as solved
2 Replies
656 Views
I'm working with the metal-cpp header library in xcode and getting a linker error: But it does not tell me what symbols are undefined and adding -v to the "Other Linker Flags" section of the build tab seems to do nothing. I can't seem to find anyone else that has had this issue so any help would be greatly appreciated.
Posted
by jEckert.
Last updated
.
Post marked as solved
3 Replies
982 Views
Hello everyone! I have a small concern about one little thing when it comes to programming in metal. There are some models that I wish to use along with animations and skins on them, the file extension for them is called gltf. glTF has been used in a number of projects such as unity and unreal engine and godot and blender. I was wondering if metal supports this file extension or not. Anyone here knows the answer?
Posted Last updated
.
Post not yet marked as solved
0 Replies
624 Views
Is there any way to use metal-cpp in a Swift project? I have a platform layer I've written in Swift that handles Window/View creation, as well as event handling, etc. I've been trying to bridge this layer with my C++ layer as you normally would using a pure C interface, but using Metal instances that cross this boundary just doesn't seem to work. e.g. Currently I initialize a CAMetalLayer for my NSView, setting that as the layer for the view. I've tried passing this Metal layer into my C++ code via a void* pointer through a C interface, and then casting it to a CA::MetalView to be used. When this didn't work, I tried creating the CA::MetalLayer in C++ and passing that back to the Swift layer as a void* pointer, then binding it to a CAMetalLayer type. And of course, this didn't work either. So are the options for metal-cpp to use either Objective-C or just pure C++ (using AppKit.hpp)? Or am I missing something for how to integrate with Swift?
Posted Last updated
.
Post not yet marked as solved
0 Replies
563 Views
Hi, I trying to use Metal cpp, but I have compile error: ISO C++ requires the name after '::' to be found in the same scope as the name before '::' metal-cpp/Foundation/NSSharedPtr.hpp(162): template <class _Class> _NS_INLINE NS::SharedPtr<_Class>::~SharedPtr() { if (m_pObject) { m_pObject->release(); } } Use of old-style cast metal-cpp/Foundation/NSObject.hpp(149): template <class _Dst> _NS_INLINE _Dst NS::Object::bridgingCast(const void* pObj) { #ifdef __OBJC__ return (__bridge _Dst)pObj; #else return (_Dst)pObj; #endif // __OBJC__ } XCode Project was generated using CMake: target_compile_features(${MODULE_NAME} PRIVATE cxx_std_20) target_compile_options(${MODULE_NAME} PRIVATE "-Wgnu-anonymous-struct" "-Wold-style-cast" "-Wdtor-name" "-Wpedantic" "-Wno-gnu" ) May be need to set some CMake flags for C++ compiler ?
Posted Last updated
.
Post not yet marked as solved
0 Replies
540 Views
Hello, I’ve started testing the Metal Shader Converter to convert my HLSL shaders to metallib directly, and I was wondering if the option ’-frecord-sources’ was supported in any way? Usually I’m compiling my shaders as follows (from Metal): xcrun -sdk macosx metal -c -frecord-sources shaders/shaders.metal -o shaders/shaders.air xcrun -sdk macosx metallib shaders/shaders.air -o shaders/shaders.metallib The -frecord-sources allow me to see the source when debugging and profiling a Metal frame. Now with DXC we have a similar option, I can compile a typical HLSL shader with embedded debug symbols with: dxc -T vs_6_0 -E VSMain shaders/triangle.hlsl -Fo shaders/triangle.dxil -Zi -O0 -Qembed_debug The important options here are ’-Zi` and ’-Qembed_debug’, as they make sure debug symbols are embedded in the DXIL. It seems that right now Metal Shader Converter doesn’t pass through the DXIL debug information, and I was wondering if it was possible. I’ve looked at all the options in the utility and haven’t seen anything that looked like it. Right now debug symbols in my shaders is a must-have both for profiling and debugging. For reference an alternative pipeline would be to use spir-v cross instead. For reference here's what a typical pipeline with dxc and spir-v cross look like: HLSL -> SPIRV -> AIR -> METALLIB dxc -T ps_6_0 -E PSMain -spirv shaders/triangle.hlsl -Zi -Qembed_debug -O0 -Fo shaders/triangle.frag.spirv spirv-cross --msl shaders/triangle.frag.spirv --output shaders/triangle.frag.metal xcrun -sdk macosx metal -c -frecord-sources shaders/triangle.frag.metal -o shaders/triangle.frag.air xcrun -sdk macosx metallib shaders/triangle.frag.air -o shaders/triangle.frag.metallib As you can see, it's a lot more steps than metal shader converter, but after all those steps you can get some sort of shader symbols in xcode when debugging a metal frame, which is better than nothing: Please let me know if I can provide files, projects or anything that can help supporting shader symbols directly with metal shader converter. Thank you for your time!
Posted
by jmbn.
Last updated
.
Post not yet marked as solved
1 Replies
723 Views
Hi, I am generating a Metal library that I build using the command line tools on macOS for iphoneos, following the instructions here. Then I serialise this to a binary blob that I load at runtime, which seems to work ok as everything renders as expected. When I am doing a frame capture and open up a shader function it tries to load the symbols and fails. I tried pointing it to the directory (and the file) containing the symbols file, but it never resolves those. In the bottom half of the Import External Sources dialogue there is one entry in the Library | Debug Info section: The library name is Library 0x21816b5dc0 and below Debug Info it says Invalid UUID. The validation layer doesn't flag any invalid behaviour so I am a bit lost and not sure what to try next?
Posted
by marco-swe.
Last updated
.
Post not yet marked as solved
0 Replies
580 Views
I am following this https://developer.apple.com/documentation/metal/performing_calculations_on_a_gpu on building a metal app for performing a GPU calculation. I am not able to figure out how to build and execute the project from the command line. Any help on how to build a main.m file using xcrun will be useful. I have tried xcrun -sdk macosx clang MetalComputeBasic/main.m but it doesn't work.
Posted
by arunppsg.
Last updated
.
Post not yet marked as solved
1 Replies
1.3k Views
What is the best source for information/tutorial material on using Metal with C++? metal-cpp?
Posted
by g-wright.
Last updated
.
Post not yet marked as solved
1 Replies
692 Views
Hi, I am experimenting with the 05-perspective.cpp code in LearnMetalCPP. I have made the window resizable by dragging an edge or corner with the mouse. But this causes the image to distort because the original drawing was square and the aspect set to 1.0. Now I want to compute aspect = width/height to use in simd::float4x4 makePerspective( float fovRadians, float aspect, float znear, float zfar ) in the 05-perspective.cpp app. What is the best way to get width and height, such as from CGSize.width and CGSize.height?
Posted
by raw915.
Last updated
.
Post not yet marked as solved
4 Replies
2.5k Views
Hi, I am new to Metal and macOS development, and trying to learn Metal with the CPP wrapper for a toy rendering engine. I am mostly following the "Learn Metal with C++" sample code. I am trying to read mouse and keyboard input. It seems like the Objective-C or Swift wrappers allow you to override your own MTK::View class, and then override the respective keyDown(), keyUp() methods. However, when looking at the CPP wrapper, MTK::View doesn't have any virtual functions to override. How can I read mouse and keyboard inputs in my application? Hopefully without having an Objective-C bridge. Thank you, Robin
Posted Last updated
.