Skip to content

Commit 1d0f78f

Browse files
committedJun 18, 2018
Continued with Metal render context.
1 parent 5b86fb5 commit 1d0f78f

16 files changed

+503
-30
lines changed
 

‎CMakeLists.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ set(FilesTest5 ${PROJECT_SOURCE_DIR}/test/Test5_Vulkan.cpp)
282282
set(FilesTest6 ${PROJECT_SOURCE_DIR}/test/Test6_Performance.cpp)
283283
set(FilesTest7 ${PROJECT_SOURCE_DIR}/test/Test7_Display.cpp)
284284
set(FilesTest8 ${PROJECT_SOURCE_DIR}/test/Test8_Image.cpp)
285+
set(FilesTest9 ${PROJECT_SOURCE_DIR}/test/Test9_Metal.cpp)
285286

286287
# Tutorial files
287288
file(GLOB FilesTutorialBase ${PROJECT_SOURCE_DIR}/tutorial/TutorialBase/*.*)
@@ -542,7 +543,8 @@ endif()
542543
if(APPLE AND LLGL_BUILD_RENDERER_METAL)
543544
# Metal Renderer
544545
include(cmake/FindMetal.cmake)
545-
if(METAL_FOUND)
546+
include(cmake/FindMetalKit.cmake)
547+
if(METAL_FOUND AND METALKIT_FOUND)
546548
if(LLGL_BUILD_STATIC_LIB)
547549
add_library(LLGL_Metal STATIC ${FilesMTL})
548550
set(TEST_PROJECT_LIBS LLGL_Metal)
@@ -551,10 +553,10 @@ if(APPLE AND LLGL_BUILD_RENDERER_METAL)
551553
endif()
552554

553555
set_target_properties(LLGL_Metal PROPERTIES LINKER_LANGUAGE CXX DEBUG_POSTFIX "D")
554-
target_link_libraries(LLGL_Metal LLGL ${METAL_LIBRARY})
556+
target_link_libraries(LLGL_Metal LLGL ${METAL_LIBRARY} ${METALKIT_LIBRARY})
555557
ENABLE_CXX11(LLGL_Metal)
556558
else()
557-
message("Missing Metal -> LLGL_Metal renderer will be excluded from project")
559+
message("Missing Metal/MetalKit -> LLGL_Metal renderer will be excluded from project")
558560
endif()
559561
endif()
560562

@@ -604,6 +606,9 @@ if(GaussLib_INCLUDE_DIR)
604606
ADD_TEST_PROJECT(Test6_Performance "${FilesTest6}" "${TEST_PROJECT_LIBS}")
605607
ADD_TEST_PROJECT(Test7_Display "${FilesTest7}" "${TEST_PROJECT_LIBS}")
606608
ADD_TEST_PROJECT(Test8_Image "${FilesTest8}" "${TEST_PROJECT_LIBS}")
609+
if(APPLE)
610+
ADD_TEST_PROJECT(Test9_Metal "${FilesTest9}" "${TEST_PROJECT_LIBS}")
611+
endif()
607612
endif()
608613

609614
# Tutorial Projects

‎cmake/FindMetalKit.cmake

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Find Metal
2+
#
3+
# METALKIT_INCLUDE_DIR
4+
# METALKIT_LIBRARY
5+
# METALKIT_LIBRARY_DIR
6+
# METALKIT_FOUND
7+
8+
if(APPLE)
9+
find_path(METALKIT_INCLUDE_DIR NAMES metalkit/metalkit.h PATHS ${CMAKE_FIND_ROOT_PATH})
10+
find_library(METALKIT_LIBRARY NAMES metalkit PATHS ${CMAKE_FIND_ROOT_PATH})
11+
get_filename_component(METALKIT_LIBRARY_DIR ${METALKIT_LIBRARY} DIRECTORY)
12+
13+
include(FindPackageHandleStandardArgs)
14+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(MetalKit DEFAULT_MSG METALKIT_LIBRARY METALKIT_INCLUDE_DIR)
15+
16+
mark_as_advanced(METALKIT_INCLUDE_DIR METALKIT_LIBRARY)
17+
endif()
18+

‎include/LLGL/Format.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ enum class DataType
3535
UInt32, //!< 32-bit unsigned integer (unsiged int).
3636

3737
Float, //!< 32-bit floating-point (float).
38-
Double, //!< 64-bit real type (double).
38+
Double, //!< 64-bit real type (double). \note Only supported with: OpenGL
3939
};
4040

4141
/**

‎include/LLGL/TextureFlags.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ enum class TextureFormat
7373
RGB16Sgn, //!< Color format: red, green, blue 16-bit normalized signed interger components. \note Only supported with: OpenGL, Vulkan.
7474
RGB16Float, //!< Color format: red, green, blue 16-bit floating point components. \note Only supported with: OpenGL, Vulkan.
7575

76-
RGB32UInt, //!< Color format: red, green, blue 32-bit un-normalized unsigned interger components.
77-
RGB32SInt, //!< Color format: red, green, blue 32-bit un-normalized signed interger components.
78-
RGB32Float, //!< Color format: red, green, blue 32-bit floating point components.
76+
RGB32UInt, //!< Color format: red, green, blue 32-bit un-normalized unsigned interger components. \note Only supported with: OpenGL, Vulkan, Direct3D 11, Direct3D 12.
77+
RGB32SInt, //!< Color format: red, green, blue 32-bit un-normalized signed interger components. \note Only supported with: OpenGL, Vulkan, Direct3D 11, Direct3D 12.
78+
RGB32Float, //!< Color format: red, green, blue 32-bit floating point components. \note Only supported with: OpenGL, Vulkan, Direct3D 11, Direct3D 12.
7979

8080
RGBA8, //!< Color format: red, green, blue, alpha 8-bit normalized unsigned integer components.
8181
RGBA8Sgn, //!< Color format: red, green, blue, alpha 8-bit normalized signed integer components.

‎sources/Platform/MacOS/MacOSWindow.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ class MacOSWindow : public Window
6363

6464
NSWindow* CreateNSWindow(const WindowDescriptor& desc);
6565

66-
NSWindow* wnd_;
67-
66+
NSWindow* wnd_ = nullptr;
6867
Offset2D prevMotionOffset_;
6968

7069
};

‎sources/Renderer/Metal/MTCommandBuffer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class MTCommandBuffer : public CommandBuffer
110110

111111
private:
112112

113-
//id<MTLCommandQueue> commandQueue_ = nil;
114-
id<MTLCommandBuffer> commandBuffer_ = nil;
113+
id<MTLCommandQueue> cmdQueue_ = nil;
114+
id<MTLCommandBuffer> cmdBuffer_ = nil;
115115

116116
id<MTLRenderCommandEncoder> renderEncoder_ = nil;
117117
//id<MTLComputeCommandEncoder> computeEncoder_ = nil;

‎sources/Renderer/Metal/MTCommandBuffer.mm

+47-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88
#include "MTCommandBuffer.h"
99
#include "MTRenderContext.h"
1010
#include "../CheckedCast.h"
11+
#include <algorithm>
1112

1213

1314
namespace LLGL
1415
{
1516

1617

17-
MTCommandBuffer::MTCommandBuffer(id<MTLCommandQueue> commandQueue)
18+
static const std::uint32_t g_maxNumViewportsAndScissors = 32;
19+
20+
MTCommandBuffer::MTCommandBuffer(id<MTLCommandQueue> commandQueue) :
21+
cmdQueue_ { commandQueue }
1822
{
19-
commandBuffer_ = [commandQueue commandBuffer];
2023
}
2124

2225
MTCommandBuffer::~MTCommandBuffer()
2326
{
24-
[commandBuffer_ release];
27+
[cmdBuffer_ release];
2528
}
2629

2730
/* ----- Configuration ----- */
@@ -33,20 +36,58 @@
3336

3437
/* ----- Viewport and Scissor ----- */
3538

39+
static void Convert(MTLViewport& dst, const Viewport& src)
40+
{
41+
dst.originX = static_cast<double>(src.x);
42+
dst.originY = static_cast<double>(src.y);
43+
dst.width = static_cast<double>(src.width);
44+
dst.height = static_cast<double>(src.height);
45+
dst.znear = static_cast<double>(src.minDepth);
46+
dst.zfar = static_cast<double>(src.maxDepth);
47+
}
48+
3649
void MTCommandBuffer::SetViewport(const Viewport& viewport)
3750
{
51+
MTLViewport viewportMT;
52+
Convert(viewportMT, viewport);
53+
[renderEncoder_ setViewport:viewportMT];
3854
}
3955

4056
void MTCommandBuffer::SetViewports(std::uint32_t numViewports, const Viewport* viewports)
4157
{
58+
MTLViewport viewportsML[g_maxNumViewportsAndScissors];
59+
60+
numViewports = std::min(numViewports, g_maxNumViewportsAndScissors);
61+
for (std::uint32_t i = 0; i < numViewports; ++i)
62+
Convert(viewportsML[i], viewports[i]);
63+
64+
[renderEncoder_ setViewports:viewportsML count:(NSUInteger)numViewports];
65+
}
66+
67+
static void Convert(MTLScissorRect& dst, const Scissor& scissor)
68+
{
69+
dst.x = static_cast<NSUInteger>(std::max(0, scissor.x));
70+
dst.y = static_cast<NSUInteger>(std::max(0, scissor.y));
71+
dst.width = static_cast<NSUInteger>(std::max(0, scissor.width));
72+
dst.height = static_cast<NSUInteger>(std::max(0, scissor.height));
4273
}
4374

4475
void MTCommandBuffer::SetScissor(const Scissor& scissor)
4576
{
77+
MTLScissorRect rect;
78+
Convert(rect, scissor);
79+
[renderEncoder_ setScissorRect:rect];
4680
}
4781

4882
void MTCommandBuffer::SetScissors(std::uint32_t numScissors, const Scissor* scissors)
4983
{
84+
MTLScissorRect rects[g_maxNumViewportsAndScissors];
85+
86+
numScissors = std::min(numScissors, g_maxNumViewportsAndScissors);
87+
for (std::uint32_t i = 0; i < numScissors; ++i)
88+
Convert(rects[i], scissors[i]);
89+
90+
[renderEncoder_ setScissorRects:rects count:(NSUInteger)numScissors];
5091
}
5192

5293
/* ----- Clear ----- */
@@ -136,7 +177,9 @@
136177

137178
void MTCommandBuffer::SetRenderTarget(RenderContext& renderContext)
138179
{
139-
//todo
180+
auto& renderContextMT = LLGL_CAST(MTRenderContext&, renderContext);
181+
cmdBuffer_ = [cmdQueue_ commandBuffer];
182+
renderContextMT.SetCommandBufferForPresent(cmdBuffer_);
140183
}
141184

142185

‎sources/Renderer/Metal/MTCommandQueue.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class MTCommandQueue : public CommandQueue
2525

2626
/* ----- Common ----- */
2727

28-
MTCommandQueue();
28+
MTCommandQueue(id<MTLDevice> device);
29+
~MTCommandQueue();
2930

3031
/* ----- Command queues ----- */
3132

@@ -37,9 +38,18 @@ class MTCommandQueue : public CommandQueue
3738

3839
bool WaitFence(Fence& fence, std::uint64_t timeout) override;
3940
void WaitIdle() override;
41+
42+
/* ----- Extended functions ----- */
43+
44+
// Returns the native MTLCommandQueue object.
45+
inline id<MTLCommandQueue> GetNative() const
46+
{
47+
return queue_;
48+
}
4049

4150
private:
4251

52+
id<MTLCommandQueue> queue_;
4353

4454
};
4555

