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