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 | f684040 | 2018-07-18 14:00:56 +0200 | [diff] [blame] | 17 | #include "utils/DawnHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 19 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 20 | DawnDevice device; |
| 21 | DawnQueue queue; |
| 22 | DawnSwapChain swapchain; |
| 23 | DawnRenderPipeline pipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 25 | DawnTextureFormat 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 | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 29 | queue = dawnDeviceCreateQueue(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 | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 32 | DawnSwapChainDescriptor descriptor; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 33 | descriptor.nextInChain = nullptr; |
| 34 | descriptor.implementation = GetSwapChainImplementation(); |
| 35 | swapchain = dawnDeviceCreateSwapChain(device, &descriptor); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 36 | } |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 37 | swapChainFormat = static_cast<DawnTextureFormat>(GetPreferredSwapChainTextureFormat()); |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 38 | dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_OUTPUT_ATTACHMENT, 640, |
| 39 | 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 | 2a1d8c2 | 2019-07-12 17:52:22 +0000 | [diff] [blame] | 47 | DawnShaderModule vsModule = |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 48 | utils::CreateShaderModule(dawn::Device(device), utils::SingleShaderStage::Vertex, vs) |
| 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 | 2a1d8c2 | 2019-07-12 17:52:22 +0000 | [diff] [blame] | 57 | DawnShaderModule 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 | { |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 61 | DawnRenderPipelineDescriptor descriptor; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 62 | descriptor.nextInChain = nullptr; |
| 63 | |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 64 | descriptor.vertexStage.nextInChain = nullptr; |
| 65 | descriptor.vertexStage.module = vsModule; |
| 66 | descriptor.vertexStage.entryPoint = "main"; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 67 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 68 | DawnPipelineStageDescriptor fragmentStage; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 69 | fragmentStage.nextInChain = nullptr; |
| 70 | fragmentStage.module = fsModule; |
| 71 | fragmentStage.entryPoint = "main"; |
| 72 | descriptor.fragmentStage = &fragmentStage; |
| 73 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 74 | descriptor.sampleCount = 1; |
| 75 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 76 | DawnBlendDescriptor blendDescriptor; |
Yunchao He | 92700bf | 2018-12-27 06:32:31 +0000 | [diff] [blame] | 77 | blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD; |
| 78 | blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE; |
| 79 | blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE; |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 80 | DawnColorStateDescriptor colorStateDescriptor; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 81 | colorStateDescriptor.nextInChain = nullptr; |
| 82 | colorStateDescriptor.format = swapChainFormat; |
| 83 | colorStateDescriptor.alphaBlend = blendDescriptor; |
| 84 | colorStateDescriptor.colorBlend = blendDescriptor; |
Austin Eng | 0c133bb | 2019-04-15 20:43:35 +0000 | [diff] [blame] | 85 | colorStateDescriptor.writeMask = DAWN_COLOR_WRITE_MASK_ALL; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 86 | |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 87 | descriptor.colorStateCount = 1; |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 88 | DawnColorStateDescriptor* colorStatesPtr[] = {&colorStateDescriptor}; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 89 | descriptor.colorStates = colorStatesPtr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 90 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 91 | DawnPipelineLayoutDescriptor pl; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 92 | pl.nextInChain = nullptr; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 93 | pl.bindGroupLayoutCount = 0; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 94 | pl.bindGroupLayouts = nullptr; |
| 95 | descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl); |
| 96 | |
Yunchao He | eea2091 | 2019-05-22 22:46:32 +0000 | [diff] [blame] | 97 | DawnVertexInputDescriptor vertexInput; |
| 98 | vertexInput.nextInChain = nullptr; |
| 99 | vertexInput.indexFormat = DAWN_INDEX_FORMAT_UINT32; |
Yunchao He | 2d4b529 | 2019-06-06 17:54:30 +0000 | [diff] [blame] | 100 | vertexInput.bufferCount = 0; |
Yunchao He | eea2091 | 2019-05-22 22:46:32 +0000 | [diff] [blame] | 101 | vertexInput.buffers = nullptr; |
Yunchao He | eea2091 | 2019-05-22 22:46:32 +0000 | [diff] [blame] | 102 | descriptor.vertexInput = &vertexInput; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 103 | |
Yunchao He | c33a8c1 | 2019-04-11 18:46:54 +0000 | [diff] [blame] | 104 | DawnRasterizationStateDescriptor rasterizationState; |
| 105 | rasterizationState.nextInChain = nullptr; |
| 106 | rasterizationState.frontFace = DAWN_FRONT_FACE_CCW; |
| 107 | rasterizationState.cullMode = DAWN_CULL_MODE_NONE; |
| 108 | rasterizationState.depthBias = 0; |
| 109 | rasterizationState.depthBiasSlopeScale = 0.0; |
| 110 | rasterizationState.depthBiasClamp = 0.0; |
| 111 | descriptor.rasterizationState = &rasterizationState; |
| 112 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 113 | descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Corentin Wallez | f07e85c | 2019-07-15 20:47:56 +0000 | [diff] [blame] | 114 | descriptor.sampleMask = 0xFFFFFFFF; |
| 115 | descriptor.alphaToCoverageEnabled = false; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 116 | |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 117 | descriptor.depthStencilState = nullptr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 118 | |
| 119 | pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 122 | dawnShaderModuleRelease(vsModule); |
| 123 | dawnShaderModuleRelease(fsModule); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void frame() { |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 127 | DawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain); |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 128 | DawnTextureView backbufferView = dawnTextureCreateView(backbuffer, nullptr); |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 129 | DawnRenderPassDescriptor renderpassInfo; |
| 130 | DawnRenderPassColorAttachmentDescriptor colorAttachment; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 131 | { |
Jiawei Shao | 5e811ae | 2018-12-19 08:21:13 +0000 | [diff] [blame] | 132 | colorAttachment.attachment = backbufferView; |
| 133 | colorAttachment.resolveTarget = nullptr; |
| 134 | colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 135 | colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR; |
| 136 | colorAttachment.storeOp = DAWN_STORE_OP_STORE; |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 137 | renderpassInfo.colorAttachmentCount = 1; |
Corentin Wallez | a838c7d | 2019-09-20 22:59:47 +0000 | [diff] [blame^] | 138 | renderpassInfo.colorAttachments = &colorAttachment; |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 139 | renderpassInfo.depthStencilAttachment = nullptr; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 140 | } |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 141 | DawnCommandBuffer commands; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 142 | { |
Corentin Wallez | 4b90c47 | 2019-07-10 20:43:13 +0000 | [diff] [blame] | 143 | DawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device, nullptr); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 144 | |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 145 | DawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, &renderpassInfo); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 146 | dawnRenderPassEncoderSetPipeline(pass, pipeline); |
Jiawei Shao | c789b84 | 2018-12-10 05:20:19 +0000 | [diff] [blame] | 147 | dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 148 | dawnRenderPassEncoderEndPass(pass); |
| 149 | dawnRenderPassEncoderRelease(pass); |
| 150 | |
Corentin Wallez | 4b90c47 | 2019-07-10 20:43:13 +0000 | [diff] [blame] | 151 | commands = dawnCommandEncoderFinish(encoder, nullptr); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 152 | dawnCommandEncoderRelease(encoder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 155 | dawnQueueSubmit(queue, 1, &commands); |
| 156 | dawnCommandBufferRelease(commands); |
| 157 | dawnSwapChainPresent(swapchain, backbuffer); |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 158 | dawnTextureViewRelease(backbufferView); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 159 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 160 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 164 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | init(); |
| 168 | |
| 169 | while (!ShouldQuit()) { |
| 170 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 171 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // TODO release stuff |
| 175 | } |