blob: 44b262135baaacf0381395dfdd172a89a15cd901 [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);
75 dawn::TextureCopyView textureCopyView =
76 utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0}, dawn::TextureAspect::Color);
77 dawn::Extent3D copySize = {1024, 1024, 1};
78 dawn::CommandBuffer copy =
79 device.CreateCommandBufferBuilder()
80 .CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize)
81 .GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040082
83 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040084}
85
86void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020087 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040088
Corentin Wallezb703def2018-06-14 20:26:27 -040089 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070090 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040091 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +020092 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093
94 initBuffers();
95 initTextures();
96
Corentin Wallez4828d922018-07-18 13:45:46 +020097 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040098 #version 450
99 layout(location = 0) in vec4 pos;
100 void main() {
101 gl_Position = pos;
102 })"
103 );
104
Corentin Wallez4828d922018-07-18 13:45:46 +0200105 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106 #version 450
107 layout(set = 0, binding = 0) uniform sampler mySampler;
108 layout(set = 0, binding = 1) uniform texture2D myTexture;
109
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400110 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400111 void main() {
112 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400113 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114
115 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200116 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
117 .SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118 .GetResult();
119
Kai Ninomiya234becf2018-07-10 12:23:50 -0700120 auto bgl = utils::MakeBindGroupLayout(
121 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200122 {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
123 {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700124 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125
Corentin Wallez4828d922018-07-18 13:45:46 +0200126 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400127
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700128 depthStencilView = CreateDefaultDepthStencilView(device);
129
Yan, Shaoboa4924272018-12-10 19:47:22 +0000130 utils::ComboRenderPipelineDescriptor descriptor(device);
131 descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
132 descriptor.cVertexStage.module = vsModule;
133 descriptor.cFragmentStage.module = fsModule;
134 descriptor.inputState = inputState;
135 descriptor.cAttachmentsState.hasDepthStencilAttachment = true;
136 descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint;
137 descriptor.cColorAttachments[0].format =
138 GetPreferredSwapChainTextureFormat();
139
140 pipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400141
Jiawei Shao3d670502018-09-18 00:31:57 +0000142 dawn::TextureView view = texture.CreateDefaultTextureView();
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
Corentin Wallez4828d922018-07-18 13:45:46 +0200156 dawn::Texture backbuffer;
157 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400158 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700159
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400160 static const uint32_t vertexBufferOffsets[1] = {0};
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000161 dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
162 {
163 dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
164 pass.SetRenderPipeline(pipeline);
165 pass.SetBindGroup(0, bindGroup);
166 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
167 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000168 pass.DrawIndexed(3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000169 pass.EndPass();
170 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400171
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000172 dawn::CommandBuffer commands = builder.GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400173 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700174 swapchain.Present(backbuffer);
175 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176}
177
178int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400179 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400180 return 1;
181 }
182 init();
183
184 while (!ShouldQuit()) {
185 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400186 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400187 }
188
189 // TODO release stuff
190}