blob: 03f629ccbf88580266c5e35f25d08f393ec82ed6 [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 Shao8bff3b22018-12-12 09:27:16 +000057 descriptor.arraySize = 1;
58 descriptor.sampleCount = 1;
Jiawei Shao425428f2018-08-27 08:44:48 +080059 descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
Jiawei Shao84cde512018-10-31 10:51:11 +000060 descriptor.levelCount = 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};
77 dawn::CommandBuffer copy =
78 device.CreateCommandBufferBuilder()
79 .CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize)
80 .GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
82 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
114 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200115 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
116 .SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117 .GetResult();
118
Kai Ninomiya234becf2018-07-10 12:23:50 -0700119 auto bgl = utils::MakeBindGroupLayout(
120 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200121 {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
122 {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700123 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124
Corentin Wallez4828d922018-07-18 13:45:46 +0200125 dawn::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);
131 descriptor.cVertexStage.module = vsModule;
132 descriptor.cFragmentStage.module = fsModule;
133 descriptor.inputState = inputState;
134 descriptor.cAttachmentsState.hasDepthStencilAttachment = true;
135 descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint;
136 descriptor.cColorAttachments[0].format =
137 GetPreferredSwapChainTextureFormat();
138
139 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400140
Jiawei Shao3d670502018-09-18 00:31:57 +0000141 dawn::TextureView view = texture.CreateDefaultTextureView();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400142
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000143 bindGroup = utils::MakeBindGroup(device, bgl, {
144 {0, sampler},
145 {1, view}
146 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400147}
148
149struct {uint32_t a; float b;} s;
150void frame() {
151 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400152 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400153 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700154
Corentin Wallez4828d922018-07-18 13:45:46 +0200155 dawn::Texture backbuffer;
156 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400157 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700158
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159 static const uint32_t vertexBufferOffsets[1] = {0};
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000160 dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
161 {
162 dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
163 pass.SetRenderPipeline(pipeline);
164 pass.SetBindGroup(0, bindGroup);
165 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
166 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000167 pass.DrawIndexed(3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000168 pass.EndPass();
169 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000171 dawn::CommandBuffer commands = builder.GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700173 swapchain.Present(backbuffer);
174 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}