‎sources/Renderer/Metal/MTCommandQueue.mm

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
{
1414

1515

16-
MTCommandQueue::MTCommandQueue()
16+
MTCommandQueue::MTCommandQueue(id<MTLDevice> device)
1717
{
18+
queue_ = [device newCommandQueue];
19+
}
20+
21+
MTCommandQueue::~MTCommandQueue()
22+
{
23+
[queue_ release];
1824
}
1925

2026
/* ----- Command queues ----- */

‎sources/Renderer/Metal/MTRenderContext.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,26 @@ class MTRenderContext : public RenderContext
2727
/* ----- Common ----- */
2828

2929
MTRenderContext(
30+
id<MTLDevice> device,
3031
RenderContextDescriptor desc,
3132
const std::shared_ptr<Surface>& surface
3233
);
3334

3435
~MTRenderContext();
3536

3637
void Present() override;
38+
39+
/* ----- Extended functions ----- */
40+
41+
void SetCommandBufferForPresent(id<MTLCommandBuffer> commandBuffer);
3742

3843
private:
44+
45+
bool OnSetVideoMode(const VideoModeDescriptor& videoModeDesc) override;
46+
bool OnSetVsync(const VsyncDescriptor& vsyncDesc) override;
3947

40-
41-
/* ----- Common objects ----- */
42-
48+
MTKView* view_ = nullptr;
49+
id<MTLCommandBuffer> commandBuffer_ = nil;
4350

4451
};
4552

