Browse Source

[spirv] Add translation of RWTexture... types. (#602)

* [spirv] Add translation of RWTexture... types.
* Update documentation for RWTexture types.
* Fix table formatting in doc.
Ehsan 8 years ago
parent
commit
d5592ce089

+ 18 - 13
docs/SPIR-V.rst

@@ -231,19 +231,24 @@ Textures
 `Texture types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509700(v=vs.85).aspx>`_
 are translated into SPIR-V ``OpTypeImage``, with parameters:
 
-====================   ========== ===== ======= == ======= ================ =================
-HLSL Texture Type          Dim    Depth Arrayed MS Sampled  Image Format       Capability
-====================   ========== ===== ======= == ======= ================ =================
-``Texture1D``          ``1D``      0       0    0    1     ``Unknown``
-``Texture2D``          ``2D``      0       0    0    1     ``Unknown``
-``Texture3D``          ``3D``      0       0    0    1     ``Unknown``
-``TextureCube``        ``Cube``    0       0    0    1     ``Unknown``
-``Texture1DArray``     ``1D``      0       1    0    1     ``Unknown``
-``Texture2DArray``     ``2D``      0       1    0    1     ``Unknown``
-``TextureCubeArray``   ``3D``      0       1    0    1     ``Unknown``
-``Buffer<T>``          ``Buffer``  0       0    0    1     Depends on ``T`` ``SampledBuffer``
-``RWBuffer<T>``        ``Buffer``  0       0    0    2     Depends on ``T`` ``SampledBuffer``
-====================   ========== ===== ======= == ======= ================ =================
+======================= ========== ===== ======= == ======= ================ =================
+HLSL Texture Type           Dim    Depth Arrayed MS Sampled  Image Format       Capability
+======================= ========== ===== ======= == ======= ================ =================
+``Texture1D``           ``1D``      0       0    0    1     ``Unknown``
+``Texture2D``           ``2D``      0       0    0    1     ``Unknown``
+``Texture3D``           ``3D``      0       0    0    1     ``Unknown``
+``TextureCube``         ``Cube``    0       0    0    1     ``Unknown``
+``Texture1DArray``      ``1D``      0       1    0    1     ``Unknown``
+``Texture2DArray``      ``2D``      0       1    0    1     ``Unknown``
+``TextureCubeArray``    ``3D``      0       1    0    1     ``Unknown``
+``Buffer<T>``           ``Buffer``  0       0    0    1     Depends on ``T`` ``SampledBuffer``
+``RWBuffer<T>``         ``Buffer``  0       0    0    2     Depends on ``T`` ``SampledBuffer``
+``RWTexture1D<T>``      ``1D``      0       0    0    2     Depends on ``T``
+``RWTexture2D<T>``      ``2D``      0       0    0    2     Depends on ``T``
+``RWTexture3D<T>``      ``3D``      0       0    0    2     Depends on ``T``
+``RWTexture1DArray<T>`` ``1D``      0       1    0    2     Depends on ``T``
+``RWTexture2DArray<T>`` ``2D``      0       1    0    2     Depends on ``T``
+======================= ========== ===== ======= == ======= ================ =================
 
 The meanings of the headers in the above table is explained in ``OpTypeImage``
 of the SPIR-V spec.

+ 2 - 0
tools/clang/lib/SPIRV/ModuleBuilder.cpp

@@ -677,6 +677,8 @@ uint32_t ModuleBuilder::getImageType(uint32_t sampledType, spv::Dim dim,
     requireCapability(spv::Capability::StorageImageExtendedFormats);
   }
 
+  if (dim == spv::Dim::Dim1D)
+    requireCapability(spv::Capability::Sampled1D);
   if (dim == spv::Dim::Buffer)
     requireCapability(spv::Capability::SampledBuffer);
 

+ 13 - 3
tools/clang/lib/SPIRV/TypeTranslator.cpp

@@ -444,7 +444,6 @@ uint32_t TypeTranslator::translateResourceType(QualType type, LayoutRule rule) {
   { // Texture types
     spv::Dim dim = {};
     bool isArray = {};
-
     if ((dim = spv::Dim::Dim1D, isArray = false, name == "Texture1D") ||
         (dim = spv::Dim::Dim2D, isArray = false, name == "Texture2D") ||
         (dim = spv::Dim::Dim3D, isArray = false, name == "Texture3D") ||
@@ -453,12 +452,23 @@ uint32_t TypeTranslator::translateResourceType(QualType type, LayoutRule rule) {
         (dim = spv::Dim::Dim2D, isArray = true, name == "Texture2DArray") ||
         // There is no Texture3DArray
         (dim = spv::Dim::Cube, isArray = true, name == "TextureCubeArray")) {
-      if (dim == spv::Dim::Dim1D)
-        theBuilder.requireCapability(spv::Capability::Sampled1D);
       const auto sampledType = hlsl::GetHLSLResourceResultType(type);
       return theBuilder.getImageType(translateType(getElementType(sampledType)),
                                      dim, /*depth*/ 0, isArray);
     }
+
+    // There is no RWTexture3DArray
+    if ((dim = spv::Dim::Dim1D, isArray = false, name == "RWTexture1D") ||
+        (dim = spv::Dim::Dim2D, isArray = false, name == "RWTexture2D") ||
+        (dim = spv::Dim::Dim3D, isArray = false, name == "RWTexture3D") ||
+        (dim = spv::Dim::Dim1D, isArray = true, name == "RWTexture1DArray") ||
+        (dim = spv::Dim::Dim2D, isArray = true, name == "RWTexture2DArray")) {
+      const auto sampledType = hlsl::GetHLSLResourceResultType(type);
+      const auto format = translateSampledTypeToImageFormat(sampledType);
+      return theBuilder.getImageType(translateType(getElementType(sampledType)),
+                                     dim, /*depth*/ 0, isArray, /*MS*/ 0,
+                                     /*Sampled*/ 2u, format);
+    }
   }
 
   // Sampler types

+ 43 - 0
tools/clang/test/CodeGenSPIRV/type.rwtexture.hlsl

@@ -0,0 +1,43 @@
+// Run: %dxc -T vs_6_0 -E main
+
+// CHECK: %type_1d_image = OpTypeImage %int 1D 0 0 0 2 R32i
+// CHECK: %_ptr_UniformConstant_type_1d_image = OpTypePointer UniformConstant %type_1d_image
+// CHECK: %type_2d_image = OpTypeImage %uint 2D 0 0 0 2 Rg32ui
+// CHECK: %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
+// CHECK: %type_3d_image = OpTypeImage %float 3D 0 0 0 2 Rgba32f
+// CHECK: %_ptr_UniformConstant_type_3d_image = OpTypePointer UniformConstant %type_3d_image
+// CHECK: %type_1d_image_array = OpTypeImage %int 1D 0 1 0 2 R32i
+// CHECK: %_ptr_UniformConstant_type_1d_image_array = OpTypePointer UniformConstant %type_1d_image_array
+// CHECK: %type_2d_image_array = OpTypeImage %uint 2D 0 1 0 2 Rg32ui
+// CHECK: %_ptr_UniformConstant_type_2d_image_array = OpTypePointer UniformConstant %type_2d_image_array
+// CHECK: %type_1d_image_array_0 = OpTypeImage %float 1D 0 1 0 2 Rgba32f
+// CHECK: %_ptr_UniformConstant_type_1d_image_array_0 = OpTypePointer UniformConstant %type_1d_image_array_0
+// CHECK: %type_2d_image_array_0 = OpTypeImage %float 2D 0 1 0 2 Rgba32f
+// CHECK: %_ptr_UniformConstant_type_2d_image_array_0 = OpTypePointer UniformConstant %type_2d_image_array_0
+
+
+// CHECK: %t1 = OpVariable %_ptr_UniformConstant_type_1d_image UniformConstant
+RWTexture1D   <int>    t1 ;
+
+// CHECK: %t2 = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
+RWTexture2D   <uint2>  t2 ;
+
+// CHECK: %t3 = OpVariable %_ptr_UniformConstant_type_3d_image UniformConstant
+RWTexture3D   <float3> t3 ;
+
+// CHECK: %t4 = OpVariable %_ptr_UniformConstant_type_3d_image UniformConstant
+RWTexture3D   <float4> t4 ;
+
+// CHECK: %t5 = OpVariable %_ptr_UniformConstant_type_1d_image_array UniformConstant
+RWTexture1DArray<int>    t5;
+
+// CHECK: %t6 = OpVariable %_ptr_UniformConstant_type_2d_image_array UniformConstant
+RWTexture2DArray<uint2>  t6;
+
+// CHECK: %t7 = OpVariable %_ptr_UniformConstant_type_1d_image_array_0 UniformConstant
+RWTexture1DArray<float3> t7;
+
+// CHECK: %t8 = OpVariable %_ptr_UniformConstant_type_2d_image_array_0 UniformConstant
+RWTexture2DArray<float4> t8;
+
+void main() {}

+ 1 - 0
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -47,6 +47,7 @@ TEST_F(FileTest, ArrayTypes) { runFileTest("type.array.hlsl"); }
 TEST_F(FileTest, TypedefTypes) { runFileTest("type.typedef.hlsl"); }
 TEST_F(FileTest, SamplerTypes) { runFileTest("type.sampler.hlsl"); }
 TEST_F(FileTest, TextureTypes) { runFileTest("type.texture.hlsl"); }
+TEST_F(FileTest, RWTextureTypes) { runFileTest("type.rwtexture.hlsl"); }
 TEST_F(FileTest, BufferType) { runFileTest("type.buffer.hlsl"); }
 TEST_F(FileTest, CBufferType) { runFileTest("type.cbuffer.hlsl"); }
 TEST_F(FileTest, ConstantBufferType) {