diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-01-28 12:41:09 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-28 12:41:09 -0500 |
| commit | b3e0b0d491c55bfdc1c40d26a421910103c1b9f2 (patch) | |
| tree | 27f58dc4bdf49cd786644adda6d59fbe333a1a4a /source | |
| parent | 5c6ab6d5198ebff276da9cb8d3024802f22ba9f3 (diff) | |
Synthesizing CUDA tests (#1183)
* When using setUniform clamp the amount of data written to the buffer size.
* CUDA implement StructuredBuffer/ByteAddressBuffer as pointer/count as is on CPU.
Allow bounds check to zero index.
Update docs.
* Synthesize tests.
* Fix bug in CUDA output.
* Fixing more tests to run on CUDA.
* Added BaseType for layout of Vector and Matrix - as they are held as int32_t vector array types.
* Enable unbound array support on CUDA.
* Added unsized array support for CUDA documentation.
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-emit.cpp | 1 | ||||
| -rw-r--r-- | source/slang/slang-type-layout.cpp | 149 | ||||
| -rw-r--r-- | source/slang/slang-type-layout.h | 12 |
3 files changed, 142 insertions, 20 deletions
diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 66e5714c3..2a216994b 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -518,6 +518,7 @@ String emitEntryPointSourceFromIR( case SourceStyle::CPP: case SourceStyle::C: + case SourceStyle::CUDA: linkingAndOptimizationOptions.shouldLegalizeExistentialAndResourceTypes = false; break; } diff --git a/source/slang/slang-type-layout.cpp b/source/slang/slang-type-layout.cpp index 2eec26ee6..08c789609 100644 --- a/source/slang/slang-type-layout.cpp +++ b/source/slang/slang-type-layout.cpp @@ -112,8 +112,9 @@ struct DefaultLayoutRulesImpl : SimpleLayoutRulesImpl return arrayInfo; } - SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) override + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t elementCount) override { + SLANG_UNUSED(elementType); SimpleLayoutInfo vectorInfo; vectorInfo.kind = elementInfo.kind; vectorInfo.size = elementInfo.size * elementCount; @@ -121,7 +122,7 @@ struct DefaultLayoutRulesImpl : SimpleLayoutRulesImpl return vectorInfo; } - SimpleArrayLayoutInfo GetMatrixLayout(SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) override + SimpleArrayLayoutInfo GetMatrixLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) override { // The default behavior here is to lay out a matrix // as an array of row vectors (that is row-major). @@ -131,7 +132,7 @@ struct DefaultLayoutRulesImpl : SimpleLayoutRulesImpl // to get layouts with a different convention. // return GetArrayLayout( - GetVectorLayout(elementInfo, columnCount), + GetVectorLayout(elementType, elementInfo, columnCount), rowCount); } @@ -204,8 +205,9 @@ struct GLSLBaseLayoutRulesImpl : DefaultLayoutRulesImpl { typedef DefaultLayoutRulesImpl Super; - SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) override + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t elementCount) override { + SLANG_UNUSED(elementType); // The `std140` and `std430` rules require vectors to be aligned to the next power of // two up from their size (so a `float2` is 8-byte aligned, and a `float3` is // 16-byte aligned). @@ -224,7 +226,7 @@ struct GLSLBaseLayoutRulesImpl : DefaultLayoutRulesImpl return vectorInfo; } - SimpleArrayLayoutInfo GetArrayLayout( SimpleLayoutInfo elementInfo, LayoutSize elementCount) override + SimpleArrayLayoutInfo GetArrayLayout(SimpleLayoutInfo elementInfo, LayoutSize elementCount) override { // The size of an array must be rounded up to be a multiple of its alignment. // @@ -376,7 +378,7 @@ struct CPULayoutRulesImpl : DefaultLayoutRulesImpl // So it is actually a Array<T> on CPU which is a pointer and a size info.size = sizeof(void*) * 2; - info.alignment = sizeof(void*); + info.alignment = SLANG_ALIGN_OF(void*); return info; } @@ -398,12 +400,115 @@ struct CPULayoutRulesImpl : DefaultLayoutRulesImpl } }; -// TODO(JS): Most likely wrong. For layout for CUDA, we'll just do the default to get things up and running struct CUDALayoutRulesImpl : DefaultLayoutRulesImpl { typedef DefaultLayoutRulesImpl Super; -}; + SimpleLayoutInfo GetScalarLayout(BaseType baseType) override + { + switch (baseType) + { + case BaseType::Bool: + { + // In memory a bool is a byte. BUT when in a vector or matrix it will actually be a int32_t + return SimpleLayoutInfo(LayoutResourceKind::Uniform, sizeof(uint8_t), SLANG_ALIGN_OF(uint8_t)); + } + + default: return Super::GetScalarLayout(baseType); + } + } + + SimpleArrayLayoutInfo GetArrayLayout(SimpleLayoutInfo elementInfo, LayoutSize elementCount) override + { + SLANG_RELEASE_ASSERT(elementInfo.size.isFinite()); + auto elementSize = elementInfo.size.getFiniteValue(); + auto elementAlignment = elementInfo.alignment; + auto elementStride = RoundToAlignment(elementSize, elementAlignment); + + if (elementCount.isInfinite()) + { + // This is an unsized array, get information for element + auto info = Super::GetArrayLayout(elementInfo, LayoutSize(1)); + + // So it is actually a Array<T> on CUDA which is a pointer and a size + info.size = sizeof(void*) * 2; + info.alignment = SLANG_ALIGN_OF(void*); + return info; + } + + // An array with no elements will have zero size. + // + LayoutSize arraySize = 0; + // + // Any array with a non-zero number of elements will need + // to have space for N elements of size `elementSize`, with + // the constraints that there must be `elementStride` bytes + // between consecutive elements. + // + if (elementCount > 0) + { + // We can think of this as either allocating (N-1) + // chunks of size `elementStride` (for most of the elements) + // and then one final chunk of size `elementSize` for + // the last element, or equivalently as allocating + // N chunks of size `elementStride` and then "giving back" + // the final `elementStride - elementSize` bytes. + // + arraySize = (elementStride * (elementCount - 1)) + elementSize; + } + + SimpleArrayLayoutInfo arrayInfo; + arrayInfo.kind = elementInfo.kind; + arrayInfo.size = arraySize; + arrayInfo.alignment = elementAlignment; + arrayInfo.elementStride = elementStride; + return arrayInfo; + } + + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t elementCount) override + { + // Special case bool + if (elementType == BaseType::Bool) + { + SimpleLayoutInfo fixInfo(elementInfo); + fixInfo.size = sizeof(int32_t); + fixInfo.alignment = SLANG_ALIGN_OF(int32_t); + return GetVectorLayout(BaseType::Int, fixInfo, elementCount); + } + + SimpleLayoutInfo vectorInfo; + vectorInfo.kind = elementInfo.kind; + vectorInfo.size = elementInfo.size * elementCount; + vectorInfo.alignment = elementInfo.alignment; + + return vectorInfo; + } + + SimpleArrayLayoutInfo GetMatrixLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) override + { + // Special case bool + if (elementType == BaseType::Bool) + { + SimpleLayoutInfo fixInfo(elementInfo); + fixInfo.size = sizeof(int32_t); + fixInfo.alignment = SLANG_ALIGN_OF(int32_t); + return GetMatrixLayout(BaseType::Int, fixInfo, rowCount, columnCount); + } + + return Super::GetMatrixLayout(elementType, elementInfo, rowCount, columnCount); + } + + UniformLayoutInfo BeginStructLayout() override + { + return Super::BeginStructLayout(); + } + + void EndStructLayout(UniformLayoutInfo* ioStructInfo) override + { + // Conform to CUDA/C/C++ size is adjusted to the largest alignment + ioStructInfo->size = RoundToAlignment(ioStructInfo->size, ioStructInfo->alignment); + } +}; struct HLSLStructuredBufferLayoutRulesImpl : DefaultLayoutRulesImpl { @@ -436,8 +541,9 @@ struct DefaultVaryingLayoutRulesImpl : DefaultLayoutRulesImpl 1); } - SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo, size_t) override + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo, size_t) override { + SLANG_UNUSED(elementType); // Vectors take up one slot by default // // TODO: some platforms may decide that vectors of `double` need @@ -479,8 +585,9 @@ struct GLSLSpecializationConstantLayoutRulesImpl : DefaultLayoutRulesImpl 1); } - SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo, size_t elementCount) override + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo, size_t elementCount) override { + SLANG_UNUSED(elementType); // GLSL doesn't support vectors of specialization constants, // but we will assume that, if supported, they would use one slot per element. return SimpleLayoutInfo( @@ -3052,7 +3159,13 @@ static TypeLayoutResult _createTypeLayout( context, elementType); - auto info = rules->GetVectorLayout(element.info, elementCount); + BaseType elementBaseType = BaseType::Void; + if (auto elementBasicType = as<BasicExpressionType>(elementType)) + { + elementBaseType = elementBasicType->baseType; + } + + auto info = rules->GetVectorLayout(elementBaseType, element.info, elementCount); RefPtr<VectorTypeLayout> typeLayout = new VectorTypeLayout(); typeLayout->type = type; @@ -3078,6 +3191,12 @@ static TypeLayoutResult _createTypeLayout( auto elementTypeLayout = elementResult.layout; auto elementInfo = elementResult.info; + BaseType elementBaseType = BaseType::Void; + if (auto elementBasicType = as<BasicExpressionType>(elementType)) + { + elementBaseType = elementBasicType->baseType; + } + // The `GetMatrixLayout` implementation in the layout rules // currently defaults to assuming row-major layout, // so if we want column-major layout we achieve it here by @@ -3092,6 +3211,7 @@ static TypeLayoutResult _createTypeLayout( layoutMinorCount = tmp; } auto info = rules->GetMatrixLayout( + elementBaseType, elementInfo, layoutMajorCount, layoutMinorCount); @@ -3100,6 +3220,7 @@ static TypeLayoutResult _createTypeLayout( RefPtr<VectorTypeLayout> rowTypeLayout = new VectorTypeLayout(); auto rowInfo = rules->GetVectorLayout( + elementBaseType, elementInfo, colCount); @@ -3680,7 +3801,7 @@ RefPtr<TypeLayout> getSimpleVaryingParameterTypeLayout( { auto varyingRuleSet = varyingRules[rr]; auto elementInfo = varyingRuleSet->GetScalarLayout(elementBaseType); - auto info = varyingRuleSet->GetVectorLayout(elementInfo, elementCount); + auto info = varyingRuleSet->GetVectorLayout(elementBaseType, elementInfo, elementCount); typeLayout->addResourceUsage(info.kind, info.size); } @@ -3735,14 +3856,14 @@ RefPtr<TypeLayout> getSimpleVaryingParameterTypeLayout( auto varyingRuleSet = varyingRules[rr]; auto elementInfo = varyingRuleSet->GetScalarLayout(elementBaseType); - auto info = varyingRuleSet->GetMatrixLayout(elementInfo, layoutMajorCount, layoutMinorCount); + auto info = varyingRuleSet->GetMatrixLayout(elementBaseType, elementInfo, layoutMajorCount, layoutMinorCount); typeLayout->addResourceUsage(info.kind, info.size); if(context.matrixLayoutMode == kMatrixLayoutMode_RowMajor) { // For row-major matrices only, we can compute an effective // resource usage for the row type. - auto rowInfo = varyingRuleSet->GetVectorLayout(elementInfo, colCount); + auto rowInfo = varyingRuleSet->GetVectorLayout(elementBaseType, elementInfo, colCount); rowTypeLayout->addResourceUsage(rowInfo.kind, rowInfo.size); } } diff --git a/source/slang/slang-type-layout.h b/source/slang/slang-type-layout.h index afca06ce1..2cc6f338c 100644 --- a/source/slang/slang-type-layout.h +++ b/source/slang/slang-type-layout.h @@ -814,8 +814,8 @@ struct SimpleLayoutRulesImpl virtual SimpleArrayLayoutInfo GetArrayLayout(SimpleLayoutInfo elementInfo, LayoutSize elementCount) = 0; // Get layout for a vector or matrix type - virtual SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) = 0; - virtual SimpleArrayLayoutInfo GetMatrixLayout(SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) = 0; + virtual SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t elementCount) = 0; + virtual SimpleArrayLayoutInfo GetMatrixLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) = 0; // Begin doing layout on a `struct` type virtual UniformLayoutInfo BeginStructLayout() = 0; @@ -851,14 +851,14 @@ struct LayoutRulesImpl return simpleRules->GetArrayLayout(elementInfo, elementCount); } - SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) + SimpleLayoutInfo GetVectorLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t elementCount) { - return simpleRules->GetVectorLayout(elementInfo, elementCount); + return simpleRules->GetVectorLayout(elementType, elementInfo, elementCount); } - SimpleArrayLayoutInfo GetMatrixLayout(SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) + SimpleArrayLayoutInfo GetMatrixLayout(BaseType elementType, SimpleLayoutInfo elementInfo, size_t rowCount, size_t columnCount) { - return simpleRules->GetMatrixLayout(elementInfo, rowCount, columnCount); + return simpleRules->GetMatrixLayout(elementType, elementInfo, rowCount, columnCount); } UniformLayoutInfo BeginStructLayout() |
