{"id":126102,"date":"2022-06-08T14:05:16","date_gmt":"2022-06-08T14:05:16","guid":{"rendered":"https:\/\/developer.apple.com\/news\/?id=yrb1yuf3"},"modified":"2022-06-08T14:05:16","modified_gmt":"2022-06-08T14:05:16","slug":"challenge-draw-with-metal-cpp","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/06\/08\/challenge-draw-with-metal-cpp\/","title":{"rendered":"Challenge: Draw with metal-cpp"},"content":{"rendered":"<div class=\"inline-article-image\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2022\/06\/challenge-draw-with-metal-cpp.jpg\" data-hires=\"false\" alt><\/div>\n<p>Metal is the foundation for accelerated graphics and compute power on Apple platforms \u2014 and if you\u2019re familiar with C++, now\u2019s the perfect time to explore its incredible power. For this challenge, we&#8217;re inviting you to try out metal-cpp and render your own triangle, sphere, or even a mesh in Xcode.<\/p>\n<p>We also welcome you to visit the Graphics &amp; Games Study Hall during the day to collaborate on this challenge! Ask questions, connect with other developers, and share your creations.<\/p>\n<h3>Begin the challenge<\/h3>\n<p>Before you begin, you\u2019ll want to watch \u201cProgram Metal in C++ with metal-cpp\u201d and download the LearnMetalCPP project, which contains a series of C++ samples.<\/p>\n<section class=\"grid activity\">\n<section class=\"row\">\n<section class=\"column large-4 small-4 no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc22\/10160\" class=\"activity-image-link\"> <img decoding=\"async\" class=\"actiity-image medium-scale\" width=\"250\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2022\/06\/challenge-draw-with-metal-cpp-1.jpg\" data-hires=\"false\" alt> <\/a> <\/section>\n<section class=\"column large-8 small-8 padding-left-small padding-top-small padding-bottom-small no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc22\/10160\"> <\/p>\n<h4 class=\"no-margin-bottom activity-title\">Program Metal in C++ with metal-cpp<\/h4>\n<p class=\"activity-description\">Your C++ games and apps can now tap into the power of Metal. We&#8217;ll show you how metal-cpp helps you bridge your C++ code to Metal, explore how each manages object lifecycles, and demonstrate utilities that can help these language cooperate in your app. We&#8217;ll also share best practices for designing&#8230;<\/p>\n<p> <\/a> <\/section>\n<\/section>\n<\/section>\n<p><a href=\"https:\/\/developer.apple.com\/metal\/LearnMetalCPP.zip\" class=\"icon icon-after icon-chevronright\">Download the LearnMetalCPP project<\/a><\/p>\n<p>Open the project in Xcode, and choose 00-window.cpp as your base code. To render your image, you\u2019ll need to set up a few things within your project.<\/p>\n<p>First, create a <code>MTL::RenderPipelineState<\/code> object with a <code>MTL::RenderPipelineDescriptor<\/code>. To do this, you\u2019ll need to create a function, like <code>buildShaders()<\/code>. In the code snippet below, we\u2019ve provided the shader code needed to render a single triangle. <\/p>\n<pre class=\"code-source\"><code>void Renderer::buildShaders()\n{ using NS::StringEncoding::UTF8StringEncoding; const char* shaderSrc = R\"( #include  using namespace metal; struct AAPLVertex { float3 position; half3 color; }; \/\/ Welcome to modify the mesh as you want constant AAPLVertex triangles[] = { { float3{ -0.8f, 0.8f, 0.0f }, half3{ 1.0, 0.3f, 0.2f } }, { float3{ 0.0f, -0.8f, 0.0f }, half3{ 0.8f, 1.0, 0.0f } }, { float3{ +0.8f, 0.8f, 0.0f }, half3{ 0.8f, 0.0f, 1.0 } } }; struct v2f { float4 position [[position]]; half3 color; }; v2f vertex vertexMain( uint vertexId [[vertex_id]]) { v2f o; o.position = float4( triangles[ vertexId ].position, 1.0 ); o.color = half3 ( triangles[ vertexId ].color ); return o; } half4 fragment fragmentMain( v2f in [[stage_in]] ) { return half4( in.color, 1.0 ); } )\"; \/\/ TODO: Create a MTL::RenderPipelineDescriptor \/\/ TODO: Allocate a MTL::RenderPipelineState object\n}<\/code><\/pre>\n<p>Then, extend the <code>Renderer::draw( MTK::View* pView)<\/code> function by setting a <code>MTL::RenderPipelineState<\/code> and inserting draw calls. <\/p>\n<pre class=\"code-source\"><code><span class=\"syntax-function\"><span class=\"syntax-type\">void<\/span> <span class=\"syntax-title\">Renderer::draw<\/span><span class=\"syntax-params\">( MTK::View* pView )<\/span>\n<\/span>{\n... ...\n}<\/code><\/pre>\n<p>After that:<\/p>\n<ul>\n<li>Create the <code>MTL::RenderPipelineDescriptor<\/code> object and set up some properties. <\/li>\n<li>Create the <code>MTL::RenderPipelineState<\/code> object.<\/li>\n<li>Tip: Be careful with object lifecycles. <\/li>\n<\/ul>\n<p>Ready to share your metal-cpp art with the community? Show us what you\u2019ve made on Twitter with the hashtag #WWDC22Challenges, or share your work in the Graphics &amp; Games Study Hall. And if you&#8217;d like to discuss metal-cpp and other Graphics &amp; Games topics, join the team at events all throughout the week at WWDC22.<\/p>\n<p><a href=\"https:\/\/twitter.com\/search?q=%23WWDC22Challenges\" class=\"icon icon-after icon-chevronright\">Explore #WWDC22Challenges on social media<\/a><\/p>\n<p><a href=\"https:\/\/developer.apple.com\/wwdc22\/challenges\/terms\/WWDC22_Challenge_Terms_Conditions.pdf\" class=\"icon icon-after icon-chevronright\">Read the WWDC22 Challenges Terms and Conditions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Metal is the foundation for accelerated graphics and compute power on Apple platforms \u2014 and if you\u2019re familiar with C++, now\u2019s the perfect time to explore its incredible power. For this challenge, we&#8217;re inviting you to try out metal-cpp and render your own triangle, sphere, or even a mesh in Xcode. We also welcome you [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":126103,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-126102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple-developer-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126102"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126102\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/126103"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}