Revision 725985528f77ba939a5cddc71e5006fee7638465 authored by Tim Foley on 29 October 2018, 21:44:39 UTC, committed by GitHub on 29 October 2018, 21:44:39 UTC
* Rework command-line options handling for entry points and targets

Overview:

* The biggest functionality change is that the implicit ordering constraints when multiple `-entry` options are reversed: any `-stage` option affects the `-entry` to its *left* instead of to its *right* as it used to. This is technically a breaking change, but I expect most users aren't using this feature.

* The options parsing tries to handle profile versions and stages as distinct data (rather than using the combined `Profile` type all over), and treats a `-profile` option that specifies both a profile version and a stage (e.g., `-profile ps_5_0`) as if it were sugar for both a `-profile` and a `-stage` (e.g., `-profile sm_5_0 -stage fragment`).

* We now technically handle multiple `-target` options in one invocation of `-slangc`, but do not advertise that fact in the documentation because it might be confusing for users. Similar to the relationship between `-stage` and `-entry`, any `-profile` option affects the most recent `-target` option unless there is only one `-target`.

* The logic for associating `-o` options with corresponding entry points and targets has been beefed up. The rule is that a `-o` option for a compiled kernel binds to the entry point to its left, unless there is only one entry point (just like for `-stage`). The associated target for a `-o` option is found via a search, however, because otherwise it would be impossible to specify `-o` options for both SPIR-V and DXIL in one pass.

* The handling of output paths for entry points in the internal compiler structures was changed, because previously it could only handle one output path per entry point (even when there are multiple targets). The new logic builds up a per-target mapping from an entry point to its desired output path (if any).

Details:

* Support for formatting profile versions, stages, and compile targets (formats) was added to diagnostic printing, so that we can make better error messages. This is fairly ad hoc, and it would be nice to have all of the string<->enum stuff be more data-driven throughout the codebase.

* Test cases were added for (almost) all of the error conditions in the current options validation. The main one that is missing is around specifying an `-entry` option before any source file when compiling multiple files. This is because the test runner is putting the source file name first on the command line automatically, so we can't reproduce that case.

* Several reflection-related tests now reflect entry points where they didn't before, because the logic for detecting when to infer a default `main` entry point have been made more loose

* On the dxc path, beefed up the handling of mapping from Slang `Profile`s to the coresponding string to use when invoking dxc.

* A bunch of tests cases were in violation of the newly imposed rules, so those needed to be cleaned up.

* There were also a bunch of test cases that had accidentally gotten "disabled" at some point because there were comparing output from `slangc` both with and without a `-pass-through` option, but that meant that any errors in command-line parsing produced the *same* error output in both the Slang and pass-through cases. This change updates `slang-test` to always expect a successful run for these tests, and then manually updates or disables the various test cases that are affected.

* When merging the updated test for matrix layout mode, I found that the new command-line logic was failing to propagate a matrix layout mode passed to `render-test` into the compiler. This was because the `-matrix-layout*` options were implemented as per-target, but the target was being set by API while the option came in via command line (passed through the API). It seems like we want matrix layout mode to be a global option anyway (rather than per-target), so I made that change here.

* Add missing expected output files

* A 64-bit fix

* Remove commented-out code noted in review
1 parent 56c9de0
Raw File
slang-com-ptr.h
#ifndef SLANG_COM_PTR_H
#define SLANG_COM_PTR_H

#include "slang-com-helper.h"

#include <assert.h>

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 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.
Using 'detach' allows a pointer to be removed from the management of the ComPtr.
To set the smart pointer to null, there is the method setNull, or alternatively just assign SLANG_NULL/nullptr.

One edge case using the template is that sometimes you want access as a pointer to a pointer. Sometimes this
is to write into the smart pointer, other times to pass as an array. To handle these different behaviors
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

A more common scenario writing to the pointer

\code
IUnknown* unk = ...;

ComPtr<ID3D12Resource> resource;
Result res = unk->QueryInterface(resource.writeRef());
\endcode
*/

// Enum to force initializing as an attach (without adding a reference)
enum InitAttach
{
    INIT_ATTACH
};

template <class T>
class ComPtr
{
public:
	typedef T Type;
	typedef ComPtr ThisType;
	typedef ISlangUnknown* Ptr;

