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 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [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 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [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 | { |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 32 | dawnSwapChainDescriptor descriptor; |
| 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 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 37 | swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat()); |
| 38 | dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640, |
Corentin Wallez | e98678f | 2018-01-19 12:51:18 -0500 | [diff] [blame] | 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 | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 47 | dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | |
| 49 | const char* fs = |
| 50 | "#version 450\n" |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 51 | "layout(location = 0) out vec4 fragColor;" |
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 | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 55 | dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 56 | |
| 57 | { |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 58 | dawnRenderPipelineDescriptor descriptor; |
| 59 | descriptor.nextInChain = nullptr; |
| 60 | |
| 61 | dawnPipelineStageDescriptor vertexStage; |
| 62 | vertexStage.nextInChain = nullptr; |
| 63 | vertexStage.module = vsModule; |
| 64 | vertexStage.entryPoint = "main"; |
| 65 | descriptor.vertexStage = &vertexStage; |
| 66 | |
| 67 | dawnPipelineStageDescriptor fragmentStage; |
| 68 | fragmentStage.nextInChain = nullptr; |
| 69 | fragmentStage.module = fsModule; |
| 70 | fragmentStage.entryPoint = "main"; |
| 71 | descriptor.fragmentStage = &fragmentStage; |
| 72 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 73 | descriptor.sampleCount = 1; |
| 74 | |
Yunchao He | 92700bf | 2018-12-27 06:32:31 +0000 | [diff] [blame] | 75 | dawnBlendDescriptor blendDescriptor; |
| 76 | blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD; |
| 77 | blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE; |
| 78 | blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 79 | dawnColorStateDescriptor colorStateDescriptor; |
| 80 | colorStateDescriptor.nextInChain = nullptr; |
| 81 | colorStateDescriptor.format = swapChainFormat; |
| 82 | colorStateDescriptor.alphaBlend = blendDescriptor; |
| 83 | colorStateDescriptor.colorBlend = blendDescriptor; |
| 84 | colorStateDescriptor.colorWriteMask = DAWN_COLOR_WRITE_MASK_ALL; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame^] | 85 | |
| 86 | descriptor.numColorStates = 1; |
| 87 | dawnColorStateDescriptor* colorStatesPtr[] = {&colorStateDescriptor}; |
| 88 | descriptor.colorStates = colorStatesPtr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 89 | |
| 90 | dawnPipelineLayoutDescriptor pl; |
| 91 | pl.nextInChain = nullptr; |
| 92 | pl.numBindGroupLayouts = 0; |
| 93 | pl.bindGroupLayouts = nullptr; |
| 94 | descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl); |
| 95 | |
| 96 | dawnInputStateBuilder inputStateBuilder = dawnDeviceCreateInputStateBuilder(device); |
| 97 | descriptor.inputState = dawnInputStateBuilderGetResult(inputStateBuilder); |
| 98 | dawnInputStateBuilderRelease(inputStateBuilder); |
| 99 | |
| 100 | descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32; |
| 101 | descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 102 | |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 103 | descriptor.depthStencilState = nullptr; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 104 | |
| 105 | pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor); |
| 106 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 107 | dawnInputStateRelease(descriptor.inputState); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 110 | dawnShaderModuleRelease(vsModule); |
| 111 | dawnShaderModuleRelease(fsModule); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void frame() { |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 115 | dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain); |
| 116 | dawnTextureView backbufferView; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 117 | { |
Jiawei Shao | 3d67050 | 2018-09-18 00:31:57 +0000 | [diff] [blame] | 118 | backbufferView = dawnTextureCreateDefaultTextureView(backbuffer); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 119 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 120 | dawnRenderPassDescriptor renderpassInfo; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 121 | { |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 122 | dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device); |
Jiawei Shao | 5e811ae | 2018-12-19 08:21:13 +0000 | [diff] [blame] | 123 | dawnRenderPassColorAttachmentDescriptor colorAttachment; |
| 124 | colorAttachment.attachment = backbufferView; |
| 125 | colorAttachment.resolveTarget = nullptr; |
| 126 | colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 127 | colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR; |
| 128 | colorAttachment.storeOp = DAWN_STORE_OP_STORE; |
| 129 | dawnRenderPassDescriptorBuilderSetColorAttachments(builder, 1, &colorAttachment); |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 130 | renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder); |
| 131 | dawnRenderPassDescriptorBuilderRelease(builder); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 132 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 133 | dawnCommandBuffer commands; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 134 | { |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 135 | dawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 136 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 137 | dawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, renderpassInfo); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 138 | dawnRenderPassEncoderSetPipeline(pass, pipeline); |
Jiawei Shao | c789b84 | 2018-12-10 05:20:19 +0000 | [diff] [blame] | 139 | dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 140 | dawnRenderPassEncoderEndPass(pass); |
| 141 | dawnRenderPassEncoderRelease(pass); |
| 142 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 143 | commands = dawnCommandEncoderFinish(encoder); |
| 144 | dawnCommandEncoderRelease(encoder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 147 | dawnQueueSubmit(queue, 1, &commands); |
| 148 | dawnCommandBufferRelease(commands); |
| 149 | dawnSwapChainPresent(swapchain, backbuffer); |
| 150 | dawnRenderPassDescriptorRelease(renderpassInfo); |
| 151 | dawnTextureViewRelease(backbufferView); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 152 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 153 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 157 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 158 | return 1; |
| 159 | } |
| 160 | init(); |
| 161 | |
| 162 | while (!ShouldQuit()) { |
| 163 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 164 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // TODO release stuff |
| 168 | } |