Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame^] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | nxtDevice device; |
| 21 | nxtQueue queue; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 22 | nxtSwapChain swapchain; |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 23 | nxtRenderPipeline pipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
Corentin Wallez | e98678f | 2018-01-19 12:51:18 -0500 | [diff] [blame] | 25 | nxtTextureFormat swapChainFormat; |
| 26 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | void init() { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 28 | device = CreateCppNXTDevice().Release(); |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 29 | queue = nxtDeviceCreateQueue(device); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 31 | { |
| 32 | nxtSwapChainBuilder builder = nxtDeviceCreateSwapChainBuilder(device); |
| 33 | uint64_t swapchainImpl = GetSwapChainImplementation(); |
| 34 | nxtSwapChainBuilderSetImplementation(builder, swapchainImpl); |
| 35 | swapchain = nxtSwapChainBuilderGetResult(builder); |
| 36 | nxtSwapChainBuilderRelease(builder); |
| 37 | } |
Corentin Wallez | e98678f | 2018-01-19 12:51:18 -0500 | [diff] [blame] | 38 | swapChainFormat = static_cast<nxtTextureFormat>(GetPreferredSwapChainTextureFormat()); |
| 39 | nxtSwapChainConfigure(swapchain, swapChainFormat, NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640, |
| 40 | 480); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 41 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | const char* vs = |
| 43 | "#version 450\n" |
| 44 | "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n" |
| 45 | "void main() {\n" |
| 46 | " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n" |
| 47 | "}\n"; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 48 | nxtShaderModule vsModule = utils::CreateShaderModule(nxt::Device(device), nxt::ShaderStage::Vertex, vs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 49 | |
| 50 | const char* fs = |
| 51 | "#version 450\n" |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 52 | "layout(location = 0) out vec4 fragColor;" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 53 | "void main() {\n" |
| 54 | " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 55 | "}\n"; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 56 | nxtShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 57 | |
| 58 | { |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 59 | nxtRenderPipelineBuilder builder = nxtDeviceCreateRenderPipelineBuilder(device); |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 60 | nxtRenderPipelineBuilderSetColorAttachmentFormat(builder, 0, swapChainFormat); |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 61 | nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_VERTEX, vsModule, "main"); |
| 62 | nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_FRAGMENT, fsModule, "main"); |
| 63 | pipeline = nxtRenderPipelineBuilderGetResult(builder); |
| 64 | nxtRenderPipelineBuilderRelease(builder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | nxtShaderModuleRelease(vsModule); |
| 68 | nxtShaderModuleRelease(fsModule); |
| 69 | } |
| 70 | |
| 71 | void frame() { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 72 | nxtTexture backbuffer = nxtSwapChainGetNextTexture(swapchain); |
| 73 | nxtTextureView backbufferView; |
| 74 | { |
| 75 | nxtTextureViewBuilder builder = nxtTextureCreateTextureViewBuilder(backbuffer); |
| 76 | backbufferView = nxtTextureViewBuilderGetResult(builder); |
| 77 | nxtTextureViewBuilderRelease(builder); |
| 78 | } |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 79 | nxtRenderPassDescriptor renderpassInfo; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 80 | { |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 81 | nxtRenderPassDescriptorBuilder builder = nxtDeviceCreateRenderPassDescriptorBuilder(device); |
| 82 | nxtRenderPassDescriptorBuilderSetColorAttachment(builder, 0, backbufferView, NXT_LOAD_OP_CLEAR); |
| 83 | renderpassInfo = nxtRenderPassDescriptorBuilderGetResult(builder); |
| 84 | nxtRenderPassDescriptorBuilderRelease(builder); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 85 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 86 | nxtCommandBuffer commands; |
| 87 | { |
| 88 | nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device); |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 89 | nxtCommandBufferBuilderBeginRenderPass(builder, renderpassInfo); |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 90 | nxtCommandBufferBuilderSetRenderPipeline(builder, pipeline); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 91 | nxtCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0); |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 92 | nxtCommandBufferBuilderEndRenderPass(builder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 93 | commands = nxtCommandBufferBuilderGetResult(builder); |
| 94 | nxtCommandBufferBuilderRelease(builder); |
| 95 | } |
| 96 | |
| 97 | nxtQueueSubmit(queue, 1, &commands); |
| 98 | nxtCommandBufferRelease(commands); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 99 | nxtSwapChainPresent(swapchain, backbuffer); |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 100 | nxtRenderPassDescriptorRelease(renderpassInfo); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 101 | nxtTextureViewRelease(backbufferView); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 102 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 103 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 107 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | return 1; |
| 109 | } |
| 110 | init(); |
| 111 | |
| 112 | while (!ShouldQuit()) { |
| 113 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 114 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // TODO release stuff |
| 118 | } |