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