blob: 4c5c7cc2b1b3c70ba25f861008649be974a0f294 [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 Wallez134e0802017-07-17 17:13:57 -040017#include "utils/SystemUtils.h"
Corentin Wallez04863c42019-10-25 11:36:47 +000018#include "utils/WGPUHelpers.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallez04863c42019-10-25 11:36:47 +000020WGPUDevice device;
21WGPUQueue queue;
22WGPUSwapChain swapchain;
23WGPURenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Wallez04863c42019-10-25 11:36:47 +000025WGPUTextureFormat 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 Wallez8a437942020-04-17 16:45:17 +000029 queue = wgpuDeviceGetDefaultQueue(device);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031 {
Austin Eng3ded65e2020-02-28 22:29:15 +000032 WGPUSwapChainDescriptor descriptor = {};
Corentin Wallez7be2a712019-02-15 11:15:58 +000033 descriptor.implementation = GetSwapChainImplementation();
Corentin Wallezd87e6762020-01-23 17:20:38 +000034 swapchain = wgpuDeviceCreateSwapChain(device, nullptr, &descriptor);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070035 }
Corentin Wallez04863c42019-10-25 11:36:47 +000036 swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
Corentin Wallez6b087812020-10-27 15:35:56 +000037 wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070038
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040039 const char* vs =
40 "#version 450\n"
41 "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
42 "void main() {\n"
43 " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
44 "}\n";
Corentin Wallez04863c42019-10-25 11:36:47 +000045 WGPUShaderModule vsModule =
46 utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
Corentin Wallezb9b088f2019-08-27 08:42:29 +000047 .Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048
49 const char* fs =
50 "#version 450\n"
Corentin Wallez11652ff2020-03-20 17:07:20 +000051 "layout(location = 0) out vec4 fragColor;\n"
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 Wallez04863c42019-10-25 11:36:47 +000055 WGPUShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +000056 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
58 {
Austin Eng3ded65e2020-02-28 22:29:15 +000059 WGPURenderPipelineDescriptor descriptor = {};
Yan, Shaoboa4924272018-12-10 19:47:22 +000060
Corentin Wallezc6c7a422019-09-05 09:35:07 +000061 descriptor.vertexStage.module = vsModule;
62 descriptor.vertexStage.entryPoint = "main";
Yan, Shaoboa4924272018-12-10 19:47:22 +000063
Austin Eng3ded65e2020-02-28 22:29:15 +000064 WGPUProgrammableStageDescriptor fragmentStage = {};
Yan, Shaoboa4924272018-12-10 19:47:22 +000065 fragmentStage.module = fsModule;
66 fragmentStage.entryPoint = "main";
67 descriptor.fragmentStage = &fragmentStage;
68
Yan, Shaoboa4924272018-12-10 19:47:22 +000069 descriptor.sampleCount = 1;
70
Austin Eng3ded65e2020-02-28 22:29:15 +000071 WGPUBlendDescriptor blendDescriptor = {};
Corentin Wallez04863c42019-10-25 11:36:47 +000072 blendDescriptor.operation = WGPUBlendOperation_Add;
73 blendDescriptor.srcFactor = WGPUBlendFactor_One;
74 blendDescriptor.dstFactor = WGPUBlendFactor_One;
Austin Eng3ded65e2020-02-28 22:29:15 +000075 WGPUColorStateDescriptor colorStateDescriptor = {};
Yunchao He108bcbd2019-02-15 02:20:57 +000076 colorStateDescriptor.format = swapChainFormat;
77 colorStateDescriptor.alphaBlend = blendDescriptor;
78 colorStateDescriptor.colorBlend = blendDescriptor;
Corentin Wallez04863c42019-10-25 11:36:47 +000079 colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
Yunchao He938811e2019-02-20 13:00:36 +000080
Jiawei Shao20301662019-02-21 00:45:19 +000081 descriptor.colorStateCount = 1;
Corentin Wallezc81a7172019-09-20 23:22:27 +000082 descriptor.colorStates = &colorStateDescriptor;
Yan, Shaoboa4924272018-12-10 19:47:22 +000083
Austin Eng3ded65e2020-02-28 22:29:15 +000084 WGPUPipelineLayoutDescriptor pl = {};
Jiawei Shao20301662019-02-21 00:45:19 +000085 pl.bindGroupLayoutCount = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +000086 pl.bindGroupLayouts = nullptr;
Corentin Wallez04863c42019-10-25 11:36:47 +000087 descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
Yan, Shaoboa4924272018-12-10 19:47:22 +000088
Austin Eng3ded65e2020-02-28 22:29:15 +000089 WGPUVertexStateDescriptor vertexState = {};
Kai Ninomiyaae1f25f2019-11-07 22:23:29 +000090 vertexState.indexFormat = WGPUIndexFormat_Uint32;
91 vertexState.vertexBufferCount = 0;
92 vertexState.vertexBuffers = nullptr;
93 descriptor.vertexState = &vertexState;
Yan, Shaoboa4924272018-12-10 19:47:22 +000094
Austin Eng3ded65e2020-02-28 22:29:15 +000095 WGPURasterizationStateDescriptor rasterizationState = {};
Corentin Wallez04863c42019-10-25 11:36:47 +000096 rasterizationState.frontFace = WGPUFrontFace_CCW;
97 rasterizationState.cullMode = WGPUCullMode_None;
Yunchao Hec33a8c12019-04-11 18:46:54 +000098 rasterizationState.depthBias = 0;
99 rasterizationState.depthBiasSlopeScale = 0.0;
100 rasterizationState.depthBiasClamp = 0.0;
101 descriptor.rasterizationState = &rasterizationState;
102
Corentin Wallez04863c42019-10-25 11:36:47 +0000103 descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
Corentin Wallezf07e85c2019-07-15 20:47:56 +0000104 descriptor.sampleMask = 0xFFFFFFFF;
105 descriptor.alphaToCoverageEnabled = false;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000106
Yunchao He108bcbd2019-02-15 02:20:57 +0000107 descriptor.depthStencilState = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000108
Corentin Wallez04863c42019-10-25 11:36:47 +0000109 pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400110 }
111
Corentin Wallez04863c42019-10-25 11:36:47 +0000112 wgpuShaderModuleRelease(vsModule);
113 wgpuShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114}
115
116void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000117 WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
Austin Eng3ded65e2020-02-28 22:29:15 +0000118 WGPURenderPassDescriptor renderpassInfo = {};
119 WGPURenderPassColorAttachmentDescriptor colorAttachment = {};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700120 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000121 colorAttachment.attachment = backbufferView;
122 colorAttachment.resolveTarget = nullptr;
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000123 colorAttachment.clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
Corentin Wallez04863c42019-10-25 11:36:47 +0000124 colorAttachment.loadOp = WGPULoadOp_Clear;
125 colorAttachment.storeOp = WGPUStoreOp_Store;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000126 renderpassInfo.colorAttachmentCount = 1;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000127 renderpassInfo.colorAttachments = &colorAttachment;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000128 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700129 }
Corentin Wallez04863c42019-10-25 11:36:47 +0000130 WGPUCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400131 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000132 WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000133
Corentin Wallez04863c42019-10-25 11:36:47 +0000134 WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
135 wgpuRenderPassEncoderSetPipeline(pass, pipeline);
136 wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
137 wgpuRenderPassEncoderEndPass(pass);
138 wgpuRenderPassEncoderRelease(pass);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000139
Corentin Wallez04863c42019-10-25 11:36:47 +0000140 commands = wgpuCommandEncoderFinish(encoder, nullptr);
141 wgpuCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400142 }
143
Corentin Wallez04863c42019-10-25 11:36:47 +0000144 wgpuQueueSubmit(queue, 1, &commands);
145 wgpuCommandBufferRelease(commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000146 wgpuSwapChainPresent(swapchain);
Corentin Wallez04863c42019-10-25 11:36:47 +0000147 wgpuTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400148
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700149 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400150}
151
152int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400153 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400154 return 1;
155 }
156 init();
157
158 while (!ShouldQuit()) {
159 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400160 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400161 }
162
163 // TODO release stuff
164}