blob: f13d0ba1e4bd527f832ed81801e5cf83cb4d8f62 [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 Wallez4828d922018-07-18 13:45:46 +020041 indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040042
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 Wallez4828d922018-07-18 13:45:46 +020048 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049}
50
51void initTextures() {
Jiawei Shao425428f2018-08-27 08:44:48 +080052 dawn::TextureDescriptor descriptor;
53 descriptor.dimension = dawn::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +000054 descriptor.size.width = 1024;
55 descriptor.size.height = 1024;
56 descriptor.size.depth = 1;
Jiawei Shao20301662019-02-21 00:45:19 +000057 descriptor.arrayLayerCount = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +000058 descriptor.sampleCount = 1;
Jiawei Shao425428f2018-08-27 08:44:48 +080059 descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
Jiawei Shao20301662019-02-21 00:45:19 +000060 descriptor.mipLevelCount = 1;
Jiawei Shao425428f2018-08-27 08:44:48 +080061 descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled;
62 texture = device.CreateTexture(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040063
Corentin Wallez4828d922018-07-18 13:45:46 +020064 dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
Corentin Wallezb711b9b2018-05-22 14:50:59 -040065 sampler = device.CreateSampler(&samplerDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040066
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 Ninomiya78c8b832017-07-21 17:00:22 -070070 data[i] = static_cast<uint8_t>(i % 253);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040071 }
72
Corentin Wallez4828d922018-07-18 13:45:46 +020073 dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
Brandon Jonesac71e342018-11-28 17:54:13 +000074 dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
Brandon Jones069664e2018-12-12 09:27:46 +000075 dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
Brandon Jonesac71e342018-11-28 17:54:13 +000076 dawn::Extent3D copySize = {1024, 1024, 1};
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040077
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000078 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
79 encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize);
80
81 dawn::CommandBuffer copy = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040082 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040083}
84
85void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020086 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040087
Corentin Wallezb703def2018-06-14 20:26:27 -040088 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070089 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040090 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +020091 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040092
93 initBuffers();
94 initTextures();
95
Corentin Wallez4828d922018-07-18 13:45:46 +020096 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040097 #version 450
98 layout(location = 0) in vec4 pos;
99 void main() {
100 gl_Position = pos;
101 })"
102 );
103
Corentin Wallez4828d922018-07-18 13:45:46 +0200104 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400105 #version 450
106 layout(set = 0, binding = 0) uniform sampler mySampler;
107 layout(set = 0, binding = 1) uniform texture2D myTexture;
108
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400109 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400110 void main() {
111 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400112 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400113
Kai Ninomiya234becf2018-07-10 12:23:50 -0700114 auto bgl = utils::MakeBindGroupLayout(
115 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200116 {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
117 {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700118 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400119
Corentin Wallez4828d922018-07-18 13:45:46 +0200120 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700122 depthStencilView = CreateDefaultDepthStencilView(device);
123
Yan, Shaoboa4924272018-12-10 19:47:22 +0000124 utils::ComboRenderPipelineDescriptor descriptor(device);
125 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
126 descriptor.cVertexStage.module = vsModule;
127 descriptor.cFragmentStage.module = fsModule;
Yunchao He889d7432019-03-27 18:08:50 +0000128 descriptor.cInputState.numAttributes = 1;
Yunchao He1ba2cb82019-03-28 05:09:01 +0000129 descriptor.cInputState.cAttributes[0].format = dawn::VertexFormat::Float4;
Yunchao He889d7432019-03-27 18:08:50 +0000130 descriptor.cInputState.numInputs = 1;
Yunchao He1ba2cb82019-03-28 05:09:01 +0000131 descriptor.cInputState.cInputs[0].stride = 4 * sizeof(float);
Yunchao He108bcbd2019-02-15 02:20:57 +0000132 descriptor.depthStencilState = &descriptor.cDepthStencilState;
133 descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint;
Yunchao He938811e2019-02-20 13:00:36 +0000134 descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000135
136 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400137
Jiawei Shao3d670502018-09-18 00:31:57 +0000138 dawn::TextureView view = texture.CreateDefaultTextureView();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400139
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000140 bindGroup = utils::MakeBindGroup(device, bgl, {
141 {0, sampler},
142 {1, view}
143 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144}
145
146struct {uint32_t a; float b;} s;
147void frame() {
148 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400149 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400150 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700151
Jiawei Shaob2c50232019-02-27 09:21:56 +0000152 dawn::Texture backbuffer = swapchain.GetNextTexture();
153 utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultTextureView()},
154 depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700155
Austin Engcf52d712019-04-05 20:51:29 +0000156 static const uint64_t vertexBufferOffsets[1] = {0};
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000157 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000158 {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000159 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000160 pass.SetPipeline(pipeline);
Yan, Shaobo991ab982019-03-18 06:01:37 +0000161 pass.SetBindGroup(0, bindGroup, 0, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000162 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
163 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000164 pass.DrawIndexed(3, 1, 0, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000165 pass.EndPass();
166 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400167
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000168 dawn::CommandBuffer commands = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400169 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700170 swapchain.Present(backbuffer);
171 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172}
173
174int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400175 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176 return 1;
177 }
178 init();
179
180 while (!ShouldQuit()) {
181 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400182 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400183 }
184
185 // TODO release stuff
186}