Skip to content

[SPIR-V] Global struct variables containing only resources fail validation with '-O0' optimization level #3100

Closed

Description

Hi, it seems that the fix to #2695 doesn't work when optimization is disabled.

Functional impact

The shader doesn't compile. Cannot use the unoptimized version of the shader for debugging purposes when shader uses the legacy combined texture sampler emulation workaround mentioned in #2695

Minimal repro steps

The repro source is the same:

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);
}

Compiling with opt. level 0: dxc.exe -T ps_6_0 -E PSMain repro_shader.txt -spirv -O0 results in error:

fatal error: generated SPIR-V is invalid: UniformConstant OpVariable <id> '8[%g_CombinedTextureSampler]' has illegal type.
From Vulkan spec, section 14.5.2: 
Variables identified with the UniformConstant storage class are used only as handles to refer to opaque resources. Such variables must be typed as OpTypeImage, OpTypeSampler, OpTypeSampledImage, OpTypeAccelerationStructureNV, OpTypeAccelerationStructureKHR, OpTypeRayQueryProvisionalKHR, or an array of one of these types.
  %g_CombinedTextureSampler = OpVariable %_ptr_UniformConstant_sampler2D UniformConstant

note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

Further technical details

Used latest DXC on Windows built from 530958e

Activity

  1. added
    spirv
    on Aug 27, 2020
  2. ehsannas commented on Nov 12, 2020

    @ehsannas
    Contributor

    This is expected I'm afraid. DXC relies on SPIRV-Tools (spirv-opt in particular) to properly handle these structures. What essentially happens is that spirv-opt "flattens" these structures and resolves all references to their members as standalone resources with their own dset/binding. So, sadly as it stands today, disabling spirv-opt will prevent this from working.

  3. ehsannas commented on Nov 13, 2020

    @ehsannas
    Contributor

    I just realized that we can modify the interaction between DXC and spirv-opt such that we run the necessary spirv-opt pass for such cases as part of "legalization" rather than "optimization" (which sounds like a better fit anyway, as without this the result is not valid/legal).

    I've been thinking about whether moving this pass earlier in the flow could have any unforeseen consequences, but I can't think of any. Our testing is with pretty lightweight shaders, and they all pass. So I'm going to post a PR for it.

  4. ehsannas commented on Nov 13, 2020

    @ehsannas
    Contributor

    Posted the PR. Lukas Taparauskas (@Dredhog) Please let me know if you face any issues with this. Thanks!

  5. Dredhog commented on Nov 16, 2020

    @Dredhog
    Author

    Great news Ehsan (@ehsannas), I'll try it out!

  6. Dredhog commented on Nov 17, 2020

    @Dredhog
    Author

    Seems to be fixed, didn't find any regressions!

  7. ehsannas commented on Nov 17, 2020

    @ehsannas
    Contributor

    Awesome! Thanks for checking 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    spirv

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions