diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2018-06-22 13:09:01 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-22 13:09:01 -0400 |
| commit | d0c9571be3a2167a9f019310aca8f7cd326972c0 (patch) | |
| tree | 52aa2f14ec2c9d8d42bc6fcbd381ed9799c19533 | |
| parent | e66d66b88e1c6ef8499708952fcbe3ba873f6e4c (diff) | |
Expose macros/functionality for defining interfaces (#604)
* Added Result definitions to the slang.h
* Removed slang-result.h and added slang-com-helper.h
* Move slang-com-ptr.h to be publically available.
* Add SLANG_IUNKNOWN macros to simplify implementing interfaces.
Use the SLANG_IUNKNOWN macros to in slang.c
* Removed slang-defines.h added outstanding defines to slang.h
25 files changed, 510 insertions, 732 deletions
diff --git a/slang-com-helper.h b/slang-com-helper.h new file mode 100644 index 000000000..1b4469db1 --- /dev/null +++ b/slang-com-helper.h @@ -0,0 +1,107 @@ +#ifndef SLANG_COM_HELPER_H +#define SLANG_COM_HELPER_H + +/** \file slang-com-helper.h +*/ + +#include "slang.h" + +/* !!!!!!!!!!!!!!!!!!!!! Macros to help checking SlangResult !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +/*! Set SLANG_HANDLE_RESULT_FAIL(x) to code to be executed whenever an error occurs, and is detected by one of the macros */ +#ifndef SLANG_HANDLE_RESULT_FAIL +# define SLANG_HANDLE_RESULT_FAIL(x) +#endif + +//! Helper macro, that makes it easy to add result checking to calls in functions/methods that themselves return Result. +#define SLANG_RETURN_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return _res; } } +//! Helper macro that can be used to test the return value from a call, and will return in a void method/function +#define SLANG_RETURN_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return; } } +//! Helper macro that will return false on failure. +#define SLANG_RETURN_FALSE_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return false; } } +//! Helper macro that will return nullptr on failure. +#define SLANG_RETURN_NULL_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return nullptr; } } + +//! Helper macro that will assert if the return code from a call is failure, also returns the failure. +#define SLANG_ASSERT_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return _res; } } +//! Helper macro that will assert if the result from a call is a failure, also returns. +#define SLANG_ASSERT_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return; } } + +/* !!!!!!!!!!!!!!!!!!!!!!! C++ helpers !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +#if defined(__cplusplus) +namespace Slang { + +// Alias SlangResult to Slang::Result +typedef SlangResult Result; +// Alias SlangUUID to Slang::Guid +typedef SlangUUID Guid; + +SLANG_FORCE_INLINE bool operator==(const Guid& aIn, const Guid& bIn) +{ + // Use the largest type the honors the alignment of Guid + typedef uint32_t CmpType; + union GuidCompare + { + Guid guid; + CmpType data[sizeof(Guid) / sizeof(CmpType)]; + }; + // Type pun - so compiler can 'see' the pun and not break aliasing rules + const CmpType* a = reinterpret_cast<const GuidCompare&>(aIn).data; + const CmpType* b = reinterpret_cast<const GuidCompare&>(bIn).data; + // Make the guid comparison a single branch, by not using short circuit + return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]) | (a[3] ^ b[3])) == 0; +} + +SLANG_FORCE_INLINE bool operator!=(const Guid& a, const Guid& b) +{ + return !(a == b); +} + + +/* !!!!!!!! Macros to simplify implementing COM interfaces !!!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +/* Assumes underlying implementation has a member m_refCount that is initialized to 0 and can have ++ and -- operate on it. +For SLANG_IUNKNOWN_QUERY_INTERFACE to work - must have a method 'getInterface' that returns valid pointers for the Guid, or nullptr +if not found. */ + +#define SLANG_IUNKNOWN_QUERY_INTERFACE \ +SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) \ +{ \ + ISlangUnknown* intf = getInterface(uuid); \ + if (intf) \ + { \ + addRef(); \ + *outObject = intf; \ + return SLANG_OK;\ + } \ + return SLANG_E_NO_INTERFACE;\ +} + +#define SLANG_IUNKNOWN_ADD_REF \ +SLANG_NO_THROW uint32_t SLANG_MCALL addRef() \ +{ \ + return ++m_refCount; \ +} + +#define SLANG_IUNKNOWN_RELEASE \ +SLANG_NO_THROW uint32_t SLANG_MCALL release() \ +{ \ + --m_refCount; \ + if (m_refCount == 0) \ + { \ + delete this; \ + return 0; \ + } \ + return m_refCount; \ +} \ + +#define SLANG_IUNKNOWN_ALL \ + SLANG_IUNKNOWN_QUERY_INTERFACE \ + SLANG_IUNKNOWN_ADD_REF \ + SLANG_IUNKNOWN_RELEASE + +} // namespace Slang +#endif // defined(__cplusplus) + +#endif diff --git a/source/core/slang-com-ptr.h b/slang-com-ptr.h index 765edbee5..7860256cc 100644 --- a/source/core/slang-com-ptr.h +++ b/slang-com-ptr.h @@ -1,8 +1,7 @@ -#ifndef SLANG_COM_PTR_H +#ifndef SLANG_COM_PTR_H #define SLANG_COM_PTR_H -#include "slang-defines.h" -#include "slang-result.h" +#include "slang-com-helper.h" #include <assert.h> @@ -10,7 +9,7 @@ namespace Slang { /*! \brief ComPtr is a simple smart pointer that manages types which implement COM based interfaces. \details A class that implements a COM, must derive from the IUnknown interface or a type that matches -it's layout exactly (such as IForwardUnknown). Trying to use this template with a class that doesn't follow +it's layout exactly (such as ISlangUnknown). Trying to use this template with a class that doesn't follow these rules, will lead to undefined behavior. This is a 'strong' pointer type, and will AddRef when a non null pointer is set and Release when the pointer leaves scope. @@ -22,12 +21,9 @@ is to write into the smart pointer, other times to pass as an array. To handle t there are the methods readRef and writeRef, which are used instead of the & (ref) operator. For example \code - Void doSomething(ID3D12Resource** resources, IndexT numResources); - // ... ComPtr<ID3D12Resource> resources[3]; - doSomething(resources[0].readRef(), SLANG_COUNT_OF(resource)); \endcode @@ -41,45 +37,6 @@ Result res = unk->QueryInterface(resource.writeRef()); \endcode */ -typedef SlangUUID Guid; - -SLANG_FORCE_INLINE bool operator==(const Guid& aIn, const Guid& bIn) -{ - // Use the largest type the honors the alignment of Guid - typedef uint32_t CmpType; - union GuidCompare - { - Guid guid; - CmpType data[sizeof(Guid) / sizeof(CmpType)]; - }; - // Type pun - so compiler can 'see' the pun and not break aliasing rules - const CmpType* a = reinterpret_cast<const GuidCompare&>(aIn).data; - const CmpType* b = reinterpret_cast<const GuidCompare&>(bIn).data; - // Make the guid comparison a single branch, by not using short circuit - return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]) | (a[3] ^ b[3])) == 0; -} - -SLANG_FORCE_INLINE bool operator!=(const Guid& a, const Guid& b) -{ - return !(a == b); -} - -// Allows for defining of a GUID that works in C++ and C which defines in a format similar to microsofts INTERFACE style -// MIDL_INTERFACE("00000000-0000-0000-C000-00 00 00 00 00 46") - -#define SLANG_GUID_BYTE(x, index) ((uint8_t)(SLANG_UINT64(0x##x) >> (8 * index))) - -#define SLANG_MAKE_GUID(data0, data1, data2, shortTail, tail) \ - { (uint32_t)(0x##data0), (uint16_t)(0x##data1), (uint16_t)(0x##data2), \ - { (uint8_t)(0x##shortTail >> 8), (uint8_t)(0x##shortTail & 0xff), \ - SLANG_GUID_BYTE(tail,5), SLANG_GUID_BYTE(tail,4), SLANG_GUID_BYTE(tail,3), SLANG_GUID_BYTE(tail,2), SLANG_GUID_BYTE(tail,1), SLANG_GUID_BYTE(tail,0) \ - }} - -// Compatible with Microsoft IUnknown -static const Guid IID_IComUnknown = SLANG_MAKE_GUID(00000000, 0000, 0000, C000, 000000000046); - -typedef ISlangUnknown IComUnknown; - // Enum to force initializing as an attach (without adding a reference) enum InitAttach { @@ -92,7 +49,7 @@ class ComPtr public: typedef T Type; typedef ComPtr ThisType; - typedef IComUnknown* Ptr; + typedef ISlangUnknown* Ptr; /// Constructors /// Default Ctor. Sets to nullptr @@ -152,7 +109,7 @@ public: protected: /// Gets the address of the dumb pointer. // Disabled: use writeRef and readRef to get a reference based on usage. - SLANG_FORCE_INLINE T** operator&(); + SLANG_FORCE_INLINE T** operator&() = delete; T* m_ptr; }; @@ -168,13 +125,6 @@ void ComPtr<T>::setNull() } } //---------------------------------------------------------------------------- -/* template <typename T> -T** ComPtr<T>::operator&() -{ - assert(m_ptr == nullptr); - return &m_ptr; -} */ -//---------------------------------------------------------------------------- template <typename T> const ComPtr<T>& ComPtr<T>::operator=(const ThisType& rhs) { @@ -1,4 +1,4 @@ -#ifndef SLANG_H +#ifndef SLANG_H #define SLANG_H /** \file slang.h @@ -17,55 +17,55 @@ used later in the file. Most applications should not need to touch this section. */ #ifndef SLANG_COMPILER -# define SLANG_COMPILER +# define SLANG_COMPILER /* Compiler defines, see http://sourceforge.net/p/predef/wiki/Compilers/ NOTE that SLANG_VC holds the compiler version - not just 1 or 0 */ -# if defined(_MSC_VER) -# if _MSC_VER >= 1900 -# define SLANG_VC 14 -# elif _MSC_VER >= 1800 -# define SLANG_VC 12 -# elif _MSC_VER >= 1700 -# define SLANG_VC 11 -# elif _MSC_VER >= 1600 -# define SLANG_VC 10 -# elif _MSC_VER >= 1500 -# define SLANG_VC 9 -# else -# error "unknown version of Visual C++ compiler" -# endif -# elif defined(__clang__) -# define SLANG_CLANG 1 -# elif defined(__SNC__) -# define SLANG_SNC 1 -# elif defined(__ghs__) -# define SLANG_GHS 1 -# elif defined(__GNUC__) /* note: __clang__, __SNC__, or __ghs__ imply __GNUC__ */ -# define SLANG_GCC 1 -# else -# error "unknown compiler" -# endif +# if defined(_MSC_VER) +# if _MSC_VER >= 1900 +# define SLANG_VC 14 +# elif _MSC_VER >= 1800 +# define SLANG_VC 12 +# elif _MSC_VER >= 1700 +# define SLANG_VC 11 +# elif _MSC_VER >= 1600 +# define SLANG_VC 10 +# elif _MSC_VER >= 1500 +# define SLANG_VC 9 +# else +# error "unknown version of Visual C++ compiler" +# endif +# elif defined(__clang__) +# define SLANG_CLANG 1 +# elif defined(__SNC__) +# define SLANG_SNC 1 +# elif defined(__ghs__) +# define SLANG_GHS 1 +# elif defined(__GNUC__) /* note: __clang__, __SNC__, or __ghs__ imply __GNUC__ */ +# define SLANG_GCC 1 +# else +# error "unknown compiler" +# endif /* Any compilers not detected by the above logic are now now explicitly zeroed out. */ -# ifndef SLANG_VC -# define SLANG_VC 0 -# endif -# ifndef SLANG_CLANG -# define SLANG_CLANG 0 -# endif -# ifndef SLANG_SNC -# define SLANG_SNC 0 -# endif -# ifndef SLANG_GHS -# define SLANG_GHS 0 -# endif -# ifndef SLANG_GCC -# define SLANG_GCC 0 -# endif +# ifndef SLANG_VC +# define SLANG_VC 0 +# endif +# ifndef SLANG_CLANG +# define SLANG_CLANG 0 +# endif +# ifndef SLANG_SNC +# define SLANG_SNC 0 +# endif +# ifndef SLANG_GHS +# define SLANG_GHS 0 +# endif +# ifndef SLANG_GCC +# define SLANG_GCC 0 +# endif #endif /* SLANG_COMPILER */ /* @@ -78,81 +78,81 @@ used later in the file. Most applications should not need to touch this section. */ #ifndef SLANG_PLATFORM -# define SLANG_PLATFORM +# define SLANG_PLATFORM /** Operating system defines, see http://sourceforge.net/p/predef/wiki/OperatingSystems/ */ -# if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_PARTITION_APP -# define SLANG_WINRT 1 /* Windows Runtime, either on Windows RT or Windows 8 */ -# elif defined(XBOXONE) -# define SLANG_XBOXONE 1 -# elif defined(_WIN64) /* note: XBOXONE implies _WIN64 */ -# define SLANG_WIN64 1 -# elif defined(_M_PPC) -# define SLANG_X360 1 -# elif defined(_WIN32) /* note: _M_PPC implies _WIN32 */ -# define SLANG_WIN32 1 -# elif defined(__ANDROID__) -# define SLANG_ANDROID 1 -# elif defined(__linux__) || defined(__CYGWIN__) /* note: __ANDROID__ implies __linux__ */ -# define SLANG_LINUX 1 -# elif defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) -# define SLANG_IOS 1 -# elif defined(__APPLE__) -# define SLANG_OSX 1 -# elif defined(__CELLOS_LV2__) -# define SLANG_PS3 1 -# elif defined(__ORBIS__) -# define SLANG_PS4 1 -# elif defined(__SNC__) && defined(__arm__) -# define SLANG_PSP2 1 -# elif defined(__ghs__) -# define SLANG_WIIU 1 -# else -# error "unknown target platform" -# endif +# if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_PARTITION_APP +# define SLANG_WINRT 1 /* Windows Runtime, either on Windows RT or Windows 8 */ +# elif defined(XBOXONE) +# define SLANG_XBOXONE 1 +# elif defined(_WIN64) /* note: XBOXONE implies _WIN64 */ +# define SLANG_WIN64 1 +# elif defined(_M_PPC) +# define SLANG_X360 1 +# elif defined(_WIN32) /* note: _M_PPC implies _WIN32 */ +# define SLANG_WIN32 1 +# elif defined(__ANDROID__) +# define SLANG_ANDROID 1 +# elif defined(__linux__) || defined(__CYGWIN__) /* note: __ANDROID__ implies __linux__ */ +# define SLANG_LINUX 1 +# elif defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) +# define SLANG_IOS 1 +# elif defined(__APPLE__) +# define SLANG_OSX 1 +# elif defined(__CELLOS_LV2__) +# define SLANG_PS3 1 +# elif defined(__ORBIS__) +# define SLANG_PS4 1 +# elif defined(__SNC__) && defined(__arm__) +# define SLANG_PSP2 1 +# elif defined(__ghs__) +# define SLANG_WIIU 1 +# else +# error "unknown target platform" +# endif /* Any platforms not detected by the above logic are now now explicitly zeroed out. */ -# ifndef SLANG_WINRT -# define SLANG_WINRT 0 -# endif -# ifndef SLANG_XBOXONE -# define SLANG_XBOXONE 0 -# endif -# ifndef SLANG_WIN64 -# define SLANG_WIN64 0 -# endif -# ifndef SLANG_X360 -# define SLANG_X360 0 -# endif -# ifndef SLANG_WIN32 -# define SLANG_WIN32 0 -# endif -# ifndef SLANG_ANDROID -# define SLANG_ANDROID 0 -# endif -# ifndef SLANG_LINUX -# define SLANG_LINUX 0 -# endif -# ifndef SLANG_IOS -# define SLANG_IOS 0 -# endif -# ifndef SLANG_OSX -# define SLANG_OSX 0 -# endif -# ifndef SLANG_PS3 -# define SLANG_PS3 0 -# endif -# ifndef SLANG_PS4 -# define SLANG_PS4 0 -# endif -# ifndef SLANG_PSP2 -# define SLANG_PSP2 0 -# endif -# ifndef SLANG_WIIU -# define SLANG_WIIU 0 -# endif +# ifndef SLANG_WINRT +# define SLANG_WINRT 0 +# endif +# ifndef SLANG_XBOXONE +# define SLANG_XBOXONE 0 +# endif +# ifndef SLANG_WIN64 +# define SLANG_WIN64 0 +# endif +# ifndef SLANG_X360 +# define SLANG_X360 0 +# endif +# ifndef SLANG_WIN32 +# define SLANG_WIN32 0 +# endif +# ifndef SLANG_ANDROID +# define SLANG_ANDROID 0 +# endif +# ifndef SLANG_LINUX +# define SLANG_LINUX 0 +# endif +# ifndef SLANG_IOS +# define SLANG_IOS 0 +# endif +# ifndef SLANG_OSX +# define SLANG_OSX 0 +# endif +# ifndef SLANG_PS3 +# define SLANG_PS3 0 +# endif +# ifndef SLANG_PS4 +# define SLANG_PS4 0 +# endif +# ifndef SLANG_PSP2 +# define SLANG_PSP2 0 +# endif +# ifndef SLANG_WIIU +# define SLANG_WIIU 0 +# endif #endif /* SLANG_PLATFORM */ /* Shorthands for "families" of compilers/platforms */ @@ -208,9 +208,113 @@ convention for interface methods. #endif #ifndef SLANG_API -#define SLANG_API +# define SLANG_API +#endif + +// GCC Specific +#if SLANG_GCC_FAMILY +// This doesn't work on clang - because the typedef is seen as multiply defined, use the line numbered version defined later +# if !defined(__clang__) && (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__ORBIS__)) +# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SlangCompileTimeAssert_Dummy[(exp) ? 1 : -1] __attribute__((unused)) +# endif + +# define SLANG_NO_INLINE __attribute__((noinline)) +# define SLANG_FORCE_INLINE inline __attribute__((always_inline)) +# define SLANG_BREAKPOINT(id) __builtin_trap(); +# define SLANG_ALIGN_OF(T) __alignof__(T) +#endif // SLANG_GCC_FAMILY + +// Microsoft VC specific +#if SLANG_MICROSOFT_FAMILY +# define SLANG_NO_INLINE __declspec(noinline) +# define SLANG_FORCE_INLINE __forceinline +# define SLANG_BREAKPOINT(id) __debugbreak(); +# define SLANG_ALIGN_OF(T) __alignof(T) +#endif // SLANG_MICROSOFT_FAMILY + +#ifndef SLANG_FORCE_INLINE +# define SLANG_FORCE_INLINE inline +#endif +#ifndef SLANG_NO_INLINE +# define SLANG_NO_INLINE +#endif + +#ifndef SLANG_COMPILE_TIME_ASSERT +# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SLANG_CONCAT(SlangCompileTimeAssert,__LINE__)[(exp) ? 1 : -1] +#endif + +#ifndef SLANG_OFFSET_OF +# define SLANG_OFFSET_OF(X, Y) offsetof(X, Y) +#endif + +#ifndef SLANG_BREAKPOINT +// Make it crash with a write to 0! +# define SLANG_BREAKPOINT(id) (*((int*)0) = int(id)); +#endif + +// Use for getting the amount of members of a standard C array. +#define SLANG_COUNT_OF(x) (sizeof(x)/sizeof(x[0])) +/// SLANG_INLINE exists to have a way to inline consistent with SLANG_ALWAYS_INLINE +#define SLANG_INLINE inline + +// Other defines +#define SLANG_STRINGIZE_HELPER(X) #X +#define SLANG_STRINGIZE(X) SLANG_STRINGIZE_HELPER(X) + +#define SLANG_CONCAT_HELPER(X, Y) X##Y +#define SLANG_CONCAT(X, Y) SLANG_CONCAT_HELPER(X, Y) + +#ifndef SLANG_UNUSED +# define SLANG_UNUSED(v) (void)v; #endif +#ifdef __cplusplus +// C++ specific macros +// Gcc +# if SLANG_GCC_FAMILY +// Check for C++11 +# if (__cplusplus >= 201103L) +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 +# define SLANG_HAS_MOVE_SEMANTICS 1 +# endif +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 +# define SLANG_HAS_ENUM_CLASS 1 +# endif +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 407 +# define SLANG_OVERRIDE override +# endif +# endif +# endif // SLANG_GCC_FAMILY + +// Visual Studio + +# if SLANG_VC +// C4481: nonstandard extension used: override specifier 'override' +# if _MSC_VER < 1700 +# pragma warning(disable : 4481) +# endif +# define SLANG_OVERRIDE override +# if _MSC_VER >= 1600 +# define SLANG_HAS_MOVE_SEMANTICS 1 +# endif +# if _MSC_VER >= 1700 +# define SLANG_HAS_ENUM_CLASS 1 +# endif +# endif // SLANG_VC + +// Set non set +# ifndef SLANG_OVERRIDE +# define SLANG_OVERRIDE +# endif +# ifndef SLANG_HAS_ENUM_CLASS +# define SLANG_HAS_ENUM_CLASS 0 +# endif +# ifndef SLANG_HAS_MOVE_SEMANTICS +# define SLANG_HAS_MOVE_SEMANTICS 0 +# endif + +#endif // __cplusplus + #ifndef SLANG_NO_INTTYPES #include <inttypes.h> #endif // ! SLANG_NO_INTTYPES @@ -367,16 +471,115 @@ extern "C" /** A result code for a Slang API operation. - This type is generally compatible with the Windows API - `HRESULT` type. In particular, negative values indicate - failure results, while zero or positive results indicate - success. + This type is generally compatible with the Windows API `HRESULT` type. In particular, negative values indicate + failure results, while zero or positive results indicate success. + + In general, Slang APIs always return a zero result on success, unless documented otherwise. Strictly speaking + a negative value indicates an error, a positive (or 0) value indicates success. This can be tested for with the macros + SLANG_SUCCEEDED(x) or SLANG_FAILED(x). + + It can represent if the call was successful or not. It can also specify in an extensible manner what facility + produced the result (as the integral 'facility') as well as what caused it (as an integral 'code'). + Under the covers SlangResult is represented as a int32_t. + + SlangResult is designed to be compatible with COM HRESULT. + + It's layout in bits is as follows - In general, Slang APIs always return a zero result on - success, unless documented otherwise. + Severity | Facility | Code + ---------|----------|----- + 31 | 30-16 | 15-0 + + Severity - 1 fail, 0 is success - as SlangResult is signed 32 bits, means negative number indicates failure. + Facility is where the error originated from. Code is the code specific to the facility. + + Result codes have the following styles, + 1) SLANG_name + 2) SLANG_s_f_name + 3) SLANG_s_name + + where s is S for success, E for error + f is the short version of the facility name + + Style 1 is reserved for SLANG_OK and SLANG_FAIL as they are so commonly used. + + It is acceptable to expand 'f' to a longer name to differentiate a name or drop if unique without it. + ie for a facility 'DRIVER' it might make sense to have an error of the form SLANG_E_DRIVER_OUT_OF_MEMORY */ + typedef int32_t SlangResult; + //! Use to test if a result was failure. Never use result != SLANG_OK to test for failure, as there may be successful codes != SLANG_OK. +#define SLANG_FAILED(status) ((status) < 0) + //! Use to test if a result succeeded. Never use result == SLANG_OK to test for success, as will detect other successful codes as a failure. +#define SLANG_SUCCEEDED(status) ((status) >= 0) + + //! Get the facility the result is associated with +#define SLANG_GET_RESULT_FACILITY(r) ((int32_t)(((r) >> 16) & 0x7fff)) + //! Get the result code for the facility +#define SLANG_GET_RESULT_CODE(r) ((int32_t)((r) & 0xffff)) + +#define SLANG_MAKE_ERROR(fac, code) ((((int32_t)(fac)) << 16) | ((int32_t)(code)) | 0x80000000) +#define SLANG_MAKE_SUCCESS(fac, code) ((((int32_t)(fac)) << 16) | ((int32_t)(code))) + + /*************************** Facilities ************************************/ + + //! Facilities compatible with windows COM - only use if known code is compatible +#define SLANG_FACILITY_WIN_GENERAL 0 +#define SLANG_FACILITY_WIN_INTERFACE 4 +#define SLANG_FACILITY_WIN_API 7 + + //! Base facility -> so as to not clash with HRESULT values (values in 0x200 range do not appear used) +#define SLANG_FACILITY_BASE 0x200 + + /*! Facilities numbers must be unique across a project to make the resulting result a unique number. + It can be useful to have a consistent short name for a facility, as used in the name prefix */ +#define SLANG_FACILITY_CORE SLANG_FACILITY_BASE + + /// Base for external facilities. Facilities should be unique across modules. +#define SLANG_FACILITY_EXTERNAL_BASE 0x210 + + /* ************************ Win COM compatible Results ******************************/ + // https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx + + //! SLANG_OK indicates success, and is equivalent to SLANG_MAKE_SUCCESS(SLANG_FACILITY_WIN_GENERAL, 0) +#define SLANG_OK 0 + //! SLANG_FAIL is the generic failure code - meaning a serious error occurred and the call couldn't complete +#define SLANG_FAIL SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_INTERFACE, 5) + +#define SLANG_MAKE_WIN_INTERFACE_ERROR(code) SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_INTERFACE, code) +#define SLANG_MAKE_WIN_API_ERROR(code) SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_API, code) + + //! Functionality is not implemented +#define SLANG_E_NOT_IMPLEMENTED SLANG_MAKE_WIN_INTERFACE_ERROR(1) + //! Interface not be found +#define SLANG_E_NO_INTERFACE SLANG_MAKE_WIN_INTERFACE_ERROR(2) + //! Operation was aborted (did not correctly complete) +#define SLANG_E_ABORT SLANG_MAKE_WIN_INTERFACE_ERROR(4) + + //! Indicates that a handle passed in as parameter to a method is invalid. +#define SLANG_E_INVALID_HANDLE SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_API, 6) + //! Indicates that an argument passed in as parameter to a method is invalid. +#define SLANG_E_INVALID_ARG SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_API, 0x57) + //! Operation could not complete - ran out of memory +#define SLANG_E_OUT_OF_MEMORY SLANG_MAKE_ERROR(SLANG_FACILITY_WIN_API, 0xe) + + /* *************************** other Results **************************************/ + +#define SLANG_MAKE_CORE_ERROR(code) SLANG_MAKE_ERROR(SLANG_FACILITY_CORE, code) + + // Supplied buffer is too small to be able to complete +#define SLANG_E_BUFFER_TOO_SMALL SLANG_MAKE_CORE_ERROR(1) + //! Used to identify a Result that has yet to be initialized. + //! It defaults to failure such that if used incorrectly will fail, as similar in concept to using an uninitialized variable. +#define SLANG_E_UNINITIALIZED SLANG_MAKE_CORE_ERROR(2) + //! Returned from an async method meaning the output is invalid (thus an error), but a result for the request is pending, and will be returned on a subsequent call with the async handle. +#define SLANG_E_PENDING SLANG_MAKE_CORE_ERROR(3) + //! Indicates a file/resource could not be opened +#define SLANG_E_CANNOT_OPEN SLANG_MAKE_CORE_ERROR(4) + //! Indicates a file/resource could not be found +#define SLANG_E_NOT_FOUND SLANG_MAKE_CORE_ERROR(5) + /** A "Universally Unique Identifier" (UUID) The Slang API uses UUIDs to identify interfaces when diff --git a/source/core/list.h b/source/core/list.h index 3a325955a..6df60b74b 100644 --- a/source/core/list.h +++ b/source/core/list.h @@ -1,10 +1,11 @@ -#ifndef FUNDAMENTAL_LIB_LIST_H +#ifndef FUNDAMENTAL_LIB_LIST_H #define FUNDAMENTAL_LIB_LIST_H +#include "../../slang.h" + #include "allocator.h" #include "slang-math.h" #include "array-view.h" -#include "slang-defines.h" #include <algorithm> #include <new> diff --git a/source/core/slang-defines.h b/source/core/slang-defines.h deleted file mode 100644 index 580dd801c..000000000 --- a/source/core/slang-defines.h +++ /dev/null @@ -1,314 +0,0 @@ -#ifndef SLANG_DEFINES_H -#define SLANG_DEFINES_H - -/* -Some of our `#define`s are needed in the public API header as well, so -we will go ahead and include that here so that we can share the definitions. -*/ -#include "../../slang.h" - -/* -The following preprocessor identifiers specify compiler, OS, and architecture. -All definitions have a value of 1 or 0, use '#if' instead of '#ifdef'. -*/ - -#ifndef SLANG_PROCESSOR -# define SLANG_PROCESSOR - -/* Architecture defines, see http://sourceforge.net/p/predef/wiki/Architectures/ */ -# if defined(__x86_64__) || defined(_M_X64) // ps4 compiler defines _M_X64 without value -# define SLANG_X64 1 -# elif defined(__i386__) || defined(_M_IX86) -# define SLANG_X86 1 -# elif defined(__arm64__) || defined(__aarch64__) -# define SLANG_A64 1 -# elif defined(__arm__) || defined(_M_ARM) -# define SLANG_ARM 1 -# elif defined(__SPU__) -# define SLANG_SPU 1 -# elif defined(__ppc__) || defined(_M_PPC) || defined(__CELLOS_LV2__) -# define SLANG_PPC 1 -# else -# error "Unknown architecture" -# endif - -/** -SIMD defines -*/ -# if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) -# define SLANG_SSE2 1 -# endif -# if defined(_M_ARM) || defined(__ARM_NEON__) -# define SLANG_NEON 1 -# endif -# if defined(_M_PPC) || defined(__CELLOS_LV2__) -# define SLANG_VMX 1 -# endif - -// Zero unset -# ifndef SLANG_X64 -# define SLANG_X64 0 -# endif -# ifndef SLANG_X86 -# define SLANG_X86 0 -# endif -# ifndef SLANG_A64 -# define SLANG_A64 0 -# endif -# ifndef SLANG_ARM -# define SLANG_ARM 0 -# endif -# ifndef SLANG_SPU -# define SLANG_SPU 0 -# endif -# ifndef SLANG_PPC -# define SLANG_PPC 0 -# endif -# ifndef SLANG_SSE2 -# define SLANG_SSE2 0 -# endif -# ifndef SLANG_NEON -# define SLANG_NEON 0 -# endif -# ifndef SLANG_VMX -# define SLANG_VMX 0 -# endif - -#endif // SLANG_PROCESSOR - -/* -define anything not defined through the command line to 0 -*/ -#ifndef SLANG_DEBUG -# define SLANG_DEBUG 0 -#endif -#ifndef SLANG_CHECKED -# define SLANG_CHECKED 0 -#endif -#ifndef SLANG_PROFILE -# define SLANG_PROFILE 0 -#endif - -/** -family shortcuts -*/ -// architecture -#define SLANG_INTEL_FAMILY (SLANG_X64 || SLANG_X86) // Intel x86 family -#define SLANG_ARM_FAMILY (SLANG_ARM || SLANG_A64) -#define SLANG_PPC_FAMILY (SLANG_PPC) - -#define SLANG_P64_FAMILY (SLANG_X64 || SLANG_A64) // shortcut for 64-bit architectures - -// Use for getting the amount of members of a standard C array. -#define SLANG_COUNT_OF(x) (sizeof(x)/sizeof(x[0])) -/// SLANG_INLINE exists to have a way to inline consistent with SLANG_ALWAYS_INLINE -#define SLANG_INLINE inline - -// Other defines -#define SLANG_STRINGIZE_HELPER(X) #X -#define SLANG_STRINGIZE(X) SLANG_STRINGIZE_HELPER(X) - -#define SLANG_CONCAT_HELPER(X, Y) X##Y -#define SLANG_CONCAT(X, Y) SLANG_CONCAT_HELPER(X, Y) - -#ifndef SLANG_UNUSED -# define SLANG_UNUSED(v) (void)v; -#endif - -/** -General defines -*/ - -// GCC Specific -#if SLANG_GCC_FAMILY -# define SLANG_NO_INLINE __attribute__((noinline)) - -// This doesn't work on clang - because the typedef is seen as multiply defined, use the line numbered version defined later -# if !defined(__clang__) && (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__ORBIS__)) -# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SlangCompileTimeAssert_Dummy[(exp) ? 1 : -1] __attribute__((unused)) -# endif - -# if !SLANG_SNC && !SLANG_GHS -# define SLANG_OFFSET_OF(X, Y) __builtin_offsetof(X, Y) -# endif - -//# if !SLANG_LINUX // Workaround; Fedora Core 3 do not agree with force inline -# define SLANG_FORCE_INLINE inline __attribute__((always_inline)) -//# endif - -# define SLANG_BREAKPOINT(id) __builtin_trap(); - -# if (__cplusplus >= 201103L) && ((__GNUC__ > 4) || (__GNUC__ ==4 && __GNUC_MINOR__ >= 6)) -# define SLANG_NULL nullptr -# else -# define SLANG_NULL __null -# endif - -# define SLANG_ALIGN_OF(T) __alignof__(T) - -# define SLANG_FUNCTION_SIG __PRETTY_FUNCTION__ - -#endif // SLANG_GCC_FAMILY - -// Microsoft VC specific -#if SLANG_MICROSOFT_FAMILY - -# pragma inline_depth(255) - -# pragma warning(disable : 4324 ) // C4324: structure was padded due to alignment specifier -# pragma warning(disable : 4514 ) // 'function' : unreferenced inline function has been removed -# pragma warning(disable : 4710 ) // 'function' : function not inlined -# pragma warning(disable : 4711 ) // function 'function' selected for inline expansion -# pragma warning(disable : 4127 ) // C4127: conditional expression is constant - -# define SLANG_NO_ALIAS __declspec(noalias) -# define SLANG_NO_INLINE __declspec(noinline) -# define SLANG_FORCE_INLINE __forceinline -# define SLANG_PUSH_PACK_DEFAULT __pragma(pack(push, 8)) -# define SLANG_POP_PACK __pragma(pack(pop)) - -# define SLANG_BREAKPOINT(id) __debugbreak(); - -# ifdef __cplusplus -# define SLANG_NULL nullptr -# endif - -# define SLANG_ALIGN_OF(T) __alignof(T) - -# define SLANG_FUNCTION_SIG __FUNCSIG__ - -# define SLANG_INT64(x) (x##i64) -# define SLANG_UINT64(x) (x##ui64) - -# define SLANG_CALL_CONV __cdecl - -#endif // SLANG_MICROSOFT_FAMILY - -// Set defaults if not set - -// Used for doing constant literals -#ifndef SLANG_INT64 -# define SLANG_INT64(x) (x##ll) -#endif -#ifndef SLANG_UINT64 -# define SLANG_UINT64(x) (x##ull) -#endif - -#ifndef SLANG_NULL -# if defined(NULL) -# define SLANG_NULL NULL -# else -# define SLANG_NULL 0 -# endif -#endif - -#ifndef SLANG_FORCE_INLINE -# define SLANG_FORCE_INLINE inline -#endif -#ifndef SLANG_NO_INLINE -# define SLANG_NO_INLINE -#endif -#ifndef SLANG_NO_ALIAS -# define SLANG_NO_ALIAS -#endif -#ifndef SLANG_COMPILE_TIME_ASSERT -# define SLANG_COMPILE_TIME_ASSERT(exp) typedef char SLANG_CONCAT(SlangCompileTimeAssert,__LINE__)[(exp) ? 1 : -1] -#endif -#ifndef SLANG_OFFSET_OF -# define SLANG_OFFSET_OF(X, Y) offsetof(X, Y) -#endif -#ifndef SLANG_BREAKPOINT -// Make it crash with a write to 0! -# define SLANG_BREAKPOINT(id) (*((int*)0) = int(id)); -#endif - -#ifndef SLANG_FUNCTION_NAME -# define SLANG_FUNCTION_NAME __FUNCTION__ -#endif -#ifndef SLANG_FUNCTION_SIG -# define SLANG_FUNCTION_SIG SLANG_FUNCTION_NAME -#endif - -#ifndef SLANG_CALL_CONV -# define SLANG_CALL_CONV -#endif - -//! casting the null ptr takes a special-case code path, which we don't want -#define SLANG_OFFSETOF_BASE 0x100 -#define SLANG_OFFSET_OF_RT(Class, Member) \ - (reinterpret_cast<size_t>(&reinterpret_cast<Class*>(SLANG_OFFSETOF_BASE)->Member) - size_t(SLANG_OFFSETOF_BASE)) - -#ifdef __CUDACC__ -# define SLANG_CUDA_CALLABLE __host__ __device__ -#else -# define SLANG_CUDA_CALLABLE -#endif - -#if SLANG_GCC_FAMILY && !SLANG_GHS -# define SLANG_WEAK_SYMBOL __attribute__((weak)) // this is to support SIMD constant merging in template specialization -#else -# define SLANG_WEAK_SYMBOL -#endif - -// C++ Symbols -#ifdef __cplusplus - -// C++ specific macros - -// Gcc -# if SLANG_GCC_FAMILY -// Check for C++11 -# if (__cplusplus >= 201103L) -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 -# define SLANG_HAS_MOVE_SEMANTICS 1 -# endif -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 -# define SLANG_HAS_ENUM_CLASS 1 -# endif -# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 407 -# define SLANG_OVERRIDE override -# endif -# endif -# endif // SLANG_GCC_FAMILY - -// Visual Studio - -# if SLANG_VC -// C4481: nonstandard extension used: override specifier 'override' -# if _MSC_VER < 1700 -# pragma warning(disable : 4481) -# endif -# define SLANG_OVERRIDE override -# if _MSC_VER >= 1600 -# define SLANG_HAS_MOVE_SEMANTICS 1 -# endif -# if _MSC_VER >= 1700 -# define SLANG_HAS_ENUM_CLASS 1 -# endif -# endif // SLANG_VC - -// Clang specific -# if SLANG_CLANG -# endif // SLANG_CLANG - -// Set non set - -#ifndef SLANG_NO_THROW -# define SLANG_NO_THROW -#endif -#ifndef SLANG_OVERRIDE -# define SLANG_OVERRIDE -#endif -#ifndef SLANG_HAS_ENUM_CLASS -# define SLANG_HAS_ENUM_CLASS 0 -#endif -#ifndef SLANG_HAS_MOVE_SEMANTICS -# define SLANG_HAS_MOVE_SEMANTICS 0 -#endif - -#include <new> // For placement new - -#endif // __cplusplus - - -#endif // SLANG_DEFINES_H diff --git a/source/core/slang-free-list.h b/source/core/slang-free-list.h index 10f0a39ba..dfa924797 100644 --- a/source/core/slang-free-list.h +++ b/source/core/slang-free-list.h @@ -1,8 +1,10 @@ -#ifndef SLANG_FREE_LIST_H +#ifndef SLANG_FREE_LIST_H #define SLANG_FREE_LIST_H -#include "slang-defines.h" -#include "slang-result.h" +#include "../../slang.h" + +#include <assert.h> + #include <stdlib.h> #include <string.h> diff --git a/source/core/slang-result.h b/source/core/slang-result.h deleted file mode 100644 index 348029af4..000000000 --- a/source/core/slang-result.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef SLANG_RESULT_H -#define SLANG_RESULT_H - -#include <cstdint> -#include <assert.h> - -/*! SlangResult is a type that represents the status result after a call to a function/method. It can represent if the call was successful or -not. It can also specify in an extensible manner what facility produced the result (as the integral 'facility') as well as what caused it (as an integral 'code'). -Under the covers SlangResult is represented as a int32_t. A negative value indicates an error, a positive (or 0) value indicates success. - -SlangResult is designed to be compatible with COM HRESULT. - -It's layout in bits is as follows - -Severity | Facility | Code ----------|----------|----- -31 | 30-16 | 15-0 - -Severity - 1 fail, 0 is success. -Facility is where the error originated from -Code is the code specific to the facility. - -The layout is designed such that failure is a negative number, and success is positive due to Result -being represented by an Int32. - -Result codes have the following style - -1. SLANG_name -2. SLANG_s_f_name -3. SLANG_s_name - -where s is severity as a single letter S - success, and E for error -Style 1 is reserved for SLANG_OK and SLANG_FAIL as they are so common and not tied to a facility - -s is S for success, E for error -f is the short version of the facility name - -For the common used SLANG_OK and SLANG_FAIL, the name prefix is dropped. -It is acceptable to expand 'f' to a longer name to differentiate a name -ie for a facility 'DRIVER' it might make sense to have an error of the form SLANG_E_DRIVER_OUT_OF_MEMORY -*/ - -typedef int32_t SlangResult; - -// Make just the identifier for the code -#define SLANG_MAKE_RESULT_ID(fac, code) ((((int32_t)(fac))<<16) | ((int32_t)(code))) - -//! Make a result -#define SLANG_MAKE_RESULT(sev, fac, code) ((((int32_t)(sev))<<31) | SLANG_MAKE_RESULT_ID(fac, code)) - -//! Will be 0 - for ok, 1 for failure -#define SLANG_GET_RESULT_SEVERITY(r) ((int32_t)(((uint32_t(r)) >> 31)) -//! Get the facility the result is associated with -#define SLANG_GET_RESULT_FACILITY(r) ((int32_t)(((r) >> 16) & 0x7fff)) -//! Get the result code for the facility -#define SLANG_GET_RESULT_CODE(r) ((int32_t)((r) & 0xffff)) - -#define SLANG_MAKE_ERROR(fac, code) (SLANG_MAKE_RESULT_ID(fac, code) | 0x80000000) -#define SLANG_MAKE_SUCCESS(fac, code) SLANG_MAKE_RESULT_ID(fac, code) - -/*************************** Facilities ************************************/ - -//! General - careful to make compatible with HRESULT -#define SLANG_FACILITY_GENERAL 0 - -//! Base facility -> so as to not clash with HRESULT values -#define SLANG_FACILITY_BASE 0x100 - -/*! Facilities numbers must be unique across a project to make the resulting result a unique number! -It can be useful to have a consistent short name for a facility, as used in the name prefix */ -#define SLANG_FACILITY_DISK (SLANG_FACILITY_BASE + 1) -#define SLANG_FACILITY_INTERFACE (SLANG_FACILITY_BASE + 2) -#define SLANG_FACILITY_UNKNOWN (SLANG_FACILITY_BASE + 3) -#define SLANG_FACILITY_MEMORY (SLANG_FACILITY_BASE + 4) -#define SLANG_FACILITY_MISC (SLANG_FACILITY_BASE + 5) - -/// Base for external facilities. Facilities should be unique across modules. -#define SLANG_FACILITY_EXTERNAL_BASE 0x210 -#define SLANG_FACILITY_CORE (SLANG_FACILITY_EXTERNAL_BASE + 1) - -/* *************************** Codes **************************************/ - -// Memory -#define SLANG_E_MEM_OUT_OF_MEMORY SLANG_MAKE_ERROR(SLANG_FACILITY_MEMORY, 1) -#define SLANG_E_MEM_BUFFER_TOO_SMALL SLANG_MAKE_ERROR(SLANG_FACILITY_MEMORY, 2) - -//! SLANG_OK indicates success, and is equivalent to SLANG_MAKE_RESULT(0, GENERAL, 0) -#define SLANG_OK 0 -//! SLANG_FAIL is the generic failure code - meaning a serious error occurred and the call couldn't complete -#define SLANG_FAIL SLANG_MAKE_ERROR(SLANG_FACILITY_GENERAL, 1) - -//! Used to identify a Result that has yet to be initialized. -//! It defaults to failure such that if used incorrectly will fail, as similar in concept to using an uninitialized variable. -#define SLANG_E_MISC_UNINITIALIZED SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 2) -//! Returned from an async method meaning the output is invalid (thus an error), but a result for the request is pending, and will be returned on a subsequent call with the async handle. -#define SLANG_E_MISC_PENDING SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 3) -//! Indicates that a handle passed in as parameter to a method is invalid. -#define SLANG_E_MISC_INVALID_HANDLE SLANG_MAKE_ERROR(SLANG_FACILITY_MISC, 4) - -//! Indicates that tn interface could not be found -#define SLANG_E_INTF_NO_INTERFACE SLANG_MAKE_ERROR(SLANG_FACILITY_INTERFACE, 2) - -/*! Set SLANG_HANDLE_RESULT_FAIL(x) to code to be executed whenever an error occurs, and is detected by one of the macros */ -#ifndef SLANG_HANDLE_RESULT_FAIL -# define SLANG_HANDLE_RESULT_FAIL(x) -#endif - -/* !!!!!!!!!!!!!!!!!!!!!!!!! Checking codes !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ - -//! Use to test if a result was failure. Never use result != SLANG_OK to test for failure, as there may be successful codes != SLANG_OK. -#define SLANG_FAILED(status) ((status) < 0) -//! Use to test if a result succeeded. Never use result == SLANG_OK to test for success, as will detect other successful codes as a failure. -#define SLANG_SUCCEEDED(status) ((status) >= 0) - -//! Helper macro, that makes it easy to add result checking to calls in functions/methods that themselves return Result. -#define SLANG_RETURN_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return _res; } } -//! Helper macro that can be used to test the return value from a call, and will return in a void method/function -#define SLANG_RETURN_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return; } } -//! Helper macro that will return false on failure. -#define SLANG_RETURN_FALSE_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return false; } } -//! Helper macro that will return nullptr on failure. -#define SLANG_RETURN_NULL_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { SLANG_HANDLE_RESULT_FAIL(_res); return nullptr; } } - -//! Helper macro that will assert if the return code from a call is failure, also returns the failure. -#define SLANG_ASSERT_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return _res; } } -//! Helper macro that will assert if the result from a call is a failure, also returns. -#define SLANG_ASSERT_VOID_ON_FAIL(x) { SlangResult _res = (x); if (SLANG_FAILED(_res)) { assert(false); return; } } - -#if defined(__cplusplus) -namespace Slang { -typedef SlangResult Result; -} // namespace Slang -#endif // defined(__cplusplus) - -#endif // SLANG_RESULT_H
\ No newline at end of file diff --git a/source/slang/compiler.h b/source/slang/compiler.h index acbad9a7e..398c7ac62 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -1,8 +1,8 @@ -#ifndef RASTER_SHADER_COMPILER_H +#ifndef RASTER_SHADER_COMPILER_H #define RASTER_SHADER_COMPILER_H #include "../core/basic.h" -#include "../core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" #include "diagnostics.h" #include "name.h" diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 24dff88a8..ced2beb14 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1,4 +1,4 @@ -#include "../../slang.h" +#include "../../slang.h" #include "../core/slang-io.h" #include "parameter-binding.h" @@ -211,46 +211,20 @@ static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob; class BlobBase : public ISlangBlob { public: - BlobBase() {} - virtual ~BlobBase() {} - - uint32_t referenceCount = 0; + uint32_t m_refCount = 0; // ISlangUnknown + SLANG_IUNKNOWN_ALL - SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE - { - if(uuid == IID_IComUnknown) - { - *(ISlangUnknown**)outObject = this; - addRef(); - return SLANG_OK; - } - else if(uuid == IID_ISlangBlob) - { - *(ISlangBlob**)outObject = this; - addRef(); - return SLANG_OK; - } - - return SLANG_FAIL; - } - - SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE - { - referenceCount++; - return 0; - } + /// Need virtual dtor, because BlobBase is derived from and release impl used is the one in the base class (that doesn't know the derived type) + /// Alternatively could be implemented by always using SLANG_IUNKNOWN_RELEASE in derived types - this would make derived types slightly smaller/faster + virtual ~BlobBase() {} - SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE +protected: + SLANG_FORCE_INLINE ISlangUnknown* getInterface(const Guid& guid) { - if(--referenceCount == 0) - { - delete this; - } - return 0; + return (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) ? static_cast<ISlangBlob*>(this) : nullptr; } - }; /** A blob that uses a `String` for its storage. @@ -258,23 +232,15 @@ public: class StringBlob : public BlobBase { public: - String string; - + // ISlangBlob + SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_string.Buffer(); } + SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_string.Length(); } + explicit StringBlob(String const& string) - : string(string) + : m_string(string) {} - - // ISlangBlob - - SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE - { - return string.Buffer(); - } - - SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE - { - return string.Length(); - } +protected: + String m_string; }; ComPtr<ISlangBlob> createStringBlob(String const& string) @@ -287,41 +253,32 @@ ComPtr<ISlangBlob> createStringBlob(String const& string) class RawBlob : public BlobBase { public: - void* data; - size_t size; - - ~RawBlob() - { - free(data); - } - // ISlangBlob + SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; } + SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_size; } - SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE + // Ctor + RawBlob(const void* data, size_t size): + m_size(size) { - return data; + m_data = malloc(size); + memcpy(m_data, data, size); } - - SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE + ~RawBlob() { - return size; + free(m_data); } -}; +protected: + void* m_data; + size_t m_size; +}; ComPtr<ISlangBlob> createRawBlob(void const* inData, size_t size) { - void* dataCopy = malloc(size); - memcpy(dataCopy, inData, size); - - RawBlob* rawBlob = new RawBlob(); - rawBlob->data = dataCopy; - rawBlob->size = size; - - return ComPtr<ISlangBlob>(rawBlob); + return ComPtr<ISlangBlob>(new RawBlob(inData, size)); } - SlangResult CompileRequest::loadFile(String const& path, ISlangBlob** outBlob) { // If there is a used-defined filesystem, then use that to load files. diff --git a/source/slang/source-loc.h b/source/slang/source-loc.h index 88ec6b62a..c4a2301a0 100644 --- a/source/slang/source-loc.h +++ b/source/slang/source-loc.h @@ -1,9 +1,9 @@ -// source-loc.h +// source-loc.h #ifndef SLANG_SOURCE_LOC_H_INCLUDED #define SLANG_SOURCE_LOC_H_INCLUDED #include "../core/basic.h" -#include "../core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" #include "../../slang.h" diff --git a/tools/render-test/circular-resource-heap-d3d12.h b/tools/render-test/circular-resource-heap-d3d12.h index 6f251c476..baa9a9b0c 100644 --- a/tools/render-test/circular-resource-heap-d3d12.h +++ b/tools/render-test/circular-resource-heap-d3d12.h @@ -1,6 +1,6 @@ -#pragma once +#pragma once -#include "../../source/core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" #include "../../source/core/list.h" #include "../../source/core/slang-free-list.h" diff --git a/tools/render-test/d3d-util.h b/tools/render-test/d3d-util.h index 51eefd151..b7a268fd8 100644 --- a/tools/render-test/d3d-util.h +++ b/tools/render-test/d3d-util.h @@ -1,9 +1,11 @@ -// d3d-util.h +// d3d-util.h #pragma once #include <stdint.h> -#include "../../source/core/slang-result.h" -#include "../../source/core/slang-com-ptr.h" + +#include "../../slang-com-helper.h" + +#include "../../slang-com-ptr.h" #include "../../source/core/list.h" #include "render.h" diff --git a/tools/render-test/descriptor-heap-d3d12.h b/tools/render-test/descriptor-heap-d3d12.h index 11fb40810..1d0302d2c 100644 --- a/tools/render-test/descriptor-heap-d3d12.h +++ b/tools/render-test/descriptor-heap-d3d12.h @@ -1,10 +1,10 @@ -#pragma once +#pragma once #include <dxgi.h> #include <d3d12.h> -#include "../../source/core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" namespace renderer_test { diff --git a/tools/render-test/options.h b/tools/render-test/options.h index 4c0f1098f..a14acec15 100644 --- a/tools/render-test/options.h +++ b/tools/render-test/options.h @@ -1,9 +1,9 @@ -// options.h +// options.h #pragma once #include <stdint.h> -#include "../../source/core/slang-result.h" +#include "../../slang-com-helper.h" #include "render.h" diff --git a/tools/render-test/render-d3d11.cpp b/tools/render-test/render-d3d11.cpp index 1ca293fad..109c5aca1 100644 --- a/tools/render-test/render-d3d11.cpp +++ b/tools/render-test/render-d3d11.cpp @@ -14,7 +14,7 @@ #include <slang.h> -#include "../../source/core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" // We will be rendering with Direct3D 11, so we need to include // the Windows and D3D11 headers diff --git a/tools/render-test/render-d3d12.cpp b/tools/render-test/render-d3d12.cpp index 593991205..7603332b4 100644 --- a/tools/render-test/render-d3d12.cpp +++ b/tools/render-test/render-d3d12.cpp @@ -25,7 +25,7 @@ #include <d3d12.h> #include <d3dcompiler.h> -#include "../../source/core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" #include "resource-d3d12.h" #include "descriptor-heap-d3d12.h" diff --git a/tools/render-test/render.h b/tools/render-test/render.h index 2ad736c27..6a3c58a05 100644 --- a/tools/render-test/render.h +++ b/tools/render-test/render.h @@ -1,11 +1,12 @@ -// render.h +// render.h #pragma once #include "window.h" //#include "shader-input-layout.h" -#include "../../source/core/slang-result.h" +#include "../../slang-com-helper.h" + #include "../../source/core/smart-pointer.h" #include "../../source/core/list.h" diff --git a/tools/render-test/resource-d3d12.h b/tools/render-test/resource-d3d12.h index 1de915489..1f0ba638b 100644 --- a/tools/render-test/resource-d3d12.h +++ b/tools/render-test/resource-d3d12.h @@ -1,4 +1,4 @@ -// resource-d3d12.h +// resource-d3d12.h #pragma once #define WIN32_LEAN_AND_MEAN @@ -10,7 +10,7 @@ #include <dxgi1_4.h> #include <d3d12.h> -#include "../../source/core/slang-com-ptr.h" +#include "../../slang-com-ptr.h" #include "d3d-util.h" namespace renderer_test { diff --git a/tools/render-test/surface.cpp b/tools/render-test/surface.cpp index 4d761389a..eabe555c3 100644 --- a/tools/render-test/surface.cpp +++ b/tools/render-test/surface.cpp @@ -1,4 +1,4 @@ -// surface.cpp +// surface.cpp #include "surface.h" #include <stdlib.h> @@ -46,7 +46,7 @@ Slang::Result MallocSurfaceAllocator::allocate(int width, int height, Format for uint8_t* data = (uint8_t*)::malloc(totalSize); if (!data) { - return SLANG_E_MEM_OUT_OF_MEMORY; + return SLANG_E_OUT_OF_MEMORY; } surface.m_data = data; diff --git a/tools/render-test/vk-device-queue.cpp b/tools/render-test/vk-device-queue.cpp index 733ee6125..d40442c22 100644 --- a/tools/render-test/vk-device-queue.cpp +++ b/tools/render-test/vk-device-queue.cpp @@ -1,8 +1,9 @@ -// vk-device-queue.cpp +// vk-device-queue.cpp #include "vk-device-queue.h" #include <stdlib.h> #include <stdio.h> +#include <assert.h> namespace renderer_test { using namespace Slang; diff --git a/tools/render-test/vk-module.cpp b/tools/render-test/vk-module.cpp index a10333584..e98bd08cd 100644 --- a/tools/render-test/vk-module.cpp +++ b/tools/render-test/vk-module.cpp @@ -1,8 +1,9 @@ -// module.cpp +// module.cpp #include "vk-module.h" #include <stdlib.h> #include <stdio.h> +#include <assert.h> #if SLANG_WINDOWS_FAMILY # include <windows.h> diff --git a/tools/render-test/vk-module.h b/tools/render-test/vk-module.h index dd3c1eaa1..0aed2303f 100644 --- a/tools/render-test/vk-module.h +++ b/tools/render-test/vk-module.h @@ -1,8 +1,9 @@ -// vk-module.h +// vk-module.h #pragma once -#include "../../source/core/slang-defines.h" -#include "../../source/core/slang-result.h" +#include "../../slang.h" + +#include "../../slang-com-helper.h" #if SLANG_WINDOWS_FAMILY # define VK_USE_PLATFORM_WIN32_KHR 1 diff --git a/tools/slang-test/main.cpp b/tools/slang-test/main.cpp index 32f1c9d4c..12434301c 100644 --- a/tools/slang-test/main.cpp +++ b/tools/slang-test/main.cpp @@ -1,9 +1,10 @@ -// main.cpp +// main.cpp #include "../../source/core/slang-io.h" #include "../../source/core/token-reader.h" -#include "../../source/core/slang-result.h" +#include "../../slang-com-helper.h" + #include "../../source/core/slang-string-util.h" using namespace Slang; diff --git a/tools/slang-test/render-api-util.cpp b/tools/slang-test/render-api-util.cpp index b8697c54b..5f47f3b49 100644 --- a/tools/slang-test/render-api-util.cpp +++ b/tools/slang-test/render-api-util.cpp @@ -1,7 +1,7 @@ - + #include "render-api-util.h" -#include "../../source/core/slang-defines.h" +#include "../../slang.h" #include "../../source/core/list.h" #include "../../source/core/slang-string-util.h" diff --git a/tools/slang-test/render-api-util.h b/tools/slang-test/render-api-util.h index eec447af9..a44528faf 100644 --- a/tools/slang-test/render-api-util.h +++ b/tools/slang-test/render-api-util.h @@ -1,9 +1,9 @@ -#ifndef SLANG_RENDER_API_UTIL_H +#ifndef SLANG_RENDER_API_UTIL_H #define SLANG_RENDER_API_UTIL_H #include "../../source/core/slang-string.h" -#include "../../source/core/slang-result.h" +#include "../../slang-com-helper.h" enum class RenderApiType { |
