blob: bf14e1aae7afc53c7ebe9133cde66e1c0451d8cb [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
Corentin Wallezf6840402018-07-18 14:00:56 +020017#include "utils/DawnHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040020#include <vector>
21
Corentin Wallez4828d922018-07-18 13:45:46 +020022dawn::Device device;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040023
Corentin Wallez4828d922018-07-18 13:45:46 +020024dawn::Buffer indexBuffer;
25dawn::Buffer vertexBuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040026
Corentin Wallez4828d922018-07-18 13:45:46 +020027dawn::Texture texture;
28dawn::Sampler sampler;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040029
Corentin Wallez4828d922018-07-18 13:45:46 +020030dawn::Queue queue;
31dawn::SwapChain swapchain;
32dawn::TextureView depthStencilView;
33dawn::RenderPipeline pipeline;
34dawn::BindGroup bindGroup;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040035
36void initBuffers() {
37 static const uint32_t indexData[3] = {
38 0, 1, 2,
39 };
Corentin Wallez4828d922018-07-18 13:45:46 +020040 indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040041
42 static const float vertexData[12] = {
43 0.0f, 0.5f, 0.0f, 1.0f,
44 -0.5f, -0.5f, 0.0f, 1.0f,
45 0.5f, -0.5f, 0.0f, 1.0f,
46 };
Corentin Wallez4828d922018-07-18 13:45:46 +020047 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048}
49
50void initTextures() {
Jiawei Shao425428f2018-08-27 08:44:48 +080051 dawn::TextureDescriptor descriptor;
52 descriptor.dimension = dawn::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +000053 descriptor.size.width = 1024;
54 descriptor.size.height = 1024;
55 descriptor.size.depth = 1;
Jiawei Shao425428f2018-08-27 08:44:48 +080056 descriptor.arrayLayer = 1;
57 descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
Jiawei Shao84cde512018-10-31 10:51:11 +000058 descriptor.levelCount = 1;
Jiawei Shao425428f2018-08-27 08:44:48 +080059 descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled;
60 texture = device.CreateTexture(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040061
Corentin Wallez4828d922018-07-18 13:45:46 +020062 dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
Corentin Wallezb711b9b2018-05-22 14:50:59 -040063 sampler = device.CreateSampler(&samplerDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040064
65 // Initialize the texture with arbitrary data until we can load images
66 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
67 for (size_t i = 0; i < data.size(); ++i) {
Kai Ninomiya78c8b832017-07-21 17:00:22 -070068 data[i] = static_cast<uint8_t>(i % 253);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069 }
70
Corentin Wallez4828d922018-07-18 13:45:46 +020071 dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
Brandon Jonesac71e342018-11-28 17:54:13 +000072 dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
73 dawn::TextureCopyView textureCopyView =
74 utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0}, dawn::TextureAspect::Color);
75 dawn::Extent3D copySize = {1024, 1024, 1};
76 dawn::CommandBuffer copy =
77 device.CreateCommandBufferBuilder()
78 .CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize)
79 .GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040080
81 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040082}
83
84void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020085 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086
Corentin Wallezb703def2018-06-14 20:26:27 -040087 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070088 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040089 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +020090 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091
92 initBuffers();
93 initTextures();
94
Corentin Wallez4828d922018-07-18 13:45:46 +020095 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040096 #version 450
97 layout(location = 0) in vec4 pos;
98 void main() {
99 gl_Position = pos;
100 })"
101 );
102
Corentin Wallez4828d922018-07-18 13:45:46 +0200103 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104 #version 450
105 layout(set = 0, binding = 0) uniform sampler mySampler;
106 layout(set = 0, binding = 1) uniform texture2D myTexture;
107
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400108 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400109 void main() {
110 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400111 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112
113 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200114 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
115 .SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116 .GetResult();
117
Kai Ninomiya234becf2018-07-10 12:23:50 -0700118 auto bgl = utils::MakeBindGroupLayout(
119 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200120 {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
121 {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700122 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400123
Corentin Wallez4828d922018-07-18 13:45:46 +0200124 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700126 depthStencilView = CreateDefaultDepthStencilView(device);
127
Corentin Wallez66ff4472017-07-14 11:32:57 -0400128 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400129 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
Corentin Wallez4828d922018-07-18 13:45:46 +0200130 .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400131 .SetLayout(pl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200132 .SetStage(dawn::ShaderStage::Vertex, vsModule, "main")
133 .SetStage(dawn::ShaderStage::Fragment, fsModule, "main")
134 .SetIndexFormat(dawn::IndexFormat::Uint32)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400135 .SetInputState(inputState)
136 .GetResult();
137
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
Corentin Wallez4828d922018-07-18 13:45:46 +0200152 dawn::Texture backbuffer;
153 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400154 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700155
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400156 static const uint32_t vertexBufferOffsets[1] = {0};
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000157 dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
158 {
159 dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
160 pass.SetRenderPipeline(pipeline);
161 pass.SetBindGroup(0, bindGroup);
162 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
163 pass.SetIndexBuffer(indexBuffer, 0);
164 pass.DrawElements(3, 1, 0, 0);
165 pass.EndPass();
166 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400167
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000168 dawn::CommandBuffer commands = builder.GetResult();
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}