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