diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-05-22 09:58:19 -0700 |
|---|---|---|
| committer | jsmall-nvidia <jsmall@nvidia.com> | 2019-05-22 12:58:19 -0400 |
| commit | 06e1ab6982df097727b74174d5b39a7f27c2dae3 (patch) | |
| tree | b6eff701534108dd863a8948c1fc3732f3db69d6 | |
| parent | 3247174cdb00836435794e3f07daad70bc92b66f (diff) | |
Translate .Load() to imageLoad() for Vulkan (#967)
* Translate .Load() to imageLoad() for Vulkan
We were already emitting calls to `imageLoad()` and `imageStore` when a `RWTexture*` was used with `operator[]`:
```hlsl
RWTexture2D<float> myTex;
...
float value = myTex[xy]; // becomes an imageLoad
myTex[xy] = value; // becomes an imageStore
```
However, we were *not* correctly handling the translation of an explicit `.Load()` operation:
```hlsl
float value = myTex.Load(xy);
```
The `.Load()` operation was being translated to a GLSL `texelFetch` as it would be a for a `Texture2D`, and not to an `imageLoad()` as would make sense for a `RWTexture2D` (which becomes a GLSL `image2D`).
This fix is confined to the stdlib, and is mostly a matter of emitting either `texelFetch` or `imageLoad` as the GLSL function name depending on the "access" of the resource type. It is messy code, but straightforward.
One extra detail was that there had been logic to emit a `, 0` argument in the `texelFetch` calls in the non-read-only case, because `texelFetch` usualy requires an explicit mip-level argument and `.Load()` on a `RWTexture*` doesn't recieve an LOD parameter. This is a non-issue now that we are calling `imageLoad()` instead, because `imageLoad` doesn't need/want the extra argument.
* fixup: change test baseline based on recent GLSL output changes
* fixup: review feedback
| -rw-r--r-- | source/slang/core.meta.slang | 12 | ||||
| -rw-r--r-- | source/slang/core.meta.slang.h | 14 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang | 11 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang.h | 13 | ||||
| -rw-r--r-- | tests/cross-compile/image-load.slang | 19 | ||||
| -rw-r--r-- | tests/cross-compile/image-load.slang.glsl | 23 |
6 files changed, 72 insertions, 20 deletions
diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index a3d73f66a..bc9c9b50a 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -687,6 +687,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) int loadCoordCount = kBaseTextureTypes[tt].coordCount + isArray + (needsMipLevel?1:0); + char const* glslFuncName = (access == SLANG_RESOURCE_ACCESS_READ) ? "texelFetch" : "imageLoad"; + // When translating to GLSL, we need to break apart the `location` argument. // // TODO: this should realy be handled by having this member actually get lowered! @@ -703,19 +705,19 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if (isMultisample) { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, $1, $3)$z\")\n"; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, $1, $3)$z\")\n"; } else { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, "; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, "; if( needsMipLevel ) { sb << "($1)." << kGLSLLoadCoordsSwizzle[loadCoordCount] << ", ($1)." << kGLSLLoadLODSwizzle[loadCoordCount]; } else { - sb << "$1, 0"; + sb << "$1"; } sb << ")$z\")\n"; } @@ -730,12 +732,12 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if (isMultisample) { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetchOffset($0, $0, $1, $2)$z\")\n"; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, $0, $1, $2)$z\")\n"; } else { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, "; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, "; if( needsMipLevel ) { sb << "($1)." << kGLSLLoadCoordsSwizzle[loadCoordCount] << ", ($1)." << kGLSLLoadLODSwizzle[loadCoordCount]; diff --git a/source/slang/core.meta.slang.h b/source/slang/core.meta.slang.h index ba6636ffc..8362a2c4b 100644 --- a/source/slang/core.meta.slang.h +++ b/source/slang/core.meta.slang.h @@ -705,6 +705,8 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) int loadCoordCount = kBaseTextureTypes[tt].coordCount + isArray + (needsMipLevel?1:0); + char const* glslFuncName = (access == SLANG_RESOURCE_ACCESS_READ) ? "texelFetch" : "imageLoad"; + // When translating to GLSL, we need to break apart the `location` argument. // // TODO: this should realy be handled by having this member actually get lowered! @@ -721,19 +723,19 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if (isMultisample) { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, $1, $3)$z\")\n"; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, $1, $3)$z\")\n"; } else { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, "; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, "; if( needsMipLevel ) { sb << "($1)." << kGLSLLoadCoordsSwizzle[loadCoordCount] << ", ($1)." << kGLSLLoadLODSwizzle[loadCoordCount]; } else { - sb << "$1, 0"; + sb << "$1"; } sb << ")$z\")\n"; } @@ -748,12 +750,12 @@ for (int tt = 0; tt < kBaseTextureTypeCount; ++tt) if (isMultisample) { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetchOffset($0, $0, $1, $2)$z\")\n"; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, $0, $1, $2)$z\")\n"; } else { sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"$ctexelFetch($0, "; + sb << "__target_intrinsic(glsl, \"$c" << glslFuncName << "($0, "; if( needsMipLevel ) { sb << "($1)." << kGLSLLoadCoordsSwizzle[loadCoordCount] << ", ($1)." << kGLSLLoadLODSwizzle[loadCoordCount]; @@ -1205,7 +1207,7 @@ for (auto op : binaryOps) sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << "matrix<" << leftType << ",N,M> left, " << rightType << " right);\n"; } } -SLANG_RAW("#line 1190 \"core.meta.slang\"") +SLANG_RAW("#line 1192 \"core.meta.slang\"") SLANG_RAW("\n") SLANG_RAW("\n") SLANG_RAW("// Operators to apply to `enum` types\n") diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 40bd02597..e2d79e073 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -1429,7 +1429,8 @@ static const int kBaseBufferAccessLevelCount = sizeof(kBaseBufferAccessLevels) / for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) { - auto flavor = TextureFlavor::create(TextureFlavor::Shape::ShapeBuffer, kBaseBufferAccessLevels[aa].access).flavor; + auto access = kBaseBufferAccessLevels[aa].access; + auto flavor = TextureFlavor::create(TextureFlavor::Shape::ShapeBuffer, access).flavor; sb << "__generic<T>\n"; sb << "__magic_type(Texture," << int(flavor) << ")\n"; sb << "__intrinsic_type(" << (kIROp_TextureType + (int(flavor) << kIROpMeta_OtherShift)) << ")\n"; @@ -1439,8 +1440,10 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "void GetDimensions(out uint dim);\n"; + char const* glslLoadFuncName = (access == SLANG_RESOURCE_ACCESS_READ) ? "texelFetch" : "imageLoad"; + sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"texelFetch($0, $1)$z\")\n"; + sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, $1)$z\")\n"; sb << "T Load(int location);\n"; sb << "T Load(int location, out uint status);\n"; @@ -1448,9 +1451,9 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "__subscript(uint index) -> T {\n"; sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"texelFetch($0, int($1))$z\") get;\n"; + sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, int($1))$z\") get;\n"; - if (kBaseBufferAccessLevels[aa].access != SLANG_RESOURCE_ACCESS_READ) + if (access != SLANG_RESOURCE_ACCESS_READ) { sb << "ref;\n"; } diff --git a/source/slang/hlsl.meta.slang.h b/source/slang/hlsl.meta.slang.h index b7bbc3985..fa018189c 100644 --- a/source/slang/hlsl.meta.slang.h +++ b/source/slang/hlsl.meta.slang.h @@ -1505,7 +1505,8 @@ static const int kBaseBufferAccessLevelCount = sizeof(kBaseBufferAccessLevels) / for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) { - auto flavor = TextureFlavor::create(TextureFlavor::Shape::ShapeBuffer, kBaseBufferAccessLevels[aa].access).flavor; + auto access = kBaseBufferAccessLevels[aa].access; + auto flavor = TextureFlavor::create(TextureFlavor::Shape::ShapeBuffer, access).flavor; sb << "__generic<T>\n"; sb << "__magic_type(Texture," << int(flavor) << ")\n"; sb << "__intrinsic_type(" << (kIROp_TextureType + (int(flavor) << kIROpMeta_OtherShift)) << ")\n"; @@ -1515,8 +1516,10 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "void GetDimensions(out uint dim);\n"; + char const* glslLoadFuncName = (access == SLANG_RESOURCE_ACCESS_READ) ? "texelFetch" : "imageLoad"; + sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"texelFetch($0, $1)$z\")\n"; + sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, $1)$z\")\n"; sb << "T Load(int location);\n"; sb << "T Load(int location, out uint status);\n"; @@ -1524,9 +1527,9 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "__subscript(uint index) -> T {\n"; sb << "__glsl_extension(GL_EXT_samplerless_texture_functions)"; - sb << "__target_intrinsic(glsl, \"texelFetch($0, int($1))$z\") get;\n"; + sb << "__target_intrinsic(glsl, \"" << glslLoadFuncName << "($0, int($1))$z\") get;\n"; - if (kBaseBufferAccessLevels[aa].access != SLANG_RESOURCE_ACCESS_READ) + if (access != SLANG_RESOURCE_ACCESS_READ) { sb << "ref;\n"; } @@ -1535,7 +1538,7 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa) sb << "};\n"; } -SLANG_RAW("#line 1462 \"hlsl.meta.slang\"") +SLANG_RAW("#line 1465 \"hlsl.meta.slang\"") SLANG_RAW("\n") SLANG_RAW("\n") SLANG_RAW("\n") diff --git a/tests/cross-compile/image-load.slang b/tests/cross-compile/image-load.slang new file mode 100644 index 000000000..7f125bd5a --- /dev/null +++ b/tests/cross-compile/image-load.slang @@ -0,0 +1,19 @@ +// image-load.slang + +// This test confirms that `.Load()` on a `RWTexure*` +// gets properly converted to a call to `imageLoad` +// and not just `texelFetch` as it would for a `Texture*`. + +//TEST:CROSS_COMPILE:-target spirv-assembly -entry main -stage compute + +struct Params +{ + RWTexture2DArray<float> tex; +} + +ParameterBlock<Params> gParams; + +void main(uint3 tid : SV_DispatchThreadID) +{ + float f = gParams.tex.Load(int3(tid.xy, tid.z)); +} diff --git a/tests/cross-compile/image-load.slang.glsl b/tests/cross-compile/image-load.slang.glsl new file mode 100644 index 000000000..c7233c38c --- /dev/null +++ b/tests/cross-compile/image-load.slang.glsl @@ -0,0 +1,23 @@ +// image-load.slang.glsl +//TEST_IGNORE_FILE: + +#version 450 + +#extension GL_EXT_samplerless_texture_functions : require + +layout(r32f) +layout(binding = 0) +uniform image2DArray gParams_tex_0; + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + +void main() +{ + float _S1 = imageLoad( + gParams_tex_0, + ivec3( + ivec2(gl_GlobalInvocationID.xy), + int(gl_GlobalInvocationID.z))).x; + + return; +} |
