챌린지: Metal-cpp로 그리기

Metal은 Apple 플랫폼에서 가속 그래픽과 컴퓨팅 성능을 활용하기 위한 기반입니다. C++에 익숙하시다면 지금이 Metal의 놀라운 성능을 살펴볼 수 있는 최적의 기회입니다. 이번 챌린지에서 Metal-cpp를 사용해 보고 Xcode에서 나만의 삼각형, 구 또는 메시도 렌더링해 보시기 바랍니다.

하루 동안 그래픽 및 게임 스터디 홀(Study Hall)을 방문하여 이 챌린지를 공동으로 진행할 수도 있습니다. 질문하거나 다른 개발자와 소통하거나 창작물을 공유해 보세요.

Study Hall: Draw with metal-cpp

Get ready to render: We're inviting you to try out metal-cpp and render your own triangle, sphere, or even a mesh in Xcode. Visit the Graphics & Games Study Hall to collaborate on the “Draw with metal-cpp” coding challenge. Ask questions, connect with other developers, and share your creations.

View now

챌린지 시작하기

시작하기 전에 ‘Metal-cpp를 통해 C++로 Metal 프로그래밍’을 시청할 수 있으며, 일련의 C++ 샘플이 포함된 LearnMetalCPP 프로젝트를 다운로드할 수 있습니다.

Metal-cpp를 통해 C++로 Metal 프로그래밍

이제 C++ 게임과 앱에서 Metal의 강력한 기능을 활용할 수 있습니다. Metal-cpp를 통해 C++ 코드를 Metal과 연동하는 방법을 보여드리고, 각각을 통해 개체 라이프사이클을 관리하는 방법을 살펴보며, 이러한 언어가 앱에서 협력할 수...

Watch now

Download the LearnMetalCPP project

Xcode에서 프로젝트를 열고 00-window.cpp를 기본 코드로 선택합니다. 이미지를 렌더링하려면 프로젝트 내에서 몇 가지를 설정해야 합니다.

먼저, MTL::RenderPipelineDescriptorMTL::RenderPipelineState 개체를 만듭니다. 이를 위해서는 buildShaders()와 같은 함수를 만들어야 합니다. 아래의 코드 스니펫에 하나의 삼각형을 렌더링하는 데 필요한 셰이더 코드가 있습니다.

void Renderer::buildShaders()
{
    using NS::StringEncoding::UTF8StringEncoding;

    const char shaderSrc = R"(
        #include <metal_stdlib>
        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
}

그런 다음 Renderer::draw( MTK::View pView) 함수를 확장할 수 있도록 MTL::RenderPipelineState를 설정하고 그리기 호출을 삽입합니다.

void Renderer::draw(  MTK::View pView  )
{
...
    // TODO: Set MTL::RenderPipelineState
    // TODO: Draw a single triangle or more!
...
}

그런 다음,

  • MTL::RenderPipelineDescriptor 개체를 만들고 일부 속성을 설정합니다.
  • MTL::RenderPipelineState 개체를 만듭니다.
  • 팁: 개체 라이프사이클에 주의하세요.

Metal-cpp 아트를 커뮤니티에 공유할 준비가 되셨나요? 여러분의 창작물을 Twitter에서 해시태그 #WWDC22Challenges로 공유하거나 그래픽 및 게임 스터디 홀(Study Hall)에서 여러분의 작업을 공유해 보세요. 그리고 Metal-cpp 및 기타 그래픽 및 게임 주제에 대해 의견을 나누고 싶다면 WWDC22 주간에 진행되는 이벤트에 참여하시기 바랍니다.

Explore #WWDC22Challenges on social media

Read the WWDC22 Challenges Terms and Conditions