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 | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 22 | dawn::Device device; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 23 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 24 | dawn::Buffer indexBuffer; |
| 25 | dawn::Buffer vertexBuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 26 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 27 | dawn::Texture texture; |
| 28 | dawn::Sampler sampler; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 29 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 30 | dawn::Queue queue; |
| 31 | dawn::SwapChain swapchain; |
| 32 | dawn::TextureView depthStencilView; |
| 33 | dawn::RenderPipeline pipeline; |
| 34 | dawn::BindGroup bindGroup; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 35 | |
| 36 | void initBuffers() { |
| 37 | static const uint32_t indexData[3] = { |
| 38 | 0, 1, 2, |
| 39 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 40 | indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 41 | |
| 42 | static const float vertexData[12] = { |
| 43 | 0.0f, 0.5f, 0.0f, 1.0f, |
| 44 | -0.5f, -0.5f, 0.0f, 1.0f, |
| 45 | 0.5f, -0.5f, 0.0f, 1.0f, |
| 46 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 47 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void initTextures() { |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 51 | dawn::TextureDescriptor descriptor; |
| 52 | descriptor.dimension = dawn::TextureDimension::e2D; |
Corentin Wallez | 29353d6 | 2018-09-18 12:49:22 +0000 | [diff] [blame] | 53 | descriptor.size.width = 1024; |
| 54 | descriptor.size.height = 1024; |
| 55 | descriptor.size.depth = 1; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 56 | descriptor.arrayLayer = 1; |
| 57 | descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm; |
Jiawei Shao | 84cde51 | 2018-10-31 10:51:11 +0000 | [diff] [blame^] | 58 | descriptor.levelCount = 1; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 59 | descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled; |
| 60 | texture = device.CreateTexture(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 61 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 62 | dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor(); |
Corentin Wallez | b711b9b | 2018-05-22 14:50:59 -0400 | [diff] [blame] | 63 | sampler = device.CreateSampler(&samplerDesc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 64 | |
| 65 | // Initialize the texture with arbitrary data until we can load images |
| 66 | std::vector<uint8_t> data(4 * 1024 * 1024, 0); |
| 67 | for (size_t i = 0; i < data.size(); ++i) { |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 68 | data[i] = static_cast<uint8_t>(i % 253); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 71 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 72 | dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc); |
| 73 | dawn::CommandBuffer copy = device.CreateCommandBufferBuilder() |
Jiawei Shao | 4b74dbe | 2018-08-30 16:27:38 +0800 | [diff] [blame] | 74 | .CopyBufferToTexture(stagingBuffer, 0, 0, texture, 0, 0, 0, 1024, 1024, 1, 0, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 75 | .GetResult(); |
| 76 | |
| 77 | queue.Submit(1, ©); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 81 | device = CreateCppDawnDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 82 | |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 83 | queue = device.CreateQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 84 | swapchain = GetSwapChain(device); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 85 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 86 | dawn::TextureUsageBit::OutputAttachment, 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 87 | |
| 88 | initBuffers(); |
| 89 | initTextures(); |
| 90 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 91 | dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 92 | #version 450 |
| 93 | layout(location = 0) in vec4 pos; |
| 94 | void main() { |
| 95 | gl_Position = pos; |
| 96 | })" |
| 97 | ); |
| 98 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 99 | dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 100 | #version 450 |
| 101 | layout(set = 0, binding = 0) uniform sampler mySampler; |
| 102 | layout(set = 0, binding = 1) uniform texture2D myTexture; |
| 103 | |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 104 | layout(location = 0) out vec4 fragColor; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 105 | void main() { |
| 106 | fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0)); |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 107 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | |
| 109 | auto inputState = device.CreateInputStateBuilder() |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 110 | .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0) |
| 111 | .SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | .GetResult(); |
| 113 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 114 | auto bgl = utils::MakeBindGroupLayout( |
| 115 | device, { |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 116 | {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler}, |
| 117 | {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 118 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 119 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 120 | dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 122 | depthStencilView = CreateDefaultDepthStencilView(device); |
| 123 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 124 | pipeline = device.CreateRenderPipelineBuilder() |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 125 | .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat()) |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 126 | .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 127 | .SetLayout(pl) |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 128 | .SetStage(dawn::ShaderStage::Vertex, vsModule, "main") |
| 129 | .SetStage(dawn::ShaderStage::Fragment, fsModule, "main") |
| 130 | .SetIndexFormat(dawn::IndexFormat::Uint32) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 131 | .SetInputState(inputState) |
| 132 | .GetResult(); |
| 133 | |
Jiawei Shao | 3d67050 | 2018-09-18 00:31:57 +0000 | [diff] [blame] | 134 | dawn::TextureView view = texture.CreateDefaultTextureView(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 135 | |
| 136 | bindGroup = device.CreateBindGroupBuilder() |
| 137 | .SetLayout(bgl) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 138 | .SetSamplers(0, 1, &sampler) |
| 139 | .SetTextureViews(1, 1, &view) |
| 140 | .GetResult(); |
| 141 | } |
| 142 | |
| 143 | struct {uint32_t a; float b;} s; |
| 144 | void frame() { |
| 145 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 146 | s.b += 0.02f; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 147 | if (s.b >= 1.0f) {s.b = 0.0f;} |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 148 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 149 | dawn::Texture backbuffer; |
| 150 | dawn::RenderPassDescriptor renderPass; |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 151 | GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 152 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 153 | static const uint32_t vertexBufferOffsets[1] = {0}; |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 154 | dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder(); |
| 155 | { |
| 156 | dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass); |
| 157 | pass.SetRenderPipeline(pipeline); |
| 158 | pass.SetBindGroup(0, bindGroup); |
| 159 | pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets); |
| 160 | pass.SetIndexBuffer(indexBuffer, 0); |
| 161 | pass.DrawElements(3, 1, 0, 0); |
| 162 | pass.EndPass(); |
| 163 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 164 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 165 | dawn::CommandBuffer commands = builder.GetResult(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 166 | queue.Submit(1, &commands); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 167 | swapchain.Present(backbuffer); |
| 168 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 172 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 173 | return 1; |
| 174 | } |
| 175 | init(); |
| 176 | |
| 177 | while (!ShouldQuit()) { |
| 178 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 179 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // TODO release stuff |
| 183 | } |