Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 2 | // |
| 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 Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 17 | #include "utils/SystemUtils.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 18 | #include "utils/WGPUHelpers.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 19 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 20 | WGPUDevice device; |
| 21 | WGPUQueue queue; |
| 22 | WGPUSwapChain swapchain; |
| 23 | WGPURenderPipeline pipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 25 | WGPUTextureFormat swapChainFormat; |
Corentin Wallez | e98678f | 2018-01-19 12:51:18 -0500 | [diff] [blame] | 26 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 28 | device = CreateCppDawnDevice().Release(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 29 | queue = wgpuDeviceCreateQueue(device); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 31 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 32 | WGPUSwapChainDescriptor descriptor; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 33 | descriptor.nextInChain = nullptr; |
François Beaufort | 277d2e1 | 2019-10-03 14:56:49 +0000 | [diff] [blame] | 34 | descriptor.label = nullptr; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 35 | descriptor.implementation = GetSwapChainImplementation(); |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame^] | 36 | swapchain = wgpuDeviceCreateSwapChain(device, nullptr, &descriptor); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 37 | } |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 38 | swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat()); |
| 39 | wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_OutputAttachment, 640, 480); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 40 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 41 | 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 Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 47 | WGPUShaderModule vsModule = |
| 48 | utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs) |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 49 | .Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 50 | |
| 51 | const char* fs = |
| 52 | "#version 450\n" |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 53 | "layout(location = 0) out vec4 fragColor;" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 54 | "void main() {\n" |
| 55 | " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 56 | "}\n"; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 57 | WGPUShaderModule fsModule = |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 58 | utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 59 | |
| 60 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 61 | WGPURenderPipelineDescriptor descriptor; |
François Beaufort | 277d2e1 | 2019-10-03 14:56:49 +0000 | [diff] [blame] | 62 | descriptor.label = nullptr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 63 | descriptor.nextInChain = nullptr; |
| 64 | |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 65 | descriptor.vertexStage.nextInChain = nullptr; |
| 66 | descriptor.vertexStage.module = vsModule; |
| 67 | descriptor.vertexStage.entryPoint = "main"; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 68 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 69 | WGPUProgrammableStageDescriptor fragmentStage; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 70 | fragmentStage.nextInChain = nullptr; |
| 71 | fragmentStage.module = fsModule; |
| 72 | fragmentStage.entryPoint = "main"; |
| 73 | descriptor.fragmentStage = &fragmentStage; |
| 74 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 75 | descriptor.sampleCount = 1; |
| 76 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 77 | WGPUBlendDescriptor blendDescriptor; |
| 78 | blendDescriptor.operation = WGPUBlendOperation_Add; |
| 79 | blendDescriptor.srcFactor = WGPUBlendFactor_One; |
| 80 | blendDescriptor.dstFactor = WGPUBlendFactor_One; |
| 81 | WGPUColorStateDescriptor colorStateDescriptor; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 82 | colorStateDescriptor.nextInChain = nullptr; |
| 83 | colorStateDescriptor.format = swapChainFormat; |
| 84 | colorStateDescriptor.alphaBlend = blendDescriptor; |
| 85 | colorStateDescriptor.colorBlend = blendDescriptor; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 86 | colorStateDescriptor.writeMask = WGPUColorWriteMask_All; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 87 | |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 88 | descriptor.colorStateCount = 1; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 89 | descriptor.colorStates = &colorStateDescriptor; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 90 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 91 | WGPUPipelineLayoutDescriptor pl; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 92 | pl.nextInChain = nullptr; |
François Beaufort | 277d2e1 | 2019-10-03 14:56:49 +0000 | [diff] [blame] | 93 | pl.label = nullptr; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 94 | pl.bindGroupLayoutCount = 0; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 95 | pl.bindGroupLayouts = nullptr; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 96 | descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 97 | |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 98 | WGPUVertexStateDescriptor vertexState; |
| 99 | vertexState.nextInChain = nullptr; |
| 100 | vertexState.indexFormat = WGPUIndexFormat_Uint32; |
| 101 | vertexState.vertexBufferCount = 0; |
| 102 | vertexState.vertexBuffers = nullptr; |
| 103 | descriptor.vertexState = &vertexState; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 104 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 105 | WGPURasterizationStateDescriptor rasterizationState; |
Yunchao He | c33a8c1 | 2019-04-11 18:46:54 +0000 | [diff] [blame] | 106 | rasterizationState.nextInChain = nullptr; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 107 | rasterizationState.frontFace = WGPUFrontFace_CCW; |
| 108 | rasterizationState.cullMode = WGPUCullMode_None; |
Yunchao He | c33a8c1 | 2019-04-11 18:46:54 +0000 | [diff] [blame] | 109 | rasterizationState.depthBias = 0; |
| 110 | rasterizationState.depthBiasSlopeScale = 0.0; |
| 111 | rasterizationState.depthBiasClamp = 0.0; |
| 112 | descriptor.rasterizationState = &rasterizationState; |
| 113 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 114 | descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList; |
Corentin Wallez | f07e85c | 2019-07-15 20:47:56 +0000 | [diff] [blame] | 115 | descriptor.sampleMask = 0xFFFFFFFF; |
| 116 | descriptor.alphaToCoverageEnabled = false; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 117 | |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 118 | descriptor.depthStencilState = nullptr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 119 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 120 | pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 123 | wgpuShaderModuleRelease(vsModule); |
| 124 | wgpuShaderModuleRelease(fsModule); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void frame() { |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 128 | WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 129 | WGPURenderPassDescriptor renderpassInfo; |
Corentin Wallez | 5f53d53 | 2019-10-08 07:34:43 +0000 | [diff] [blame] | 130 | renderpassInfo.nextInChain = nullptr; |
François Beaufort | 277d2e1 | 2019-10-03 14:56:49 +0000 | [diff] [blame] | 131 | renderpassInfo.label = nullptr; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 132 | WGPURenderPassColorAttachmentDescriptor colorAttachment; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 133 | { |
Jiawei Shao | 5e811ae | 2018-12-19 08:21:13 +0000 | [diff] [blame] | 134 | colorAttachment.attachment = backbufferView; |
| 135 | colorAttachment.resolveTarget = nullptr; |
| 136 | colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 137 | colorAttachment.loadOp = WGPULoadOp_Clear; |
| 138 | colorAttachment.storeOp = WGPUStoreOp_Store; |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 139 | renderpassInfo.colorAttachmentCount = 1; |
Corentin Wallez | a838c7d | 2019-09-20 22:59:47 +0000 | [diff] [blame] | 140 | renderpassInfo.colorAttachments = &colorAttachment; |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 141 | renderpassInfo.depthStencilAttachment = nullptr; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 142 | } |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 143 | WGPUCommandBuffer commands; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 144 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 145 | WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 146 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 147 | WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo); |
| 148 | wgpuRenderPassEncoderSetPipeline(pass, pipeline); |
| 149 | wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); |
| 150 | wgpuRenderPassEncoderEndPass(pass); |
| 151 | wgpuRenderPassEncoderRelease(pass); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 152 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 153 | commands = wgpuCommandEncoderFinish(encoder, nullptr); |
| 154 | wgpuCommandEncoderRelease(encoder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 157 | wgpuQueueSubmit(queue, 1, &commands); |
| 158 | wgpuCommandBufferRelease(commands); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 159 | wgpuSwapChainPresent(swapchain); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 160 | wgpuTextureViewRelease(backbufferView); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 161 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 162 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 166 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 167 | return 1; |
| 168 | } |
| 169 | init(); |
| 170 | |
| 171 | while (!ShouldQuit()) { |
| 172 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 173 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // TODO release stuff |
| 177 | } |