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 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 17 | #include "utils/NXTHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 19 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | nxt::Device device; |
| 23 | |
| 24 | nxt::Buffer indexBuffer; |
| 25 | nxt::Buffer vertexBuffer; |
| 26 | |
| 27 | nxt::Texture texture; |
| 28 | nxt::Sampler sampler; |
| 29 | |
| 30 | nxt::Queue queue; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 31 | nxt::SwapChain swapchain; |
| 32 | nxt::TextureView depthStencilView; |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 33 | nxt::RenderPipeline pipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | nxt::BindGroup bindGroup; |
| 35 | |
| 36 | void initBuffers() { |
| 37 | static const uint32_t indexData[3] = { |
| 38 | 0, 1, 2, |
| 39 | }; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 40 | indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 41 | |
| 42 | static const float vertexData[12] = { |
| 43 | 0.0f, 0.5f, 0.0f, 1.0f, |
| 44 | -0.5f, -0.5f, 0.0f, 1.0f, |
| 45 | 0.5f, -0.5f, 0.0f, 1.0f, |
| 46 | }; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 47 | vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void initTextures() { |
| 51 | texture = device.CreateTextureBuilder() |
| 52 | .SetDimension(nxt::TextureDimension::e2D) |
| 53 | .SetExtent(1024, 1024, 1) |
| 54 | .SetFormat(nxt::TextureFormat::R8G8B8A8Unorm) |
| 55 | .SetMipLevels(1) |
| 56 | .SetAllowedUsage(nxt::TextureUsageBit::TransferDst | nxt::TextureUsageBit::Sampled) |
| 57 | .GetResult(); |
| 58 | |
Corentin Wallez | b711b9b | 2018-05-22 14:50:59 -0400 | [diff] [blame] | 59 | nxt::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor(); |
| 60 | sampler = device.CreateSampler(&samplerDesc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 61 | |
| 62 | // Initialize the texture with arbitrary data until we can load images |
| 63 | std::vector<uint8_t> data(4 * 1024 * 1024, 0); |
| 64 | for (size_t i = 0; i < data.size(); ++i) { |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 65 | data[i] = static_cast<uint8_t>(i % 253); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 69 | nxt::Buffer stagingBuffer = utils::CreateFrozenBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), nxt::BufferUsageBit::TransferSrc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 70 | nxt::CommandBuffer copy = device.CreateCommandBufferBuilder() |
| 71 | .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst) |
Austin Eng | c5f8e7a | 2017-07-13 11:19:14 -0400 | [diff] [blame] | 72 | .CopyBufferToTexture(stagingBuffer, 0, 0, texture, 0, 0, 0, 1024, 1024, 1, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 73 | .GetResult(); |
| 74 | |
| 75 | queue.Submit(1, ©); |
| 76 | texture.FreezeUsage(nxt::TextureUsageBit::Sampled); |
| 77 | } |
| 78 | |
| 79 | void init() { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 80 | device = CreateCppNXTDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 81 | |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 82 | queue = device.CreateQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 83 | swapchain = GetSwapChain(device); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 84 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), |
| 85 | nxt::TextureUsageBit::OutputAttachment, 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 86 | |
| 87 | initBuffers(); |
| 88 | initTextures(); |
| 89 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 90 | nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 91 | #version 450 |
| 92 | layout(location = 0) in vec4 pos; |
| 93 | void main() { |
| 94 | gl_Position = pos; |
| 95 | })" |
| 96 | ); |
| 97 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 98 | nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 99 | #version 450 |
| 100 | layout(set = 0, binding = 0) uniform sampler mySampler; |
| 101 | layout(set = 0, binding = 1) uniform texture2D myTexture; |
| 102 | |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 103 | layout(location = 0) out vec4 fragColor; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 104 | void main() { |
| 105 | fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0)); |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 106 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 107 | |
| 108 | auto inputState = device.CreateInputStateBuilder() |
| 109 | .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0) |
| 110 | .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex) |
| 111 | .GetResult(); |
| 112 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame^] | 113 | auto bgl = utils::MakeBindGroupLayout( |
| 114 | device, { |
| 115 | {0, nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler}, |
| 116 | {1, nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture}, |
| 117 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 118 | |
Kai Ninomiya | f53f98b | 2018-06-27 16:21:39 -0700 | [diff] [blame] | 119 | nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 120 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 121 | depthStencilView = CreateDefaultDepthStencilView(device); |
| 122 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 123 | pipeline = device.CreateRenderPipelineBuilder() |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 124 | .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat()) |
| 125 | .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 126 | .SetLayout(pl) |
| 127 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 128 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
Corentin Wallez | 405dcd6 | 2017-09-19 13:38:05 -0400 | [diff] [blame] | 129 | .SetIndexFormat(nxt::IndexFormat::Uint32) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 130 | .SetInputState(inputState) |
| 131 | .GetResult(); |
| 132 | |
| 133 | nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult(); |
| 134 | |
| 135 | bindGroup = device.CreateBindGroupBuilder() |
| 136 | .SetLayout(bgl) |
| 137 | .SetUsage(nxt::BindGroupUsage::Frozen) |
| 138 | .SetSamplers(0, 1, &sampler) |
| 139 | .SetTextureViews(1, 1, &view) |
| 140 | .GetResult(); |
| 141 | } |
| 142 | |
| 143 | struct {uint32_t a; float b;} s; |
| 144 | void frame() { |
| 145 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 146 | s.b += 0.02f; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 147 | if (s.b >= 1.0f) {s.b = 0.0f;} |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 148 | |
| 149 | nxt::Texture backbuffer; |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 150 | nxt::RenderPassDescriptor renderPass; |
| 151 | GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 152 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 153 | static const uint32_t vertexBufferOffsets[1] = {0}; |
| 154 | nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 155 | .BeginRenderPass(renderPass) |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 156 | .SetRenderPipeline(pipeline) |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 157 | .SetBindGroup(0, bindGroup) |
| 158 | .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets) |
Corentin Wallez | 405dcd6 | 2017-09-19 13:38:05 -0400 | [diff] [blame] | 159 | .SetIndexBuffer(indexBuffer, 0) |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 160 | .DrawElements(3, 1, 0, 0) |
| 161 | .EndRenderPass() |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 162 | .GetResult(); |
| 163 | |
| 164 | queue.Submit(1, &commands); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 165 | backbuffer.TransitionUsage(nxt::TextureUsageBit::Present); |
| 166 | swapchain.Present(backbuffer); |
| 167 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 171 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 172 | return 1; |
| 173 | } |
| 174 | init(); |
| 175 | |
| 176 | while (!ShouldQuit()) { |
| 177 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 178 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // TODO release stuff |
| 182 | } |