		/// Constructors
		/// Default Ctor. Sets to nullptr
	SLANG_FORCE_INLINE ComPtr() :m_ptr(nullptr) {}
		/// Sets, and ref counts.
	SLANG_FORCE_INLINE explicit ComPtr(T* ptr) :m_ptr(ptr) { if (ptr) ((Ptr)ptr)->addRef(); }
		/// The copy ctor
	SLANG_FORCE_INLINE ComPtr(const ThisType& rhs) : m_ptr(rhs.m_ptr) { if (m_ptr) ((Ptr)m_ptr)->addRef(); }

        /// Ctor without adding to ref count.
    SLANG_FORCE_INLINE explicit ComPtr(InitAttach, T* ptr) :m_ptr(ptr) { }
        /// Ctor without adding to ref count
    SLANG_FORCE_INLINE ComPtr(InitAttach, const ThisType& rhs) : m_ptr(rhs.m_ptr) { }

#ifdef SLANG_HAS_MOVE_SEMANTICS
		/// Move Ctor
	SLANG_FORCE_INLINE ComPtr(ThisType&& rhs) : m_ptr(rhs.m_ptr) { rhs.m_ptr = nullptr; }
		/// Move assign
	SLANG_FORCE_INLINE ComPtr& operator=(ThisType&& rhs) { T* swap = m_ptr; m_ptr = rhs.m_ptr; rhs.m_ptr = swap; return *this; }
#endif

	/// Destructor releases the pointer, assuming it is set
	SLANG_FORCE_INLINE ~ComPtr() { if (m_ptr) ((Ptr)m_ptr)->release(); }

	// !!! Operators !!!

	  /// Returns the dumb pointer
	SLANG_FORCE_INLINE operator T *() const { return m_ptr; }

	SLANG_FORCE_INLINE T& operator*() { return *m_ptr; }
		/// For making method invocations through the smart pointer work through the dumb pointer
	SLANG_FORCE_INLINE T* operator->() const { return m_ptr; }

		/// Assign
	SLANG_FORCE_INLINE const ThisType &operator=(const ThisType& rhs);
		/// Assign from dumb ptr
	SLANG_FORCE_INLINE T* operator=(T* in);

		/// Get the pointer and don't ref
	SLANG_FORCE_INLINE T* get() const { return m_ptr; }
		/// Release a contained nullptr pointer if set
	SLANG_FORCE_INLINE void setNull();

		/// Detach
	SLANG_FORCE_INLINE T* detach() { T* ptr = m_ptr; m_ptr = nullptr; return ptr; }
		/// Set to a pointer without changing the ref count
	SLANG_FORCE_INLINE void attach(T* in) { m_ptr = in; }

		/// Get ready for writing (nulls contents)
	SLANG_FORCE_INLINE T** writeRef() { setNull(); return &m_ptr; }
		/// Get for read access
	SLANG_FORCE_INLINE T*const* readRef() const { return &m_ptr; }

		/// Swap
	void swap(ThisType& rhs);

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&() = delete;

	T* m_ptr;
};

//----------------------------------------------------------------------------
template <typename T>
void ComPtr<T>::setNull()
{
	if (m_ptr)
	{
		((Ptr)m_ptr)->release();
		m_ptr = nullptr;
	}
}
//----------------------------------------------------------------------------
template <typename T>
const ComPtr<T>& ComPtr<T>::operator=(const ThisType& rhs)
{
	if (rhs.m_ptr) ((Ptr)rhs.m_ptr)->addRef();
	if (m_ptr) ((Ptr)m_ptr)->release();
	m_ptr = rhs.m_ptr;
	return *this;
}
//----------------------------------------------------------------------------
template <typename T>
T* ComPtr<T>::operator=(T* ptr)
{
	if (ptr) ((Ptr)ptr)->addRef();
	if (m_ptr) ((Ptr)m_ptr)->release();
	m_ptr = ptr;
	return m_ptr;
}
//----------------------------------------------------------------------------
template <typename T>
void ComPtr<T>::swap(ThisType& rhs)
{
	T* tmp = m_ptr;
	m_ptr = rhs.m_ptr;
	rhs.m_ptr = tmp;
}

} // namespace Slang

#endif // SLANG_COM_PTR_H
back to top