yum-mirror/slang

Making it easier to work with shaders

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

CopilotAdd utility to trace creation of problematic IRInsts to assist LLM in debugging (#7820)368ddbb7b

master
5.6 KiB167 linesraw
1// slang-platform.h
2#ifndef SLANG_CORE_PLATFORM_H
3#define SLANG_CORE_PLATFORM_H
4
5#include "../core/slang-string.h"
6#include "slang.h"
7
8namespace Slang
9{
10enum class PlatformKind : uint8_t
11{
12    Unknown,
13    WinRT,
14    XBoxOne,
15    Win64,
16    Win32,
17    X360,
18    Android,
19    Linux,
20    IOS,
21    OSX,
22    PS3,
23    PS4,
24    PSP2,
25    WIIU,
26    CountOf,
27};
28
29typedef uint32_t PlatformFlags;
30struct PlatformFlag
31{
32    enum Enum
33    {
34        Unknown = 1 << int(PlatformKind::Unknown),
35        WinRT = 1 << int(PlatformKind::WinRT),
36        XBoxOne = 1 << int(PlatformKind::XBoxOne),
37        Win64 = 1 << int(PlatformKind::Win64),
38        Win32 = 1 << int(PlatformKind::Win32),
39        X360 = 1 << int(PlatformKind::X360),
40        Android = 1 << int(PlatformKind::Android),
41        Linux = 1 << int(PlatformKind::Linux),
42        IOS = 1 << int(PlatformKind::IOS),
43        OSX = 1 << int(PlatformKind::OSX),
44        PS3 = 1 << int(PlatformKind::PS3),
45        PS4 = 1 << int(PlatformKind::PS4),
46        PSP2 = 1 << int(PlatformKind::PSP2),
47        WIIU = 1 << int(PlatformKind::WIIU),
48    };
49};
50
51enum class PlatformFamily : uint8_t
52{
53    Unknown,
54    Windows,
55    Microsoft,
56    Linux,
57    Apple,
58    Unix,
59    CountOf,
60};
61
62// Interface for working with shared libraries
63// in a platform-independent fashion.
64struct SharedLibrary
65{
66    typedef struct SharedLibraryImpl* Handle;
67
68    typedef void (*FuncPtr)(void);
69
70    /// Load via an unadorned path/filename.
71    ///
72    /// The unadorned path here means without a path without any platform specific filename
73    /// elements. This typically means no extension and no prefix. On windows this means without the
74    /// '.dll' extension. On linux this means without the 'lib' prefix and '.so' extension. To load
75    /// with a platform specific filename use the 'loadWithPlatformFilename' API Most platforms have
76    /// a built in mechanism to search for shared libraries, such that shared libraries are often
77    /// just passed by filename.
78    ///
79    /// @param the unadorned filename/path
80    /// @return Returns a non null handle for the shared library on success. nullptr indicated
81    /// failure
82    static SlangResult load(const char* path, Handle& handleOut);
83
84    /// Attempt to load a shared library for
85    /// the current platform. Returns null handle on failure
86    /// The platform specific filename can be generated from a call to appendPlatformFileName
87    ///
88    /// @param platform the platform specific file name/ or path
89    /// @return Returns a non null handle for the shared library on success. nullptr indicated
90    /// failure
91    static SlangResult loadWithPlatformPath(char const* platformPath, Handle& handleOut);
92
93    /// Unload the library that was returned from load as handle
94    /// @param The valid handle returned from load
95    static void unload(Handle handle);
96
97    /// Given a shared library handle and a name, return the associated object
98    /// Return nullptr if object is not found
99    /// @param The shared library handle as returned by loadPlatformLibrary
100    static void* findSymbolAddressByName(Handle handle, char const* name);
101
102    /// Append to the end of dst, the name, with any platform specific additions
103    /// The input name should be unadorned with any 'lib' prefix or extension
104    static void appendPlatformFileName(const UnownedStringSlice& name, StringBuilder& dst);
105
106    /// Given a path, calculate that path with the filename replaced with the platform filename
107    /// (using appendPlatformFilename)
108    static String calcPlatformPath(const UnownedStringSlice& path);
109    static void calcPlatformPath(const UnownedStringSlice& path, StringBuilder& outBuilder);
110
111
112private:
113    /// Not constructible!
114    SharedLibrary();
115};
116
117struct PlatformUtil
118{
119    /// Appends a text interpretation of a result (as defined by supporting OS)
120    /// @param res Result to produce a string for
121    /// @param builderOut Append the string produced to builderOut
122    /// @return SLANG_OK if string is found and appended. Fail otherwise. SLANG_E_NOT_IMPLEMENTED if
123    /// there is no impl for this platform.
124    static SlangResult appendResult(SlangResult res, StringBuilder& builderOut);
125
126    /// Get the platform kind as determined at compile time
127    static PlatformKind getPlatformKind();
128
129    /// Get the platforms that make up a family
130    static PlatformFlags getPlatformFlags(PlatformFamily family);
131
132    /// True if the kind is part of the family
133    static bool isFamily(PlatformFamily family, PlatformKind kind)
134    {
135        return (getPlatformFlags(family) & (PlatformFlags(1) << int(kind))) != 0;
136    }
137
138    /// Given an environment name returns the set system variable.
139    /// Will return SLANG_E_NOT_FOUND if the variable is not set
140    static SlangResult getEnvironmentVariable(const UnownedStringSlice& name, StringBuilder& out);
141
142    /// Get the path to this instance (the path to the dll/executable/shared library the call is in)
143    /// NOTE! This is not supported on all platforms, and will return SLANG_E_NOT_IMPLEMENTED in
144    /// that scenario
145    static SlangResult getInstancePath(StringBuilder& out);
146
147    /// Outputs message to a debug stream. Not all platforms support
148    /// this feature.
149    ///
150    /// @param text Text to be displayed in 'debugger output'
151    /// @return SLANG_E_NOT_AVAILABLE if not on this platform, and potentially other errors
152    static SlangResult outputDebugMessage(const char* text);
153
154    /// Print a stack trace to stderr for debugging purposes.
155    /// Only available on Linux family platforms.
156    static void backtrace();
157};
158
159#ifndef _MSC_VER
160#define _fileno fileno
161#define _isatty isatty
162#define _setmode setmode
163#define _O_BINARY O_BINARY
164#endif
165} // namespace Slang
166
167#endif