blob: 67d34057a8cb4b42ce0b1ba6080f4169dc02fd87 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
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 Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Yan, Shaoboa4924272018-12-10 19:47:22 +000017#include "utils/ComboRenderPipelineDescriptor.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez04863c42019-10-25 11:36:47 +000019#include "utils/WGPUHelpers.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040020
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021#include <cstdio>
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000022#include <cstdlib>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040023#include <vector>
24
Corentin Wallez04863c42019-10-25 11:36:47 +000025wgpu::Device device;
26wgpu::Queue queue;
27wgpu::SwapChain swapchain;
28wgpu::RenderPipeline pipeline;
29wgpu::BindGroup bindGroup;
30wgpu::Buffer ubo;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031
32float RandomFloat(float min, float max) {
33 float zeroOne = rand() / float(RAND_MAX);
34 return zeroOne * (max - min) + min;
35}
36
Corentin Wallez8dfc5932019-05-29 13:16:06 +000037constexpr size_t kNumTriangles = 10000;
38
39struct alignas(kMinDynamicBufferOffsetAlignment) ShaderData {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040040 float scale;
41 float time;
42 float offsetX;
43 float offsetY;
44 float scalar;
45 float scalarOffset;
46};
47
48static std::vector<ShaderData> shaderData;
49
50void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020051 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040052
Corentin Wallez6d315da2021-02-04 15:33:42 +000053 queue = device.GetQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070054 swapchain = GetSwapChain(device);
Corentin Wallez6b087812020-10-27 15:35:56 +000055 swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000056 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
Corentin Wallez7aec4ae2021-03-24 15:55:32 +000058 wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
Corentin Wallez266bce82021-03-23 17:26:20 +000059 [[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 Wallezf07e3bd2017-04-20 14:38:20 -040068
Corentin Wallez266bce82021-03-23 17:26:20 +000069 [[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 Wallezf07e3bd2017-04-20 14:38:20 -040072
Corentin Wallez266bce82021-03-23 17:26:20 +000073 [[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 Wallezf07e3bd2017-04-20 14:38:20 -040079
Corentin Wallez266bce82021-03-23 17:26:20 +000080 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 Wallezf07e3bd2017-04-20 14:38:20 -040085
Corentin Wallez266bce82021-03-23 17:26:20 +000086 var position : vec4<f32> = positions[VertexIndex];
87 var color : vec4<f32> = colors[VertexIndex];
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040088
Corentin Wallez266bce82021-03-23 17:26:20 +000089 // 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 Wallezf07e3bd2017-04-20 14:38:20 -040092 if (fade < 0.5) {
93 fade = fade * 2.0;
94 } else {
95 fade = (1.0 - fade) * 2.0;
96 }
Corentin Wallez266bce82021-03-23 17:26:20 +000097
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 Wallezf07e3bd2017-04-20 14:38:20 -0400103 xpos = xrot + c.offsetX;
104 ypos = yrot + c.offsetY;
Corentin Wallez266bce82021-03-23 17:26:20 +0000105
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 Wallez8dfc5932019-05-29 13:16:06 +0000108 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400109
Corentin Wallez7aec4ae2021-03-24 15:55:32 +0000110 wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000111 [[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 Wallezb6fb5f32017-08-29 13:37:45 -0400116 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117
Corentin Wallez04863c42019-10-25 11:36:47 +0000118 wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
Corentin Wallez9895c272021-03-18 16:46:58 +0000119 device, {{0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true}});
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700120
Corentin Wallez9895c272021-03-18 16:46:58 +0000121 utils::ComboRenderPipelineDescriptor2 descriptor;
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000122 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallez9895c272021-03-18 16:46:58 +0000123 descriptor.vertex.module = vsModule;
124 descriptor.cFragment.module = fsModule;
125 descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000126
Corentin Wallez9895c272021-03-18 16:46:58 +0000127 pipeline = device.CreateRenderPipeline2(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400128
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000129 shaderData.resize(kNumTriangles);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400130 for (auto& data : shaderData) {
Corentin Wallez83e779d2017-07-10 21:44:06 -0400131 data.scale = RandomFloat(0.2f, 0.4f);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400132 data.time = 0.0;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400133 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 Wallezf07e3bd2017-04-20 14:38:20 -0400137 }
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000138
Corentin Wallez04863c42019-10-25 11:36:47 +0000139 wgpu::BufferDescriptor bufferDesc;
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000140 bufferDesc.size = kNumTriangles * sizeof(ShaderData);
Corentin Wallez04863c42019-10-25 11:36:47 +0000141 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000142 ubo = device.CreateBuffer(&bufferDesc);
143
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000144 bindGroup = utils::MakeBindGroup(device, bgl, {{0, ubo, 0, sizeof(ShaderData)}});
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400145}
146
147void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000148 wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700149
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400150 static int f = 0;
151 f++;
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000152 for (auto& data : shaderData) {
153 data.time = f / 60.0f;
154 }
Corentin Wallez47a33412020-06-02 09:24:39 +0000155 queue.WriteBuffer(ubo, 0, shaderData.data(), kNumTriangles * sizeof(ShaderData));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400156
Corentin Wallez604072b2019-11-12 18:30:11 +0000157 utils::ComboRenderPassDescriptor renderPass({backbufferView});
Corentin Wallez04863c42019-10-25 11:36:47 +0000158 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Kai Ninomiyab9854312017-08-11 14:36:20 -0700159 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000160 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000161 pass.SetPipeline(pipeline);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400162
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000163 for (size_t i = 0; i < kNumTriangles; i++) {
Austin Eng314fd352019-11-01 15:51:01 +0000164 uint32_t offset = i * sizeof(ShaderData);
Corentin Wallez8dfc5932019-05-29 13:16:06 +0000165 pass.SetBindGroup(0, bindGroup, 1, &offset);
Corentin Wallez67b1ad72020-03-31 16:21:35 +0000166 pass.Draw(3);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400167 }
168
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000169 pass.EndPass();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170 }
171
Corentin Wallez04863c42019-10-25 11:36:47 +0000172 wgpu::CommandBuffer commands = encoder.Finish();
Kai Ninomiyab9854312017-08-11 14:36:20 -0700173 queue.Submit(1, &commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000174 swapchain.Present();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700175 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176 fprintf(stderr, "frame %i\n", f);
177}
178
179int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400180 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400181 return 1;
182 }
183 init();
184
185 while (!ShouldQuit()) {
186 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400187 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400188 }
189
190 // TODO release stuff
191}