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