Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | #include "Utils.h" |
| 16 | |
| 17 | #include <cstdlib> |
| 18 | #include <cstdio> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | nxt::Device device; |
| 22 | nxt::Queue queue; |
| 23 | nxt::Pipeline pipeline; |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 24 | nxt::RenderPass renderpass; |
| 25 | nxt::Framebuffer framebuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 26 | |
| 27 | float RandomFloat(float min, float max) { |
| 28 | float zeroOne = rand() / float(RAND_MAX); |
| 29 | return zeroOne * (max - min) + min; |
| 30 | } |
| 31 | |
| 32 | struct ShaderData { |
| 33 | float scale; |
| 34 | float time; |
| 35 | float offsetX; |
| 36 | float offsetY; |
| 37 | float scalar; |
| 38 | float scalarOffset; |
| 39 | }; |
| 40 | |
| 41 | static std::vector<ShaderData> shaderData; |
| 42 | |
| 43 | void init() { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame^] | 44 | device = CreateCppNXTDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 45 | |
| 46 | queue = device.CreateQueueBuilder().GetResult(); |
| 47 | |
| 48 | nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( |
| 49 | #version 450 |
| 50 | |
| 51 | layout(push_constant) uniform ConstantsBlock { |
| 52 | float scale; |
| 53 | float time; |
| 54 | float offsetX; |
| 55 | float offsetY; |
| 56 | float scalar; |
| 57 | float scalarOffset; |
| 58 | } c; |
| 59 | |
| 60 | out vec4 v_color; |
| 61 | |
| 62 | const vec4 positions[3] = vec4[3]( |
| 63 | vec4( 0.0f, 0.1f, 0.0f, 1.0f), |
| 64 | vec4(-0.1f, -0.1f, 0.0f, 1.0f), |
| 65 | vec4( 0.1f, -0.1f, 0.0f, 1.0f) |
| 66 | ); |
| 67 | |
| 68 | const vec4 colors[3] = vec4[3]( |
| 69 | vec4(1.0f, 0.0f, 0.0f, 1.0f), |
| 70 | vec4(0.0f, 1.0f, 0.0f, 1.0f), |
| 71 | vec4(0.0f, 0.0f, 1.0f, 1.0f) |
| 72 | ); |
| 73 | |
| 74 | void main() { |
| 75 | vec4 position = positions[gl_VertexIndex]; |
| 76 | vec4 color = colors[gl_VertexIndex]; |
| 77 | |
| 78 | float fade = mod(c.scalarOffset + c.time * c.scalar / 10.0, 1.0); |
| 79 | if (fade < 0.5) { |
| 80 | fade = fade * 2.0; |
| 81 | } else { |
| 82 | fade = (1.0 - fade) * 2.0; |
| 83 | } |
| 84 | float xpos = position.x * c.scale; |
| 85 | float ypos = position.y * c.scale; |
| 86 | float angle = 3.14159 * 2.0 * fade; |
| 87 | float xrot = xpos * cos(angle) - ypos * sin(angle); |
| 88 | float yrot = xpos * sin(angle) + ypos * cos(angle); |
| 89 | xpos = xrot + c.offsetX; |
| 90 | ypos = yrot + c.offsetY; |
| 91 | v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color; |
| 92 | gl_Position = vec4(xpos, ypos, 0.0, 1.0); |
| 93 | })" |
| 94 | ); |
| 95 | |
| 96 | nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
| 97 | #version 450 |
| 98 | out vec4 fragColor; |
| 99 | in vec4 v_color; |
| 100 | void main() { |
| 101 | fragColor = v_color; |
| 102 | })" |
| 103 | ); |
| 104 | |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 105 | CreateDefaultRenderPass(device, &renderpass, &framebuffer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 106 | pipeline = device.CreatePipelineBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 107 | .SetSubpass(renderpass, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 109 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 110 | .GetResult(); |
| 111 | |
| 112 | shaderData.resize(10000); |
| 113 | for (auto& data : shaderData) { |
| 114 | data.scale = RandomFloat(0.2, 0.4); |
| 115 | data.time = 0.0; |
| 116 | data.offsetX = RandomFloat(-0.9, 0.9); |
| 117 | data.offsetY = RandomFloat(-0.9, 0.9); |
| 118 | data.scalar = RandomFloat(0.5, 2.0); |
| 119 | data.scalarOffset = RandomFloat(0.0, 10.0); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void frame() { |
| 124 | static int f = 0; |
| 125 | f++; |
| 126 | |
| 127 | size_t i = 0; |
| 128 | |
| 129 | std::vector<nxt::CommandBuffer> commands(50); |
| 130 | for (int j = 0; j < 50; j++) { |
| 131 | |
| 132 | nxt::CommandBufferBuilder builder = device.CreateCommandBufferBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 133 | .BeginRenderPass(renderpass, framebuffer) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 134 | .SetPipeline(pipeline) |
| 135 | .Clone(); |
| 136 | |
| 137 | for (int k = 0; k < 200; k++) { |
| 138 | |
| 139 | shaderData[i].time = f / 60.0f; |
| 140 | builder.SetPushConstants(nxt::ShaderStageBit::Vertex, 0, 6, reinterpret_cast<uint32_t*>(&shaderData[i])) |
| 141 | .DrawArrays(3, 1, 0, 0); |
| 142 | i++; |
| 143 | } |
| 144 | |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 145 | builder.EndRenderPass(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 146 | commands[j] = builder.GetResult(); |
| 147 | } |
| 148 | |
| 149 | queue.Submit(50, commands.data()); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame^] | 150 | DoSwapBuffers(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 151 | fprintf(stderr, "frame %i\n", f); |
| 152 | } |
| 153 | |
| 154 | int main(int argc, const char* argv[]) { |
| 155 | if (!InitUtils(argc, argv)) { |
| 156 | return 1; |
| 157 | } |
| 158 | init(); |
| 159 | |
| 160 | while (!ShouldQuit()) { |
| 161 | frame(); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame^] | 162 | USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // TODO release stuff |
| 166 | } |