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 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | nxt::Device device; |
| 20 | |
| 21 | nxt::Buffer indexBuffer; |
| 22 | nxt::Buffer vertexBuffer; |
| 23 | |
| 24 | nxt::Texture texture; |
| 25 | nxt::Sampler sampler; |
| 26 | |
| 27 | nxt::Queue queue; |
| 28 | nxt::Pipeline pipeline; |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 29 | nxt::RenderPass renderpass; |
| 30 | nxt::Framebuffer framebuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | nxt::BindGroup bindGroup; |
| 32 | |
| 33 | void initBuffers() { |
| 34 | static const uint32_t indexData[3] = { |
| 35 | 0, 1, 2, |
| 36 | }; |
| 37 | indexBuffer = device.CreateBufferBuilder() |
| 38 | .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Index) |
| 39 | .SetInitialUsage(nxt::BufferUsageBit::Mapped) |
| 40 | .SetSize(sizeof(indexData)) |
| 41 | .GetResult(); |
| 42 | indexBuffer.SetSubData(0, sizeof(indexData) / sizeof(uint32_t), indexData); |
| 43 | indexBuffer.FreezeUsage(nxt::BufferUsageBit::Index); |
| 44 | |
| 45 | static const float vertexData[12] = { |
| 46 | 0.0f, 0.5f, 0.0f, 1.0f, |
| 47 | -0.5f, -0.5f, 0.0f, 1.0f, |
| 48 | 0.5f, -0.5f, 0.0f, 1.0f, |
| 49 | }; |
| 50 | vertexBuffer = device.CreateBufferBuilder() |
| 51 | .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex) |
| 52 | .SetInitialUsage(nxt::BufferUsageBit::Mapped) |
| 53 | .SetSize(sizeof(vertexData)) |
| 54 | .GetResult(); |
| 55 | vertexBuffer.SetSubData(0, sizeof(vertexData) / sizeof(uint32_t), |
| 56 | reinterpret_cast<const uint32_t*>(vertexData)); |
| 57 | vertexBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex); |
| 58 | } |
| 59 | |
| 60 | void initTextures() { |
| 61 | texture = device.CreateTextureBuilder() |
| 62 | .SetDimension(nxt::TextureDimension::e2D) |
| 63 | .SetExtent(1024, 1024, 1) |
| 64 | .SetFormat(nxt::TextureFormat::R8G8B8A8Unorm) |
| 65 | .SetMipLevels(1) |
| 66 | .SetAllowedUsage(nxt::TextureUsageBit::TransferDst | nxt::TextureUsageBit::Sampled) |
| 67 | .GetResult(); |
| 68 | |
| 69 | sampler = device.CreateSamplerBuilder() |
| 70 | .SetFilterMode(nxt::FilterMode::Linear, nxt::FilterMode::Linear, nxt::FilterMode::Linear) |
| 71 | .GetResult(); |
| 72 | |
| 73 | // Initialize the texture with arbitrary data until we can load images |
| 74 | std::vector<uint8_t> data(4 * 1024 * 1024, 0); |
| 75 | for (size_t i = 0; i < data.size(); ++i) { |
| 76 | data[i] = i % 253; |
| 77 | } |
| 78 | |
| 79 | nxt::Buffer stagingBuffer = device.CreateBufferBuilder() |
| 80 | .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::TransferSrc) |
| 81 | .SetInitialUsage(nxt::BufferUsageBit::Mapped) |
| 82 | .SetSize(data.size()) |
| 83 | .GetResult(); |
| 84 | stagingBuffer.SetSubData(0, data.size() / sizeof(uint32_t), reinterpret_cast<uint32_t*>(data.data())); |
| 85 | stagingBuffer.FreezeUsage(nxt::BufferUsageBit::TransferSrc); |
| 86 | |
| 87 | nxt::CommandBuffer copy = device.CreateCommandBufferBuilder() |
| 88 | .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst) |
Corentin Wallez | 7f433a5 | 2017-05-09 16:57:52 +0200 | [diff] [blame] | 89 | .CopyBufferToTexture(stagingBuffer, 0, texture, 0, 0, 0, 1024, 1024, 1, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 90 | .GetResult(); |
| 91 | |
| 92 | queue.Submit(1, ©); |
| 93 | texture.FreezeUsage(nxt::TextureUsageBit::Sampled); |
| 94 | } |
| 95 | |
| 96 | void init() { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 97 | device = CreateCppNXTDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 98 | |
| 99 | queue = device.CreateQueueBuilder().GetResult(); |
| 100 | |
| 101 | initBuffers(); |
| 102 | initTextures(); |
| 103 | |
| 104 | nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( |
| 105 | #version 450 |
| 106 | layout(location = 0) in vec4 pos; |
| 107 | void main() { |
| 108 | gl_Position = pos; |
| 109 | })" |
| 110 | ); |
| 111 | |
| 112 | nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
| 113 | #version 450 |
| 114 | layout(set = 0, binding = 0) uniform sampler mySampler; |
| 115 | layout(set = 0, binding = 1) uniform texture2D myTexture; |
| 116 | |
| 117 | out vec4 fragColor; |
| 118 | void main() { |
| 119 | fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0)); |
| 120 | })" |
| 121 | ); |
| 122 | |
| 123 | auto inputState = device.CreateInputStateBuilder() |
| 124 | .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0) |
| 125 | .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex) |
| 126 | .GetResult(); |
| 127 | |
| 128 | nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder() |
| 129 | .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler, 0, 1) |
| 130 | .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture, 1, 1) |
| 131 | .GetResult(); |
| 132 | |
| 133 | nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder() |
| 134 | .SetBindGroupLayout(0, bgl) |
| 135 | .GetResult(); |
| 136 | |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 137 | CreateDefaultRenderPass(device, &renderpass, &framebuffer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 138 | pipeline = device.CreatePipelineBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 139 | .SetSubpass(renderpass, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 140 | .SetLayout(pl) |
| 141 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 142 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 143 | .SetInputState(inputState) |
| 144 | .GetResult(); |
| 145 | |
| 146 | nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult(); |
| 147 | |
| 148 | bindGroup = device.CreateBindGroupBuilder() |
| 149 | .SetLayout(bgl) |
| 150 | .SetUsage(nxt::BindGroupUsage::Frozen) |
| 151 | .SetSamplers(0, 1, &sampler) |
| 152 | .SetTextureViews(1, 1, &view) |
| 153 | .GetResult(); |
| 154 | } |
| 155 | |
| 156 | struct {uint32_t a; float b;} s; |
| 157 | void frame() { |
| 158 | s.a = (s.a + 1) % 256; |
| 159 | s.b += 0.02; |
| 160 | if (s.b >= 1.0f) {s.b = 0.0f;} |
| 161 | static const uint32_t vertexBufferOffsets[1] = {0}; |
| 162 | nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 163 | .BeginRenderPass(renderpass, framebuffer) |
| 164 | .SetPipeline(pipeline) |
| 165 | .SetBindGroup(0, bindGroup) |
| 166 | .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets) |
| 167 | .SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32) |
| 168 | .DrawElements(3, 1, 0, 0) |
| 169 | .EndRenderPass() |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 170 | .GetResult(); |
| 171 | |
| 172 | queue.Submit(1, &commands); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 173 | DoSwapBuffers(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int main(int argc, const char* argv[]) { |
| 177 | if (!InitUtils(argc, argv)) { |
| 178 | return 1; |
| 179 | } |
| 180 | init(); |
| 181 | |
| 182 | while (!ShouldQuit()) { |
| 183 | frame(); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 184 | USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // TODO release stuff |
| 188 | } |