blob: 1ee720705deec221a80ca2eac07e2dd05f97083e [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// Copyright 2017 The NXT Authors
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 Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040017#include "utils/NXTHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040020#include <cstdlib>
21#include <cstdio>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040022#include <vector>
23
24nxt::Device device;
25nxt::Queue queue;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070026nxt::SwapChain swapchain;
27nxt::TextureView depthStencilView;
Corentin Wallez66ff4472017-07-14 11:32:57 -040028nxt::RenderPipeline pipeline;
Kai Ninomiya68df8b02017-05-16 14:04:22 -070029nxt::RenderPass renderpass;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
31float RandomFloat(float min, float max) {
32 float zeroOne = rand() / float(RAND_MAX);
33 return zeroOne * (max - min) + min;
34}
35
36struct ShaderData {
37 float scale;
38 float time;
39 float offsetX;
40 float offsetY;
41 float scalar;
42 float scalarOffset;
43};
44
45static std::vector<ShaderData> shaderData;
46
47void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -070048 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049
50 queue = device.CreateQueueBuilder().GetResult();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070051 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040052 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
53 nxt::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040054
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040055 nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040056 #version 450
57
58 layout(push_constant) uniform ConstantsBlock {
59 float scale;
60 float time;
61 float offsetX;
62 float offsetY;
63 float scalar;
64 float scalarOffset;
65 } c;
66
Corentin Wallezca9af202017-06-05 16:23:18 -040067 layout(location = 0) out vec4 v_color;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040068
69 const vec4 positions[3] = vec4[3](
70 vec4( 0.0f, 0.1f, 0.0f, 1.0f),
71 vec4(-0.1f, -0.1f, 0.0f, 1.0f),
72 vec4( 0.1f, -0.1f, 0.0f, 1.0f)
73 );
74
75 const vec4 colors[3] = vec4[3](
76 vec4(1.0f, 0.0f, 0.0f, 1.0f),
77 vec4(0.0f, 1.0f, 0.0f, 1.0f),
78 vec4(0.0f, 0.0f, 1.0f, 1.0f)
79 );
80
81 void main() {
82 vec4 position = positions[gl_VertexIndex];
83 vec4 color = colors[gl_VertexIndex];
84
85 float fade = mod(c.scalarOffset + c.time * c.scalar / 10.0, 1.0);
86 if (fade < 0.5) {
87 fade = fade * 2.0;
88 } else {
89 fade = (1.0 - fade) * 2.0;
90 }
91 float xpos = position.x * c.scale;
92 float ypos = position.y * c.scale;
93 float angle = 3.14159 * 2.0 * fade;
94 float xrot = xpos * cos(angle) - ypos * sin(angle);
95 float yrot = xpos * sin(angle) + ypos * cos(angle);
96 xpos = xrot + c.offsetX;
97 ypos = yrot + c.offsetY;
98 v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color;
99 gl_Position = vec4(xpos, ypos, 0.0, 1.0);
100 })"
101 );
102
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400103 nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104 #version 450
105 out vec4 fragColor;
Corentin Wallezca9af202017-06-05 16:23:18 -0400106 layout(location = 0) in vec4 v_color;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400107 void main() {
108 fragColor = v_color;
109 })"
110 );
111
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700112 renderpass = CreateDefaultRenderPass(device);
113 depthStencilView = CreateDefaultDepthStencilView(device);
114
Corentin Wallez66ff4472017-07-14 11:32:57 -0400115 pipeline = device.CreateRenderPipelineBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700116 .SetSubpass(renderpass, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
118 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
119 .GetResult();
120
121 shaderData.resize(10000);
122 for (auto& data : shaderData) {
Corentin Wallez83e779d2017-07-10 21:44:06 -0400123 data.scale = RandomFloat(0.2f, 0.4f);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124 data.time = 0.0;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400125 data.offsetX = RandomFloat(-0.9f, 0.9f);
126 data.offsetY = RandomFloat(-0.9f, 0.9f);
127 data.scalar = RandomFloat(0.5f, 2.0f);
128 data.scalarOffset = RandomFloat(0.0f, 10.0f);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400129 }
130}
131
132void frame() {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700133 nxt::Texture backbuffer;
134 nxt::Framebuffer framebuffer;
135 GetNextFramebuffer(device, renderpass, swapchain, depthStencilView, &backbuffer, &framebuffer);
136
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400137 static int f = 0;
138 f++;
139
140 size_t i = 0;
141
Kai Ninomiyab9854312017-08-11 14:36:20 -0700142 nxt::CommandBuffer commands;
143 {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144 nxt::CommandBufferBuilder builder = device.CreateCommandBufferBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700145 .BeginRenderPass(renderpass, framebuffer)
Kai Ninomiyafa37f222017-06-29 23:53:08 -0700146 .BeginRenderSubpass()
Corentin Wallez66ff4472017-07-14 11:32:57 -0400147 .SetRenderPipeline(pipeline)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400148 .Clone();
149
Kai Ninomiyab9854312017-08-11 14:36:20 -0700150 for (int k = 0; k < 10000; k++) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151 shaderData[i].time = f / 60.0f;
152 builder.SetPushConstants(nxt::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i]))
153 .DrawArrays(3, 1, 0, 0);
154 i++;
155 }
156
Kai Ninomiyafa37f222017-06-29 23:53:08 -0700157 builder.EndRenderSubpass();
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700158 builder.EndRenderPass();
Kai Ninomiyab9854312017-08-11 14:36:20 -0700159 commands = builder.GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400160 }
161
Kai Ninomiyab9854312017-08-11 14:36:20 -0700162 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700163 backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
164 swapchain.Present(backbuffer);
165 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400166 fprintf(stderr, "frame %i\n", f);
167}
168
169int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400170 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400171 return 1;
172 }
173 init();
174
175 while (!ShouldQuit()) {
176 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400177 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400178 }
179
180 // TODO release stuff
181}