-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animometer.cpp
189 lines (156 loc) · 5.71 KB
/
Animometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2017 The Dawn Authors
//
// 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.
#include "utils/SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include <cstdlib>
#include <cstdio>
#include <vector>
dawn::Device device;
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::RenderPipeline pipeline;
dawn::BindGroup bindGroup;
dawn::Buffer ubo;
float RandomFloat(float min, float max) {
float zeroOne = rand() / float(RAND_MAX);
return zeroOne * (max - min) + min;
}
constexpr size_t kNumTriangles = 10000;
struct alignas(kMinDynamicBufferOffsetAlignment) ShaderData {
float scale;
float time;
float offsetX;
float offsetY;
float scalar;
float scalarOffset;
};
static std::vector<ShaderData> shaderData;
void init() {
device = CreateCppDawnDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
640, 480);
dawn::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(std140, set = 0, binding = 0) uniform Constants {
float scale;
float time;
float offsetX;
float offsetY;
float scalar;
float scalarOffset;
} c;
layout(location = 0) out vec4 v_color;
const vec4 positions[3] = vec4[3](
vec4( 0.0f, 0.1f, 0.0f, 1.0f),
vec4(-0.1f, -0.1f, 0.0f, 1.0f),
vec4( 0.1f, -0.1f, 0.0f, 1.0f)
);
const vec4 colors[3] = vec4[3](
vec4(1.0f, 0.0f, 0.0f, 1.0f),
vec4(0.0f, 1.0f, 0.0f, 1.0f),
vec4(0.0f, 0.0f, 1.0f, 1.0f)
);
void main() {
vec4 position = positions[gl_VertexIndex];
vec4 color = colors[gl_VertexIndex];
float fade = mod(c.scalarOffset + c.time * c.scalar / 10.0, 1.0);
if (fade < 0.5) {
fade = fade * 2.0;
} else {
fade = (1.0 - fade) * 2.0;
}
float xpos = position.x * c.scale;
float ypos = position.y * c.scale;
float angle = 3.14159 * 2.0 * fade;
float xrot = xpos * cos(angle) - ypos * sin(angle);
float yrot = xpos * sin(angle) + ypos * cos(angle);
xpos = xrot + c.offsetX;
ypos = yrot + c.offsetY;
v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color;
gl_Position = vec4(xpos, ypos, 0.0, 1.0);
})");
dawn::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
layout(location = 0) in vec4 v_color;
void main() {
fragColor = v_color;
})");
dawn::BindGroupLayout bgl = utils::MakeBindGroupLayout(
device, {{0, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer, true}});
utils::ComboRenderPipelineDescriptor descriptor(device);
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
descriptor.vertexStage.module = vsModule;
descriptor.cFragmentStage.module = fsModule;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
pipeline = device.CreateRenderPipeline(&descriptor);
shaderData.resize(kNumTriangles);
for (auto& data : shaderData) {
data.scale = RandomFloat(0.2f, 0.4f);
data.time = 0.0;
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);
}
dawn::BufferDescriptor bufferDesc;
bufferDesc.size = kNumTriangles * sizeof(ShaderData);
bufferDesc.usage = dawn::BufferUsage::CopyDst | dawn::BufferUsage::Uniform;
ubo = device.CreateBuffer(&bufferDesc);
bindGroup =
utils::MakeBindGroup(device, bgl, {{0, ubo, 0, sizeof(ShaderData)}});
}
void frame() {
dawn::Texture backbuffer = swapchain.GetNextTexture();
static int f = 0;
f++;
for (auto& data : shaderData) {
data.time = f / 60.0f;
}
ubo.SetSubData(0, kNumTriangles * sizeof(ShaderData), shaderData.data());
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()});
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(pipeline);
for (size_t i = 0; i < kNumTriangles; i++) {
uint64_t offset = i * sizeof(ShaderData);
pass.SetBindGroup(0, bindGroup, 1, &offset);
pass.Draw(3, 1, 0, 0);
}
pass.EndPass();
}
dawn::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
swapchain.Present(backbuffer);
DoFlush();
fprintf(stderr, "frame %i\n", f);
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}