From f9d217b0e90085f56982a166159e38ab4e0dc825 Mon Sep 17 00:00:00 2001 From: Jiawei Shao Date: Tue, 23 Oct 2018 11:08:20 +0000 Subject: [PATCH] Implement creating texture view with descriptor on Vulkan This patch implements creating a texture view with given texture view descriptor on Vulkan back-ends. This patch also updates TextureViewTests to test various mipmap levels and adds several tests to cover all added features. BUG=dawn:16 TEST=dawn_end2end_tests Change-Id: I602e5a076e4f717f555cb9a9ef98d5dfceadbe81 Reviewed-on: https://dawn-review.googlesource.com/c/1880 Commit-Queue: Corentin Wallez Reviewed-by: Corentin Wallez --- src/dawn_native/vulkan/TextureVk.cpp | 20 +-- src/tests/end2end/TextureViewTests.cpp | 194 +++++++++++++++++++++---- 2 files changed, 179 insertions(+), 35 deletions(-) diff --git a/src/dawn_native/vulkan/TextureVk.cpp b/src/dawn_native/vulkan/TextureVk.cpp index 0f523adc26..f486dc1aa4 100644 --- a/src/dawn_native/vulkan/TextureVk.cpp +++ b/src/dawn_native/vulkan/TextureVk.cpp @@ -34,10 +34,12 @@ namespace dawn_native { namespace vulkan { // Converts an Dawn texture dimension to a Vulkan image view type. // Contrary to image types, image view types include arrayness and cubemapness - VkImageViewType VulkanImageViewType(dawn::TextureDimension dimension) { + VkImageViewType VulkanImageViewType(dawn::TextureViewDimension dimension) { switch (dimension) { - case dawn::TextureDimension::e2D: + case dawn::TextureViewDimension::e2D: return VK_IMAGE_VIEW_TYPE_2D; + case dawn::TextureViewDimension::e2DArray: + return VK_IMAGE_VIEW_TYPE_2D_ARRAY; default: UNREACHABLE(); } @@ -356,15 +358,15 @@ namespace dawn_native { namespace vulkan { createInfo.pNext = nullptr; createInfo.flags = 0; createInfo.image = ToBackend(GetTexture())->GetHandle(); - createInfo.viewType = VulkanImageViewType(GetTexture()->GetDimension()); - createInfo.format = VulkanImageFormat(GetTexture()->GetFormat()); + createInfo.viewType = VulkanImageViewType(descriptor->dimension); + createInfo.format = VulkanImageFormat(descriptor->format); createInfo.components = VkComponentMapping{VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A}; - createInfo.subresourceRange.aspectMask = VulkanAspectMask(GetTexture()->GetFormat()); - createInfo.subresourceRange.baseMipLevel = 0; - createInfo.subresourceRange.levelCount = GetTexture()->GetNumMipLevels(); - createInfo.subresourceRange.baseArrayLayer = 0; - createInfo.subresourceRange.layerCount = GetTexture()->GetArrayLayers(); + createInfo.subresourceRange.aspectMask = VulkanAspectMask(descriptor->format); + createInfo.subresourceRange.baseMipLevel = descriptor->baseMipLevel; + createInfo.subresourceRange.levelCount = descriptor->levelCount; + createInfo.subresourceRange.baseArrayLayer = descriptor->baseArrayLayer; + createInfo.subresourceRange.layerCount = descriptor->layerCount; if (device->fn.CreateImageView(device->GetVkDevice(), &createInfo, nullptr, &mHandle) != VK_SUCCESS) { diff --git a/src/tests/end2end/TextureViewTests.cpp b/src/tests/end2end/TextureViewTests.cpp index d645e45b89..8e3abd80ad 100644 --- a/src/tests/end2end/TextureViewTests.cpp +++ b/src/tests/end2end/TextureViewTests.cpp @@ -22,6 +22,12 @@ constexpr static unsigned int kRTSize = 64; class TextureViewTest : public DawnTest { protected: + // Generates an arbitrary pixel value per-layer-per-level, used for the "actual" uploaded + // textures and the "expected" results. + static int GenerateTestPixelValue(uint32_t layer, uint32_t level) { + return static_cast(level * 10) + static_cast(layer + 1); + } + void SetUp() override { DawnTest::SetUp(); @@ -61,39 +67,63 @@ protected: )"); } - void initTexture(uint32_t layerCount) { - ASSERT(layerCount > 0); + void initTexture(uint32_t layerCount, uint32_t levelCount) { + ASSERT(layerCount > 0 && levelCount > 0); + + constexpr dawn::TextureFormat kFormat = dawn::TextureFormat::R8G8B8A8Unorm; + + const uint32_t textureWidthLevel0 = 1 << levelCount; + const uint32_t textureHeightLevel0 = 1 << levelCount; dawn::TextureDescriptor descriptor; descriptor.dimension = dawn::TextureDimension::e2D; - descriptor.size.width = 2; - descriptor.size.height = 2; + descriptor.size.width = textureWidthLevel0; + descriptor.size.height = textureHeightLevel0; descriptor.size.depth = 1; descriptor.arrayLayer = layerCount; - descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm; - descriptor.mipLevel = 1; + descriptor.format = kFormat; + descriptor.mipLevel = levelCount; descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled; mTexture = device.CreateTexture(&descriptor); - // Create a 2x2 checkerboard texture, with black in the top left and bottom right corners. - constexpr uint32_t kRowPixels = kTextureRowPitchAlignment / sizeof(RGBA8); + mDefaultTextureViewDescriptor.nextInChain = nullptr; + mDefaultTextureViewDescriptor.dimension = dawn::TextureViewDimension::e2DArray; + mDefaultTextureViewDescriptor.format = kFormat; + mDefaultTextureViewDescriptor.baseMipLevel = 0; + mDefaultTextureViewDescriptor.levelCount = levelCount; + mDefaultTextureViewDescriptor.baseArrayLayer = 0; + mDefaultTextureViewDescriptor.layerCount = layerCount; + + // Create a texture with pixel = (0, 0, 0, level * 10 + layer + 1) at level `level` and + // layer `layer`. + static_assert((kTextureRowPitchAlignment % sizeof(RGBA8)) == 0, + "Texture row pitch alignment must be a multiple of sizeof(RGBA8)."); + constexpr uint32_t kPixelsPerRowPitch = kTextureRowPitchAlignment / sizeof(RGBA8); + ASSERT_LE(textureWidthLevel0, kPixelsPerRowPitch); dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder(); for (uint32_t layer = 0; layer < layerCount; ++layer) { - RGBA8 data[kRowPixels * 2]; - int pixelValue = static_cast(layer); - data[0] = data[kRowPixels + 1] = RGBA8(0, 0, 0, pixelValue); - data[1] = data[kRowPixels] = RGBA8(pixelValue, pixelValue, pixelValue, pixelValue);; - dawn::Buffer stagingBuffer = utils::CreateBufferFromData( - device, data, sizeof(data), dawn::BufferUsageBit::TransferSrc); - builder.CopyBufferToTexture( - stagingBuffer, 0, 256, mTexture, 0, 0, 0, 2, 2, 1, 0, layer); + for (uint32_t level = 0; level < levelCount; ++level) { + const uint32_t texWidth = textureWidthLevel0 >> level; + const uint32_t texHeight = textureHeightLevel0 >> level; + + const int pixelValue = GenerateTestPixelValue(layer, level); + + constexpr uint32_t kPaddedTexWidth = kPixelsPerRowPitch; + std::vector data(kPaddedTexWidth * texHeight, RGBA8(0, 0, 0, pixelValue)); + dawn::Buffer stagingBuffer = utils::CreateBufferFromData( + device, data.data(), data.size() * sizeof(RGBA8), + dawn::BufferUsageBit::TransferSrc); + builder.CopyBufferToTexture( + stagingBuffer, 0, kTextureRowPitchAlignment, mTexture, 0, 0, 0, texWidth, + texHeight, 1, level, layer); + } } dawn::CommandBuffer copy = builder.GetResult(); queue.Submit(1, ©); } - void Test(const dawn::TextureView &textureView, const char* fragmentShader, int expected) { + void Verify(const dawn::TextureView &textureView, const char* fragmentShader, int expected) { dawn::BindGroup bindGroup = device.CreateBindGroupBuilder() .SetLayout(mBindGroupLayout) .SetSamplers(0, 1, &mSampler) @@ -122,30 +152,114 @@ protected: dawn::CommandBuffer commands = builder.GetResult(); queue.Submit(1, &commands); - RGBA8 expectedPixel0(0, 0, 0, expected); - RGBA8 expectedPixel1(expected, expected, expected, expected); - EXPECT_PIXEL_RGBA8_EQ(expectedPixel0, mRenderPass.color, 0, 0); - EXPECT_PIXEL_RGBA8_EQ(expectedPixel1, mRenderPass.color, 0, 1); - EXPECT_PIXEL_RGBA8_EQ(expectedPixel1, mRenderPass.color, 1, 0); - EXPECT_PIXEL_RGBA8_EQ(expectedPixel0, mRenderPass.color, 1, 1); + RGBA8 expectedPixel(0, 0, 0, expected); + EXPECT_PIXEL_RGBA8_EQ(expectedPixel, mRenderPass.color, 0, 0); + EXPECT_PIXEL_RGBA8_EQ( + expectedPixel, mRenderPass.color, mRenderPass.width - 1, mRenderPass.height - 1); // TODO(jiawei.shao@intel.com): add tests for 3D textures once Dawn supports 3D textures } + void Texture2DViewTest(uint32_t textureArrayLayers, + uint32_t textureMipLevels, + uint32_t textureViewBaseLayer, + uint32_t textureViewBaseMipLevel) { + // TODO(jiawei.shao@intel.com): support creating texture view with a texture view descriptor + // on D3D12, Metal and OpenGL. + DAWN_SKIP_TEST_IF(IsD3D12() || IsMetal() || IsOpenGL()); + + ASSERT(textureViewBaseLayer < textureArrayLayers); + ASSERT(textureViewBaseMipLevel < textureMipLevels); + + initTexture(textureArrayLayers, textureMipLevels); + + dawn::TextureViewDescriptor descriptor = mDefaultTextureViewDescriptor; + descriptor.dimension = dawn::TextureViewDimension::e2D; + descriptor.baseArrayLayer = textureViewBaseLayer; + descriptor.layerCount = 1; + descriptor.baseMipLevel = textureViewBaseMipLevel; + descriptor.levelCount = 1; + dawn::TextureView textureView = mTexture.CreateTextureView(&descriptor); + + const char* fragmentShader = R"( + #version 450 + layout(set = 0, binding = 0) uniform sampler sampler0; + layout(set = 0, binding = 1) uniform texture2D texture0; + layout(location = 0) out vec4 fragColor; + + void main() { + fragColor = + texture(sampler2D(texture0, sampler0), vec2(gl_FragCoord.xy / 2.0)); + } + )"; + + const int expected = GenerateTestPixelValue(textureViewBaseLayer, textureViewBaseMipLevel); + Verify(textureView, fragmentShader, expected); + } + + void Texture2DArrayViewTest(uint32_t textureArrayLayers, + uint32_t textureMipLevels, + uint32_t textureViewBaseLayer, + uint32_t textureViewBaseMipLevel) { + // TODO(jiawei.shao@intel.com): support creating texture view with a texture view descriptor + // on D3D12, Metal and OpenGL. + DAWN_SKIP_TEST_IF(IsD3D12() || IsMetal() || IsOpenGL()); + + ASSERT(textureViewBaseLayer < textureArrayLayers); + ASSERT(textureViewBaseMipLevel < textureMipLevels); + + // We always set the layer count of the texture view to be 3 to match the fragment shader in + // this test. + constexpr uint32_t kTextureViewLayerCount = 3; + ASSERT(textureArrayLayers >= textureViewBaseLayer + kTextureViewLayerCount); + + initTexture(textureArrayLayers, textureMipLevels); + + dawn::TextureViewDescriptor descriptor = mDefaultTextureViewDescriptor; + descriptor.dimension = dawn::TextureViewDimension::e2DArray; + descriptor.baseArrayLayer = textureViewBaseLayer; + descriptor.layerCount = kTextureViewLayerCount; + descriptor.baseMipLevel = textureViewBaseMipLevel; + descriptor.levelCount = 1; + dawn::TextureView textureView = mTexture.CreateTextureView(&descriptor); + + const char* fragmentShader = R"( + #version 450 + layout(set = 0, binding = 0) uniform sampler sampler0; + layout(set = 0, binding = 1) uniform texture2DArray texture0; + layout(location = 0) out vec4 fragColor; + + void main() { + fragColor = + texture(sampler2DArray(texture0, sampler0), vec3(gl_FragCoord.xy / 2.0, 0)) + + texture(sampler2DArray(texture0, sampler0), vec3(gl_FragCoord.xy / 2.0, 1)) + + texture(sampler2DArray(texture0, sampler0), vec3(gl_FragCoord.xy / 2.0, 2)); + } + )"; + + int expected = 0; + for (int i = 0; i < static_cast(kTextureViewLayerCount); ++i) { + expected += GenerateTestPixelValue(textureViewBaseLayer + i, textureViewBaseMipLevel); + } + Verify(textureView, fragmentShader, expected); + } + dawn::BindGroupLayout mBindGroupLayout; dawn::PipelineLayout mPipelineLayout; dawn::Sampler mSampler; dawn::Texture mTexture; + dawn::TextureViewDescriptor mDefaultTextureViewDescriptor; dawn::ShaderModule mVSModule; utils::BasicRenderPass mRenderPass; }; -// Test drawing a rect with a checkerboard 2D array texture. +// Test drawing a rect with a 2D array texture. TEST_P(TextureViewTest, Default2DArrayTexture) { // TODO(cwallez@chromium.org) understand what the issue is DAWN_SKIP_TEST_IF(IsVulkan() && IsNvidia()); constexpr uint32_t kLayers = 3; - initTexture(kLayers); + constexpr uint32_t kMipLevels = 1; + initTexture(kLayers, kMipLevels); dawn::TextureView textureView = mTexture.CreateDefaultTextureView(); @@ -162,7 +276,35 @@ TEST_P(TextureViewTest, Default2DArrayTexture) { texture(sampler2DArray(texture0, sampler0), vec3(gl_FragCoord.xy / 2.0, 2)); } )"; - Test(textureView, fragmentShader, kLayers); + + const int expected = GenerateTestPixelValue(0, 0) + GenerateTestPixelValue(1, 0) + + GenerateTestPixelValue(2, 0); + Verify(textureView, fragmentShader, expected); +} + +// Test sampling from a 2D texture view created on a 2D array texture. +TEST_P(TextureViewTest, Texture2DViewOn2DArrayTexture) { + Texture2DViewTest(6, 1, 4, 0); +} + +// Test sampling from a 2D array texture view created on a 2D array texture. +TEST_P(TextureViewTest, Texture2DArrayViewOn2DArrayTexture) { + Texture2DArrayViewTest(6, 1, 2, 0); +} + +// Test sampling from a 2D texture view created on a mipmap level of a 2D texture. +TEST_P(TextureViewTest, Texture2DViewOnOneLevelOf2DTexture) { + Texture2DViewTest(1, 6, 0, 4); +} + +// Test sampling from a 2D texture view created on a mipmap level of a 2D array texture layer. +TEST_P(TextureViewTest, Texture2DViewOnOneLevelOf2DArrayTexture) { + Texture2DViewTest(6, 6, 3, 4); +} + +// Test sampling from a 2D array texture view created on a mipmap level of a 2D array texture. +TEST_P(TextureViewTest, Texture2DArrayViewOnOneLevelOf2DArrayTexture) { + Texture2DArrayViewTest(6, 6, 2, 4); } DAWN_INSTANTIATE_TEST(TextureViewTest, D3D12Backend, MetalBackend, OpenGLBackend, VulkanBackend)