blob: e04bd8e05e8d071c8d5b0a5090bbdad7a3f1e83c [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
Austin Eng45f97302019-03-11 16:52:42 +000020DawnDevice device;
21DawnQueue queue;
22DawnSwapChain swapchain;
23DawnRenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Austin Eng45f97302019-03-11 16:52:42 +000025DawnTextureFormat 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 {
Austin Eng45f97302019-03-11 16:52:42 +000032 DawnSwapChainDescriptor descriptor;
Corentin Wallez7be2a712019-02-15 11:15:58 +000033 descriptor.nextInChain = nullptr;
34 descriptor.implementation = GetSwapChainImplementation();
35 swapchain = dawnDeviceCreateSwapChain(device, &descriptor);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070036 }
Austin Eng45f97302019-03-11 16:52:42 +000037 swapChainFormat = static_cast<DawnTextureFormat>(GetPreferredSwapChainTextureFormat());
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000038 dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_OUTPUT_ATTACHMENT, 640,
39 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 Wallez2a1d8c22019-07-12 17:52:22 +000047 DawnShaderModule vsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +000048 utils::CreateShaderModule(dawn::Device(device), utils::SingleShaderStage::Vertex, vs)
49 .Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040050
51 const char* fs =
52 "#version 450\n"
Corentin Wallezb6fb5f32017-08-29 13:37:45 -040053 "layout(location = 0) out vec4 fragColor;"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040054 "void main() {\n"
55 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
56 "}\n";
Corentin Wallez2a1d8c22019-07-12 17:52:22 +000057 DawnShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +000058 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040059
60 {
Austin Eng45f97302019-03-11 16:52:42 +000061 DawnRenderPipelineDescriptor descriptor;
Yan, Shaoboa4924272018-12-10 19:47:22 +000062 descriptor.nextInChain = nullptr;
63
Corentin Wallezc6c7a422019-09-05 09:35:07 +000064 descriptor.vertexStage.nextInChain = nullptr;
65 descriptor.vertexStage.module = vsModule;
66 descriptor.vertexStage.entryPoint = "main";
Yan, Shaoboa4924272018-12-10 19:47:22 +000067
Austin Eng45f97302019-03-11 16:52:42 +000068 DawnPipelineStageDescriptor fragmentStage;
Yan, Shaoboa4924272018-12-10 19:47:22 +000069 fragmentStage.nextInChain = nullptr;
70 fragmentStage.module = fsModule;
71 fragmentStage.entryPoint = "main";
72 descriptor.fragmentStage = &fragmentStage;
73
Yan, Shaoboa4924272018-12-10 19:47:22 +000074 descriptor.sampleCount = 1;
75
Austin Eng45f97302019-03-11 16:52:42 +000076 DawnBlendDescriptor blendDescriptor;
Yunchao He92700bf2018-12-27 06:32:31 +000077 blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD;
78 blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE;
79 blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE;
Austin Eng45f97302019-03-11 16:52:42 +000080 DawnColorStateDescriptor colorStateDescriptor;
Yunchao He108bcbd2019-02-15 02:20:57 +000081 colorStateDescriptor.nextInChain = nullptr;
82 colorStateDescriptor.format = swapChainFormat;
83 colorStateDescriptor.alphaBlend = blendDescriptor;
84 colorStateDescriptor.colorBlend = blendDescriptor;
Austin Eng0c133bb2019-04-15 20:43:35 +000085 colorStateDescriptor.writeMask = DAWN_COLOR_WRITE_MASK_ALL;
Yunchao He938811e2019-02-20 13:00:36 +000086
Jiawei Shao20301662019-02-21 00:45:19 +000087 descriptor.colorStateCount = 1;
Austin Eng45f97302019-03-11 16:52:42 +000088 DawnColorStateDescriptor* colorStatesPtr[] = {&colorStateDescriptor};
Yunchao He938811e2019-02-20 13:00:36 +000089 descriptor.colorStates = colorStatesPtr;
Yan, Shaoboa4924272018-12-10 19:47:22 +000090
Austin Eng45f97302019-03-11 16:52:42 +000091 DawnPipelineLayoutDescriptor pl;
Yan, Shaoboa4924272018-12-10 19:47:22 +000092 pl.nextInChain = nullptr;
Jiawei Shao20301662019-02-21 00:45:19 +000093 pl.bindGroupLayoutCount = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +000094 pl.bindGroupLayouts = nullptr;
95 descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
96
Yunchao Heeea20912019-05-22 22:46:32 +000097 DawnVertexInputDescriptor vertexInput;
98 vertexInput.nextInChain = nullptr;
99 vertexInput.indexFormat = DAWN_INDEX_FORMAT_UINT32;
Yunchao He2d4b5292019-06-06 17:54:30 +0000100 vertexInput.bufferCount = 0;
Yunchao Heeea20912019-05-22 22:46:32 +0000101 vertexInput.buffers = nullptr;
Yunchao Heeea20912019-05-22 22:46:32 +0000102 descriptor.vertexInput = &vertexInput;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000103
Yunchao Hec33a8c12019-04-11 18:46:54 +0000104 DawnRasterizationStateDescriptor rasterizationState;
105 rasterizationState.nextInChain = nullptr;
106 rasterizationState.frontFace = DAWN_FRONT_FACE_CCW;
107 rasterizationState.cullMode = DAWN_CULL_MODE_NONE;
108 rasterizationState.depthBias = 0;
109 rasterizationState.depthBiasSlopeScale = 0.0;
110 rasterizationState.depthBiasClamp = 0.0;
111 descriptor.rasterizationState = &rasterizationState;
112
Yan, Shaoboa4924272018-12-10 19:47:22 +0000113 descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Corentin Wallezf07e85c2019-07-15 20:47:56 +0000114 descriptor.sampleMask = 0xFFFFFFFF;
115 descriptor.alphaToCoverageEnabled = false;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000116
Yunchao He108bcbd2019-02-15 02:20:57 +0000117 descriptor.depthStencilState = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000118
119 pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400120 }
121
Corentin Wallezb1669e32018-07-18 15:12:52 +0200122 dawnShaderModuleRelease(vsModule);
123 dawnShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124}
125
126void frame() {
Austin Eng45f97302019-03-11 16:52:42 +0000127 DawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000128 DawnTextureView backbufferView = dawnTextureCreateView(backbuffer, nullptr);
Austin Eng45f97302019-03-11 16:52:42 +0000129 DawnRenderPassDescriptor renderpassInfo;
130 DawnRenderPassColorAttachmentDescriptor colorAttachment;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700131 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000132 colorAttachment.attachment = backbufferView;
133 colorAttachment.resolveTarget = nullptr;
134 colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
135 colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR;
136 colorAttachment.storeOp = DAWN_STORE_OP_STORE;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000137 renderpassInfo.colorAttachmentCount = 1;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000138 renderpassInfo.colorAttachments = &colorAttachment;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000139 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700140 }
Austin Eng45f97302019-03-11 16:52:42 +0000141 DawnCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400142 {
Corentin Wallez4b90c472019-07-10 20:43:13 +0000143 DawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000144
Austin Eng45f97302019-03-11 16:52:42 +0000145 DawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000146 dawnRenderPassEncoderSetPipeline(pass, pipeline);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000147 dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000148 dawnRenderPassEncoderEndPass(pass);
149 dawnRenderPassEncoderRelease(pass);
150
Corentin Wallez4b90c472019-07-10 20:43:13 +0000151 commands = dawnCommandEncoderFinish(encoder, nullptr);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000152 dawnCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400153 }
154
Corentin Wallezb1669e32018-07-18 15:12:52 +0200155 dawnQueueSubmit(queue, 1, &commands);
156 dawnCommandBufferRelease(commands);
157 dawnSwapChainPresent(swapchain, backbuffer);
Corentin Wallezb1669e32018-07-18 15:12:52 +0200158 dawnTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700160 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400161}
162
163int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400164 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165 return 1;
166 }
167 init();
168
169 while (!ShouldQuit()) {
170 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400171 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 }
173
174 // TODO release stuff
175}