From b2b46a9ced9dad18b41df0361cb91b0afb1ea710 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 18 Jul 2017 15:13:44 -0700 Subject: Add basic GLSL lowering buffer `Buffer` loads - This isn't going to work for writable buffers, and certainly not for writes - As it exists right now, this shows a flaw in how I'm handling texture-type results on fetches --- source/slang/slang-stdlib.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'source/slang/slang-stdlib.cpp') diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp index b26fc8a8e..8aa0d0e4c 100644 --- a/source/slang/slang-stdlib.cpp +++ b/source/slang/slang-stdlib.cpp @@ -2024,9 +2024,12 @@ namespace Slang sb << "__intrinsic void GetDimensions(out uint dim);\n"; + sb << "__intrinsic(glsl, \"texelFetch($P, $0)\")\n"; sb << "__intrinsic T Load(int location);\n"; + sb << "__intrinsic T Load(int location, out uint status);\n"; + sb << "__intrinsic(glsl, \"texelFetch($P, int($0))\")\n"; sb << "__intrinsic __subscript(uint index) -> T"; if (kBaseBufferAccessLevels[aa].access != SLANG_RESOURCE_ACCESS_READ) -- cgit v1.2.3 From 4d6da3a1177f27807d47ca46ec25ed96eda4642c Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 18 Jul 2017 15:21:04 -0700 Subject: Swizzle result of buffer load based on element type When lowering `buf[i]` to `texelFetch(..., i)` we need to deal with the case where the type of `b` might be `Buffer` in which case we want to add a `.x` swizzle to the end of the fetch. --- source/slang/emit.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ source/slang/slang-stdlib.cpp | 4 ++-- 2 files changed, 48 insertions(+), 2 deletions(-) (limited to 'source/slang/slang-stdlib.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 76dc9c75f..8660b5f93 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -1943,6 +1943,52 @@ struct EmitVisitor } break; + case 'z': + // If we are calling a D3D texturing operation in the form t.Foo(s, ...), + // where `t` is a `Texture*`, then this is the step where we try to + // properly swizzle the output of the equivalent GLSL call into the right + // shape. + assert(argCount > 0); + if (auto memberExpr = callExpr->FunctionExpr.As()) + { + auto base = memberExpr->BaseExpression; + if (auto baseTextureType = base->Type->As()) + { + auto elementType = baseTextureType->elementType; + if (auto basicType = elementType->As()) + { + // A scalar result is expected + Emit(".x"); + } + else if (auto vectorType = elementType->As()) + { + // A vector result is expected + auto elementCount = GetIntVal(vectorType->elementCount); + + if (elementCount < 4) + { + char const* swiz[] = { "", ".x", ".xy", ".xyz", "" }; + Emit(swiz[elementCount]); + } + } + else + { + // What other cases are possible? + } + } + else + { + assert(!"unexpected"); + } + + } + else + { + assert(!"unexpected"); + } + break; + + default: assert(!"unexpected"); break; diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp index 8aa0d0e4c..a62db693c 100644 --- a/source/slang/slang-stdlib.cpp +++ b/source/slang/slang-stdlib.cpp @@ -2024,12 +2024,12 @@ namespace Slang sb << "__intrinsic void GetDimensions(out uint dim);\n"; - sb << "__intrinsic(glsl, \"texelFetch($P, $0)\")\n"; + sb << "__intrinsic(glsl, \"texelFetch($P, $0)$z\")\n"; sb << "__intrinsic T Load(int location);\n"; sb << "__intrinsic T Load(int location, out uint status);\n"; - sb << "__intrinsic(glsl, \"texelFetch($P, int($0))\")\n"; + sb << "__intrinsic(glsl, \"texelFetch($P, int($0))$z\")\n"; sb << "__intrinsic __subscript(uint index) -> T"; if (kBaseBufferAccessLevels[aa].access != SLANG_RESOURCE_ACCESS_READ) -- cgit v1.2.3