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 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 17 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | f684040 | 2018-07-18 14:00:56 +0200 | [diff] [blame] | 18 | #include "utils/DawnHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 19 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 20 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 23 | dawn::Device device; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 25 | dawn::Buffer indexBuffer; |
| 26 | dawn::Buffer vertexBuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 28 | dawn::Texture texture; |
| 29 | dawn::Sampler sampler; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 31 | dawn::Queue queue; |
| 32 | dawn::SwapChain swapchain; |
| 33 | dawn::TextureView depthStencilView; |
| 34 | dawn::RenderPipeline pipeline; |
| 35 | dawn::BindGroup bindGroup; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
| 37 | void initBuffers() { |
| 38 | static const uint32_t indexData[3] = { |
| 39 | 0, 1, 2, |
| 40 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 41 | indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | |
| 43 | static const float vertexData[12] = { |
| 44 | 0.0f, 0.5f, 0.0f, 1.0f, |
| 45 | -0.5f, -0.5f, 0.0f, 1.0f, |
| 46 | 0.5f, -0.5f, 0.0f, 1.0f, |
| 47 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 48 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void initTextures() { |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 52 | dawn::TextureDescriptor descriptor; |
| 53 | descriptor.dimension = dawn::TextureDimension::e2D; |
Corentin Wallez | 29353d6 | 2018-09-18 12:49:22 +0000 | [diff] [blame] | 54 | descriptor.size.width = 1024; |
| 55 | descriptor.size.height = 1024; |
| 56 | descriptor.size.depth = 1; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 57 | descriptor.arrayLayerCount = 1; |
Jiawei Shao | 8bff3b2 | 2018-12-12 09:27:16 +0000 | [diff] [blame] | 58 | descriptor.sampleCount = 1; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 59 | descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 60 | descriptor.mipLevelCount = 1; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 61 | descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled; |
| 62 | texture = device.CreateTexture(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 63 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 64 | dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor(); |
Corentin Wallez | b711b9b | 2018-05-22 14:50:59 -0400 | [diff] [blame] | 65 | sampler = device.CreateSampler(&samplerDesc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 66 | |
| 67 | // Initialize the texture with arbitrary data until we can load images |
| 68 | std::vector<uint8_t> data(4 * 1024 * 1024, 0); |
| 69 | for (size_t i = 0; i < data.size(); ++i) { |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 70 | data[i] = static_cast<uint8_t>(i % 253); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 73 | dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc); |
Brandon Jones | ac71e34 | 2018-11-28 17:54:13 +0000 | [diff] [blame] | 74 | dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0); |
Brandon Jones | 069664e | 2018-12-12 09:27:46 +0000 | [diff] [blame] | 75 | dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0}); |
Brandon Jones | ac71e34 | 2018-11-28 17:54:13 +0000 | [diff] [blame] | 76 | dawn::Extent3D copySize = {1024, 1024, 1}; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 77 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 78 | dawn::CommandEncoder encoder = device.CreateCommandEncoder(); |
| 79 | encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size); |
| 80 | |
| 81 | dawn::CommandBuffer copy = encoder.Finish(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 82 | queue.Submit(1, ©); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 86 | device = CreateCppDawnDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 87 | |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 88 | queue = device.CreateQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 89 | swapchain = GetSwapChain(device); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 90 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 91 | dawn::TextureUsageBit::OutputAttachment, 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 92 | |
| 93 | initBuffers(); |
| 94 | initTextures(); |
| 95 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 96 | dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 97 | #version 450 |
| 98 | layout(location = 0) in vec4 pos; |
| 99 | void main() { |
| 100 | gl_Position = pos; |
| 101 | })" |
| 102 | ); |
| 103 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 104 | dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 105 | #version 450 |
| 106 | layout(set = 0, binding = 0) uniform sampler mySampler; |
| 107 | layout(set = 0, binding = 1) uniform texture2D myTexture; |
| 108 | |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 109 | layout(location = 0) out vec4 fragColor; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 110 | void main() { |
| 111 | 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] | 112 | })"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 124 | utils::ComboRenderPipelineDescriptor descriptor(device); |
| 125 | descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); |
| 126 | descriptor.cVertexStage.module = vsModule; |
| 127 | descriptor.cFragmentStage.module = fsModule; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 128 | descriptor.cInputState.numAttributes = 1; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame] | 129 | descriptor.cInputState.cAttributes[0].format = dawn::VertexFormat::Float4; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 130 | descriptor.cInputState.numInputs = 1; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame] | 131 | descriptor.cInputState.cInputs[0].stride = 4 * sizeof(float); |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 132 | descriptor.depthStencilState = &descriptor.cDepthStencilState; |
| 133 | descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 134 | descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat(); |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 135 | |
| 136 | pipeline = device.CreateRenderPipeline(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 137 | |
Jiawei Shao | 3d67050 | 2018-09-18 00:31:57 +0000 | [diff] [blame] | 138 | dawn::TextureView view = texture.CreateDefaultTextureView(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 139 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 140 | bindGroup = utils::MakeBindGroup(device, bgl, { |
| 141 | {0, sampler}, |
| 142 | {1, view} |
| 143 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | struct {uint32_t a; float b;} s; |
| 147 | void frame() { |
| 148 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 149 | s.b += 0.02f; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 150 | if (s.b >= 1.0f) {s.b = 0.0f;} |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 151 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 152 | dawn::Texture backbuffer = swapchain.GetNextTexture(); |
| 153 | utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultTextureView()}, |
| 154 | depthStencilView); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 155 | |
Austin Eng | cf52d71 | 2019-04-05 20:51:29 +0000 | [diff] [blame^] | 156 | static const uint64_t vertexBufferOffsets[1] = {0}; |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 157 | dawn::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 158 | { |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 159 | dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 160 | pass.SetPipeline(pipeline); |
Yan, Shaobo | 991ab98 | 2019-03-18 06:01:37 +0000 | [diff] [blame] | 161 | pass.SetBindGroup(0, bindGroup, 0, nullptr); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 162 | pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets); |
| 163 | pass.SetIndexBuffer(indexBuffer, 0); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 164 | pass.DrawIndexed(3, 1, 0, 0, 0); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 165 | pass.EndPass(); |
| 166 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 167 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 168 | dawn::CommandBuffer commands = encoder.Finish(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 169 | queue.Submit(1, &commands); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 170 | swapchain.Present(backbuffer); |
| 171 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 175 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | init(); |
| 179 | |
| 180 | while (!ShouldQuit()) { |
| 181 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 182 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // TODO release stuff |
| 186 | } |