From c45f368ae404798db67a601749c6e0047fba75ef Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 30 Apr 2021 11:36:42 -0700 Subject: Clean up the way we stride over sub-object ranges for D3D11 and Vulkan (#1829) This change applies to the case where we have a sub-object range with more than one object in it (so arrays of constant buffers, parameter blocks, or existentials). It is worth noting that we don't really seem to have any tests covering this case right now, so it is entirely possible that things are busted already, and/or that this change doesn't work the way I think it does. When we go to actually bind the state from a sub-object range into the pipeline, we care about: * The offset to where the first object should be written * The "stride" between consecutive objects We were already capturing the offset information from Slang reflection data, and the same was true for the part of the offset that pertains to "pending" ordinary data. For other cases, though, we were manually incrementing the offset by values computed manually in `gfx` for Vulkan, and we were just skipping the offsetting step entirely for D3D11. With this change we extract more complete stride information from the Slang reflection data and use that instead of ad hoc computations. Hopefully this data is correct, and if it isn't we can consider whether it needs fixing at the Slang reflection level rather than in `gfx`. This change doesn't apply to the OpenGL back-end (which hasn't been updated to match the new approach to static specialization at all) or to the D3D12 back end (which has been updated a bit, but probably needs other cleanup work to be done first to bring it in line). --- source/slang/slang-reflection-api.cpp | 46 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'source') diff --git a/source/slang/slang-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index d75920fdb..b74a019e3 100644 --- a/source/slang/slang-reflection-api.cpp +++ b/source/slang/slang-reflection-api.cpp @@ -819,6 +819,35 @@ namespace return SLANG_UNBOUNDED_SIZE; } + + static int32_t getAlignment(TypeLayout* typeLayout, SlangParameterCategory category) + { + if( category == SLANG_PARAMETER_CATEGORY_UNIFORM ) + { + return int32_t(typeLayout->uniformAlignment); + } + else + { + return 1; + } + } + + static size_t getStride(TypeLayout* typeLayout, SlangParameterCategory category) + { + auto info = typeLayout->FindResourceInfo(LayoutResourceKind(category)); + if(!info) return 0; + + auto size = info->count; + if(size.isInfinite()) + return SLANG_UNBOUNDED_SIZE; + + size_t finiteSize = size.getFiniteValue(); + size_t alignment = getAlignment(typeLayout, category); + SLANG_ASSERT(alignment >= 1); + + auto stride = (finiteSize + (alignment-1)) & ~(alignment-1); + return stride; + } } SLANG_API size_t spReflectionTypeLayout_GetSize(SlangReflectionTypeLayout* inTypeLayout, SlangParameterCategory category) @@ -832,19 +861,20 @@ SLANG_API size_t spReflectionTypeLayout_GetSize(SlangReflectionTypeLayout* inTyp return getReflectionSize(info->count); } +SLANG_API size_t spReflectionTypeLayout_GetStride(SlangReflectionTypeLayout* inTypeLayout, SlangParameterCategory category) +{ + auto typeLayout = convert(inTypeLayout); + if(!typeLayout) return 0; + + return getStride(typeLayout, category); +} + SLANG_API int32_t spReflectionTypeLayout_getAlignment(SlangReflectionTypeLayout* inTypeLayout, SlangParameterCategory category) { auto typeLayout = convert(inTypeLayout); if(!typeLayout) return 0; - if( category == SLANG_PARAMETER_CATEGORY_UNIFORM ) - { - return int32_t(typeLayout->uniformAlignment); - } - else - { - return 1; - } + return getAlignment(typeLayout, category); } SLANG_API SlangReflectionVariableLayout* spReflectionTypeLayout_GetFieldByIndex(SlangReflectionTypeLayout* inTypeLayout, unsigned index) -- cgit v1.2.3