blob: b6b7110189127cd77286a17041d28611c06ac9b9 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
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 Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040017#include "utils/NXTHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040020nxtDevice device;
21nxtQueue queue;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070022nxtSwapChain swapchain;
Corentin Wallez66ff4472017-07-14 11:32:57 -040023nxtRenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Walleze98678f2018-01-19 12:51:18 -050025nxtTextureFormat swapChainFormat;
26
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027void init() {
Corentin Wallez931e6e82017-06-16 18:51:14 -040028 device = CreateCppNXTDevice().Release();
Corentin Wallezb703def2018-06-14 20:26:27 -040029 queue = nxtDeviceCreateQueue(device);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031 {
32 nxtSwapChainBuilder builder = nxtDeviceCreateSwapChainBuilder(device);
33 uint64_t swapchainImpl = GetSwapChainImplementation();
34 nxtSwapChainBuilderSetImplementation(builder, swapchainImpl);
35 swapchain = nxtSwapChainBuilderGetResult(builder);
36 nxtSwapChainBuilderRelease(builder);
37 }
Corentin Walleze98678f2018-01-19 12:51:18 -050038 swapChainFormat = static_cast<nxtTextureFormat>(GetPreferredSwapChainTextureFormat());
39 nxtSwapChainConfigure(swapchain, swapChainFormat, NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
40 480);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070041
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040042 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 Wallez5ee7afd2017-06-19 13:09:41 -040048 nxtShaderModule vsModule = utils::CreateShaderModule(nxt::Device(device), nxt::ShaderStage::Vertex, vs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049
50 const char* fs =
51 "#version 450\n"
Corentin Wallezb6fb5f32017-08-29 13:37:45 -040052 "layout(location = 0) out vec4 fragColor;"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040053 "void main() {\n"
54 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
55 "}\n";
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040056 nxtShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
58 {
Corentin Wallez66ff4472017-07-14 11:32:57 -040059 nxtRenderPipelineBuilder builder = nxtDeviceCreateRenderPipelineBuilder(device);
Corentin Wallez6f7749c2018-05-02 18:10:13 -040060 nxtRenderPipelineBuilderSetColorAttachmentFormat(builder, 0, swapChainFormat);
Corentin Wallez66ff4472017-07-14 11:32:57 -040061 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 Wallezf07e3bd2017-04-20 14:38:20 -040065 }
66
67 nxtShaderModuleRelease(vsModule);
68 nxtShaderModuleRelease(fsModule);
69}
70
71void frame() {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070072 nxtTexture backbuffer = nxtSwapChainGetNextTexture(swapchain);
73 nxtTextureView backbufferView;
74 {
75 nxtTextureViewBuilder builder = nxtTextureCreateTextureViewBuilder(backbuffer);
76 backbufferView = nxtTextureViewBuilderGetResult(builder);
77 nxtTextureViewBuilderRelease(builder);
78 }
Corentin Wallez8d6b5d22018-05-11 13:04:44 -040079 nxtRenderPassDescriptor renderpassInfo;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070080 {
Corentin Wallez8d6b5d22018-05-11 13:04:44 -040081 nxtRenderPassDescriptorBuilder builder = nxtDeviceCreateRenderPassDescriptorBuilder(device);
82 nxtRenderPassDescriptorBuilderSetColorAttachment(builder, 0, backbufferView, NXT_LOAD_OP_CLEAR);
83 renderpassInfo = nxtRenderPassDescriptorBuilderGetResult(builder);
84 nxtRenderPassDescriptorBuilderRelease(builder);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070085 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086 nxtCommandBuffer commands;
87 {
88 nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device);
Corentin Wallez6f7749c2018-05-02 18:10:13 -040089 nxtCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
Corentin Wallez66ff4472017-07-14 11:32:57 -040090 nxtCommandBufferBuilderSetRenderPipeline(builder, pipeline);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091 nxtCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0);
Kai Ninomiya68df8b02017-05-16 14:04:22 -070092 nxtCommandBufferBuilderEndRenderPass(builder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093 commands = nxtCommandBufferBuilderGetResult(builder);
94 nxtCommandBufferBuilderRelease(builder);
95 }
96
97 nxtQueueSubmit(queue, 1, &commands);
98 nxtCommandBufferRelease(commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070099 nxtSwapChainPresent(swapchain, backbuffer);
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400100 nxtRenderPassDescriptorRelease(renderpassInfo);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700101 nxtTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400102
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700103 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104}
105
106int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400107 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400108 return 1;
109 }
110 init();
111
112 while (!ShouldQuit()) {
113 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400114 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400115 }
116
117 // TODO release stuff
118}