blob: 935c6617a0539a4baa6f2f60f5e60962285bcfa5 [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 =
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 {
Austin Eng3ded65e2020-02-28 22:29:15 +000062 WGPURenderPipelineDescriptor descriptor = {};
Yan, Shaoboa4924272018-12-10 19:47:22 +000063
Corentin Wallezc6c7a422019-09-05 09:35:07 +000064 descriptor.vertexStage.module = vsModule;
65 descriptor.vertexStage.entryPoint = "main";
Yan, Shaoboa4924272018-12-10 19:47:22 +000066
Austin Eng3ded65e2020-02-28 22:29:15 +000067 WGPUProgrammableStageDescriptor fragmentStage = {};
Yan, Shaoboa4924272018-12-10 19:47:22 +000068 fragmentStage.module = fsModule;
69 fragmentStage.entryPoint = "main";
70 descriptor.fragmentStage = &fragmentStage;
71
Yan, Shaoboa4924272018-12-10 19:47:22 +000072 descriptor.sampleCount = 1;
73
Austin Eng3ded65e2020-02-28 22:29:15 +000074 WGPUBlendDescriptor blendDescriptor = {};
Corentin Wallez04863c42019-10-25 11:36:47 +000075 blendDescriptor.operation = WGPUBlendOperation_Add;
76 blendDescriptor.srcFactor = WGPUBlendFactor_One;
77 blendDescriptor.dstFactor = WGPUBlendFactor_One;
Austin Eng3ded65e2020-02-28 22:29:15 +000078 WGPUColorStateDescriptor colorStateDescriptor = {};
Yunchao He108bcbd2019-02-15 02:20:57 +000079 colorStateDescriptor.format = swapChainFormat;
80 colorStateDescriptor.alphaBlend = blendDescriptor;
81 colorStateDescriptor.colorBlend = blendDescriptor;
Corentin Wallez04863c42019-10-25 11:36:47 +000082 colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
Yunchao He938811e2019-02-20 13:00:36 +000083
Jiawei Shao20301662019-02-21 00:45:19 +000084 descriptor.colorStateCount = 1;
Corentin Wallezc81a7172019-09-20 23:22:27 +000085 descriptor.colorStates = &colorStateDescriptor;
Yan, Shaoboa4924272018-12-10 19:47:22 +000086
Austin Eng3ded65e2020-02-28 22:29:15 +000087 WGPUPipelineLayoutDescriptor pl = {};
Jiawei Shao20301662019-02-21 00:45:19 +000088 pl.bindGroupLayoutCount = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +000089 pl.bindGroupLayouts = nullptr;
Corentin Wallez04863c42019-10-25 11:36:47 +000090 descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
Yan, Shaoboa4924272018-12-10 19:47:22 +000091
Austin Eng3ded65e2020-02-28 22:29:15 +000092 WGPUVertexStateDescriptor vertexState = {};
Corentin Wallez4814bdb2020-11-26 16:39:46 +000093 vertexState.indexFormat = WGPUIndexFormat_Undefined;
Kai Ninomiyaae1f25f2019-11-07 22:23:29 +000094 vertexState.vertexBufferCount = 0;
95 vertexState.vertexBuffers = nullptr;
96 descriptor.vertexState = &vertexState;
Yan, Shaoboa4924272018-12-10 19:47:22 +000097
Austin Eng3ded65e2020-02-28 22:29:15 +000098 WGPURasterizationStateDescriptor rasterizationState = {};
Corentin Wallez04863c42019-10-25 11:36:47 +000099 rasterizationState.frontFace = WGPUFrontFace_CCW;
100 rasterizationState.cullMode = WGPUCullMode_None;
Yunchao Hec33a8c12019-04-11 18:46:54 +0000101 rasterizationState.depthBias = 0;
102 rasterizationState.depthBiasSlopeScale = 0.0;
103 rasterizationState.depthBiasClamp = 0.0;
104 descriptor.rasterizationState = &rasterizationState;
105
Corentin Wallez04863c42019-10-25 11:36:47 +0000106 descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
Corentin Wallezf07e85c2019-07-15 20:47:56 +0000107 descriptor.sampleMask = 0xFFFFFFFF;
108 descriptor.alphaToCoverageEnabled = false;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000109
Yunchao He108bcbd2019-02-15 02:20:57 +0000110 descriptor.depthStencilState = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000111
Corentin Wallez04863c42019-10-25 11:36:47 +0000112 pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400113 }
114
Corentin Wallez04863c42019-10-25 11:36:47 +0000115 wgpuShaderModuleRelease(vsModule);
116 wgpuShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117}
118
119void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000120 WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
Austin Eng3ded65e2020-02-28 22:29:15 +0000121 WGPURenderPassDescriptor renderpassInfo = {};
122 WGPURenderPassColorAttachmentDescriptor colorAttachment = {};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700123 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000124 colorAttachment.attachment = backbufferView;
125 colorAttachment.resolveTarget = nullptr;
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000126 colorAttachment.clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
Corentin Wallez04863c42019-10-25 11:36:47 +0000127 colorAttachment.loadOp = WGPULoadOp_Clear;
128 colorAttachment.storeOp = WGPUStoreOp_Store;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000129 renderpassInfo.colorAttachmentCount = 1;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000130 renderpassInfo.colorAttachments = &colorAttachment;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000131 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700132 }
Corentin Wallez04863c42019-10-25 11:36:47 +0000133 WGPUCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400134 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000135 WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000136
Corentin Wallez04863c42019-10-25 11:36:47 +0000137 WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
138 wgpuRenderPassEncoderSetPipeline(pass, pipeline);
139 wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
140 wgpuRenderPassEncoderEndPass(pass);
141 wgpuRenderPassEncoderRelease(pass);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000142
Corentin Wallez04863c42019-10-25 11:36:47 +0000143 commands = wgpuCommandEncoderFinish(encoder, nullptr);
144 wgpuCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400145 }
146
Corentin Wallez04863c42019-10-25 11:36:47 +0000147 wgpuQueueSubmit(queue, 1, &commands);
148 wgpuCommandBufferRelease(commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000149 wgpuSwapChainPresent(swapchain);
Corentin Wallez04863c42019-10-25 11:36:47 +0000150 wgpuTextureViewRelease(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}