blob: d457e38ab5748f9e58a227c7586ef2058802c086 [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 Wallez04863c42019-10-25 11:36:47 +000029 queue = wgpuDeviceCreateQueue(device);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031 {
Corentin Wallez04863c42019-10-25 11:36:47 +000032 WGPUSwapChainDescriptor descriptor;
Corentin Wallez7be2a712019-02-15 11:15:58 +000033 descriptor.nextInChain = nullptr;
François Beaufort277d2e12019-10-03 14:56:49 +000034 descriptor.label = nullptr;
Corentin Wallez7be2a712019-02-15 11:15:58 +000035 descriptor.implementation = GetSwapChainImplementation();
Corentin Wallez04863c42019-10-25 11:36:47 +000036 swapchain = wgpuDeviceCreateSwapChain(device, &descriptor);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070037 }
Corentin Wallez04863c42019-10-25 11:36:47 +000038 swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
39 wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_OutputAttachment, 640, 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 Wallez04863c42019-10-25 11:36:47 +000047 WGPUShaderModule vsModule =
48 utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
Corentin Wallezb9b088f2019-08-27 08:42:29 +000049 .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 Wallez04863c42019-10-25 11:36:47 +000057 WGPUShaderModule 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 {
Corentin Wallez04863c42019-10-25 11:36:47 +000061 WGPURenderPipelineDescriptor descriptor;
François Beaufort277d2e12019-10-03 14:56:49 +000062 descriptor.label = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +000063 descriptor.nextInChain = nullptr;
64
Corentin Wallezc6c7a422019-09-05 09:35:07 +000065 descriptor.vertexStage.nextInChain = nullptr;
66 descriptor.vertexStage.module = vsModule;
67 descriptor.vertexStage.entryPoint = "main";
Yan, Shaoboa4924272018-12-10 19:47:22 +000068
Corentin Wallez04863c42019-10-25 11:36:47 +000069 WGPUProgrammableStageDescriptor fragmentStage;
Yan, Shaoboa4924272018-12-10 19:47:22 +000070 fragmentStage.nextInChain = nullptr;
71 fragmentStage.module = fsModule;
72 fragmentStage.entryPoint = "main";
73 descriptor.fragmentStage = &fragmentStage;
74
Yan, Shaoboa4924272018-12-10 19:47:22 +000075 descriptor.sampleCount = 1;
76
Corentin Wallez04863c42019-10-25 11:36:47 +000077 WGPUBlendDescriptor blendDescriptor;
78 blendDescriptor.operation = WGPUBlendOperation_Add;
79 blendDescriptor.srcFactor = WGPUBlendFactor_One;
80 blendDescriptor.dstFactor = WGPUBlendFactor_One;
81 WGPUColorStateDescriptor colorStateDescriptor;
Yunchao He108bcbd2019-02-15 02:20:57 +000082 colorStateDescriptor.nextInChain = nullptr;
83 colorStateDescriptor.format = swapChainFormat;
84 colorStateDescriptor.alphaBlend = blendDescriptor;
85 colorStateDescriptor.colorBlend = blendDescriptor;
Corentin Wallez04863c42019-10-25 11:36:47 +000086 colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
Yunchao He938811e2019-02-20 13:00:36 +000087
Jiawei Shao20301662019-02-21 00:45:19 +000088 descriptor.colorStateCount = 1;
Corentin Wallezc81a7172019-09-20 23:22:27 +000089 descriptor.colorStates = &colorStateDescriptor;
Yan, Shaoboa4924272018-12-10 19:47:22 +000090
Corentin Wallez04863c42019-10-25 11:36:47 +000091 WGPUPipelineLayoutDescriptor pl;
Yan, Shaoboa4924272018-12-10 19:47:22 +000092 pl.nextInChain = nullptr;
François Beaufort277d2e12019-10-03 14:56:49 +000093 pl.label = nullptr;
Jiawei Shao20301662019-02-21 00:45:19 +000094 pl.bindGroupLayoutCount = 0;
Yan, Shaoboa4924272018-12-10 19:47:22 +000095 pl.bindGroupLayouts = nullptr;
Corentin Wallez04863c42019-10-25 11:36:47 +000096 descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
Yan, Shaoboa4924272018-12-10 19:47:22 +000097
Kai Ninomiyaae1f25f2019-11-07 22:23:29 +000098 WGPUVertexStateDescriptor vertexState;
99 vertexState.nextInChain = nullptr;
100 vertexState.indexFormat = WGPUIndexFormat_Uint32;
101 vertexState.vertexBufferCount = 0;
102 vertexState.vertexBuffers = nullptr;
103 descriptor.vertexState = &vertexState;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000104
Corentin Wallez04863c42019-10-25 11:36:47 +0000105 WGPURasterizationStateDescriptor rasterizationState;
Yunchao Hec33a8c12019-04-11 18:46:54 +0000106 rasterizationState.nextInChain = nullptr;
Corentin Wallez04863c42019-10-25 11:36:47 +0000107 rasterizationState.frontFace = WGPUFrontFace_CCW;
108 rasterizationState.cullMode = WGPUCullMode_None;
Yunchao Hec33a8c12019-04-11 18:46:54 +0000109 rasterizationState.depthBias = 0;
110 rasterizationState.depthBiasSlopeScale = 0.0;
111 rasterizationState.depthBiasClamp = 0.0;
112 descriptor.rasterizationState = &rasterizationState;
113
Corentin Wallez04863c42019-10-25 11:36:47 +0000114 descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
Corentin Wallezf07e85c2019-07-15 20:47:56 +0000115 descriptor.sampleMask = 0xFFFFFFFF;
116 descriptor.alphaToCoverageEnabled = false;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000117
Yunchao He108bcbd2019-02-15 02:20:57 +0000118 descriptor.depthStencilState = nullptr;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000119
Corentin Wallez04863c42019-10-25 11:36:47 +0000120 pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121 }
122
Corentin Wallez04863c42019-10-25 11:36:47 +0000123 wgpuShaderModuleRelease(vsModule);
124 wgpuShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125}
126
127void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000128 WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
Corentin Wallez04863c42019-10-25 11:36:47 +0000129 WGPURenderPassDescriptor renderpassInfo;
Corentin Wallez5f53d532019-10-08 07:34:43 +0000130 renderpassInfo.nextInChain = nullptr;
François Beaufort277d2e12019-10-03 14:56:49 +0000131 renderpassInfo.label = nullptr;
Corentin Wallez04863c42019-10-25 11:36:47 +0000132 WGPURenderPassColorAttachmentDescriptor colorAttachment;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700133 {
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000134 colorAttachment.attachment = backbufferView;
135 colorAttachment.resolveTarget = nullptr;
136 colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
Corentin Wallez04863c42019-10-25 11:36:47 +0000137 colorAttachment.loadOp = WGPULoadOp_Clear;
138 colorAttachment.storeOp = WGPUStoreOp_Store;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000139 renderpassInfo.colorAttachmentCount = 1;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000140 renderpassInfo.colorAttachments = &colorAttachment;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000141 renderpassInfo.depthStencilAttachment = nullptr;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700142 }
Corentin Wallez04863c42019-10-25 11:36:47 +0000143 WGPUCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000145 WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000146
Corentin Wallez04863c42019-10-25 11:36:47 +0000147 WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
148 wgpuRenderPassEncoderSetPipeline(pass, pipeline);
149 wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
150 wgpuRenderPassEncoderEndPass(pass);
151 wgpuRenderPassEncoderRelease(pass);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000152
Corentin Wallez04863c42019-10-25 11:36:47 +0000153 commands = wgpuCommandEncoderFinish(encoder, nullptr);
154 wgpuCommandEncoderRelease(encoder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400155 }
156
Corentin Wallez04863c42019-10-25 11:36:47 +0000157 wgpuQueueSubmit(queue, 1, &commands);
158 wgpuCommandBufferRelease(commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000159 wgpuSwapChainPresent(swapchain);
Corentin Wallez04863c42019-10-25 11:36:47 +0000160 wgpuTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400161
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700162 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400163}
164
165int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400166 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400167 return 1;
168 }
169 init();
170
171 while (!ShouldQuit()) {
172 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400173 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400174 }
175
176 // TODO release stuff
177}