blob: 2437e3a5219d78b1b6330c13e9871e45f270a486 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
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 Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Yan, Shaoboa4924272018-12-10 19:47:22 +000017#include "utils/ComboRenderPipelineDescriptor.h"
Corentin Wallezf6840402018-07-18 14:00:56 +020018#include "utils/DawnHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040019#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040020
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021#include <vector>
22
Corentin Wallez4828d922018-07-18 13:45:46 +020023dawn::Device device;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Wallez4828d922018-07-18 13:45:46 +020025dawn::Buffer indexBuffer;
26dawn::Buffer vertexBuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027
Corentin Wallez4828d922018-07-18 13:45:46 +020028dawn::Texture texture;
29dawn::Sampler sampler;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Corentin Wallez4828d922018-07-18 13:45:46 +020031dawn::Queue queue;
32dawn::SwapChain swapchain;
33dawn::TextureView depthStencilView;
34dawn::RenderPipeline pipeline;
35dawn::BindGroup bindGroup;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
37void initBuffers() {
38 static const uint32_t indexData[3] = {
39 0, 1, 2,
40 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000041 indexBuffer =
42 utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsage::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040043
44 static const float vertexData[12] = {
45 0.0f, 0.5f, 0.0f, 1.0f,
46 -0.5f, -0.5f, 0.0f, 1.0f,
47 0.5f, -0.5f, 0.0f, 1.0f,
48 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000049 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
50 dawn::BufferUsage::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040051}
52
53void initTextures() {
Jiawei Shao425428f2018-08-27 08:44:48 +080054 dawn::TextureDescriptor descriptor;
55 descriptor.dimension = dawn::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +000056 descriptor.size.width = 1024;
57 descriptor.size.height = 1024;
58 descriptor.size.depth = 1;
Jiawei Shao20301662019-02-21 00:45:19 +000059 descriptor.arrayLayerCount = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +000060 descriptor.sampleCount = 1;
Corentin Wallez77fa31c2019-06-19 09:26:07 +000061 descriptor.format = dawn::TextureFormat::RGBA8Unorm;
Jiawei Shao20301662019-02-21 00:45:19 +000062 descriptor.mipLevelCount = 1;
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000063 descriptor.usage = dawn::TextureUsage::CopyDst | dawn::TextureUsage::Sampled;
Jiawei Shao425428f2018-08-27 08:44:48 +080064 texture = device.CreateTexture(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040065
Corentin Wallez4828d922018-07-18 13:45:46 +020066 dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
Corentin Wallezb711b9b2018-05-22 14:50:59 -040067 sampler = device.CreateSampler(&samplerDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040068
69 // Initialize the texture with arbitrary data until we can load images
70 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
71 for (size_t i = 0; i < data.size(); ++i) {
Kai Ninomiya78c8b832017-07-21 17:00:22 -070072 data[i] = static_cast<uint8_t>(i % 253);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040073 }
74
Corentin Wallezec053552019-07-08 10:05:46 +000075 dawn::Buffer stagingBuffer = utils::CreateBufferFromData(
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000076 device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsage::CopySrc);
Brandon Jonesac71e342018-11-28 17:54:13 +000077 dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
Brandon Jones069664e2018-12-12 09:27:46 +000078 dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
Brandon Jonesac71e342018-11-28 17:54:13 +000079 dawn::Extent3D copySize = {1024, 1024, 1};
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040080
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000081 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
82 encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize);
83
84 dawn::CommandBuffer copy = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040085 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086}
87
88void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020089 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040090
Corentin Wallezb703def2018-06-14 20:26:27 -040091 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070092 swapchain = GetSwapChain(device);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000093 swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
94 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040095
96 initBuffers();
97 initTextures();
98
Corentin Wallezb9b088f2019-08-27 08:42:29 +000099 dawn::ShaderModule vsModule =
100 utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400101 #version 450
102 layout(location = 0) in vec4 pos;
103 void main() {
104 gl_Position = pos;
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000105 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000107 dawn::ShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000108 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400109 #version 450
110 layout(set = 0, binding = 0) uniform sampler mySampler;
111 layout(set = 0, binding = 1) uniform texture2D myTexture;
112
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400113 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114 void main() {
115 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400116 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117
Kai Ninomiya234becf2018-07-10 12:23:50 -0700118 auto bgl = utils::MakeBindGroupLayout(
119 device, {
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000120 {0, dawn::ShaderStage::Fragment, dawn::BindingType::Sampler},
121 {1, dawn::ShaderStage::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700122 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400123
Corentin Wallez4828d922018-07-18 13:45:46 +0200124 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700126 depthStencilView = CreateDefaultDepthStencilView(device);
127
Yan, Shaoboa4924272018-12-10 19:47:22 +0000128 utils::ComboRenderPipelineDescriptor descriptor(device);
129 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000130 descriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000131 descriptor.cFragmentStage.module = fsModule;
Yunchao He2d4b5292019-06-06 17:54:30 +0000132 descriptor.cVertexInput.bufferCount = 1;
Yunchao Heeea20912019-05-22 22:46:32 +0000133 descriptor.cVertexInput.cBuffers[0].stride = 4 * sizeof(float);
Yunchao He2d4b5292019-06-06 17:54:30 +0000134 descriptor.cVertexInput.cBuffers[0].attributeCount = 1;
Yunchao He97c08852019-06-06 01:56:57 +0000135 descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float4;
Yunchao He108bcbd2019-02-15 02:20:57 +0000136 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000137 descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000138 descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000139
140 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400141
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000142 dawn::TextureView view = texture.CreateView();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400143
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000144 bindGroup = utils::MakeBindGroup(device, bgl, {
145 {0, sampler},
146 {1, view}
147 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400148}
149
150struct {uint32_t a; float b;} s;
151void frame() {
152 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400153 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400154 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700155
Jiawei Shaob2c50232019-02-27 09:21:56 +0000156 dawn::Texture backbuffer = swapchain.GetNextTexture();
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000157 utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700158
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000159 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000160 {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000161 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000162 pass.SetPipeline(pipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000163 pass.SetBindGroup(0, bindGroup);
François Beaufort91b21422019-10-10 07:29:58 +0000164 pass.SetVertexBuffer(0, vertexBuffer);
Corentin Wallezd2855252019-10-10 17:21:48 +0000165 pass.SetIndexBuffer(indexBuffer);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000166 pass.DrawIndexed(3, 1, 0, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000167 pass.EndPass();
168 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400169
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000170 dawn::CommandBuffer commands = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400171 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700172 swapchain.Present(backbuffer);
173 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400174}
175
176int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400177 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400178 return 1;
179 }
180 init();
181
182 while (!ShouldQuit()) {
183 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400184 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400185 }
186
187 // TODO release stuff
188}