‎sources/Renderer/Metal/MTRenderContext.mm

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "MTRenderContext.h"
9+
#include <LLGL/Platform/NativeHandle.h>
910

1011

1112
namespace LLGL
@@ -15,12 +16,23 @@
1516
/* ----- Common ----- */
1617

1718
MTRenderContext::MTRenderContext(
19+
id<MTLDevice> device,
1820
RenderContextDescriptor desc,
1921
const std::shared_ptr<Surface>& surface)
2022
{
23+
/* Create surface */
2124
SetOrCreateSurface(surface, desc.videoMode, nullptr);
2225
desc.videoMode = GetVideoMode();
2326

27+
NativeHandle nativeHandle = {};
28+
GetSurface().GetNativeHandle(&nativeHandle);
29+
30+
/* Create MetalKit view */
31+
NSWindow* wnd = nativeHandle.window;
32+
view_ = [[MTKView alloc] initWithFrame:wnd.frame device:device];
33+
[wnd setContentView:view_];
34+
[wnd.contentViewController setView:view_];
35+
2436
//TODO: create swap-chain
2537
}
2638

@@ -30,14 +42,32 @@
3042

3143
void MTRenderContext::Present()
3244
{
33-
//todo
45+
[commandBuffer_ presentDrawable:view_.currentDrawable];
46+
[commandBuffer_ commit];
47+
}
48+
49+
/* ----- Extended functions ----- */
50+
51+
void MTRenderContext::SetCommandBufferForPresent(id<MTLCommandBuffer> commandBuffer)
52+
{
53+
commandBuffer_ = commandBuffer;
3454
}
3555

