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 | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 32 | dawnSwapChainBuilder builder = dawnDeviceCreateSwapChainBuilder(device); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 33 | uint64_t swapchainImpl = GetSwapChainImplementation(); |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 34 | dawnSwapChainBuilderSetImplementation(builder, swapchainImpl); |
| 35 | swapchain = dawnSwapChainBuilderGetResult(builder); |
| 36 | dawnSwapChainBuilderRelease(builder); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 37 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 38 | swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat()); |
| 39 | dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640, |
Corentin Wallez | e98678f | 2018-01-19 12:51:18 -0500 | [diff] [blame] | 40 | 480); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 41 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | const char* vs = |
| 43 | "#version 450\n" |
| 44 | "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n" |
| 45 | "void main() {\n" |
| 46 | " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n" |
| 47 | "}\n"; |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 48 | dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 49 | |
| 50 | const char* fs = |
| 51 | "#version 450\n" |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 52 | "layout(location = 0) out vec4 fragColor;" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 53 | "void main() {\n" |
| 54 | " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 55 | "}\n"; |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 56 | dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 57 | |
| 58 | { |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 59 | dawnRenderPipelineDescriptor descriptor; |
| 60 | descriptor.nextInChain = nullptr; |
| 61 | |
| 62 | dawnPipelineStageDescriptor vertexStage; |
| 63 | vertexStage.nextInChain = nullptr; |
| 64 | vertexStage.module = vsModule; |
| 65 | vertexStage.entryPoint = "main"; |
| 66 | descriptor.vertexStage = &vertexStage; |
| 67 | |
| 68 | dawnPipelineStageDescriptor fragmentStage; |
| 69 | fragmentStage.nextInChain = nullptr; |
| 70 | fragmentStage.module = fsModule; |
| 71 | fragmentStage.entryPoint = "main"; |
| 72 | descriptor.fragmentStage = &fragmentStage; |
| 73 | |
| 74 | dawnAttachmentsStateDescriptor attachmentsState; |
| 75 | attachmentsState.nextInChain = nullptr; |
| 76 | attachmentsState.numColorAttachments = 1; |
| 77 | dawnAttachmentDescriptor colorAttachment = {nullptr, swapChainFormat}; |
| 78 | attachmentsState.colorAttachments = &colorAttachment; |
| 79 | attachmentsState.hasDepthStencilAttachment = false; |
| 80 | // Even with hasDepthStencilAttachment = false, depthStencilAttachment must point to valid |
| 81 | // data because we don't have optional substructures yet. |
| 82 | attachmentsState.depthStencilAttachment = &colorAttachment; |
| 83 | descriptor.attachmentsState = &attachmentsState; |
| 84 | |
| 85 | descriptor.sampleCount = 1; |
| 86 | |
| 87 | descriptor.numBlendStates = 1; |
| 88 | dawnBlendStateBuilder blendStateBuilder = dawnDeviceCreateBlendStateBuilder(device); |
| 89 | dawnBlendState blendState = dawnBlendStateBuilderGetResult(blendStateBuilder); |
| 90 | descriptor.blendStates = &blendState; |
| 91 | dawnBlendStateBuilderRelease(blendStateBuilder); |
| 92 | |
| 93 | dawnPipelineLayoutDescriptor pl; |
| 94 | pl.nextInChain = nullptr; |
| 95 | pl.numBindGroupLayouts = 0; |
| 96 | pl.bindGroupLayouts = nullptr; |
| 97 | descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl); |
| 98 | |
| 99 | dawnInputStateBuilder inputStateBuilder = dawnDeviceCreateInputStateBuilder(device); |
| 100 | descriptor.inputState = dawnInputStateBuilderGetResult(inputStateBuilder); |
| 101 | dawnInputStateBuilderRelease(inputStateBuilder); |
| 102 | |
| 103 | descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32; |
| 104 | descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 105 | |
| 106 | dawnDepthStencilStateBuilder depthStencilBuilder = dawnDeviceCreateDepthStencilStateBuilder(device); |
| 107 | descriptor.depthStencilState = dawnDepthStencilStateBuilderGetResult(depthStencilBuilder); |
| 108 | dawnDepthStencilStateBuilderRelease(depthStencilBuilder); |
| 109 | |
| 110 | pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor); |
| 111 | |
| 112 | dawnBlendStateRelease(descriptor.blendStates[0]); |
| 113 | dawnDepthStencilStateRelease(descriptor.depthStencilState); |
| 114 | dawnInputStateRelease(descriptor.inputState); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 117 | dawnShaderModuleRelease(vsModule); |
| 118 | dawnShaderModuleRelease(fsModule); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void frame() { |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 122 | dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain); |
| 123 | dawnTextureView backbufferView; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 124 | { |
Jiawei Shao | 3d67050 | 2018-09-18 00:31:57 +0000 | [diff] [blame] | 125 | backbufferView = dawnTextureCreateDefaultTextureView(backbuffer); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 126 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 127 | dawnRenderPassDescriptor renderpassInfo; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 128 | { |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 129 | dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device); |
Jiawei Shao | 5e811ae | 2018-12-19 08:21:13 +0000 | [diff] [blame^] | 130 | dawnRenderPassColorAttachmentDescriptor colorAttachment; |
| 131 | colorAttachment.attachment = backbufferView; |
| 132 | colorAttachment.resolveTarget = nullptr; |
| 133 | colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 134 | colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR; |
| 135 | colorAttachment.storeOp = DAWN_STORE_OP_STORE; |
| 136 | dawnRenderPassDescriptorBuilderSetColorAttachments(builder, 1, &colorAttachment); |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 137 | renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder); |
| 138 | dawnRenderPassDescriptorBuilderRelease(builder); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 139 | } |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 140 | dawnCommandBuffer commands; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 141 | { |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 142 | dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 143 | |
| 144 | dawnRenderPassEncoder pass = dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo); |
| 145 | dawnRenderPassEncoderSetRenderPipeline(pass, pipeline); |
Jiawei Shao | c789b84 | 2018-12-10 05:20:19 +0000 | [diff] [blame] | 146 | dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 147 | dawnRenderPassEncoderEndPass(pass); |
| 148 | dawnRenderPassEncoderRelease(pass); |
| 149 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 150 | commands = dawnCommandBufferBuilderGetResult(builder); |
| 151 | dawnCommandBufferBuilderRelease(builder); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 154 | dawnQueueSubmit(queue, 1, &commands); |
| 155 | dawnCommandBufferRelease(commands); |
| 156 | dawnSwapChainPresent(swapchain, backbuffer); |
| 157 | dawnRenderPassDescriptorRelease(renderpassInfo); |
| 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 | } |