yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
1.4 KiB52 linesraw
1// metal-helper-functions.cpp
2#include "metal-helper-functions.h"
3
4#include "metal-device.h"
5#include "metal-util.h"
6
7namespace gfx
8{
9
10using namespace Slang;
11
12Result SLANG_MCALL getMetalAdapters(List<AdapterInfo>& outAdapters)
13{
14    AUTORELEASEPOOL
15
16    auto addAdapter = [&](MTL::Device* device)
17    {
18        AdapterInfo info = {};
19        const char* name = device->name()->cString(NS::ASCIIStringEncoding);
20        memcpy(info.name, name, Math::Min(strlen(name), sizeof(AdapterInfo::name) - 1));
21        uint64_t registryID = device->registryID();
22        memcpy(&info.luid.luid[0], &registryID, sizeof(registryID));
23        outAdapters.add(info);
24    };
25
26    NS::Array* devices = MTL::CopyAllDevices();
27    if (devices->count() > 0)
28    {
29        for (int i = 0; i < devices->count(); ++i)
30        {
31            MTL::Device* device = static_cast<MTL::Device*>(devices->object(i));
32            addAdapter(device);
33        }
34    }
35    else
36    {
37        MTL::Device* device = MTL::CreateSystemDefaultDevice();
38        addAdapter(device);
39        device->release();
40    }
41    return SLANG_OK;
42}
43
44Result SLANG_MCALL createMetalDevice(const IDevice::Desc* desc, IDevice** outRenderer)
45{
46    RefPtr<metal::DeviceImpl> result = new metal::DeviceImpl();
47    SLANG_RETURN_ON_FAIL(result->initialize(*desc));
48    returnComPtr(outRenderer, result);
49    return SLANG_OK;
50}
51
52} // namespace gfx