3656

3757
/*
3858
* ======= Private: =======
3959
*/
4060

61+
bool MTRenderContext::OnSetVideoMode(const VideoModeDescriptor& videoModeDesc)
62+
{
63+
return false; //todo
64+
}
65+
66+
bool MTRenderContext::OnSetVsync(const VsyncDescriptor& vsyncDesc)
67+
{
68+
return false; //todo
69+
}
70+
4171

4272
} // /namespace LLGL
4373

‎sources/Renderer/Metal/MTRenderSystem.h

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class MTRenderSystem : public RenderSystem
135135
private:
136136

137137
void CreateDevice();
138+
void CreateCommandQueue();
138139

139140
/* ----- Common objects ----- */
140141

‎sources/Renderer/Metal/MTRenderSystem.mm

+24-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "MTRenderSystem.h"
99
#include "../CheckedCast.h"
1010
#include "../../Core/Helper.h"
11+
#include "../../Core/Vendor.h"
1112

1213

1314
namespace LLGL
@@ -19,6 +20,7 @@
1920
MTRenderSystem::MTRenderSystem()
2021
{
2122
CreateDevice();
23+
CreateCommandQueue();
2224
}
2325

2426
MTRenderSystem::~MTRenderSystem()
@@ -29,39 +31,36 @@
2931

3032
RenderContext* MTRenderSystem::CreateRenderContext(const RenderContextDescriptor& desc, const std::shared_ptr<Surface>& surface)
3133
{
32-
return nullptr;//todo
34+
return TakeOwnership(renderContexts_, MakeUnique<MTRenderContext>(device_, desc, surface));
3335
}
3436

