summaryrefslogtreecommitdiffstats
path: root/source/slang/emit.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-11-19 09:07:34 -0800
committerGitHub <noreply@github.com>2018-11-19 09:07:34 -0800
commit7e0c5ad677e04be4ecd2ce087d856ba26f1667a4 (patch)
treefd4168b307fff97e3811defe4813029b660ec697 /source/slang/emit.cpp
parent0f67d92df9ca1f528b7a7fc8a8712aaea56f7115 (diff)
Add Vulkan cross-compilation for byte-address buffers (#721)
* Add Vulkan cross-compilation for byte-address buffers This covers `ByteAddressBuffer`, `RWByteAddressBuffer`, and `RasterizerOrderedByteAddressBuffer`. A declaration of any of these types translates to a GLSL `buffer` declaration with a single `uint` array of data. Most of the methods on these types then have straightforward translations to operations on the array. The overall translation is similar to what was already being done for structured buffers. While implementing GLSL translation for the various atomic (`Interlocked*`) methods, I discovered that some of these included declarations that aren't actually included in HLSL. I cleaned these up, including in the declarations of the global `Interlocked*` functions. The test case that is included here covers only the most basic functionality: `Load`, `Load2`, `Load4` and `Store`. We should try to back-fill tests for the remaining methods when we have time. Two large caveats with this work: 1. We don't handle arrays of byte-address buffers, just as we don't handle arrays of structured buffers. That will take additional work. 2. We don't handle byte-address (or structured) buffers being passed as function parameters, since the parameter would need to be declared as a bare `uint[]` array. * Fixup: don't lump raytracing acceleration structures in with buffers Raytracing acceleration structures share a common base class with byte-address buffers (they are both buffer resources without a specific element type), and I was mistakently matching on this base class in an attempt to have a catch-all that applied to all byte-address buffers. The fix here was to add a distinct base class for all byte-address buffers and catch that instead. * Fixup: typos
Diffstat (limited to 'source/slang/emit.cpp')
-rw-r--r--source/slang/emit.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp
index 8038561d0..a3b99691d 100644
--- a/source/slang/emit.cpp
+++ b/source/slang/emit.cpp
@@ -5999,6 +5999,66 @@ struct EmitVisitor
emit(";\n");
}
+ void emitIRByteAddressBuffer_GLSL(
+ EmitContext* ctx,
+ IRGlobalVar* varDecl,
+ IRByteAddressBufferTypeBase* /* byteAddressBufferType */)
+ {
+ // TODO: A lot of this logic is copy-pasted from `emitIRStructuredBuffer_GLSL`.
+ // It might be worthwhile to share the common code to avoid regressions sneaking
+ // in when one or the other, but not both, gets updated.
+
+ // Shader storage buffer is an OpenGL 430 feature
+ //
+ // TODO: we should require either the extension or the version...
+ requireGLSLVersion(430);
+
+ Emit("layout(std430");
+
+ auto layout = getVarLayout(ctx, varDecl);
+ if (layout)
+ {
+ LayoutResourceKind kind = LayoutResourceKind::DescriptorTableSlot;
+ EmitVarChain chain(layout);
+
+ const UInt index = getBindingOffset(&chain, kind);
+ const UInt space = getBindingSpace(&chain, kind);
+
+ Emit(", binding = ");
+ Emit(index);
+ if (space)
+ {
+ Emit(", set = ");
+ Emit(space);
+ }
+ }
+
+ emit(") buffer ");
+
+ // Generate a dummy name for the block
+ emit("_S");
+ Emit(ctx->shared->uniqueIDCounter++);
+ emit("\n{\n");
+ indent();
+
+ emit("uint ");
+ emit(getIRName(varDecl));
+ emit("[];\n");
+
+ dedent();
+ emit("}");
+
+ // TODO: we need to consider the case where the type of the variable is
+ // an *array* of structured buffers, in which case we need to declare
+ // the block as an array too.
+ //
+ // The main challenge here is that then the block will have a name,
+ // and also the field inside the block will have a name, so that when
+ // the user had written `a[i][j]` we now need to emit `a[i].someName[j]`.
+
+ emit(";\n");
+ }
+
void emitIRGlobalVar(
EmitContext* ctx,
IRGlobalVar* varDecl)
@@ -6048,6 +6108,17 @@ struct EmitVisitor
return;
}
+ // When outputting GLSL, we need to transform any declaration of
+ // a `*ByteAddressBuffer<T>` into an ordinary `buffer` declaration.
+ if( auto byteAddressBufferType = as<IRByteAddressBufferTypeBase>(unwrapArray(varType)) )
+ {
+ emitIRByteAddressBuffer_GLSL(
+ ctx,
+ varDecl,
+ byteAddressBufferType);
+ return;
+ }
+
// We want to skip the declaration of any system-value variables
// when outputting GLSL (well, except in the case where they
// actually *require* redeclaration...).