| // Copyright 2021 The Dawn Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <utility> |
| |
| #include "dawn/tests/end2end/VideoViewsTests.h" |
| #include "dawn/utils/ComboRenderPipelineDescriptor.h" |
| #include "dawn/utils/WGPUHelpers.h" |
| |
| namespace dawn { |
| |
| VideoViewsTestBackend::PlatformTexture::PlatformTexture(wgpu::Texture&& texture) |
| : wgpuTexture(texture) {} |
| VideoViewsTestBackend::PlatformTexture::~PlatformTexture() = default; |
| |
| VideoViewsTestBackend::~VideoViewsTestBackend() = default; |
| |
| constexpr std::array<utils::RGBA8, 2> VideoViewsTests::kYellowYUVColor; |
| constexpr std::array<utils::RGBA8, 2> VideoViewsTests::kWhiteYUVColor; |
| constexpr std::array<utils::RGBA8, 2> VideoViewsTests::kBlueYUVColor; |
| constexpr std::array<utils::RGBA8, 2> VideoViewsTests::kRedYUVColor; |
| |
| void VideoViewsTests::SetUp() { |
| DawnTest::SetUp(); |
| DAWN_TEST_UNSUPPORTED_IF(UsesWire()); |
| DAWN_TEST_UNSUPPORTED_IF(!IsMultiPlanarFormatsSupported()); |
| |
| mBackend = VideoViewsTestBackend::Create(); |
| mBackend->OnSetUp(device.Get()); |
| } |
| |
| void VideoViewsTests::TearDown() { |
| if (!UsesWire() && IsMultiPlanarFormatsSupported()) { |
| mBackend->OnTearDown(); |
| } |
| DawnTest::TearDown(); |
| } |
| |
| std::vector<wgpu::FeatureName> VideoViewsTests::GetRequiredFeatures() { |
| std::vector<wgpu::FeatureName> requiredFeatures = {}; |
| mIsMultiPlanarFormatsSupported = SupportsFeatures({wgpu::FeatureName::DawnMultiPlanarFormats}); |
| if (mIsMultiPlanarFormatsSupported) { |
| requiredFeatures.push_back(wgpu::FeatureName::DawnMultiPlanarFormats); |
| } |
| requiredFeatures.push_back(wgpu::FeatureName::DawnInternalUsages); |
| return requiredFeatures; |
| } |
| |
| bool VideoViewsTests::IsMultiPlanarFormatsSupported() const { |
| return mIsMultiPlanarFormatsSupported; |
| } |
| |
| // Returns a pre-prepared multi-planar formatted texture |
| // The encoded texture data represents a 4x4 converted image. When |isCheckerboard| is true, |
| // the top left is a 2x2 yellow block, bottom right is a 2x2 red block, top right is a 2x2 |
| // blue block, and bottom left is a 2x2 white block. When |isCheckerboard| is false, the |
| // image is converted from a solid yellow 4x4 block. |
| // static |
| std::vector<uint8_t> VideoViewsTests::GetTestTextureData(wgpu::TextureFormat format, |
| bool isCheckerboard) { |
| constexpr uint8_t Yy = kYellowYUVColor[kYUVLumaPlaneIndex].r; |
| constexpr uint8_t Yu = kYellowYUVColor[kYUVChromaPlaneIndex].r; |
| constexpr uint8_t Yv = kYellowYUVColor[kYUVChromaPlaneIndex].g; |
| |
| constexpr uint8_t Wy = kWhiteYUVColor[kYUVLumaPlaneIndex].r; |
| constexpr uint8_t Wu = kWhiteYUVColor[kYUVChromaPlaneIndex].r; |
| constexpr uint8_t Wv = kWhiteYUVColor[kYUVChromaPlaneIndex].g; |
| |
| constexpr uint8_t Ry = kRedYUVColor[kYUVLumaPlaneIndex].r; |
| constexpr uint8_t Ru = kRedYUVColor[kYUVChromaPlaneIndex].r; |
| constexpr uint8_t Rv = kRedYUVColor[kYUVChromaPlaneIndex].g; |
| |
| constexpr uint8_t By = kBlueYUVColor[kYUVLumaPlaneIndex].r; |
| constexpr uint8_t Bu = kBlueYUVColor[kYUVChromaPlaneIndex].r; |
| constexpr uint8_t Bv = kBlueYUVColor[kYUVChromaPlaneIndex].g; |
| |
| switch (format) { |
| // The first 16 bytes is the luma plane (Y), followed by the chroma plane (UV) which |
| // is half the number of bytes (subsampled by 2) but same bytes per line as luma |
| // plane. |
| case wgpu::TextureFormat::R8BG8Biplanar420Unorm: |
| if (isCheckerboard) { |
| return { |
| Wy, Wy, Ry, Ry, // plane 0, start + 0 |
| Wy, Wy, Ry, Ry, // |
| Yy, Yy, By, By, // |
| Yy, Yy, By, By, // |
| Wu, Wv, Ru, Rv, // plane 1, start + 16 |
| Yu, Yv, Bu, Bv, // |
| }; |
| } else { |
| return { |
| Yy, Yy, Yy, Yy, // plane 0, start + 0 |
| Yy, Yy, Yy, Yy, // |
| Yy, Yy, Yy, Yy, // |
| Yy, Yy, Yy, Yy, // |
| Yu, Yv, Yu, Yv, // plane 1, start + 16 |
| Yu, Yv, Yu, Yv, // |
| }; |
| } |
| case wgpu::TextureFormat::RGBA8Unorm: |
| // Combines both NV12 planes by directly mapping back to RGB: R=Y, G=U, B=V. |
| if (isCheckerboard) { |
| return { |
| Yy, Yu, Yv, Yy, Yu, Yv, By, Bu, Bv, By, Bu, Bv, // |
| Yy, Yu, Yv, Yy, Yu, Yv, By, Bu, Bv, By, Bu, Bv, // |
| Wy, Wu, Wv, Wy, Wu, Wv, Ry, Ru, Rv, Ry, Ru, Rv, // |
| Wy, Wu, Wv, Wy, Wu, Wv, Ry, Ru, Rv, Ry, Ru, Rv, // |
| }; |
| } else { |
| return { |
| Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, // |
| Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, // |
| Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, // |
| Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, Yy, Yu, Yv, // |
| }; |
| } |
| default: |
| UNREACHABLE(); |
| return {}; |
| } |
| } |
| |
| uint32_t VideoViewsTests::NumPlanes(wgpu::TextureFormat format) { |
| switch (format) { |
| case wgpu::TextureFormat::R8BG8Biplanar420Unorm: |
| return 2; |
| default: |
| UNREACHABLE(); |
| return 0; |
| } |
| } |
| std::vector<uint8_t> VideoViewsTests::GetTestTextureDataWithPlaneIndex(size_t planeIndex, |
| size_t bytesPerRow, |
| size_t height, |
| bool isCheckerboard) { |
| std::vector<uint8_t> texelData = VideoViewsTests::GetTestTextureData( |
| wgpu::TextureFormat::R8BG8Biplanar420Unorm, isCheckerboard); |
| const uint32_t texelDataRowBytes = kYUVImageDataWidthInTexels; |
| const uint32_t texelDataHeight = |
| planeIndex == 0 ? kYUVImageDataHeightInTexels : kYUVImageDataHeightInTexels / 2; |
| |
| std::vector<uint8_t> texels(bytesPerRow * height, 0); |
| uint32_t plane_first_texel_offset = 0; |
| // The size of the test video frame is 4 x 4 |
| switch (planeIndex) { |
| case VideoViewsTests::kYUVLumaPlaneIndex: |
| for (uint32_t i = 0; i < texelDataHeight; ++i) { |
| if (i < texelDataHeight) { |
| for (uint32_t j = 0; j < texelDataRowBytes; ++j) { |
| texels[bytesPerRow * i + j] = |
| texelData[texelDataRowBytes * i + j + plane_first_texel_offset]; |
| } |
| } |
| } |
| return texels; |
| case VideoViewsTests::kYUVChromaPlaneIndex: |
| // TexelData is 4 * 6 size, first 4 * 4 is Y plane, UV plane started |
| // at index 16. |
| plane_first_texel_offset = 16; |
| for (uint32_t i = 0; i < texelDataHeight; ++i) { |
| if (i < texelDataHeight) { |
| for (uint32_t j = 0; j < texelDataRowBytes; ++j) { |
| texels[bytesPerRow * i + j] = |
| texelData[texelDataRowBytes * i + j + plane_first_texel_offset]; |
| } |
| } |
| } |
| return texels; |
| default: |
| UNREACHABLE(); |
| return {}; |
| } |
| } |
| |
| // Vertex shader used to render a sampled texture into a quad. |
| wgpu::ShaderModule VideoViewsTests::GetTestVertexShaderModule() const { |
| return utils::CreateShaderModule(device, R"( |
| struct VertexOut { |
| @location(0) texCoord : vec2 <f32>, |
| @builtin(position) position : vec4f, |
| } |
| |
| @vertex |
| fn main(@builtin(vertex_index) VertexIndex : u32) -> VertexOut { |
| var pos = array( |
| vec2f(-1.0, 1.0), |
| vec2f(-1.0, -1.0), |
| vec2f(1.0, -1.0), |
| vec2f(-1.0, 1.0), |
| vec2f(1.0, -1.0), |
| vec2f(1.0, 1.0) |
| ); |
| var output : VertexOut; |
| output.position = vec4f(pos[VertexIndex], 0.0, 1.0); |
| output.texCoord = vec2f(output.position.xy * 0.5) + vec2f(0.5, 0.5); |
| return output; |
| })"); |
| } |
| |
| // Create video texture uninitialized. |
| TEST_P(VideoViewsTests, CreateVideoTextureWithoutInitializedData) { |
| ASSERT_DEVICE_ERROR( |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ false, |
| /*initialized*/ false)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| namespace { |
| |
| // Samples the luminance (Y) plane from an imported NV12 texture into a single channel of an RGBA |
| // output attachment and checks for the expected pixel value in the rendered quad. |
| TEST_P(VideoViewsTests, NV12SampleYtoR) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ false, |
| /*initialized*/ true); |
| ASSERT_NE(platformTexture.get(), nullptr); |
| if (!platformTexture->CanWrapAsWGPUTexture()) { |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| GTEST_SKIP() << "Skipped because not supported."; |
| } |
| wgpu::TextureViewDescriptor viewDesc; |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| wgpu::TextureView textureView = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| utils::ComboRenderPipelineDescriptor renderPipelineDescriptor; |
| renderPipelineDescriptor.vertex.module = GetTestVertexShaderModule(); |
| |
| renderPipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"( |
| @group(0) @binding(0) var sampler0 : sampler; |
| @group(0) @binding(1) var texture : texture_2d<f32>; |
| |
| @fragment |
| fn main(@location(0) texCoord : vec2f) -> @location(0) vec4f { |
| let y : f32 = textureSample(texture, sampler0, texCoord).r; |
| return vec4f(y, 0.0, 0.0, 1.0); |
| })"); |
| |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass( |
| device, kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels); |
| renderPipelineDescriptor.cTargets[0].format = renderPass.colorFormat; |
| renderPipelineDescriptor.primitive.topology = wgpu::PrimitiveTopology::TriangleList; |
| |
| wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline(&renderPipelineDescriptor); |
| |
| wgpu::Sampler sampler = device.CreateSampler(); |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| { |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo); |
| pass.SetPipeline(renderPipeline); |
| pass.SetBindGroup(0, utils::MakeBindGroup(device, renderPipeline.GetBindGroupLayout(0), |
| {{0, sampler}, {1, textureView}})); |
| pass.Draw(6); |
| pass.End(); |
| } |
| |
| wgpu::CommandBuffer commands = encoder.Finish(); |
| queue.Submit(1, &commands); |
| |
| // Test the luma plane in the top left corner of RGB image. |
| EXPECT_PIXEL_RGBA8_EQ(kYellowYUVColor[kYUVLumaPlaneIndex], renderPass.color, 0, 0); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Samples the chrominance (UV) plane from an imported texture into two channels of an RGBA output |
| // attachment and checks for the expected pixel value in the rendered quad. |
| TEST_P(VideoViewsTests, NV12SampleUVtoRG) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ false, |
| /*initialized*/ true); |
| ASSERT_NE(platformTexture.get(), nullptr); |
| if (!platformTexture->CanWrapAsWGPUTexture()) { |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| GTEST_SKIP() << "Skipped because not supported."; |
| } |
| |
| wgpu::TextureViewDescriptor viewDesc; |
| viewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| wgpu::TextureView textureView = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| utils::ComboRenderPipelineDescriptor renderPipelineDescriptor; |
| renderPipelineDescriptor.vertex.module = GetTestVertexShaderModule(); |
| |
| renderPipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"( |
| @group(0) @binding(0) var sampler0 : sampler; |
| @group(0) @binding(1) var texture : texture_2d<f32>; |
| |
| @fragment |
| fn main(@location(0) texCoord : vec2f) -> @location(0) vec4f { |
| let u : f32 = textureSample(texture, sampler0, texCoord).r; |
| let v : f32 = textureSample(texture, sampler0, texCoord).g; |
| return vec4f(u, v, 0.0, 1.0); |
| })"); |
| |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass( |
| device, kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels); |
| renderPipelineDescriptor.cTargets[0].format = renderPass.colorFormat; |
| renderPipelineDescriptor.primitive.topology = wgpu::PrimitiveTopology::TriangleList; |
| |
| wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline(&renderPipelineDescriptor); |
| |
| wgpu::Sampler sampler = device.CreateSampler(); |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| { |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo); |
| pass.SetPipeline(renderPipeline); |
| pass.SetBindGroup(0, utils::MakeBindGroup(device, renderPipeline.GetBindGroupLayout(0), |
| {{0, sampler}, {1, textureView}})); |
| pass.Draw(6); |
| pass.End(); |
| } |
| |
| wgpu::CommandBuffer commands = encoder.Finish(); |
| queue.Submit(1, &commands); |
| |
| // Test the chroma plane in the top left corner of RGB image. |
| EXPECT_PIXEL_RGBA8_EQ(kYellowYUVColor[kYUVChromaPlaneIndex], renderPass.color, 0, 0); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Renders a NV12 "checkerboard" texture into a RGB quad, then checks the the entire |
| // contents to ensure the image has not been flipped. |
| TEST_P(VideoViewsTests, NV12SampleYUVtoRGB) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| ASSERT_NE(platformTexture.get(), nullptr); |
| if (!platformTexture->CanWrapAsWGPUTexture()) { |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| GTEST_SKIP() << "Skipped because not supported."; |
| } |
| |
| wgpu::TextureViewDescriptor lumaViewDesc; |
| lumaViewDesc.format = wgpu::TextureFormat::R8Unorm; |
| lumaViewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| wgpu::TextureView lumaTextureView = platformTexture->wgpuTexture.CreateView(&lumaViewDesc); |
| |
| wgpu::TextureViewDescriptor chromaViewDesc; |
| chromaViewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| chromaViewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| wgpu::TextureView chromaTextureView = platformTexture->wgpuTexture.CreateView(&chromaViewDesc); |
| |
| utils::ComboRenderPipelineDescriptor renderPipelineDescriptor; |
| renderPipelineDescriptor.vertex.module = GetTestVertexShaderModule(); |
| |
| renderPipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"( |
| @group(0) @binding(0) var sampler0 : sampler; |
| @group(0) @binding(1) var lumaTexture : texture_2d<f32>; |
| @group(0) @binding(2) var chromaTexture : texture_2d<f32>; |
| |
| @fragment |
| fn main(@location(0) texCoord : vec2f) -> @location(0) vec4f { |
| let y : f32 = textureSample(lumaTexture, sampler0, texCoord).r; |
| let u : f32 = textureSample(chromaTexture, sampler0, texCoord).r; |
| let v : f32 = textureSample(chromaTexture, sampler0, texCoord).g; |
| return vec4f(y, u, v, 1.0); |
| })"); |
| |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass( |
| device, kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels); |
| renderPipelineDescriptor.cTargets[0].format = renderPass.colorFormat; |
| |
| wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline(&renderPipelineDescriptor); |
| |
| wgpu::Sampler sampler = device.CreateSampler(); |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| { |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo); |
| pass.SetPipeline(renderPipeline); |
| pass.SetBindGroup( |
| 0, utils::MakeBindGroup(device, renderPipeline.GetBindGroupLayout(0), |
| {{0, sampler}, {1, lumaTextureView}, {2, chromaTextureView}})); |
| pass.Draw(6); |
| pass.End(); |
| } |
| |
| wgpu::CommandBuffer commands = encoder.Finish(); |
| queue.Submit(1, &commands); |
| |
| std::vector<uint8_t> expectedData = GetTestTextureData(wgpu::TextureFormat::RGBA8Unorm, true); |
| std::vector<utils::RGBA8> expectedRGBA; |
| for (uint8_t i = 0; i < expectedData.size(); i += 3) { |
| expectedRGBA.push_back({expectedData[i], expectedData[i + 1], expectedData[i + 2], 0xFF}); |
| } |
| |
| EXPECT_TEXTURE_EQ(expectedRGBA.data(), renderPass.color, {0, 0}, |
| {kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels}); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Renders a NV12 "checkerboard" texture into a RGB quad with two samplers, then checks the the |
| // entire contents to ensure the image has not been flipped. |
| TEST_P(VideoViewsTests, NV12SampleYUVtoRGBMultipleSamplers) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| ASSERT_NE(platformTexture.get(), nullptr); |
| if (!platformTexture->CanWrapAsWGPUTexture()) { |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| GTEST_SKIP() << "Skipped because not supported."; |
| } |
| |
| wgpu::TextureViewDescriptor lumaViewDesc; |
| lumaViewDesc.format = wgpu::TextureFormat::R8Unorm; |
| lumaViewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| wgpu::TextureView lumaTextureView = platformTexture->wgpuTexture.CreateView(&lumaViewDesc); |
| |
| wgpu::TextureViewDescriptor chromaViewDesc; |
| chromaViewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| chromaViewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| wgpu::TextureView chromaTextureView = platformTexture->wgpuTexture.CreateView(&chromaViewDesc); |
| |
| utils::ComboRenderPipelineDescriptor renderPipelineDescriptor; |
| renderPipelineDescriptor.vertex.module = GetTestVertexShaderModule(); |
| |
| renderPipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, R"( |
| @group(0) @binding(0) var sampler0 : sampler; |
| @group(0) @binding(1) var sampler1 : sampler; |
| @group(0) @binding(2) var lumaTexture : texture_2d<f32>; |
| @group(0) @binding(3) var chromaTexture : texture_2d<f32>; |
| |
| @fragment |
| fn main(@location(0) texCoord : vec2f) -> @location(0) vec4f { |
| let y : f32 = textureSample(lumaTexture, sampler0, texCoord).r; |
| let u : f32 = textureSample(chromaTexture, sampler1, texCoord).r; |
| let v : f32 = textureSample(chromaTexture, sampler1, texCoord).g; |
| return vec4f(y, u, v, 1.0); |
| })"); |
| |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass( |
| device, kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels); |
| renderPipelineDescriptor.cTargets[0].format = renderPass.colorFormat; |
| |
| wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline(&renderPipelineDescriptor); |
| |
| wgpu::Sampler sampler0 = device.CreateSampler(); |
| wgpu::Sampler sampler1 = device.CreateSampler(); |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| { |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo); |
| pass.SetPipeline(renderPipeline); |
| pass.SetBindGroup( |
| 0, utils::MakeBindGroup( |
| device, renderPipeline.GetBindGroupLayout(0), |
| {{0, sampler0}, {1, sampler1}, {2, lumaTextureView}, {3, chromaTextureView}})); |
| pass.Draw(6); |
| pass.End(); |
| } |
| |
| wgpu::CommandBuffer commands = encoder.Finish(); |
| queue.Submit(1, &commands); |
| |
| std::vector<uint8_t> expectedData = GetTestTextureData(wgpu::TextureFormat::RGBA8Unorm, true); |
| std::vector<utils::RGBA8> expectedRGBA; |
| for (uint8_t i = 0; i < expectedData.size(); i += 3) { |
| expectedRGBA.push_back({expectedData[i], expectedData[i + 1], expectedData[i + 2], 0xFF}); |
| } |
| |
| EXPECT_TEXTURE_EQ(expectedRGBA.data(), renderPass.color, {0, 0}, |
| {kYUVImageDataWidthInTexels, kYUVImageDataHeightInTexels}); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| class VideoViewsValidationTests : public VideoViewsTests { |
| protected: |
| void SetUp() override { |
| VideoViewsTests::SetUp(); |
| DAWN_TEST_UNSUPPORTED_IF(HasToggleEnabled("skip_validation")); |
| } |
| }; |
| |
| // Test explicitly creating a multiplanar format is not allowed |
| TEST_P(VideoViewsValidationTests, ExplicitCreation) { |
| wgpu::TextureDescriptor descriptor; |
| descriptor.dimension = wgpu::TextureDimension::e2D; |
| descriptor.size.width = 1; |
| descriptor.size.height = 1; |
| descriptor.format = wgpu::TextureFormat::R8BG8Biplanar420Unorm; |
| descriptor.usage = wgpu::TextureUsage::TextureBinding; |
| ASSERT_DEVICE_ERROR(device.CreateTexture(&descriptor)); |
| } |
| |
| // Test texture view creation rules. |
| TEST_P(VideoViewsValidationTests, CreateViewValidation) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| ASSERT_NE(platformTexture.get(), nullptr); |
| if (!platformTexture->CanWrapAsWGPUTexture()) { |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| GTEST_SKIP() << "Skipped because not supported."; |
| } |
| |
| wgpu::TextureViewDescriptor viewDesc = {}; |
| |
| // Success case: Per plane view formats unspecified. |
| { |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| wgpu::TextureView plane0View = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| wgpu::TextureView plane1View = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| ASSERT_NE(plane0View.Get(), nullptr); |
| ASSERT_NE(plane1View.Get(), nullptr); |
| } |
| |
| // Success case: Per plane view formats specified and aspect. |
| { |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| wgpu::TextureView plane0View = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| viewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| wgpu::TextureView plane1View = platformTexture->wgpuTexture.CreateView(&viewDesc); |
| |
| ASSERT_NE(plane0View.Get(), nullptr); |
| ASSERT_NE(plane1View.Get(), nullptr); |
| } |
| |
| // Some valid view format, but no plane specified. |
| viewDesc = {}; |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| ASSERT_DEVICE_ERROR(platformTexture->wgpuTexture.CreateView(&viewDesc)); |
| |
| // Some valid view format, but no plane specified. |
| viewDesc = {}; |
| viewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| ASSERT_DEVICE_ERROR(platformTexture->wgpuTexture.CreateView(&viewDesc)); |
| |
| // Correct plane index but incompatible view format. |
| viewDesc.format = wgpu::TextureFormat::R8Uint; |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| ASSERT_DEVICE_ERROR(platformTexture->wgpuTexture.CreateView(&viewDesc)); |
| |
| // Compatible view format but wrong plane index. |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| ASSERT_DEVICE_ERROR(platformTexture->wgpuTexture.CreateView(&viewDesc)); |
| |
| // Compatible view format but wrong aspect. |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| viewDesc.aspect = wgpu::TextureAspect::All; |
| ASSERT_DEVICE_ERROR(platformTexture->wgpuTexture.CreateView(&viewDesc)); |
| |
| // Create a single plane texture. |
| wgpu::TextureDescriptor desc; |
| desc.format = wgpu::TextureFormat::RGBA8Unorm; |
| desc.dimension = wgpu::TextureDimension::e2D; |
| desc.usage = wgpu::TextureUsage::TextureBinding; |
| desc.size = {1, 1, 1}; |
| |
| wgpu::Texture texture = device.CreateTexture(&desc); |
| |
| // Plane aspect specified with non-planar texture. |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| ASSERT_DEVICE_ERROR(texture.CreateView(&viewDesc)); |
| |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| ASSERT_DEVICE_ERROR(texture.CreateView(&viewDesc)); |
| |
| // Planar views with non-planar texture. |
| viewDesc.aspect = wgpu::TextureAspect::Plane0Only; |
| viewDesc.format = wgpu::TextureFormat::R8Unorm; |
| ASSERT_DEVICE_ERROR(texture.CreateView(&viewDesc)); |
| |
| viewDesc.aspect = wgpu::TextureAspect::Plane1Only; |
| viewDesc.format = wgpu::TextureFormat::RG8Unorm; |
| ASSERT_DEVICE_ERROR(texture.CreateView(&viewDesc)); |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Test copying from one multi-planar format into another fails. |
| TEST_P(VideoViewsValidationTests, T2TCopyAllAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture1 = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture2 = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| wgpu::Texture srcTexture = platformTexture1->wgpuTexture; |
| wgpu::Texture dstTexture = platformTexture2->wgpuTexture; |
| |
| wgpu::ImageCopyTexture copySrc = utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}); |
| |
| wgpu::ImageCopyTexture copyDst = utils::CreateImageCopyTexture(dstTexture, 0, {0, 0, 0}); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture1)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture2)); |
| } |
| |
| // Test copying from one multi-planar format into another per plane fails. |
| TEST_P(VideoViewsValidationTests, T2TCopyPlaneAspectFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture1 = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture2 = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| wgpu::Texture srcTexture = platformTexture1->wgpuTexture; |
| wgpu::Texture dstTexture = platformTexture2->wgpuTexture; |
| |
| wgpu::ImageCopyTexture copySrc = |
| utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane0Only); |
| |
| wgpu::ImageCopyTexture copyDst = |
| utils::CreateImageCopyTexture(dstTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane1Only); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| |
| copySrc = |
| utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane1Only); |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture1)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture2)); |
| } |
| |
| // Test copying from a multi-planar format to a buffer fails. |
| TEST_P(VideoViewsValidationTests, T2BCopyAllAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| wgpu::Texture srcTexture = platformTexture->wgpuTexture; |
| |
| wgpu::BufferDescriptor bufferDescriptor; |
| bufferDescriptor.size = 1; |
| bufferDescriptor.usage = wgpu::BufferUsage::CopyDst; |
| wgpu::Buffer dstBuffer = device.CreateBuffer(&bufferDescriptor); |
| |
| wgpu::ImageCopyTexture copySrc = utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}); |
| |
| wgpu::ImageCopyBuffer copyDst = utils::CreateImageCopyBuffer(dstBuffer, 0, 4); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToBuffer(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Test copying from multi-planar format per plane to a buffer fails. |
| TEST_P(VideoViewsValidationTests, T2BCopyPlaneAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| wgpu::Texture srcTexture = platformTexture->wgpuTexture; |
| |
| wgpu::BufferDescriptor bufferDescriptor; |
| bufferDescriptor.size = 1; |
| bufferDescriptor.usage = wgpu::BufferUsage::CopyDst; |
| wgpu::Buffer dstBuffer = device.CreateBuffer(&bufferDescriptor); |
| |
| wgpu::ImageCopyTexture copySrc = |
| utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane0Only); |
| |
| wgpu::ImageCopyBuffer copyDst = utils::CreateImageCopyBuffer(dstBuffer, 0, 4); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToBuffer(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| |
| copySrc = |
| utils::CreateImageCopyTexture(srcTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane1Only); |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyTextureToBuffer(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Test copying from a buffer to a multi-planar format fails. |
| TEST_P(VideoViewsValidationTests, B2TCopyAllAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| wgpu::Texture dstTexture = platformTexture->wgpuTexture; |
| |
| std::vector<uint8_t> placeholderData(4, 0); |
| |
| wgpu::Buffer srcBuffer = utils::CreateBufferFromData( |
| device, placeholderData.data(), placeholderData.size(), wgpu::BufferUsage::CopySrc); |
| |
| wgpu::ImageCopyBuffer copySrc = utils::CreateImageCopyBuffer(srcBuffer, 0, 12, 4); |
| |
| wgpu::ImageCopyTexture copyDst = utils::CreateImageCopyTexture(dstTexture, 0, {0, 0, 0}); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyBufferToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Test copying from a buffer to a multi-planar format per plane fails. |
| TEST_P(VideoViewsValidationTests, B2TCopyPlaneAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| wgpu::Texture dstTexture = platformTexture->wgpuTexture; |
| |
| std::vector<uint8_t> placeholderData(4, 0); |
| |
| wgpu::Buffer srcBuffer = utils::CreateBufferFromData( |
| device, placeholderData.data(), placeholderData.size(), wgpu::BufferUsage::CopySrc); |
| |
| wgpu::ImageCopyBuffer copySrc = utils::CreateImageCopyBuffer(srcBuffer, 0, 12, 4); |
| |
| wgpu::ImageCopyTexture copyDst = |
| utils::CreateImageCopyTexture(dstTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane0Only); |
| |
| wgpu::Extent3D copySize = {1, 1, 1}; |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyBufferToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| |
| copyDst = |
| utils::CreateImageCopyTexture(dstTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane1Only); |
| |
| { |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
| encoder.CopyBufferToTexture(©Src, ©Dst, ©Size); |
| ASSERT_DEVICE_ERROR(encoder.Finish()); |
| } |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Tests which multi-planar formats are allowed to be sampled. |
| TEST_P(VideoViewsValidationTests, SamplingMultiPlanarTexture) { |
| wgpu::BindGroupLayout layout = utils::MakeBindGroupLayout( |
| device, {{0, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float}}); |
| |
| // R8BG8Biplanar420Unorm is allowed to be sampled, if plane 0 or plane 1 is selected. |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| wgpu::TextureViewDescriptor desc = {}; |
| |
| desc.aspect = wgpu::TextureAspect::Plane0Only; |
| utils::MakeBindGroup(device, layout, {{0, platformTexture->wgpuTexture.CreateView(&desc)}}); |
| |
| desc.aspect = wgpu::TextureAspect::Plane1Only; |
| utils::MakeBindGroup(device, layout, {{0, platformTexture->wgpuTexture.CreateView(&desc)}}); |
| |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Tests creating a texture with a multi-plane format. |
| TEST_P(VideoViewsValidationTests, RenderAttachmentInvalid) { |
| // multi-planar formats are NOT allowed to be renderable. |
| ASSERT_DEVICE_ERROR(auto platformTexture = mBackend->CreateVideoTextureForTest( |
| wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::RenderAttachment, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Tests writing into a multi-planar format fails. |
| TEST_P(VideoViewsValidationTests, WriteTextureAllAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| wgpu::TextureDataLayout textureDataLayout = utils::CreateTextureDataLayout(0, 4, 4); |
| |
| wgpu::ImageCopyTexture imageCopyTexture = |
| utils::CreateImageCopyTexture(platformTexture->wgpuTexture, 0, {0, 0, 0}); |
| |
| std::vector<uint8_t> placeholderData(4, 0); |
| wgpu::Extent3D writeSize = {1, 1, 1}; |
| |
| wgpu::Queue queue = device.GetQueue(); |
| |
| ASSERT_DEVICE_ERROR(queue.WriteTexture(&imageCopyTexture, placeholderData.data(), |
| placeholderData.size(), &textureDataLayout, &writeSize)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| // Tests writing into a multi-planar format per plane fails. |
| TEST_P(VideoViewsValidationTests, WriteTexturePlaneAspectsFails) { |
| std::unique_ptr<VideoViewsTestBackend::PlatformTexture> platformTexture = |
| mBackend->CreateVideoTextureForTest(wgpu::TextureFormat::R8BG8Biplanar420Unorm, |
| wgpu::TextureUsage::TextureBinding, |
| /*isCheckerboard*/ true, |
| /*initialized*/ true); |
| |
| wgpu::TextureDataLayout textureDataLayout = utils::CreateTextureDataLayout(0, 12, 4); |
| wgpu::ImageCopyTexture imageCopyTexture = utils::CreateImageCopyTexture( |
| platformTexture->wgpuTexture, 0, {0, 0, 0}, wgpu::TextureAspect::Plane0Only); |
| |
| std::vector<uint8_t> placeholderData(4, 0); |
| wgpu::Extent3D writeSize = {1, 1, 1}; |
| |
| wgpu::Queue queue = device.GetQueue(); |
| |
| ASSERT_DEVICE_ERROR(queue.WriteTexture(&imageCopyTexture, placeholderData.data(), |
| placeholderData.size(), &textureDataLayout, &writeSize)); |
| mBackend->DestroyVideoTextureForTest(std::move(platformTexture)); |
| } |
| |
| DAWN_INSTANTIATE_TEST_V(VideoViewsTests, VideoViewsTestBackend::Backends()); |
| DAWN_INSTANTIATE_TEST_V(VideoViewsValidationTests, VideoViewsTestBackend::Backends()); |
| |
| } // anonymous namespace |
| } // namespace dawn |