3537
void MTRenderSystem::Release(RenderContext& renderContext)
3638
{
37-
//todo
38-
//RemoveFromUniqueSet(renderContexts_, &renderContext);
39+
RemoveFromUniqueSet(renderContexts_, &renderContext);
3940
}
4041

4142
/* ----- Command queues ----- */
4243

4344
CommandQueue* MTRenderSystem::GetCommandQueue()
4445
{
45-
return nullptr;//todo
46-
//return commandQueue_.get();
46+
return commandQueue_.get();
4747
}
4848

4949
/* ----- Command buffers ----- */
5050

5151
CommandBuffer* MTRenderSystem::CreateCommandBuffer()
5252
{
53-
return nullptr;//todo
53+
return TakeOwnership(commandBuffers_, MakeUnique<MTCommandBuffer>(commandQueue_->GetNative()));
5454
}
5555

5656
CommandBufferExt* MTRenderSystem::CreateCommandBufferExt()
5757
{
58-
return nullptr;//todo
58+
return nullptr; // dummy
5959
}
6060

6161
void MTRenderSystem::Release(CommandBuffer& commandBuffer)
6262
{
63-
//todo
64-
//RemoveFromUniqueSet(commandBuffers_, &commandBuffer);
63+
RemoveFromUniqueSet(commandBuffers_, &commandBuffer);
6564
}
6665

6766
/* ----- Buffers ------ */
@@ -287,9 +286,25 @@
287286

288287
void MTRenderSystem::CreateDevice()
289288
{
289+
/* Create Metal device */
290290
device_ = MTLCreateSystemDefaultDevice();
291291
if (device_ == nil)
292292
throw std::runtime_error("failed to create Metal device");
293+
294+
/* Initialize renderer information */
295+
RendererInfo info;
296+
{
297+
info.rendererName = "???";
298+
info.deviceName = [[device_ name] cStringUsingEncoding:NSUTF8StringEncoding];
299+
info.vendorName = "???";
300+
info.shadingLanguageName = "Metal";
301+
}
302+
SetRendererInfo(info);
303+
}
304+
305+
void MTRenderSystem::CreateCommandQueue()
306+
{
307+
commandQueue_ = MakeUnique<MTCommandQueue>(device_);
293308
}
294309

295310

‎sources/Renderer/Metal/MTTypes.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* MTTypes.h
3+
*
4+
* This file is part of the "LLGL" project (Copyright (c) 2015-2018 by Lukas Hermanns)
5+
* See "LICENSE.txt" for license information.
6+
*/
7+
8+
#ifndef LLGL_MT_TYPES_H
9+
#define LLGL_MT_TYPES_H
10+
11+
12+
#import <Metal/Metal.h>
13+
14+
#include <LLGL/ShaderFlags.h>
15+
#include <LLGL/PipelineLayoutFlags.h>
16+
#include <LLGL/ResourceHeapFlags.h>
17+
#include <LLGL/GraphicsPipelineFlags.h>
18+
#include <LLGL/Format.h>
19+
#include <LLGL/SamplerFlags.h>
20+
#include <LLGL/QueryFlags.h>
21+
22+
23+
namespace LLGL
24+
{
25+
26+
namespace VKTypes
27+
{
28+
29+
30+
/* ----- Map functions ----- */
31+
32+
[[noreturn]]
33+
void MapFailed(const std::string& typeName, const std::string& vknTypeName);
34+
35+
MTLDataType Map( const DataType dataType );
36+
MTLDataType Map( const VectorType vectorType );
37+
MTLPixelFormat Map( const TextureFormat textureFormat );
38+
MTLTextureType Map( const TextureType textureType );
39+
MTLPrimitiveType Map( const PrimitiveTopology primitiveTopology );
40+
MTLCullMode Map( const CullMode cullMode );
41+
42+
43+
} // /namespace VKTypes
44+
45+
} // /namespace LLGL
46+
47+
48+
#endif
49+
50+
51+
52+
// ================================================================================

