blob: 17c53f7c9593e6adb17ee86092230b72bbf4fd51 [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 Wallez6d315da2021-02-04 15:33:42 +000029 queue = wgpuDeviceGetQueue(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 =
dan sinclairc2032ea2021-01-21 15:41:20 +000040 "[[builtin(vertex_index)]] var<in> VertexIndex : u32;\n"
Corentin Wallez4814bdb2020-11-26 16:39:46 +000041 "[[builtin(position)]] var<out> Position : vec4<f32>;\n"
42 "const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(\n"
43 " vec2<f32>( 0.0, 0.5),\n"
44 " vec2<f32>(-0.5, -0.5),\n"
45 " vec2<f32>( 0.5, -0.5)\n"
46 ");\n"
47 "[[stage(vertex)]] fn main() -> void {\n"
48 " Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);\n"
49 " return;\n"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040050 "}\n";
Corentin Wallez4814bdb2020-11-26 16:39:46 +000051 WGPUShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, vs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040052
53 const char* fs =
Corentin Wallez4814bdb2020-11-26 16:39:46 +000054 "[[location(0)]] var<out> fragColor : vec4<f32>;\n"
55 "[[stage(fragment)]] fn main() -> void {\n"
56 " fragColor = vec4<f32>(1.0, 0.0, 0.0, 1.0);\n"
57 " return;\n"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040058 "}\n";
Corentin Wallez4814bdb2020-11-26 16:39:46 +000059 WGPUShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060
61 {
Corentin Wallez9895c272021-03-18 16:46:58 +000062 WGPURenderPipelineDescriptor2 descriptor = {};
Yan, Shaoboa4924272018-12-10 19:47:22 +000063
Corentin Wallez9895c272021-03-18 16:46:58 +000064 // Fragment state
65 WGPUBlendState blend = {};
66 blend.color.operation = WGPUBlendOperation_Add;
67 blend.color.srcFactor = WGPUBlendFactor_One;
68 blend.color.dstFactor = WGPUBlendFactor_One;
69 blend.alpha.operation = WGPUBlendOperation_Add;
70 blend.alpha.srcFactor = WGPUBlendFactor_One;
71 blend.alpha.dstFactor = WGPUBlendFactor_One;
Yan, Shaoboa4924272018-12-10 19:47:22 +000072
Corentin Wallez9895c272021-03-18 16:46:58 +000073 WGPUColorTargetState colorTarget = {};
74 colorTarget.format = swapChainFormat;
75 colorTarget.blend = &blend;
76 colorTarget.writeMask = WGPUColorWriteMask_All;
Yan, Shaoboa4924272018-12-10 19:47:22 +000077
Corentin Wallez9895c272021-03-18 16:46:58 +000078 WGPUFragmentState fragment = {};
79 fragment.module = fsModule;
80 fragment.entryPoint = "main";
81 fragment.targetCount = 1;
82 fragment.targets = &colorTarget;
83 descriptor.fragment = &fragment;
Yan, Shaoboa4924272018-12-10 19:47:22 +000084
Corentin Wallez9895c272021-03-18 16:46:58 +000085 // Other state
86 descriptor.layout = nullptr;
87 descriptor.depthStencil = nullptr;
Yunchao He938811e2019-02-20 13:00:36 +000088
Corentin Wallez9895c272021-03-18 16:46:58 +000089 descriptor.vertex.module = vsModule;
90 descriptor.vertex.entryPoint = "main";
91 descriptor.vertex.bufferCount = 0;
92 descriptor.vertex.buffers = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +000093
Corentin Wallez9895c272021-03-18 16:46:58 +000094 descriptor.multisample.count = 1;
95 descriptor.multisample.mask = 0xFFFFFFFF;
96 descriptor.multisample.alphaToCoverageEnabled = false;
Yan, Shaoboa4924272018-12-10 19:47:22 +000097
Corentin Wallez9895c272021-03-18 16:46:58 +000098 descriptor.primitive.frontFace = WGPUFrontFace_CCW;
99 descriptor.primitive.cullMode = WGPUCullMode_None;
100 descriptor.primitive.topology = WGPUPrimitiveTopology_TriangleList;
101 descriptor.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000102
Corentin Wallez9895c272021-03-18 16:46:58 +0000103 pipeline = wgpuDeviceCreateRenderPipeline2(device, &descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104 }
105
Corentin Wallez04863c42019-10-25 11:36:47 +0000106 wgpuShaderModuleRelease(vsModule);
107 wgpuShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400108}
109
110void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000111 WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
Austin Eng3ded65e2020-02-28 22:29:15 +0000112 WGPURenderPassDescriptor renderpassInfo = {};
113 WGPURenderPassColorAttachmentDescriptor colorAttachment = {};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700114 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000115 colorAttachment.attachment = backbufferView;
116 colorAttachment.resolveTarget = nullptr;
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000117 colorAttachment.clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
Corentin Wallez04863c42019-10-25 11:36:47 +0000118 colorAttachment.loadOp = WGPULoadOp_Clear;
119 colorAttachment.storeOp = WGPUStoreOp_Store;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000120 renderpassInfo.colorAttachmentCount = 1;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000121 renderpassInfo.colorAttachments = &colorAttachment;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000122 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700123 }
Corentin Wallez04863c42019-10-25 11:36:47 +0000124 WGPUCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000126 WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000127
Corentin Wallez04863c42019-10-25 11:36:47 +0000128 WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
129 wgpuRenderPassEncoderSetPipeline(pass, pipeline);
130 wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
131 wgpuRenderPassEncoderEndPass(pass);
132 wgpuRenderPassEncoderRelease(pass);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000133
Corentin Wallez04863c42019-10-25 11:36:47 +0000134 commands = wgpuCommandEncoderFinish(encoder, nullptr);
135 wgpuCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400136 }
137
Corentin Wallez04863c42019-10-25 11:36:47 +0000138 wgpuQueueSubmit(queue, 1, &commands);
139 wgpuCommandBufferRelease(commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000140 wgpuSwapChainPresent(swapchain);
Corentin Wallez04863c42019-10-25 11:36:47 +0000141 wgpuTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400142
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700143 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144}
145
146int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400147 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400148 return 1;
149 }
150 init();
151
152 while (!ShouldQuit()) {
153 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400154 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400155 }
156
157 // TODO release stuff
158}