2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
#include "utils/ComboRenderPipelineDescriptor.h"
|
2021-06-24 19:21:31 +00:00
|
|
|
#include "utils/ScopedAutoreleasePool.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2019-10-25 11:36:47 +00:00
|
|
|
#include "utils/WGPUHelpers.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <cstdio>
|
2020-07-10 20:33:08 +00:00
|
|
|
#include <cstdlib>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Device device;
|
|
|
|
wgpu::Queue queue;
|
|
|
|
wgpu::SwapChain swapchain;
|
|
|
|
wgpu::RenderPipeline pipeline;
|
|
|
|
wgpu::BindGroup bindGroup;
|
|
|
|
wgpu::Buffer ubo;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
float RandomFloat(float min, float max) {
|
|
|
|
float zeroOne = rand() / float(RAND_MAX);
|
|
|
|
return zeroOne * (max - min) + min;
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:16:06 +00:00
|
|
|
constexpr size_t kNumTriangles = 10000;
|
|
|
|
|
|
|
|
struct alignas(kMinDynamicBufferOffsetAlignment) ShaderData {
|
2017-04-20 18:38:20 +00:00
|
|
|
float scale;
|
|
|
|
float time;
|
|
|
|
float offsetX;
|
|
|
|
float offsetY;
|
|
|
|
float scalar;
|
|
|
|
float scalarOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<ShaderData> shaderData;
|
|
|
|
|
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-02-04 15:33:42 +00:00
|
|
|
queue = device.GetQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2020-10-27 15:35:56 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
|
2019-08-27 08:21:39 +00:00
|
|
|
640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
|
2021-03-23 17:26:20 +00:00
|
|
|
[[block]] struct Constants {
|
|
|
|
scale : f32;
|
|
|
|
time : f32;
|
|
|
|
offsetX : f32;
|
|
|
|
offsetY : f32;
|
|
|
|
scalar : f32;
|
|
|
|
scalarOffset : f32;
|
|
|
|
};
|
|
|
|
[[set(0), binding(0)]] var<uniform> c : Constants;
|
|
|
|
|
2021-04-13 14:52:44 +00:00
|
|
|
struct VertexOut {
|
|
|
|
[[location(0)]] v_color : vec4<f32>;
|
|
|
|
[[builtin(position)]] Position : vec4<f32>;
|
|
|
|
};
|
2021-03-23 17:26:20 +00:00
|
|
|
|
2021-04-13 14:52:44 +00:00
|
|
|
[[stage(vertex)]] fn main([[builtin(vertex_idx)]] VertexIndex : u32) -> VertexOut {
|
2021-03-23 17:26:20 +00:00
|
|
|
var positions : array<vec4<f32>, 3> = array<vec4<f32>, 3>(
|
|
|
|
vec4<f32>( 0.0, 0.1, 0.0, 1.0),
|
|
|
|
vec4<f32>(-0.1, -0.1, 0.0, 1.0),
|
|
|
|
vec4<f32>( 0.1, -0.1, 0.0, 1.0)
|
|
|
|
);
|
|
|
|
|
|
|
|
var colors : array<vec4<f32>, 3> = array<vec4<f32>, 3>(
|
|
|
|
vec4<f32>(1.0, 0.0, 0.0, 1.0),
|
|
|
|
vec4<f32>(0.0, 1.0, 0.0, 1.0),
|
|
|
|
vec4<f32>(0.0, 0.0, 1.0, 1.0)
|
|
|
|
);
|
|
|
|
|
|
|
|
var position : vec4<f32> = positions[VertexIndex];
|
|
|
|
var color : vec4<f32> = colors[VertexIndex];
|
|
|
|
|
|
|
|
// TODO(dawn:572): Revisit once modf has been reworked in WGSL.
|
|
|
|
var fade : f32 = c.scalarOffset + c.time * c.scalar / 10.0;
|
|
|
|
fade = fade - floor(fade);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (fade < 0.5) {
|
|
|
|
fade = fade * 2.0;
|
|
|
|
} else {
|
|
|
|
fade = (1.0 - fade) * 2.0;
|
|
|
|
}
|
2021-03-23 17:26:20 +00:00
|
|
|
|
|
|
|
var xpos : f32 = position.x * c.scale;
|
|
|
|
var ypos : f32 = position.y * c.scale;
|
2021-04-13 14:52:44 +00:00
|
|
|
let angle : f32 = 3.14159 * 2.0 * fade;
|
|
|
|
let xrot : f32 = xpos * cos(angle) - ypos * sin(angle);
|
|
|
|
let yrot : f32 = xpos * sin(angle) + ypos * cos(angle);
|
2017-04-20 18:38:20 +00:00
|
|
|
xpos = xrot + c.offsetX;
|
|
|
|
ypos = yrot + c.offsetY;
|
2021-03-23 17:26:20 +00:00
|
|
|
|
2021-04-13 14:52:44 +00:00
|
|
|
var output : VertexOut;
|
|
|
|
output.v_color = vec4<f32>(fade, 1.0 - fade, 0.0, 1.0) + color;
|
|
|
|
output.Position = vec4<f32>(xpos, ypos, 0.0, 1.0);
|
|
|
|
return output;
|
2019-05-29 13:16:06 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
|
2021-04-13 14:52:44 +00:00
|
|
|
[[stage(fragment)]] fn main([[location(0)]] v_color : vec4<f32>)
|
|
|
|
-> [[location(0)]] vec4<f32> {
|
|
|
|
return v_color;
|
2017-08-29 17:37:45 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
|
2021-03-18 16:46:58 +00:00
|
|
|
device, {{0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true}});
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2021-05-21 05:01:38 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor;
|
2019-05-29 13:16:06 +00:00
|
|
|
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
|
2021-03-18 16:46:58 +00:00
|
|
|
descriptor.vertex.module = vsModule;
|
|
|
|
descriptor.cFragment.module = fsModule;
|
|
|
|
descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2021-05-21 05:01:38 +00:00
|
|
|
pipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-05-29 13:16:06 +00:00
|
|
|
shaderData.resize(kNumTriangles);
|
2017-04-20 18:38:20 +00:00
|
|
|
for (auto& data : shaderData) {
|
2017-07-11 01:44:06 +00:00
|
|
|
data.scale = RandomFloat(0.2f, 0.4f);
|
2017-04-20 18:38:20 +00:00
|
|
|
data.time = 0.0;
|
2017-07-11 01:44:06 +00:00
|
|
|
data.offsetX = RandomFloat(-0.9f, 0.9f);
|
|
|
|
data.offsetY = RandomFloat(-0.9f, 0.9f);
|
|
|
|
data.scalar = RandomFloat(0.5f, 2.0f);
|
|
|
|
data.scalarOffset = RandomFloat(0.0f, 10.0f);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2019-05-29 13:16:06 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::BufferDescriptor bufferDesc;
|
2019-05-29 13:16:06 +00:00
|
|
|
bufferDesc.size = kNumTriangles * sizeof(ShaderData);
|
2019-10-25 11:36:47 +00:00
|
|
|
bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
|
2019-05-29 13:16:06 +00:00
|
|
|
ubo = device.CreateBuffer(&bufferDesc);
|
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
bindGroup = utils::MakeBindGroup(device, bgl, {{0, ubo, 0, sizeof(ShaderData)}});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
static int f = 0;
|
|
|
|
f++;
|
2019-05-29 13:16:06 +00:00
|
|
|
for (auto& data : shaderData) {
|
|
|
|
data.time = f / 60.0f;
|
|
|
|
}
|
2020-06-02 09:24:39 +00:00
|
|
|
queue.WriteBuffer(ubo, 0, shaderData.data(), kNumTriangles * sizeof(ShaderData));
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-11-12 18:30:11 +00:00
|
|
|
utils::ComboRenderPassDescriptor renderPass({backbufferView});
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
2017-08-11 21:36:20 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(pipeline);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-05-29 13:16:06 +00:00
|
|
|
for (size_t i = 0; i < kNumTriangles; i++) {
|
2019-11-01 15:51:01 +00:00
|
|
|
uint32_t offset = i * sizeof(ShaderData);
|
2019-05-29 13:16:06 +00:00
|
|
|
pass.SetBindGroup(0, bindGroup, 1, &offset);
|
2020-03-31 16:21:35 +00:00
|
|
|
pass.Draw(3);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandBuffer commands = encoder.Finish();
|
2017-08-11 21:36:20 +00:00
|
|
|
queue.Submit(1, &commands);
|
2019-11-12 18:30:11 +00:00
|
|
|
swapchain.Present();
|
2017-07-28 01:30:57 +00:00
|
|
|
DoFlush();
|
2017-04-20 18:38:20 +00:00
|
|
|
fprintf(stderr, "frame %i\n", f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
2021-06-24 19:21:31 +00:00
|
|
|
utils::ScopedAutoreleasePool pool;
|
2017-04-20 18:38:20 +00:00
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|