‎sources/Renderer/Metal/MTTypes.mm

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* MTTypes.mm
3+
*
4+
* This file is part of the "LLGL" project (Copyright (c) 2015-2018 by Lukas Hermanns)
5+
* See "LICENSE.txt" for license information.
6+
*/
7+
8+
#include "MTTypes.h"
9+
#include <stdexcept>
10+
#include <string>
11+
12+
13+
namespace LLGL
14+
{
15+
16+
namespace MTTypes
17+
{
18+
19+
20+
/* ----- Map functions ----- */
21+
22+
[[noreturn]]
23+
void MapFailed(const std::string& typeName, const std::string& mtlTypeName)
24+
{
25+
throw std::invalid_argument("failed to map <LLGL::" + typeName + "> to <" + mtlTypeName + "> Metal parameter");
26+
}
27+
28+
MTLDataType Map(const DataType dataType)
29+
{
30+
switch (dataType)
31+
{
32+
case DataType::Int8: return MTLDataTypeChar;
33+
case DataType::UInt8: return MTLDataTypeUChar;
34+
35+
case DataType::Int16: return MTLDataTypeShort;
36+
case DataType::UInt16: return MTLDataTypeUShort;
37+
38+
case DataType::Int32: return MTLDataTypeInt;
39+
case DataType::UInt32: return MTLDataTypeUInt;
40+
41+
case DataType::Float: return MTLDataTypeFloat;
42+
case DataType::Double: break;
43+
}
44+
MapFailed("DataType", "MTLDataType");
45+
}
46+
47+
MTLDataType Map(const VectorType vectorType)
48+
{
49+
switch (vectorType)
50+
{
51+
case VectorType::Float: return MTLDataTypeFloat;
52+
case VectorType::Float2: return MTLDataTypeFloat2;
53+
case VectorType::Float3: return MTLDataTypeFloat3;
54+
case VectorType::Float4: return MTLDataTypeFloat4;
55+
case VectorType::Double: break;
56+
case VectorType::Double2: break;
57+
case VectorType::Double3: break;
58+
case VectorType::Double4: break;
59+
case VectorType::Int: return MTLDataTypeInt;
60+
case VectorType::Int2: return MTLDataTypeInt2;
61+
case VectorType::Int3: return MTLDataTypeInt3;
62+
case VectorType::Int4: return MTLDataTypeInt4;
63+
case VectorType::UInt: return MTLDataTypeUInt;
64+
case VectorType::UInt2: return MTLDataTypeUInt2;
65+
case VectorType::UInt3: return MTLDataTypeUInt3;
66+
case VectorType::UInt4: return MTLDataTypeUInt4;
67+
}
68+
MapFailed("VectorType", "MTLDataType");
69+
}
70+
71+
MTLPixelFormat Map(const TextureFormat textureFormat)
72+
{
73+
switch (textureFormat)
74+
{
75+
case TextureFormat::Unknown: break;
76+
77+
/* --- Color formats --- */
78+
case TextureFormat::R8: return MTLPixelFormatR8Unorm;
79+
case TextureFormat::R8Sgn: return MTLPixelFormatR8Snorm;
80+
81+
case TextureFormat::R16: return MTLPixelFormatR16Unorm;
82+
case TextureFormat::R16Sgn: return MTLPixelFormatR16Snorm;
83+
case TextureFormat::R16Float: return MTLPixelFormatR16Float;
84+
85+
case TextureFormat::R32UInt: return MTLPixelFormatR32Uint;
86+
case TextureFormat::R32SInt: return MTLPixelFormatR32Sint;
87+
case TextureFormat::R32Float: return MTLPixelFormatR32Float;
88+
89+
case TextureFormat::RG8: return MTLPixelFormatRG8Unorm;
90+
case TextureFormat::RG8Sgn: return MTLPixelFormatRG8Snorm;
91+
92+
case TextureFormat::RG16: return MTLPixelFormatRG16Unorm;
93+
case TextureFormat::RG16Sgn: return MTLPixelFormatRG16Snorm;
94+
case TextureFormat::RG16Float: return MTLPixelFormatRG16Float;
95+
96+
case TextureFormat::RG32UInt: return MTLPixelFormatRG32Uint;
97+
case TextureFormat::RG32SInt: return MTLPixelFormatRG32Sint;
98+
case TextureFormat::RG32Float: return MTLPixelFormatRG32Float;
99+
100+
case TextureFormat::RGB8: break;
101+
case TextureFormat::RGB8Sgn: break;
102+
103+
case TextureFormat::RGB16: break;
104+
case TextureFormat::RGB16Sgn: break;
105+
case TextureFormat::RGB16Float: break;
106+
107+
case TextureFormat::RGB32UInt: break;
108+
case TextureFormat::RGB32SInt: break;
109+
case TextureFormat::RGB32Float: break;
110+
111+
case TextureFormat::RGBA8: return MTLPixelFormatRGBA8Unorm;
112+
case TextureFormat::RGBA8Sgn: return MTLPixelFormatRGBA8Snorm;
113+
114+
case TextureFormat::RGBA16: return MTLPixelFormatRGBA16Unorm;
115+
case TextureFormat::RGBA16Sgn: return MTLPixelFormatRGBA16Snorm;
116+
case TextureFormat::RGBA16Float: return MTLPixelFormatRGBA16Float;
117+
118+
case TextureFormat::RGBA32UInt: return MTLPixelFormatRGBA32Uint;
119+
case TextureFormat::RGBA32SInt: return MTLPixelFormatRGBA32Sint;
120+
case TextureFormat::RGBA32Float: return MTLPixelFormatRGBA32Float;
121+
122+
/* --- Depth-stencil formats --- */
123+
case TextureFormat::D32: return MTLPixelFormatDepth32Float;
124+
case TextureFormat::D24S8: return MTLPixelFormatDepth24Unorm_Stencil8;
125+
126+
/* --- Compressed color formats --- */
127+
case TextureFormat::RGB_DXT1: break;
128+
case TextureFormat::RGBA_DXT1: return MTLPixelFormatBC1_RGBA;
129+
case TextureFormat::RGBA_DXT3: return MTLPixelFormatBC2_RGBA;
130+
case TextureFormat::RGBA_DXT5: return MTLPixelFormatBC3_RGBA;
131+
}
132+
MapFailed("TextureFormat", "MTLPixelFormat");
133+
}
134+
135+
MTLTextureType Map(const TextureType textureType)
136+
{
137+
switch (textureType)
138+
{
139+
case TextureType::Texture1D: return MTLTextureType1D;
140+
case TextureType::Texture2D: return MTLTextureType2D;
141+
case TextureType::Texture3D: return MTLTextureType3D;
142+
case TextureType::TextureCube: return MTLTextureTypeCube;
143+
case TextureType::Texture1DArray: return MTLTextureType1DArray;
144+
case TextureType::Texture2DArray: return MTLTextureType2DArray;
145+
case TextureType::TextureCubeArray: return MTLTextureTypeCubeArray;
146+
case TextureType::Texture2DMS: return MTLTextureType2DMultisample;
147+
case TextureType::Texture2DMSArray: break;//return MTLTextureType2DMultisampleArray; // Beta
148+
}
149+
MapFailed("TextureType", "MTLTextureType");
150+
}
151+
152+
MTLPrimitiveType Map(const PrimitiveTopology primitiveTopology)
153+
{
154+
switch (primitiveTopology)
155+
{
156+
case PrimitiveTopology::PointList: return MTLPrimitiveTypePoint;
157+
case PrimitiveTopology::LineList: return MTLPrimitiveTypeLine;
158+
case PrimitiveTopology::LineStrip: return MTLPrimitiveTypeLineStrip;
159+
case PrimitiveTopology::TriangleList: return MTLPrimitiveTypeTriangle;
160+
case PrimitiveTopology::TriangleStrip: return MTLPrimitiveTypeTriangleStrip;
161+
default: break;
162+
}
163+
MapFailed("PrimitiveTopology", "MTLPrimitiveType");
164+
}
165+
166+
MTLCullMode Map(const CullMode cullMode)
167+
{
168+
switch (cullMode)
169+
{
170+
case CullMode::Disabled: return MTLCullModeNone;
171+
case CullMode::Front: return MTLCullModeFront;
172+
case CullMode::Back: return MTLCullModeBack;
173+
}
174+
MapFailed("CullMode", "MTLCullMode");
175+
}
176+
177+
178+
} // /namespace MTTypes
179+
180+
} // /namespace LLGL
181+
182+
183+
184+
// ================================================================================

