blob: ea3c6ebfa40d5b8124cd33dadd1ca932d7cea90d [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] = {
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000039 0,
40 1,
41 2,
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040042 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000043 indexBuffer =
Corentin Wallez04863c42019-10-25 11:36:47 +000044 utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040045
46 static const float vertexData[12] = {
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000047 0.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f,
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048 };
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;
shrekshaob00de7f2021-03-22 21:12:36 +000058 descriptor.size.depthOrArrayLayers = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +000059 descriptor.sampleCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +000060 descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
Jiawei Shao20301662019-02-21 00:45:19 +000061 descriptor.mipLevelCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +000062 descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled;
Jiawei Shao425428f2018-08-27 08:44:48 +080063 texture = device.CreateTexture(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040064
Corentin Wallezfb2e7712021-02-05 20:26:54 +000065 sampler = device.CreateSampler();
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 Wallez04863c42019-10-25 11:36:47 +000073 wgpu::Buffer stagingBuffer = utils::CreateBufferFromData(
74 device, data.data(), static_cast<uint32_t>(data.size()), wgpu::BufferUsage::CopySrc);
Corentin Wallez80915842021-03-04 18:13:45 +000075 wgpu::ImageCopyBuffer imageCopyBuffer =
76 utils::CreateImageCopyBuffer(stagingBuffer, 0, 4 * 1024);
77 wgpu::ImageCopyTexture imageCopyTexture = utils::CreateImageCopyTexture(texture, 0, {0, 0, 0});
Corentin Wallez04863c42019-10-25 11:36:47 +000078 wgpu::Extent3D copySize = {1024, 1024, 1};
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040079
Corentin Wallez04863c42019-10-25 11:36:47 +000080 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez80915842021-03-04 18:13:45 +000081 encoder.CopyBufferToTexture(&imageCopyBuffer, &imageCopyTexture, &copySize);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000082
Corentin Wallez04863c42019-10-25 11:36:47 +000083 wgpu::CommandBuffer copy = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040084 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040085}
86
87void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020088 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040089
Corentin Wallez6d315da2021-02-04 15:33:42 +000090 queue = device.GetQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070091 swapchain = GetSwapChain(device);
Corentin Wallez6b087812020-10-27 15:35:56 +000092 swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000093 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040094
95 initBuffers();
96 initTextures();
97
Corentin Wallez7aec4ae2021-03-24 15:55:32 +000098 wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
James Price9e0debd2021-04-13 14:52:44 +000099 [[stage(vertex)]] fn main([[location(0)]] pos : vec4<f32>)
100 -> [[builtin(position)]] vec4<f32> {
101 return pos;
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000102 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400103
Corentin Wallez7aec4ae2021-03-24 15:55:32 +0000104 wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
James Price7e80cce2021-02-10 20:17:14 +0000105 [[group(0), binding(0)]] var mySampler: sampler;
106 [[group(0), binding(1)]] var myTexture : texture_2d<f32>;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400107
James Priceeae70b72021-04-19 15:29:49 +0000108 [[stage(fragment)]] fn main([[builtin(position)]] FragCoord : vec4<f32>)
James Price9e0debd2021-04-13 14:52:44 +0000109 -> [[location(0)]] vec4<f32> {
110 return textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400111 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112
Kai Ninomiya234becf2018-07-10 12:23:50 -0700113 auto bgl = utils::MakeBindGroupLayout(
114 device, {
Corentin Wallez9895c272021-03-18 16:46:58 +0000115 {0, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering},
116 {1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700117 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118
Corentin Wallez04863c42019-10-25 11:36:47 +0000119 wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400120
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700121 depthStencilView = CreateDefaultDepthStencilView(device);
122
Brandon Jones41c87d92021-05-21 05:01:38 +0000123 utils::ComboRenderPipelineDescriptor descriptor;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000124 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallez9895c272021-03-18 16:46:58 +0000125 descriptor.vertex.module = vsModule;
126 descriptor.vertex.bufferCount = 1;
127 descriptor.cBuffers[0].arrayStride = 4 * sizeof(float);
128 descriptor.cBuffers[0].attributeCount = 1;
129 descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x4;
130 descriptor.cFragment.module = fsModule;
131 descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
132 descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8);
Yan, Shaoboa4924272018-12-10 19:47:22 +0000133
Brandon Jones41c87d92021-05-21 05:01:38 +0000134 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400135
Corentin Wallez04863c42019-10-25 11:36:47 +0000136 wgpu::TextureView view = texture.CreateView();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400137
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000138 bindGroup = utils::MakeBindGroup(device, bgl, {{0, sampler}, {1, view}});
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400139}
140
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000141struct {
142 uint32_t a;
143 float b;
144} s;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400145void frame() {
146 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400147 s.b += 0.02f;
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000148 if (s.b >= 1.0f) {
149 s.b = 0.0f;
150 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700151
Corentin Wallez604072b2019-11-12 18:30:11 +0000152 wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
153 utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700154
Corentin Wallez04863c42019-10-25 11:36:47 +0000155 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000156 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000157 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000158 pass.SetPipeline(pipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000159 pass.SetBindGroup(0, bindGroup);
François Beaufort91b21422019-10-10 07:29:58 +0000160 pass.SetVertexBuffer(0, vertexBuffer);
Corentin Wallez5fad85b2020-11-25 08:54:14 +0000161 pass.SetIndexBuffer(indexBuffer, wgpu::IndexFormat::Uint32);
Corentin Wallez67b1ad72020-03-31 16:21:35 +0000162 pass.DrawIndexed(3);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000163 pass.EndPass();
164 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165
Corentin Wallez04863c42019-10-25 11:36:47 +0000166 wgpu::CommandBuffer commands = encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400167 queue.Submit(1, &commands);
Corentin Wallez604072b2019-11-12 18:30:11 +0000168 swapchain.Present();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700169 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170}
171
172int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400173 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400174 return 1;
175 }
176 init();
177
178 while (!ShouldQuit()) {
179 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400180 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400181 }
182
183 // TODO release stuff
184}