Repository navigation
Description
Title
global struct variables containing only resources get placed in uniform buffer.
Global instances of structs with resource members will be put in the default uniform buffer and their resource members will not get bindings assigned to them.
Functional impact
This breaks compatibility with DXC's DXIL output which does bind the resources. This prevents us from easily recreating the legacy sampler2D type and its methods by using a struct with Texture and SamplerState members as seen in the attached example.
Another issue is with being able to put resources in constant buffers in the first place as this, in my understanding, does not have any meaning in Vulkan since you cannot write to uniform buffers from the shader or meaningfully set them through the API. This results in assertions in the SPIRV-Tools validator when it tries to get the sizes and alignments of the resources in the uniform buffers during validation, however, these are not reported to the user which looks like a separate issue.
Minimal repro steps
- Compile the following snippet with dxc.exe -T ps_6_0 -E PSMain -spirv repro_shader.txt
struct sampler2D {
Texture2D t;
SamplerState s;
};
float4 tex2D(sampler2D x, float2 v) { return x.t.Sample(x.s, v); }
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D g_CombinedTextureSampler;
float4 PSMain(v2f i) : SV_Target
{
return tex2D(g_CombinedTextureSampler, i.uv);
}
to get the output SPIR-V
; SPIR-V
; Version: 1.0
; Generator: Google spiregg; 0
; Bound: 31
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %PSMain "PSMain" %gl_FragCoord %in_var_TEXCOORD0 %out_var_SV_Target
OpExecutionMode %PSMain OriginUpperLeft
OpSource HLSL 600
OpName %type__Globals "type.$Globals"
OpMemberName %type__Globals 0 "g_CombinedTextureSampler"
OpName %sampler2D "sampler2D"
OpMemberName %sampler2D 0 "t"
OpMemberName %sampler2D 1 "s"
OpName %type_2d_image "type.2d.image"
OpName %type_sampler "type.sampler"
OpName %_Globals "$Globals"
OpName %in_var_TEXCOORD0 "in.var.TEXCOORD0"
OpName %out_var_SV_Target "out.var.SV_Target"
OpName %PSMain "PSMain"
OpName %type_sampled_image "type.sampled.image"
OpDecorate %gl_FragCoord BuiltIn FragCoord
OpDecorate %in_var_TEXCOORD0 Location 0
OpDecorate %out_var_SV_Target Location 0
OpDecorate %_Globals DescriptorSet 0
OpDecorate %_Globals Binding 0
OpMemberDecorate %sampler2D 0 Offset 0
OpMemberDecorate %sampler2D 1 Offset 32
OpMemberDecorate %type__Globals 0 Offset 0
OpDecorate %type__Globals Block
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%float = OpTypeFloat 32
%type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
%type_sampler = OpTypeSampler
%sampler2D = OpTypeStruct %type_2d_image %type_sampler
%type__Globals = OpTypeStruct %sampler2D
%_ptr_Uniform_type__Globals = OpTypePointer Uniform %type__Globals
%v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
%v2float = OpTypeVector %float 2
%_ptr_Input_v2float = OpTypePointer Input %v2float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%void = OpTypeVoid
%21 = OpTypeFunction %void
%_ptr_Uniform_sampler2D = OpTypePointer Uniform %sampler2D
%type_sampled_image = OpTypeSampledImage %type_2d_image
%_Globals = OpVariable %_ptr_Uniform_type__Globals Uniform
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
%in_var_TEXCOORD0 = OpVariable %_ptr_Input_v2float Input
%out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
%PSMain = OpFunction %void None %21
%23 = OpLabel
%24 = OpLoad %v2float %in_var_TEXCOORD0
%25 = OpAccessChain %_ptr_Uniform_sampler2D %_Globals %int_0
%26 = OpLoad %sampler2D %25
%27 = OpCompositeExtract %type_2d_image %26 0
%28 = OpCompositeExtract %type_sampler %26 1
%29 = OpSampledImage %type_sampled_image %27 %28
%30 = OpImageSampleImplicitLod %v4float %29 %24 None
OpStore %out_var_SV_Target %30
OpReturn
OpFunctionEnd
Expected result
The struct instance g_CombinedTextureSampler gets omitted from the uniform buffer as it only has resources in it and the resources get assigned bindings as if they were declared in the global scope directly so it's consistent with DXIL output.
Actual result
g_CompinedTextureSampler gets placed in the default uniform buffer. The texture and sampler do not get binding locations assigned to them. Reflection and validation libraries seem to explicitly or silently fail on this SPIR-V as they don't expect opaque types in uniform buffers, e.g. http://shader-playground.timjones.io/b5b72b052d0c030c8feaf7af8669f018, or SPIRV-Tools' validator as seen in this assertion stack from this shader's compilation
spirv_val_assertion_stack.txt
Further technical details
DXC version used corresponds to 656308d
Activity
jaebaek commentedon Feb 19, 2020
Lukas Taparauskas (@Dredhog) Thank you for reporting this issue. I will take a look.
aras-p commentedon May 19, 2020
Any plans on fixing this? Right now this is the most important blocking issue we see at Unity in adopting DXC for Vulkan/Metal.
ehsannas commentedon May 19, 2020
Hi folks, sorry for the delay. Aras Pranckevičius (@aras-p) thanks for the heads up. I'm upping the priority for this.
ehsannas commentedon Jun 2, 2020
Just an update: I am working on this. it's complicated: what if the struct contains both resources and non-resources... One approach would be to:
But it seems that this is not the approach taken in the DXIL route. Looking at DXIL in such cases (mix of resource and non-resource in the struct), the resources are placed in the $Globals cbuffer while also getting a dset/binding assignment.
http://shader-playground.timjones.io/4e8dc4ce16087b07def732e2cecc9d62
ehsannas commentedon Jun 2, 2020
It looks like for cases where we have a mix, the DXIL and SPIR-V behavior are similar. It's only for the case of resource-only struct that we have a different behavior (reading the original issue more carefully, I now realize this was highlighted already :) )Sorry; that statement was not accurate. In both cases (case 1: a global structure with only resources in it. case 2: a global struct with both resource and non-resource variables in it) the SPIR-V and DXIL backend behave differently.
aras-p commentedon Jun 3, 2020
Yeah our use case is literally just this: there's a struct with only resources (texture + sampler), and it's used as a global variable ($Globals cbuffer effectively). We want that to not be emitted as part of the constant buffer in the resulting SPIR-V.
Dredhog commentedon Jun 3, 2020
Ehsan (@ehsannas) you can see all types we want to emulate here on the Unity Graphics GitHub, these already work with DXIL. We will only define instances of these structs as global variables as Aras Pranckevičius (@aras-p) mentioned. The cases where there are also non-resource types in the struct are "technically" an issue in the SPIR-V backend, but they don't concern/block us ATM. The primary issue is that these resources don't get bindings assigned. The secondary concern is that they end up in the global cbuffer which triggers errors in SPIRV-Cross. This could potentially hinder translation to Metal and might not work on some drivers (speculating here).
ehsannas commentedon Jun 3, 2020
Thanks for the clarification folks. I'll keep at it and will provide updates.
ehsannas commentedon Jun 30, 2020
Aras Pranckevičius (@aras-p) Lukas Taparauskas (@Dredhog) I have just landed a change that should unblock you. This change supports your usecases (global structures containing samplers and textures). Please give it a try and let me know if you have any feedback. Thanks!
What's still not covered are:
I will open a separate issue for these.
Dredhog commentedon Jul 1, 2020
Ehsan (@ehsannas) sounds great, much appreciated!
Currently on vacation so I'll get back to you with feedback in 2/3 weeks if there are issues :)