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 <cstdio> |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 22 | #include <cstdlib> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 25 | wgpu::Device device; |
| 26 | wgpu::Queue queue; |
| 27 | wgpu::SwapChain swapchain; |
| 28 | wgpu::RenderPipeline pipeline; |
| 29 | wgpu::BindGroup bindGroup; |
| 30 | wgpu::Buffer ubo; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | |
| 32 | float RandomFloat(float min, float max) { |
| 33 | float zeroOne = rand() / float(RAND_MAX); |
| 34 | return zeroOne * (max - min) + min; |
| 35 | } |
| 36 | |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 37 | constexpr size_t kNumTriangles = 10000; |
| 38 | |
| 39 | struct alignas(kMinDynamicBufferOffsetAlignment) ShaderData { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 40 | float scale; |
| 41 | float time; |
| 42 | float offsetX; |
| 43 | float offsetY; |
| 44 | float scalar; |
| 45 | float scalarOffset; |
| 46 | }; |
| 47 | |
| 48 | static std::vector<ShaderData> shaderData; |
| 49 | |
| 50 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 51 | device = CreateCppDawnDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 52 | |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 53 | queue = device.GetQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 54 | swapchain = GetSwapChain(device); |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 55 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment, |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 56 | 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 57 | |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 58 | wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"( |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 59 | [[block]] struct Constants { |
| 60 | scale : f32; |
| 61 | time : f32; |
| 62 | offsetX : f32; |
| 63 | offsetY : f32; |
| 64 | scalar : f32; |
| 65 | scalarOffset : f32; |
| 66 | }; |
| 67 | [[set(0), binding(0)]] var<uniform> c : Constants; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 69 | [[location(0)]] var<out> v_color : vec4<f32>; |
| 70 | [[builtin(vertex_idx)]] var<in> VertexIndex : u32; |
| 71 | [[builtin(position)]] var<out> Position : vec4<f32>; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 72 | |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 73 | [[stage(vertex)]] fn main() -> void { |
| 74 | var positions : array<vec4<f32>, 3> = array<vec4<f32>, 3>( |
| 75 | vec4<f32>( 0.0, 0.1, 0.0, 1.0), |
| 76 | vec4<f32>(-0.1, -0.1, 0.0, 1.0), |
| 77 | vec4<f32>( 0.1, -0.1, 0.0, 1.0) |
| 78 | ); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 79 | |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 80 | var colors : array<vec4<f32>, 3> = array<vec4<f32>, 3>( |
| 81 | vec4<f32>(1.0, 0.0, 0.0, 1.0), |
| 82 | vec4<f32>(0.0, 1.0, 0.0, 1.0), |
| 83 | vec4<f32>(0.0, 0.0, 1.0, 1.0) |
| 84 | ); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 85 | |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 86 | var position : vec4<f32> = positions[VertexIndex]; |
| 87 | var color : vec4<f32> = colors[VertexIndex]; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 88 | |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 89 | // TODO(dawn:572): Revisit once modf has been reworked in WGSL. |
| 90 | var fade : f32 = c.scalarOffset + c.time * c.scalar / 10.0; |
| 91 | fade = fade - floor(fade); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 92 | if (fade < 0.5) { |
| 93 | fade = fade * 2.0; |
| 94 | } else { |
| 95 | fade = (1.0 - fade) * 2.0; |
| 96 | } |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 97 | |
| 98 | var xpos : f32 = position.x * c.scale; |
| 99 | var ypos : f32 = position.y * c.scale; |
| 100 | const angle : f32 = 3.14159 * 2.0 * fade; |
| 101 | const xrot : f32 = xpos * cos(angle) - ypos * sin(angle); |
| 102 | const yrot : f32 = xpos * sin(angle) + ypos * cos(angle); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 103 | xpos = xrot + c.offsetX; |
| 104 | ypos = yrot + c.offsetY; |
Corentin Wallez | 266bce8 | 2021-03-23 17:26:20 +0000 | [diff] [blame] | 105 | |
| 106 | v_color = vec4<f32>(fade, 1.0 - fade, 0.0, 1.0) + color; |
| 107 | Position = vec4<f32>(xpos, ypos, 0.0, 1.0); |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 108 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 109 | |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 110 | wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 111 | [[location(0)]] var<out> FragColor : vec4<f32>; |
| 112 | [[location(0)]] var<in> v_color : vec4<f32>; |
| 113 | |
| 114 | [[stage(fragment)]] fn main() -> void { |
| 115 | FragColor = v_color; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 116 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 117 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 118 | wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 119 | device, {{0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true}}); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 120 | |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 121 | utils::ComboRenderPipelineDescriptor2 descriptor; |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 122 | descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 123 | descriptor.vertex.module = vsModule; |
| 124 | descriptor.cFragment.module = fsModule; |
| 125 | descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 126 | |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 127 | pipeline = device.CreateRenderPipeline2(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 128 | |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 129 | shaderData.resize(kNumTriangles); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 130 | for (auto& data : shaderData) { |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 131 | data.scale = RandomFloat(0.2f, 0.4f); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 132 | data.time = 0.0; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 133 | data.offsetX = RandomFloat(-0.9f, 0.9f); |
| 134 | data.offsetY = RandomFloat(-0.9f, 0.9f); |
| 135 | data.scalar = RandomFloat(0.5f, 2.0f); |
| 136 | data.scalarOffset = RandomFloat(0.0f, 10.0f); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 137 | } |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 138 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 139 | wgpu::BufferDescriptor bufferDesc; |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 140 | bufferDesc.size = kNumTriangles * sizeof(ShaderData); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 141 | bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform; |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 142 | ubo = device.CreateBuffer(&bufferDesc); |
| 143 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 144 | bindGroup = utils::MakeBindGroup(device, bgl, {{0, ubo, 0, sizeof(ShaderData)}}); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void frame() { |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 148 | wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 149 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 150 | static int f = 0; |
| 151 | f++; |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 152 | for (auto& data : shaderData) { |
| 153 | data.time = f / 60.0f; |
| 154 | } |
Corentin Wallez | 47a3341 | 2020-06-02 09:24:39 +0000 | [diff] [blame] | 155 | queue.WriteBuffer(ubo, 0, shaderData.data(), kNumTriangles * sizeof(ShaderData)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 156 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 157 | utils::ComboRenderPassDescriptor renderPass({backbufferView}); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 158 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Kai Ninomiya | b985431 | 2017-08-11 14:36:20 -0700 | [diff] [blame] | 159 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 160 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 161 | pass.SetPipeline(pipeline); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 162 | |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 163 | for (size_t i = 0; i < kNumTriangles; i++) { |
Austin Eng | 314fd35 | 2019-11-01 15:51:01 +0000 | [diff] [blame] | 164 | uint32_t offset = i * sizeof(ShaderData); |
Corentin Wallez | 8dfc593 | 2019-05-29 13:16:06 +0000 | [diff] [blame] | 165 | pass.SetBindGroup(0, bindGroup, 1, &offset); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 166 | pass.Draw(3); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 169 | pass.EndPass(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 172 | wgpu::CommandBuffer commands = encoder.Finish(); |
Kai Ninomiya | b985431 | 2017-08-11 14:36:20 -0700 | [diff] [blame] | 173 | queue.Submit(1, &commands); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 174 | swapchain.Present(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 175 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 176 | fprintf(stderr, "frame %i\n", f); |
| 177 | } |
| 178 | |
| 179 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 180 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | init(); |
| 184 | |
| 185 | while (!ShouldQuit()) { |
| 186 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 187 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // TODO release stuff |
| 191 | } |