summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2019-04-08 11:09:03 -0700
committerRobert Stepinski <rob.stepinski@gmail.com>2019-04-08 14:09:03 -0400
commitdc54f1dd1b694b087816857a791e9d37dc25de6d (patch)
treeb611249a5fb5d01dbd765d67ac646fa12b495800 /tests
parentc9d06fe1f46a21c66c378ab9771495d6344db49c (diff)
Add better control over image formats for GLSL/SPIR-V targets (#939)
* Add better control over image formats for GLSL/SPIR-V targets Currently Slang emits GLSL code assuming all R/W images need to have explicit formats, and thus we try to infer a format from the element type of the image. E.g., given a `RWTexture2D<half4>` we might infer that a qualifier of `layout(rgba16f)` should be used. This strategy has two notable shortcomings: * Sometimes the user will want a format that doesn't match an existing HLSL type. E.g., if they want the equivalent of `layout(r11f_g11f_b10f)`, then what should they put in their `RWTexture2D<...>` to make the inference do what they need? * Sometimes the user knows that they don't need to specify a format *at all*, because using the `GL_EXT_shader_image_load_formatted` extension, they can still perform non-atomic load/store on images with no format specified in the SPIR-V. This change adds two features directed at these challenges. First, we add an explicit `[format(...)]` attribute that can be used to specify an explicit image format, including ones that don't match any HLSL type. An example of using this new attribute is: ```hlsl [format("r11f_g11f_b10f")] RWTexture2D<float3> myImage; ``` For simplicity in initial bring-up, the new formats all use the same naming as formats in GLSL (this should make it easy for a programmer who knows what they expect to get in the GLSL output). We can change the naming convention for formats at a later time, so long as we keep these existing names in as a compatibility feature. Note that this is *not* given a `vk::` prefix since the attribute should signal the programmer's intent to provide an image with that format on *all* targets (although only some targets might act on that information). Also note that the attribute takes a string (`[format("rgba8")`) instead of a bare identifier (`[format(rgba8)]`) because this is consistent with the existing convention for attributes in HLSL. When `[format(...)]` is left off, the default compiler behavior will still be to infer a format, but this behavior can be overidden for a single image using an explicit format of `"unknown"`: ```hlsl [format("unknown")] RWTexture2D<float4> mysteryMachine; ``` The second new feature is that if a user knows they are coding for a GPU that supports the `"unknown"` format in all non-atomic cases, then they can opt into making that the default for images without an explicit `[format(...)]`, using the new `-default-image-format-unknown` command-line option for `slangc`. The new test case included with this change confirms that we correctly see the explicit formats in the output GLSL and *no* formats for images without explicit `[format(...)]` when using the new command-line option. The test stresses images declared at global scope, in parameter blocks, and in entry-point parameter lists, to try and make sure that all the relevant IR passes in the compiler preserve the format information. * fixup: missing file
Diffstat (limited to 'tests')
-rw-r--r--tests/cross-compile/unknown-image-format.slang56
-rw-r--r--tests/cross-compile/unknown-image-format.slang.glsl65
2 files changed, 121 insertions, 0 deletions
diff --git a/tests/cross-compile/unknown-image-format.slang b/tests/cross-compile/unknown-image-format.slang
new file mode 100644
index 000000000..bf937c5fa
--- /dev/null
+++ b/tests/cross-compile/unknown-image-format.slang
@@ -0,0 +1,56 @@
+// unknown-image-format.slang
+
+//TEST:CROSS_COMPILE:-target spirv-assembly -entry main -stage fragment -default-image-format-unknown
+
+// Ensure that we can emit R/W images with an unknown format, when required.
+
+// Global Scope:
+
+RWTexture2D<float> gNoFormat;
+
+[format("r32f")]
+RWTexture2D<float> gExplicitFormat;
+
+// Nested in a Parameter Block
+
+struct PB
+{
+ RWTexture2D<float4> noFormat;
+
+ [format("rgba8")]
+ RWTexture2D<float4> explicitFormat;
+}
+ParameterBlock<PB> gBlock;
+
+cbuffer C
+{
+ uint2 index;
+}
+
+float4 main(
+
+ // In entry-point parameter list
+
+ uniform RWTexture2D<float4> noFormat,
+
+ [format("rgba16f")]
+ uniform RWTexture2D<float4> explicitFormat
+ ) : SV_Target
+{
+ float4 result = 0;
+
+#define USE(NAME) result += NAME[index]
+
+ USE(gNoFormat);
+ USE(gExplicitFormat);
+
+ USE(gBlock.noFormat);
+ USE(gBlock.explicitFormat);
+
+ USE(noFormat);
+ USE(explicitFormat);
+
+#undef USE
+
+ return result;
+}
diff --git a/tests/cross-compile/unknown-image-format.slang.glsl b/tests/cross-compile/unknown-image-format.slang.glsl
new file mode 100644
index 000000000..9995bba72
--- /dev/null
+++ b/tests/cross-compile/unknown-image-format.slang.glsl
@@ -0,0 +1,65 @@
+// unknown-image-format.slang.glsl
+//TEST_IGNORE_FILE:
+
+#version 450
+#extension GL_EXT_shader_image_load_formatted : require
+
+struct SLANG_ParameterGroup_C_0
+{
+ uvec2 index_0;
+};
+
+layout(binding = 2)
+layout(std140) uniform _S1
+{
+ SLANG_ParameterGroup_C_0 _data;
+} C_0;
+
+layout(binding = 0)
+uniform image2D gNoFormat_0;
+
+layout(r32f)
+layout(binding = 1)
+uniform image2D gExplicitFormat_0;
+
+layout(binding = 0, set = 1)
+uniform image2D gBlock_noFormat_0;
+
+layout(rgba8)
+layout(binding = 1, set = 1)
+uniform image2D gBlock_explicitFormat_0;
+
+layout(binding = 3)
+uniform image2D _S2;
+
+layout(rgba16f)
+layout(binding = 4)
+uniform image2D _S3;
+
+layout(location = 0)
+out vec4 _S4;
+
+void main()
+{
+ const vec4 result_0 = vec4(0);
+
+ float _S5 = (imageLoad((gNoFormat_0), ivec2((C_0._data.index_0))).x);
+ vec4 result_1 = result_0 + _S5;
+
+ float _S6 = (imageLoad((gExplicitFormat_0), ivec2((C_0._data.index_0))).x);
+ vec4 result_2 = result_1 + _S6;
+
+ vec4 _S7 = (imageLoad((gBlock_noFormat_0), ivec2((C_0._data.index_0))));
+ vec4 result_3 = result_2 + _S7;
+
+ vec4 _S8 = (imageLoad((gBlock_explicitFormat_0), ivec2((C_0._data.index_0))));
+ vec4 result_4 = result_3 + _S8;
+
+ vec4 _S9 = (imageLoad((_S2), ivec2((C_0._data.index_0))));
+ vec4 result_5 = result_4 + _S9;
+
+ vec4 _S10 = (imageLoad((_S3), ivec2((C_0._data.index_0))));
+ _S4 = result_5 + _S10;
+
+ return;
+}