‎test/Test9_Metal.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Test9_Metal.cpp
3+
*
4+
* This file is part of the "LLGL" project (Copyright (c) 2015-2018 by Lukas Hermanns)
5+
* See "LICENSE.txt" for license information.
6+
*/
7+
8+
#include "Helper.h"
9+
#include <memory>
10+
#include <iostream>
11+
#include <string>
12+
#include <sstream>
13+
14+
15+
int main()
16+
{
17+
try
18+
{
19+
// Load render system module
20+
auto renderer = LLGL::RenderSystem::Load("Metal");
21+
22+
// Create render context
23+
LLGL::RenderContextDescriptor contextDesc;
24+
25+
contextDesc.videoMode.resolution = { 800, 600 };
26+
contextDesc.vsync.enabled = true;
27+
28+
auto context = renderer->CreateRenderContext(contextDesc);
29+
30+
// Renderer info
31+
const auto& info = renderer->GetRendererInfo();
32+
33+
std::cout << "Device: " << info.deviceName << std::endl;
34+
std::cout << "Renderer: " << info.rendererName << std::endl;
35+
std::cout << "Vendor: " << info.vendorName << std::endl;
36+
std::cout << "Shading Language: " << info.shadingLanguageName << std::endl;
37+
38+
// Create command buffer
39+
auto commands = renderer->CreateCommandBuffer();
40+
41+
auto& window = static_cast<LLGL::Window&>(context->GetSurface());
42+
43+
// Setup window title
44+
auto title = "LLGL Test 9 ( " + renderer->GetName() + " )";
45+
window.SetTitle(std::wstring(title.begin(), title.end()));
46+
47+
// Setup input controller
48+
auto input = std::make_shared<LLGL::Input>();
49+
window.AddEventListener(input);
50+
51+
// Create vertex buffer
52+
LLGL::VertexFormat vertexFormat;
53+
vertexFormat.AppendAttribute({ "position", LLGL::VectorType::Float2 });
54+
vertexFormat.AppendAttribute({ "color", LLGL::VectorType::Float3 });
55+
56+
struct Vertex
57+
{
58+
Gs::Vector2f position;
59+
LLGL::ColorRGBf color;
60+
}
61+
vertices[] =
62+
{
63+
{ { 0.0f, 0.5f }, { 1.0f, 0.0f, 0.0f } },
64+
{ { 0.5f, -0.5f }, { 0.0f, 1.0f, 0.0f } },
65+
{ { -0.5f, -0.5f }, { 0.0f, 0.0f, 1.0f } },
66+
};
67+
68+
LLGL::BufferDescriptor vertexBufferDesc;
69+
{
70+
vertexBufferDesc.type = LLGL::BufferType::Vertex;
71+
vertexBufferDesc.size = sizeof(vertices);
72+
vertexBufferDesc.vertexBuffer.format = vertexFormat;
73+
}
74+
auto vertexBuffer = renderer->CreateBuffer(vertexBufferDesc, vertices);
75+
76+
//TODO...
77+
78+
// Main loop
79+
commands->SetClearColor(LLGL::ColorRGBAf(0.3f, 0.3f, 1));
80+
81+
while (window.ProcessEvents() && !input->KeyDown(LLGL::Key::Escape))
82+
{
83+
commands->SetRenderTarget(*context);
84+
commands->Clear(LLGL::ClearFlags::Color);
85+
86+
//commands->SetGraphicsPipeline(*pipeline);
87+
commands->SetVertexBuffer(*vertexBuffer);
88+
89+
commands->Draw(3, 0);
90+
91+
context->Present();
92+
}
93+
}
94+
catch (const std::exception& e)
95+
{
96+
std::cerr << e.what() << std::endl;
97+
#ifdef _WIN32
98+
system("pause");
99+
#endif
100+
}
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)
Please sign in to comment.