Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -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 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 17 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 19 | #include "utils/WGPUHelpers.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 20 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 23 | wgpu::Device device; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 25 | wgpu::Buffer indexBuffer; |
| 26 | wgpu::Buffer vertexBuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 28 | wgpu::Texture texture; |
| 29 | wgpu::Sampler sampler; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 31 | wgpu::Queue queue; |
| 32 | wgpu::SwapChain swapchain; |
| 33 | wgpu::TextureView depthStencilView; |
| 34 | wgpu::RenderPipeline pipeline; |
| 35 | wgpu::BindGroup bindGroup; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
| 37 | void initBuffers() { |
| 38 | static const uint32_t indexData[3] = { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 39 | 0, |
| 40 | 1, |
| 41 | 2, |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | }; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 43 | indexBuffer = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 44 | utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 45 | |
| 46 | static const float vertexData[12] = { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 47 | 0.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | }; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 49 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 50 | wgpu::BufferUsage::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void initTextures() { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 54 | wgpu::TextureDescriptor descriptor; |
| 55 | descriptor.dimension = wgpu::TextureDimension::e2D; |
Corentin Wallez | 29353d6 | 2018-09-18 12:49:22 +0000 | [diff] [blame] | 56 | descriptor.size.width = 1024; |
| 57 | descriptor.size.height = 1024; |
| 58 | descriptor.size.depth = 1; |
Jiawei Shao | 8bff3b2 | 2018-12-12 09:27:16 +0000 | [diff] [blame] | 59 | descriptor.sampleCount = 1; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 60 | descriptor.format = wgpu::TextureFormat::RGBA8Unorm; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 61 | descriptor.mipLevelCount = 1; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 62 | descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 63 | texture = device.CreateTexture(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 64 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 65 | wgpu::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor(); |
Corentin Wallez | b711b9b | 2018-05-22 14:50:59 -0400 | [diff] [blame] | 66 | sampler = device.CreateSampler(&samplerDesc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 67 | |
| 68 | // Initialize the texture with arbitrary data until we can load images |
| 69 | std::vector<uint8_t> data(4 * 1024 * 1024, 0); |
| 70 | for (size_t i = 0; i < data.size(); ++i) { |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 71 | data[i] = static_cast<uint8_t>(i % 253); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 74 | wgpu::Buffer stagingBuffer = utils::CreateBufferFromData( |
| 75 | device, data.data(), static_cast<uint32_t>(data.size()), wgpu::BufferUsage::CopySrc); |
Kai Ninomiya | 16036cf | 2020-11-06 13:41:50 +0000 | [diff] [blame] | 76 | wgpu::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 4 * 1024); |
Corentin Wallez | 984493d | 2020-06-16 03:05:17 +0000 | [diff] [blame] | 77 | wgpu::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, {0, 0, 0}); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 78 | wgpu::Extent3D copySize = {1024, 1024, 1}; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 79 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 80 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 81 | encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size); |
| 82 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 83 | wgpu::CommandBuffer copy = encoder.Finish(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 84 | queue.Submit(1, ©); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 88 | device = CreateCppDawnDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 89 | |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 90 | queue = device.GetQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 91 | swapchain = GetSwapChain(device); |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 92 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment, |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 93 | 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 94 | |
| 95 | initBuffers(); |
| 96 | initTextures(); |
| 97 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 98 | wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
| 99 | [[builtin(position)]] var<out> Position : vec4<f32>; |
| 100 | [[location(0)]] var<in> pos : vec4<f32>; |
| 101 | [[stage(vertex)]] fn main() -> void { |
| 102 | Position = pos; |
| 103 | return; |
Corentin Wallez | 2a1d8c2 | 2019-07-12 17:52:22 +0000 | [diff] [blame] | 104 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 105 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 106 | wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
| 107 | [[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>; |
dan sinclair | 0f9c2d7 | 2021-01-19 14:18:51 +0000 | [diff] [blame] | 108 | [[group(0), binding(0)]] var<uniform_constant> mySampler: sampler; |
| 109 | [[group(0), binding(1)]] var<uniform_constant> myTexture : texture_2d<f32>; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 110 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 111 | [[location(0)]] var<out> FragColor : vec4<f32>; |
| 112 | [[stage(fragment)]] fn main() -> void { |
| 113 | FragColor = textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0)); |
| 114 | return; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 115 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 116 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 117 | auto bgl = utils::MakeBindGroupLayout( |
| 118 | device, { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 119 | {0, wgpu::ShaderStage::Fragment, wgpu::BindingType::Sampler}, |
| 120 | {1, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 121 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 122 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 123 | wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 124 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 125 | depthStencilView = CreateDefaultDepthStencilView(device); |
| 126 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 127 | utils::ComboRenderPipelineDescriptor descriptor(device); |
| 128 | descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 129 | descriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 130 | descriptor.cFragmentStage.module = fsModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 131 | descriptor.cVertexState.vertexBufferCount = 1; |
| 132 | descriptor.cVertexState.cVertexBuffers[0].arrayStride = 4 * sizeof(float); |
| 133 | descriptor.cVertexState.cVertexBuffers[0].attributeCount = 1; |
| 134 | descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float4; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 135 | descriptor.depthStencilState = &descriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 136 | descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 137 | descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 138 | |
| 139 | pipeline = device.CreateRenderPipeline(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 140 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 141 | wgpu::TextureView view = texture.CreateView(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 142 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 143 | bindGroup = utils::MakeBindGroup(device, bgl, {{0, sampler}, {1, view}}); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 146 | struct { |
| 147 | uint32_t a; |
| 148 | float b; |
| 149 | } s; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 150 | void frame() { |
| 151 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 152 | s.b += 0.02f; |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 153 | if (s.b >= 1.0f) { |
| 154 | s.b = 0.0f; |
| 155 | } |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 156 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 157 | wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView(); |
| 158 | utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 159 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 160 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 161 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 162 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 163 | pass.SetPipeline(pipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 164 | pass.SetBindGroup(0, bindGroup); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 165 | pass.SetVertexBuffer(0, vertexBuffer); |
Corentin Wallez | 5fad85b | 2020-11-25 08:54:14 +0000 | [diff] [blame] | 166 | pass.SetIndexBuffer(indexBuffer, wgpu::IndexFormat::Uint32); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 167 | pass.DrawIndexed(3); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 168 | pass.EndPass(); |
| 169 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 170 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 171 | wgpu::CommandBuffer commands = encoder.Finish(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 172 | queue.Submit(1, &commands); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 173 | swapchain.Present(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 174 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 178 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 179 | return 1; |
| 180 | } |
| 181 | init(); |
| 182 | |
| 183 | while (!ShouldQuit()) { |
| 184 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 185 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // TODO release stuff |
| 189 | } |