Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLSL: Support translating non-writable images to Texture2D instead of RWTexture2D? #1306

Closed
Jiawei-Shao opened this issue Apr 2, 2020 · 0 comments · Fixed by #1308
Closed
Labels
enhancement New feature to SPIRV-Cross is desired.

Comments

@Jiawei-Shao
Copy link

Hi SPIRV-Cross developers:

I am a Chromium/Dawn developer and now I am meeting several issues in SPIR-V Cross when I am implementing storage textures in Dawn.

Now readonly-storage-texture and writeonly-storage-texture have been supported as WebGPU binding types, on D3D12. readonly-storage-texture is intended to match SRVs, and writeonly-storage-texture is intended to match UAVs.

But in SPIRV-Cross, I find whether I declare DecorationNonReadable or DecorationNonWritable on an image variable, the image variable is always translated into RWTexture2D when it is translated into HLSL. In D3D, "typed UAV loads" are not allowed on all texture formats (in fact only R32_FLOAT, R32_UINT and R32_SINT are always supported on all feature level 11_0 hardwares) so we prefer the non-writable images to be translated into read-only Texture2D in WebGPU.

Could you consider supporting a way in SPIRV-Cross to translate the image with DecorationNonWritable into Texture2D rather than RWTexture2D?

Here is an example. Considering the following GLSL shader:

// 1.comp
#version 450
layout(set = 0, binding = 0, rgba8) uniform highp readonly image2D image0;
layout(set = 0, binding = 1, rgba8) uniform highp writeonly image2D image1;
void main() {
    vec4 pixel = imageLoad(image0, ivec2(gl_LocalInvocationID));
    imageStore(image1, ivec2(gl_LocalInvocationID), pixel);
}

Here is the translation result of 1.comp with glslang. You can see NonWritable and NonReadable has been correctly translated in the result.

// 1.spv
// glslang_validator.exe -H -V -o 1.spv 1.comp
// Module Version 10000
// Generated by (magic number): 80008
// Id's are bound by 35

                              Capability Shader
               1:             ExtInstImport  "GLSL.std.450"
                              MemoryModel Logical GLSL450
                              EntryPoint GLCompute 4  "main" 17
                              ExecutionMode 4 LocalSize 1 1 1
                              Source GLSL 450
                              Name 4  "main"
                              Name 9  "pixel"
                              Name 12  "image0"
                              Name 17  "gl_LocalInvocationID"
                              Name 27  "image1"
                              Decorate 12(image0) DescriptorSet 0
                              Decorate 12(image0) Binding 0
                              Decorate 12(image0) NonWritable
                              Decorate 17(gl_LocalInvocationID) BuiltIn LocalInvocationId
                              Decorate 27(image1) DescriptorSet 0
                              Decorate 27(image1) Binding 1
                              Decorate 27(image1) NonReadable
               2:             TypeVoid
               3:             TypeFunction 2
               6:             TypeFloat 32
               7:             TypeVector 6(float) 4
               8:             TypePointer Function 7(fvec4)
              10:             TypeImage 6(float) 2D nonsampled format:Rgba8
              11:             TypePointer UniformConstant 10
      12(image0):     11(ptr) Variable UniformConstant
              14:             TypeInt 32 0
              15:             TypeVector 14(int) 3
              16:             TypePointer Input 15(ivec3)
17(gl_LocalInvocationID):     16(ptr) Variable Input
              19:             TypeInt 32 1
              20:             TypeVector 19(int) 3
              22:             TypeVector 19(int) 2
      27(image1):     11(ptr) Variable UniformConstant
         4(main):           2 Function None 3
               5:             Label
        9(pixel):      8(ptr) Variable Function
              13:          10 Load 12(image0)
              18:   15(ivec3) Load 17(gl_LocalInvocationID)
              21:   20(ivec3) Bitcast 18
              23:     19(int) CompositeExtract 21 0
              24:     19(int) CompositeExtract 21 1
              25:   22(ivec2) CompositeConstruct 23 24
              26:    7(fvec4) ImageRead 13 25
                              Store 9(pixel) 26
              28:          10 Load 27(image1)
              29:   15(ivec3) Load 17(gl_LocalInvocationID)
              30:   20(ivec3) Bitcast 29
              31:     19(int) CompositeExtract 30 0
              32:     19(int) CompositeExtract 30 1
              33:   22(ivec2) CompositeConstruct 31 32
              34:    7(fvec4) Load 9(pixel)
                              ImageWrite 28 33 34
                              Return
                              FunctionEnd

Here is the translation result of translating 1.spv into HLSL shaders. We can see both image0 and image1, and they are all translated into RWTexture2D.

// spirv-cross.exe 1.spv --dump-resources --hlsl --shader-model 50
RWTexture2D<unorm float4> image0 : register(u0);
RWTexture2D<unorm float4> image1 : register(u1);

static uint3 gl_LocalInvocationID;
struct SPIRV_Cross_Input
{
    uint3 gl_LocalInvocationID : SV_GroupThreadID;
};

void comp_main()
{
    float4 pixel = image0[int2(int3(gl_LocalInvocationID).xy)];
    image1[int2(int3(gl_LocalInvocationID).xy)] = pixel;
}

[numthreads(1, 1, 1)]
void main(SPIRV_Cross_Input stage_input)
{
    gl_LocalInvocationID = stage_input.gl_LocalInvocationID;
    comp_main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature to SPIRV-Cross is desired.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants