blob: a3f4f82b3f47d1c8cce08ce5105e66839416ab82 [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 Wallez7be2a712019-02-15 11:15:58 +000032 dawnSwapChainDescriptor descriptor;
33 descriptor.nextInChain = nullptr;
34 descriptor.implementation = GetSwapChainImplementation();
35 swapchain = dawnDeviceCreateSwapChain(device, &descriptor);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070036 }
Corentin Wallezb1669e32018-07-18 15:12:52 +020037 swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat());
38 dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
Corentin Walleze98678f2018-01-19 12:51:18 -050039 480);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070040
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040041 const char* vs =
42 "#version 450\n"
43 "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
44 "void main() {\n"
45 " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
46 "}\n";
Corentin Wallezb1669e32018-07-18 15:12:52 +020047 dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048
49 const char* fs =
50 "#version 450\n"
Corentin Wallezb6fb5f32017-08-29 13:37:45 -040051 "layout(location = 0) out vec4 fragColor;"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040052 "void main() {\n"
53 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
54 "}\n";
Corentin Wallezb1669e32018-07-18 15:12:52 +020055 dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040056
57 {
Yan, Shaoboa4924272018-12-10 19:47:22 +000058 dawnRenderPipelineDescriptor descriptor;
59 descriptor.nextInChain = nullptr;
60
61 dawnPipelineStageDescriptor vertexStage;
62 vertexStage.nextInChain = nullptr;
63 vertexStage.module = vsModule;
64 vertexStage.entryPoint = "main";
65 descriptor.vertexStage = &vertexStage;
66
67 dawnPipelineStageDescriptor fragmentStage;
68 fragmentStage.nextInChain = nullptr;
69 fragmentStage.module = fsModule;
70 fragmentStage.entryPoint = "main";
71 descriptor.fragmentStage = &fragmentStage;
72
Yan, Shaoboa4924272018-12-10 19:47:22 +000073 descriptor.sampleCount = 1;
74
Yunchao He92700bf2018-12-27 06:32:31 +000075 dawnBlendDescriptor blendDescriptor;
76 blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD;
77 blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE;
78 blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE;
Yunchao He108bcbd2019-02-15 02:20:57 +000079 dawnColorStateDescriptor colorStateDescriptor;
80 colorStateDescriptor.nextInChain = nullptr;
81 colorStateDescriptor.format = swapChainFormat;
82 colorStateDescriptor.alphaBlend = blendDescriptor;
83 colorStateDescriptor.colorBlend = blendDescriptor;
84 colorStateDescriptor.colorWriteMask = DAWN_COLOR_WRITE_MASK_ALL;
Yunchao He938811e2019-02-20 13:00:36 +000085
Jiawei Shao20301662019-02-21 00:45:19 +000086 descriptor.colorStateCount = 1;
Yunchao He938811e2019-02-20 13:00:36 +000087 dawnColorStateDescriptor* colorStatesPtr[] = {&colorStateDescriptor};
88 descriptor.colorStates = colorStatesPtr;
Yan, Shaoboa4924272018-12-10 19:47:22 +000089
90 dawnPipelineLayoutDescriptor pl;
91 pl.nextInChain = nullptr;
Jiawei Shao20301662019-02-21 00:45:19 +000092 pl.bindGroupLayoutCount = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +000093 pl.bindGroupLayouts = nullptr;
94 descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
95
96 dawnInputStateBuilder inputStateBuilder = dawnDeviceCreateInputStateBuilder(device);
97 descriptor.inputState = dawnInputStateBuilderGetResult(inputStateBuilder);
98 dawnInputStateBuilderRelease(inputStateBuilder);
99
100 descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32;
101 descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
102
Yunchao He108bcbd2019-02-15 02:20:57 +0000103 descriptor.depthStencilState = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000104
105 pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
106
Yan, Shaoboa4924272018-12-10 19:47:22 +0000107 dawnInputStateRelease(descriptor.inputState);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400108 }
109
Corentin Wallezb1669e32018-07-18 15:12:52 +0200110 dawnShaderModuleRelease(vsModule);
111 dawnShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112}
113
114void frame() {
Corentin Wallezb1669e32018-07-18 15:12:52 +0200115 dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
116 dawnTextureView backbufferView;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700117 {
Jiawei Shao3d670502018-09-18 00:31:57 +0000118 backbufferView = dawnTextureCreateDefaultTextureView(backbuffer);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700119 }
Corentin Wallezb1669e32018-07-18 15:12:52 +0200120 dawnRenderPassDescriptor renderpassInfo;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000121 dawnRenderPassColorAttachmentDescriptor colorAttachment;
122 dawnRenderPassColorAttachmentDescriptor* colorAttachments = {&colorAttachment};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700123 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000124 colorAttachment.attachment = backbufferView;
125 colorAttachment.resolveTarget = nullptr;
126 colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
127 colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR;
128 colorAttachment.storeOp = DAWN_STORE_OP_STORE;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000129 renderpassInfo.colorAttachmentCount = 1;
130 renderpassInfo.colorAttachments = &colorAttachments;
131 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700132 }
Corentin Wallezb1669e32018-07-18 15:12:52 +0200133 dawnCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400134 {
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000135 dawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000136
Jiawei Shaob2c50232019-02-27 09:21:56 +0000137 dawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000138 dawnRenderPassEncoderSetPipeline(pass, pipeline);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000139 dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000140 dawnRenderPassEncoderEndPass(pass);
141 dawnRenderPassEncoderRelease(pass);
142
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000143 commands = dawnCommandEncoderFinish(encoder);
144 dawnCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400145 }
146
Corentin Wallezb1669e32018-07-18 15:12:52 +0200147 dawnQueueSubmit(queue, 1, &commands);
148 dawnCommandBufferRelease(commands);
149 dawnSwapChainPresent(swapchain, backbuffer);
Corentin Wallezb1669e32018-07-18 15:12:52 +0200150 dawnTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700152 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400153}
154
155int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400156 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400157 return 1;
158 }
159 init();
160
161 while (!ShouldQuit()) {
162 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400163 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400164 }
165
166 // TODO release stuff
167}