summaryrefslogtreecommitdiffstats
path: root/prelude/slang-cpp-types.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-08-20 09:43:59 -0400
committerGitHub <noreply@github.com>2019-08-20 09:43:59 -0400
commit7258ef4ddebd021208a019f6ee73edcda57a88f7 (patch)
tree30cccf48c8f03e59e48a2d265e05494238fe758d /prelude/slang-cpp-types.h
parent3e78e4654cdf9556869325f2ed2da517f252d879 (diff)
User defined downstream compiler prelude (#1028)
* Added setDownstreamCompilerPrelude Renamed setPassThroughPath to setDownstreamCompilerPath. Fixed tests. Added prelude directory & code to TestToolUtil to setup default preludes for testing/command line apis. * Fix merge problem * Remove hacks to make prelude work by adding a search path as no longer needed with 'user prelude'. * Split up prelude into scalar intrinsics, and types. Use slang.h for main header. slang-cpp-prelude.h can now just include what it needs (relative to prelude directory) and define the few remaining things/work arounds. * Fix typo.
Diffstat (limited to 'prelude/slang-cpp-types.h')
-rw-r--r--prelude/slang-cpp-types.h239
1 files changed, 239 insertions, 0 deletions
diff --git a/prelude/slang-cpp-types.h b/prelude/slang-cpp-types.h
new file mode 100644
index 000000000..20e28a16f
--- /dev/null
+++ b/prelude/slang-cpp-types.h
@@ -0,0 +1,239 @@
+#ifndef SLANG_PRELUDE_CPP_TYPES_H
+#define SLANG_PRELUDE_CPP_TYPES_H
+
+#include "../slang.h"
+
+#ifdef SLANG_PRELUDE_NAMESPACE
+namespace SLANG_PRELUDE_NAMESPACE {
+#endif
+
+template <typename T, size_t SIZE>
+struct FixedArray
+{
+ const T& operator[](size_t index) const { assert(index < SIZE); return m_data[index]; }
+ T& operator[](size_t index) { assert(index < SIZE); return m_data[index]; }
+
+ T m_data[SIZE];
+};
+
+
+// Hmm... I guess a constant buffer should be unwrapped to be just a struct passed in
+/* template <typename T>
+struct ConstantBuffer
+{
+}; */
+
+template <typename T, int COUNT>
+struct Vector;
+
+template <typename T>
+struct Vector<T, 1>
+{
+ T x;
+};
+
+template <typename T>
+struct Vector<T, 2>
+{
+ T x, y;
+};
+
+template <typename T>
+struct Vector<T, 3>
+{
+ T x, y, z;
+};
+
+template <typename T>
+struct Vector<T, 4>
+{
+ T x, y, z, w;
+};
+
+
+typedef Vector<float, 2> float2;
+typedef Vector<float, 3> float3;
+typedef Vector<float, 4> float4;
+
+typedef Vector<int32_t, 2> int2;
+typedef Vector<int32_t, 3> int3;
+typedef Vector<int32_t, 4> int4;
+
+typedef Vector<uint32_t, 2> uint2;
+typedef Vector<uint32_t, 3> uint3;
+typedef Vector<uint32_t, 4> uint4;
+
+template <typename T, int ROWS, int COLS>
+struct Matrix
+{
+ Vector<T, COLS> rows[ROWS];
+};
+
+// ----------------------------- ResourceType -----------------------------------------
+
+// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-structuredbuffer-getdimensions
+// Missing Load(_In_ int Location, _Out_ uint Status);
+
+template <typename T>
+struct RWStructuredBuffer
+{
+ T& operator[](size_t index) const { assert(index < count); return data[index]; }
+ const T& Load(size_t index) const { assert(index < count); return data[index]; }
+ void GetDimensions(uint32_t& outNumStructs, uint32_t& outStride) { outNumStructs = uint32_t(count); outStride = uint32_t(sizeof(T)); }
+
+ T* data;
+ size_t count;
+};
+
+template <typename T>
+struct StructuredBuffer
+{
+ const T& operator[](size_t index) const { assert(index < count); return data[index]; }
+ const T& Load(size_t index) const { assert(index < count); return data[index]; }
+ void GetDimensions(uint32_t& outNumStructs, uint32_t& outStride) { outNumStructs = uint32_t(count); outStride = uint32_t(sizeof(T)); }
+
+ T* data;
+ size_t count;
+};
+
+// Missing Load(_In_ int Location, _Out_ uint Status);
+struct ByteAddressBuffer
+{
+ void GetDimensions(uint32_t& outDim) const { outDim = uint32_t(sizeInBytes); }
+ uint32_t Load(size_t index) const
+ {
+ assert(index + 4 <= sizeInBytes && (index & 3) == 0);
+ return data[index >> 2];
+ }
+ uint2 Load2(size_t index) const
+ {
+ assert(index + 8 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint2{data[dataIdx], data[dataIdx + 1]};
+ }
+ uint3 Load3(size_t index) const
+ {
+ assert(index + 12 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
+ }
+ uint4 Load4(size_t index) const
+ {
+ assert(index + 16 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
+ }
+
+ const uint32_t* data;
+ size_t sizeInBytes; //< Must be multiple of 4
+};
+
+// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer
+// Missing support for Atomic operations
+// Missing support for Load with status
+struct RWByteAddressBuffer
+{
+ void GetDimensions(uint32_t& outDim) const { outDim = uint32_t(sizeInBytes); }
+
+ uint32_t Load(size_t index) const
+ {
+ assert(index + 4 <= sizeInBytes && (index & 3) == 0);
+ return data[index >> 2];
+ }
+ uint2 Load2(size_t index) const
+ {
+ assert(index + 8 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint2{data[dataIdx], data[dataIdx + 1]};
+ }
+ uint3 Load3(size_t index) const
+ {
+ assert(index + 12 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
+ }
+ uint4 Load4(size_t index) const
+ {
+ assert(index + 16 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
+ }
+
+ void Store(size_t index, uint32_t v) const
+ {
+ assert(index + 4 <= sizeInBytes && (index & 3) == 0);
+ data[index >> 2] = v;
+ }
+ void Store2(size_t index, uint2 v) const
+ {
+ assert(index + 8 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ data[dataIdx + 0] = v.x;
+ data[dataIdx + 1] = v.y;
+ }
+ void Store3(size_t index, uint3 v) const
+ {
+ assert(index + 12 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ data[dataIdx + 0] = v.x;
+ data[dataIdx + 1] = v.y;
+ data[dataIdx + 2] = v.z;
+ }
+ void Store4(size_t index, uint4 v) const
+ {
+ assert(index + 16 <= sizeInBytes && (index & 3) == 0);
+ const size_t dataIdx = index >> 2;
+ data[dataIdx + 0] = v.x;
+ data[dataIdx + 1] = v.y;
+ data[dataIdx + 2] = v.z;
+ data[dataIdx + 3] = v.w;
+ }
+
+ uint32_t* data;
+ size_t sizeInBytes; //< Must be multiple of 4
+};
+
+struct ISamplerState;
+struct ISamplerComparisonState;
+
+struct SamplerState
+{
+ ISamplerState* state;
+};
+
+struct SamplerComparisonState
+{
+ ISamplerComparisonState* state;
+};
+
+// Texture
+
+struct ITexture2D
+{
+ virtual void Load(const int3& v, void* out) = 0;
+ virtual void Sample(SamplerState samplerState, const float2& loc, void* out) = 0;
+};
+
+template <typename T>
+struct Texture2D
+{
+ T Load(const int3& v) const { T out; texture->Load(v, &out); return out; }
+ T Sample(SamplerState samplerState, const float2& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
+
+ ITexture2D* texture;
+};
+
+/* Varying input for Compute */
+struct ComputeVaryingInput
+{
+ uint3 groupID;
+ uint3 groupThreadID;
+};
+
+#ifdef SLANG_PRELUDE_NAMESPACE
+}
+#endif
+
+#endif
+
+