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 | nxtDevice device; |
| 21 | nxtQueue queue; |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 22 | nxtRenderPipeline pipeline; |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 23 | nxtRenderPass renderpass; |
| 24 | nxtFramebuffer framebuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 25 | |
| 26 | void init() { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 27 | device = CreateCppNXTDevice().Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 28 | |
| 29 | { |
| 30 | nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device); |
| 31 | queue = nxtQueueBuilderGetResult(builder); |
| 32 | nxtQueueBuilderRelease(builder); |
| 33 | } |
| 34 | |
| 35 | const char* vs = |
| 36 | "#version 450\n" |
| 37 | "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n" |
| 38 | "void main() {\n" |
| 39 | " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n" |
| 40 | "}\n"; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 41 | nxtShaderModule vsModule = utils::CreateShaderModule(nxt::Device(device), nxt::ShaderStage::Vertex, vs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | |
| 43 | const char* fs = |
| 44 | "#version 450\n" |
| 45 | "out vec4 fragColor;" |
| 46 | "void main() {\n" |
| 47 | " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 48 | "}\n"; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 49 | nxtShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 50 | |
| 51 | { |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 52 | nxtRenderPassBuilder builder = nxtDeviceCreateRenderPassBuilder(device); |
| 53 | nxtRenderPassBuilderSetAttachmentCount(builder, 1); |
| 54 | nxtRenderPassBuilderAttachmentSetFormat(builder, 0, NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM); |
| 55 | nxtRenderPassBuilderSetSubpassCount(builder, 1); |
| 56 | nxtRenderPassBuilderSubpassSetColorAttachment(builder, 0, 0, 0); |
| 57 | renderpass = nxtRenderPassBuilderGetResult(builder); |
| 58 | nxtRenderPassBuilderRelease(builder); |
| 59 | } |
| 60 | { |
| 61 | nxtFramebufferBuilder builder = nxtDeviceCreateFramebufferBuilder(device); |
| 62 | nxtFramebufferBuilderSetRenderPass(builder, renderpass); |
| 63 | nxtFramebufferBuilderSetDimensions(builder, 640, 480); |
| 64 | framebuffer = nxtFramebufferBuilderGetResult(builder); |
| 65 | nxtFramebufferBuilderRelease(builder); |
| 66 | } |
| 67 | { |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 68 | nxtRenderPipelineBuilder builder = nxtDeviceCreateRenderPipelineBuilder(device); |
| 69 | nxtRenderPipelineBuilderSetSubpass(builder, renderpass, 0); |
| 70 | nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_VERTEX, vsModule, "main"); |
| 71 | nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_FRAGMENT, fsModule, "main"); |
| 72 | pipeline = nxtRenderPipelineBuilderGetResult(builder); |
| 73 | nxtRenderPipelineBuilderRelease(builder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | nxtShaderModuleRelease(vsModule); |
| 77 | nxtShaderModuleRelease(fsModule); |
| 78 | } |
| 79 | |
| 80 | void frame() { |
| 81 | nxtCommandBuffer commands; |
| 82 | { |
| 83 | nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device); |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 84 | nxtCommandBufferBuilderBeginRenderPass(builder, renderpass, framebuffer); |
Kai Ninomiya | fec8c58 | 2017-07-17 12:03:16 -0400 | [diff] [blame] | 85 | nxtCommandBufferBuilderBeginRenderSubpass(builder); |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 86 | nxtCommandBufferBuilderSetRenderPipeline(builder, pipeline); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 87 | nxtCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0); |
Kai Ninomiya | fec8c58 | 2017-07-17 12:03:16 -0400 | [diff] [blame] | 88 | nxtCommandBufferBuilderEndRenderSubpass(builder); |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 89 | nxtCommandBufferBuilderEndRenderPass(builder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 90 | commands = nxtCommandBufferBuilderGetResult(builder); |
| 91 | nxtCommandBufferBuilderRelease(builder); |
| 92 | } |
| 93 | |
| 94 | nxtQueueSubmit(queue, 1, &commands); |
| 95 | nxtCommandBufferRelease(commands); |
| 96 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 97 | DoSwapBuffers(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 101 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 102 | return 1; |
| 103 | } |
| 104 | init(); |
| 105 | |
| 106 | while (!ShouldQuit()) { |
| 107 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame^] | 108 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // TODO release stuff |
| 112 | } |