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"
|
2018-07-18 12:00:56 +00:00
|
|
|
#include "utils/DawnHelpers.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Device device;
|
|
|
|
dawn::Queue queue;
|
|
|
|
dawn::SwapChain swapchain;
|
|
|
|
dawn::TextureView depthStencilView;
|
|
|
|
dawn::RenderPipeline pipeline;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ShaderData {
|
|
|
|
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
|
|
|
|
2018-06-15 00:26:27 +00:00
|
|
|
queue = device.CreateQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-09-21 16:54:53 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(push_constant) uniform ConstantsBlock {
|
|
|
|
float scale;
|
|
|
|
float time;
|
|
|
|
float offsetX;
|
|
|
|
float offsetY;
|
|
|
|
float scalar;
|
|
|
|
float scalarOffset;
|
|
|
|
} c;
|
|
|
|
|
2017-06-05 20:23:18 +00:00
|
|
|
layout(location = 0) out vec4 v_color;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
2017-08-29 17:37:45 +00:00
|
|
|
layout(location = 0) out vec4 fragColor;
|
2017-06-05 20:23:18 +00:00
|
|
|
layout(location = 0) in vec4 v_color;
|
2017-04-20 18:38:20 +00:00
|
|
|
void main() {
|
|
|
|
fragColor = v_color;
|
2017-08-29 17:37:45 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor(device);
|
|
|
|
descriptor.cVertexStage.module = vsModule;
|
|
|
|
descriptor.cFragmentStage.module = fsModule;
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = &descriptor.cDepthStencilState;
|
|
|
|
descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint;
|
2019-02-20 13:00:36 +00:00
|
|
|
descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
pipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
shaderData.resize(10000);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Texture backbuffer;
|
|
|
|
dawn::RenderPassDescriptor renderPass;
|
2018-05-11 17:04:44 +00:00
|
|
|
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
static int f = 0;
|
|
|
|
f++;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
2017-08-11 21:36:20 +00:00
|
|
|
{
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(pipeline);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-08-11 21:36:20 +00:00
|
|
|
for (int k = 0; k < 10000; k++) {
|
2017-04-20 18:38:20 +00:00
|
|
|
shaderData[i].time = f / 60.0f;
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetPushConstants(dawn::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i]));
|
2018-12-10 05:20:19 +00:00
|
|
|
pass.Draw(3, 1, 0, 0);
|
2017-04-20 18:38:20 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandBuffer commands = encoder.Finish();
|
2017-08-11 21:36:20 +00:00
|
|
|
queue.Submit(1, &commands);
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain.Present(backbuffer);
|
|
|
|
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()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|