blob: bc89a47cb202f597e12d05eca01999b2e1881280 [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 Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez04863c42019-10-25 11:36:47 +000019#include "utils/WGPUHelpers.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040020
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021#include <vector>
22
Corentin Wallez04863c42019-10-25 11:36:47 +000023wgpu::Device device;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Wallez04863c42019-10-25 11:36:47 +000025wgpu::Buffer indexBuffer;
26wgpu::Buffer vertexBuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027
Corentin Wallez04863c42019-10-25 11:36:47 +000028wgpu::Texture texture;
29wgpu::Sampler sampler;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Corentin Wallez04863c42019-10-25 11:36:47 +000031wgpu::Queue queue;
32wgpu::SwapChain swapchain;
33wgpu::TextureView depthStencilView;
34wgpu::RenderPipeline pipeline;
35wgpu::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 =
Corentin Wallez04863c42019-10-25 11:36:47 +000042 utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::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),
Corentin Wallez04863c42019-10-25 11:36:47 +000050 wgpu::BufferUsage::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040051}
52
53void initTextures() {
Corentin Wallez04863c42019-10-25 11:36:47 +000054 wgpu::TextureDescriptor descriptor;
55 descriptor.dimension = wgpu::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 Wallez04863c42019-10-25 11:36:47 +000061 descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
Jiawei Shao20301662019-02-21 00:45:19 +000062 descriptor.mipLevelCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +000063 descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled;
Jiawei Shao425428f2018-08-27 08:44:48 +080064 texture = device.CreateTexture(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040065
Corentin Wallez04863c42019-10-25 11:36:47 +000066 wgpu::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 Wallez04863c42019-10-25 11:36:47 +000075 wgpu::Buffer stagingBuffer = utils::CreateBufferFromData(
76 device, data.data(), static_cast<uint32_t>(data.size()), wgpu::BufferUsage::CopySrc);
Corentin Wallezf5657af2020-06-08 16:13:41 +000077 wgpu::BufferCopyView bufferCopyView =
78 utils::CreateBufferCopyView(stagingBuffer, 0, 4 * 1024, 0);
Corentin Wallez04863c42019-10-25 11:36:47 +000079 wgpu::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
80 wgpu::Extent3D copySize = {1024, 1024, 1};
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
Corentin Wallez04863c42019-10-25 11:36:47 +000082 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000083 encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize);
84
Corentin Wallez04863c42019-10-25 11:36:47 +000085 wgpu::CommandBuffer copy = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040087}
88
89void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020090 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091
Corentin Wallez8a437942020-04-17 16:45:17 +000092 queue = device.GetDefaultQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070093 swapchain = GetSwapChain(device);
Corentin Wallez04863c42019-10-25 11:36:47 +000094 swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000095 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040096
97 initBuffers();
98 initTextures();
99
Corentin Wallez04863c42019-10-25 11:36:47 +0000100 wgpu::ShaderModule vsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000101 utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400102 #version 450
103 layout(location = 0) in vec4 pos;
104 void main() {
105 gl_Position = pos;
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000106 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400107
Corentin Wallez04863c42019-10-25 11:36:47 +0000108 wgpu::ShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000109 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400110 #version 450
111 layout(set = 0, binding = 0) uniform sampler mySampler;
112 layout(set = 0, binding = 1) uniform texture2D myTexture;
113
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400114 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400115 void main() {
116 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400117 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118
Kai Ninomiya234becf2018-07-10 12:23:50 -0700119 auto bgl = utils::MakeBindGroupLayout(
120 device, {
Corentin Wallez04863c42019-10-25 11:36:47 +0000121 {0, wgpu::ShaderStage::Fragment, wgpu::BindingType::Sampler},
122 {1, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700123 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124
Corentin Wallez04863c42019-10-25 11:36:47 +0000125 wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400126
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700127 depthStencilView = CreateDefaultDepthStencilView(device);
128
Yan, Shaoboa4924272018-12-10 19:47:22 +0000129 utils::ComboRenderPipelineDescriptor descriptor(device);
130 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000131 descriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000132 descriptor.cFragmentStage.module = fsModule;
Kai Ninomiyaae1f25f2019-11-07 22:23:29 +0000133 descriptor.cVertexState.vertexBufferCount = 1;
134 descriptor.cVertexState.cVertexBuffers[0].arrayStride = 4 * sizeof(float);
135 descriptor.cVertexState.cVertexBuffers[0].attributeCount = 1;
136 descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float4;
Yunchao He108bcbd2019-02-15 02:20:57 +0000137 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez04863c42019-10-25 11:36:47 +0000138 descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000139 descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000140
141 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400142
Corentin Wallez04863c42019-10-25 11:36:47 +0000143 wgpu::TextureView view = texture.CreateView();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000145 bindGroup = utils::MakeBindGroup(device, bgl, {
146 {0, sampler},
147 {1, view}
148 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400149}
150
151struct {uint32_t a; float b;} s;
152void frame() {
153 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400154 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400155 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700156
Corentin Wallez604072b2019-11-12 18:30:11 +0000157 wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
158 utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700159
Corentin Wallez04863c42019-10-25 11:36:47 +0000160 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000161 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000162 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000163 pass.SetPipeline(pipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000164 pass.SetBindGroup(0, bindGroup);
François Beaufort91b21422019-10-10 07:29:58 +0000165 pass.SetVertexBuffer(0, vertexBuffer);
Corentin Wallezd2855252019-10-10 17:21:48 +0000166 pass.SetIndexBuffer(indexBuffer);
Corentin Wallez67b1ad72020-03-31 16:21:35 +0000167 pass.DrawIndexed(3);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000168 pass.EndPass();
169 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170
Corentin Wallez04863c42019-10-25 11:36:47 +0000171 wgpu::CommandBuffer commands = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 queue.Submit(1, &commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000173 swapchain.Present();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700174 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400175}
176
177int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400178 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400179 return 1;
180 }
181 init();
182
183 while (!ShouldQuit()) {
184 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400185 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400186 }
187
188 // TODO release stuff
189}