Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2018 The Dawn Authors |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 15 | #include <cmath> |
| 16 | |
Corentin Wallez | a4da032 | 2018-07-18 15:18:25 +0200 | [diff] [blame] | 17 | #include "tests/DawnTest.h" |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 18 | |
| 19 | #include "common/Assert.h" |
| 20 | #include "common/Constants.h" |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 21 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 22 | #include "utils/WGPUHelpers.h" |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 23 | |
| 24 | constexpr static unsigned int kRTSize = 64; |
| 25 | |
| 26 | namespace { |
| 27 | struct AddressModeTestCase { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 28 | wgpu::AddressMode mMode; |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 29 | uint8_t mExpected2; |
| 30 | uint8_t mExpected3; |
| 31 | }; |
| 32 | AddressModeTestCase addressModes[] = { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 33 | { |
| 34 | wgpu::AddressMode::Repeat, |
| 35 | 0, |
| 36 | 255, |
| 37 | }, |
| 38 | { |
| 39 | wgpu::AddressMode::MirrorRepeat, |
| 40 | 255, |
| 41 | 0, |
| 42 | }, |
| 43 | { |
| 44 | wgpu::AddressMode::ClampToEdge, |
| 45 | 255, |
| 46 | 255, |
| 47 | }, |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 48 | }; |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 49 | } // namespace |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 50 | |
Corentin Wallez | a4da032 | 2018-07-18 15:18:25 +0200 | [diff] [blame] | 51 | class SamplerTest : public DawnTest { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 52 | protected: |
Austin Eng | 40dc5d3 | 2020-05-15 22:06:35 +0000 | [diff] [blame] | 53 | void SetUp() override { |
| 54 | DawnTest::SetUp(); |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 55 | mRenderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 56 | |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 57 | auto vsModule = utils::CreateShaderModule(device, R"( |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 58 | [[stage(vertex)]] |
| 59 | fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> { |
Corentin Wallez | b86e45f | 2021-06-17 21:36:11 +0000 | [diff] [blame^] | 60 | var pos = array<vec2<f32>, 6>( |
Austin Eng | 69afcb4 | 2020-11-26 13:41:55 +0000 | [diff] [blame] | 61 | vec2<f32>(-2.0, -2.0), |
| 62 | vec2<f32>(-2.0, 2.0), |
| 63 | vec2<f32>( 2.0, -2.0), |
| 64 | vec2<f32>(-2.0, 2.0), |
| 65 | vec2<f32>( 2.0, -2.0), |
| 66 | vec2<f32>( 2.0, 2.0)); |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 67 | return vec4<f32>(pos[VertexIndex], 0.0, 1.0); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 68 | } |
| 69 | )"); |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 70 | auto fsModule = utils::CreateShaderModule(device, R"( |
James Price | 7e80cce | 2021-02-10 20:17:14 +0000 | [diff] [blame] | 71 | [[group(0), binding(0)]] var sampler0 : sampler; |
| 72 | [[group(0), binding(1)]] var texture0 : texture_2d<f32>; |
Austin Eng | 69afcb4 | 2020-11-26 13:41:55 +0000 | [diff] [blame] | 73 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 74 | [[stage(fragment)]] |
James Price | eae70b7 | 2021-04-19 15:29:49 +0000 | [diff] [blame] | 75 | fn main([[builtin(position)]] FragCoord : vec4<f32>) -> [[location(0)]] vec4<f32> { |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 76 | return textureSample(texture0, sampler0, FragCoord.xy / vec2<f32>(2.0, 2.0)); |
Austin Eng | 69afcb4 | 2020-11-26 13:41:55 +0000 | [diff] [blame] | 77 | })"); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 78 | |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 79 | utils::ComboRenderPipelineDescriptor pipelineDescriptor; |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 80 | pipelineDescriptor.vertex.module = vsModule; |
| 81 | pipelineDescriptor.cFragment.module = fsModule; |
| 82 | pipelineDescriptor.cTargets[0].format = mRenderPass.colorFormat; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 83 | |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 84 | mPipeline = device.CreateRenderPipeline(&pipelineDescriptor); |
Austin Eng | 1fe9979 | 2019-12-03 21:32:55 +0000 | [diff] [blame] | 85 | mBindGroupLayout = mPipeline.GetBindGroupLayout(0); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 86 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 87 | wgpu::TextureDescriptor descriptor; |
| 88 | descriptor.dimension = wgpu::TextureDimension::e2D; |
Corentin Wallez | 29353d6 | 2018-09-18 12:49:22 +0000 | [diff] [blame] | 89 | descriptor.size.width = 2; |
| 90 | descriptor.size.height = 2; |
shrekshao | b00de7f | 2021-03-22 21:12:36 +0000 | [diff] [blame] | 91 | descriptor.size.depthOrArrayLayers = 1; |
Jiawei Shao | 8bff3b2 | 2018-12-12 09:27:16 +0000 | [diff] [blame] | 92 | descriptor.sampleCount = 1; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 93 | descriptor.format = wgpu::TextureFormat::RGBA8Unorm; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 94 | descriptor.mipLevelCount = 1; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 95 | descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled; |
| 96 | wgpu::Texture texture = device.CreateTexture(&descriptor); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 97 | |
| 98 | // Create a 2x2 checkerboard texture, with black in the top left and bottom right corners. |
Corentin Wallez | cdf2d8d | 2020-04-24 10:02:43 +0000 | [diff] [blame] | 99 | const uint32_t rowPixels = kTextureBytesPerRowAlignment / sizeof(RGBA8); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 100 | RGBA8 data[rowPixels * 2]; |
Yunchao He | 0c02f54 | 2019-11-19 17:57:30 +0000 | [diff] [blame] | 101 | data[0] = data[rowPixels + 1] = RGBA8::kBlack; |
| 102 | data[1] = data[rowPixels] = RGBA8::kWhite; |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 103 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 104 | wgpu::Buffer stagingBuffer = |
| 105 | utils::CreateBufferFromData(device, data, sizeof(data), wgpu::BufferUsage::CopySrc); |
Corentin Wallez | 8091584 | 2021-03-04 18:13:45 +0000 | [diff] [blame] | 106 | wgpu::ImageCopyBuffer imageCopyBuffer = utils::CreateImageCopyBuffer(stagingBuffer, 0, 256); |
| 107 | wgpu::ImageCopyTexture imageCopyTexture = |
| 108 | utils::CreateImageCopyTexture(texture, 0, {0, 0, 0}); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 109 | wgpu::Extent3D copySize = {2, 2, 1}; |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 110 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 111 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 8091584 | 2021-03-04 18:13:45 +0000 | [diff] [blame] | 112 | encoder.CopyBufferToTexture(&imageCopyBuffer, &imageCopyTexture, ©Size); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 113 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 114 | wgpu::CommandBuffer copy = encoder.Finish(); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 115 | queue.Submit(1, ©); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 116 | |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 117 | mTextureView = texture.CreateView(); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void TestAddressModes(AddressModeTestCase u, AddressModeTestCase v, AddressModeTestCase w) { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 121 | wgpu::Sampler sampler; |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 122 | { |
Austin Eng | 7817a9a | 2020-04-20 23:43:20 +0000 | [diff] [blame] | 123 | wgpu::SamplerDescriptor descriptor = {}; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 124 | descriptor.minFilter = wgpu::FilterMode::Nearest; |
| 125 | descriptor.magFilter = wgpu::FilterMode::Nearest; |
| 126 | descriptor.mipmapFilter = wgpu::FilterMode::Nearest; |
Corentin Wallez | 1ae19e8 | 2018-05-17 17:09:07 -0400 | [diff] [blame] | 127 | descriptor.addressModeU = u.mMode; |
| 128 | descriptor.addressModeV = v.mMode; |
| 129 | descriptor.addressModeW = w.mMode; |
| 130 | sampler = device.CreateSampler(&descriptor); |
| 131 | } |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 132 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 133 | wgpu::BindGroup bindGroup = |
| 134 | utils::MakeBindGroup(device, mBindGroupLayout, {{0, sampler}, {1, mTextureView}}); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 135 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 136 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 137 | { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 138 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&mRenderPass.renderPassInfo); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 139 | pass.SetPipeline(mPipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 140 | pass.SetBindGroup(0, bindGroup); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 141 | pass.Draw(6); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 142 | pass.EndPass(); |
| 143 | } |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 144 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 145 | wgpu::CommandBuffer commands = encoder.Finish(); |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 146 | queue.Submit(1, &commands); |
| 147 | |
| 148 | RGBA8 expectedU2(u.mExpected2, u.mExpected2, u.mExpected2, 255); |
| 149 | RGBA8 expectedU3(u.mExpected3, u.mExpected3, u.mExpected3, 255); |
| 150 | RGBA8 expectedV2(v.mExpected2, v.mExpected2, v.mExpected2, 255); |
| 151 | RGBA8 expectedV3(v.mExpected3, v.mExpected3, v.mExpected3, 255); |
Yunchao He | 0c02f54 | 2019-11-19 17:57:30 +0000 | [diff] [blame] | 152 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kBlack, mRenderPass.color, 0, 0); |
| 153 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kWhite, mRenderPass.color, 0, 1); |
| 154 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kWhite, mRenderPass.color, 1, 0); |
| 155 | EXPECT_PIXEL_RGBA8_EQ(RGBA8::kBlack, mRenderPass.color, 1, 1); |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 156 | EXPECT_PIXEL_RGBA8_EQ(expectedU2, mRenderPass.color, 2, 0); |
| 157 | EXPECT_PIXEL_RGBA8_EQ(expectedU3, mRenderPass.color, 3, 0); |
| 158 | EXPECT_PIXEL_RGBA8_EQ(expectedV2, mRenderPass.color, 0, 2); |
| 159 | EXPECT_PIXEL_RGBA8_EQ(expectedV3, mRenderPass.color, 0, 3); |
Corentin Wallez | 9fc6534 | 2018-07-18 15:28:38 +0200 | [diff] [blame] | 160 | // TODO: add tests for W address mode, once Dawn supports 3D textures |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 163 | utils::BasicRenderPass mRenderPass; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 164 | wgpu::BindGroupLayout mBindGroupLayout; |
| 165 | wgpu::RenderPipeline mPipeline; |
| 166 | wgpu::TextureView mTextureView; |
Stephen White | 1c24abc | 2018-04-03 11:41:05 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // Test drawing a rect with a checkerboard texture with different address modes. |
| 170 | TEST_P(SamplerTest, AddressMode) { |
| 171 | for (auto u : addressModes) { |
| 172 | for (auto v : addressModes) { |
| 173 | for (auto w : addressModes) { |
| 174 | TestAddressModes(u, v, w); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 180 | DAWN_INSTANTIATE_TEST(SamplerTest, |
| 181 | D3D12Backend(), |
| 182 | MetalBackend(), |
| 183 | OpenGLBackend(), |
Stephen White | f31b78e | 2020-12-04 15:59:29 +0000 | [diff] [blame] | 184 | OpenGLESBackend(), |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 185 | VulkanBackend()); |