blob: 8fc4ceed5807938f769aae55775195156f31272f [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 Wallezf6840402018-07-18 14:00:56 +020017#include "utils/DawnHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezb1669e32018-07-18 15:12:52 +020020dawnDevice device;
21dawnQueue queue;
22dawnSwapChain swapchain;
23dawnRenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Wallezb1669e32018-07-18 15:12:52 +020025dawnTextureFormat swapChainFormat;
Corentin Walleze98678f2018-01-19 12:51:18 -050026
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020028 device = CreateCppDawnDevice().Release();
Corentin Wallezb1669e32018-07-18 15:12:52 +020029 queue = dawnDeviceCreateQueue(device);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031 {
Corentin Wallezb1669e32018-07-18 15:12:52 +020032 dawnSwapChainBuilder builder = dawnDeviceCreateSwapChainBuilder(device);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070033 uint64_t swapchainImpl = GetSwapChainImplementation();
Corentin Wallezb1669e32018-07-18 15:12:52 +020034 dawnSwapChainBuilderSetImplementation(builder, swapchainImpl);
35 swapchain = dawnSwapChainBuilderGetResult(builder);
36 dawnSwapChainBuilderRelease(builder);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070037 }
Corentin Wallezb1669e32018-07-18 15:12:52 +020038 swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat());
39 dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
Corentin Walleze98678f2018-01-19 12:51:18 -050040 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 Wallezb1669e32018-07-18 15:12:52 +020048 dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::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 Wallezb1669e32018-07-18 15:12:52 +020056 dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
58 {
Corentin Wallezb1669e32018-07-18 15:12:52 +020059 dawnRenderPipelineBuilder builder = dawnDeviceCreateRenderPipelineBuilder(device);
60 dawnRenderPipelineBuilderSetColorAttachmentFormat(builder, 0, swapChainFormat);
61 dawnRenderPipelineBuilderSetStage(builder, DAWN_SHADER_STAGE_VERTEX, vsModule, "main");
62 dawnRenderPipelineBuilderSetStage(builder, DAWN_SHADER_STAGE_FRAGMENT, fsModule, "main");
63 pipeline = dawnRenderPipelineBuilderGetResult(builder);
64 dawnRenderPipelineBuilderRelease(builder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040065 }
66
Corentin Wallezb1669e32018-07-18 15:12:52 +020067 dawnShaderModuleRelease(vsModule);
68 dawnShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069}
70
71void frame() {
Corentin Wallezb1669e32018-07-18 15:12:52 +020072 dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
73 dawnTextureView backbufferView;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070074 {
Jiawei Shao3d670502018-09-18 00:31:57 +000075 backbufferView = dawnTextureCreateDefaultTextureView(backbuffer);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070076 }
Corentin Wallezb1669e32018-07-18 15:12:52 +020077 dawnRenderPassDescriptor renderpassInfo;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070078 {
Corentin Wallezb1669e32018-07-18 15:12:52 +020079 dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device);
80 dawnRenderPassDescriptorBuilderSetColorAttachment(builder, 0, backbufferView, DAWN_LOAD_OP_CLEAR);
81 renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder);
82 dawnRenderPassDescriptorBuilderRelease(builder);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070083 }
Corentin Wallezb1669e32018-07-18 15:12:52 +020084 dawnCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040085 {
Corentin Wallezb1669e32018-07-18 15:12:52 +020086 dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device);
Corentin Wallez82fbccb2018-09-21 00:24:37 +000087
88 dawnRenderPassEncoder pass = dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
89 dawnRenderPassEncoderSetRenderPipeline(pass, pipeline);
Jiawei Shaoc789b842018-12-10 05:20:19 +000090 dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +000091 dawnRenderPassEncoderEndPass(pass);
92 dawnRenderPassEncoderRelease(pass);
93
Corentin Wallezb1669e32018-07-18 15:12:52 +020094 commands = dawnCommandBufferBuilderGetResult(builder);
95 dawnCommandBufferBuilderRelease(builder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040096 }
97
Corentin Wallezb1669e32018-07-18 15:12:52 +020098 dawnQueueSubmit(queue, 1, &commands);
99 dawnCommandBufferRelease(commands);
100 dawnSwapChainPresent(swapchain, backbuffer);
101 dawnRenderPassDescriptorRelease(renderpassInfo);
102 dawnTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400103
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700104 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400105}
106
107int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400108 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400109 return 1;
110 }
111 init();
112
113 while (!ShouldQuit()) {
114 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400115 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116 }
117
118 // TODO release stuff
119}