blob: 345593103f9f6baedb43e1ce76b5fa74c5f72643 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Austin Eng376f1c62017-05-30 20:03:44 -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"
Austin Eng376f1c62017-05-30 20:03:44 -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
Austin Eng376f1c62017-05-30 20:03:44 -040021#include <vector>
Corentin Wallez4d7d1692018-08-13 08:23:27 +020022#include <glm/glm.hpp>
23#include <glm/gtc/matrix_transform.hpp>
24#include <glm/gtc/type_ptr.hpp>
Austin Eng376f1c62017-05-30 20:03:44 -040025
Corentin Wallez4828d922018-07-18 13:45:46 +020026dawn::Device device;
Austin Eng376f1c62017-05-30 20:03:44 -040027
Corentin Wallez4828d922018-07-18 13:45:46 +020028dawn::Buffer indexBuffer;
29dawn::Buffer vertexBuffer;
30dawn::Buffer planeBuffer;
31dawn::Buffer cameraBuffer;
32dawn::Buffer transformBuffer[2];
Austin Eng376f1c62017-05-30 20:03:44 -040033
Corentin Wallez4828d922018-07-18 13:45:46 +020034dawn::BindGroup cameraBindGroup;
35dawn::BindGroup bindGroup[2];
36dawn::BindGroup cubeTransformBindGroup[2];
Austin Eng376f1c62017-05-30 20:03:44 -040037
Corentin Wallez4828d922018-07-18 13:45:46 +020038dawn::Queue queue;
39dawn::SwapChain swapchain;
40dawn::TextureView depthStencilView;
41dawn::RenderPipeline pipeline;
42dawn::RenderPipeline planePipeline;
43dawn::RenderPipeline reflectionPipeline;
Austin Eng376f1c62017-05-30 20:03:44 -040044
45void initBuffers() {
46 static const uint32_t indexData[6*6] = {
47 0, 1, 2,
48 0, 2, 3,
49
50 4, 5, 6,
51 4, 6, 7,
52
53 8, 9, 10,
54 8, 10, 11,
55
56 12, 13, 14,
57 12, 14, 15,
58
59 16, 17, 18,
60 16, 18, 19,
61
62 20, 21, 22,
63 20, 22, 23
64 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000065 indexBuffer =
66 utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsage::Index);
Austin Eng376f1c62017-05-30 20:03:44 -040067
68 static const float vertexData[6 * 4 * 6] = {
69 -1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
70 1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
71 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
72 -1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
73
74 -1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
75 -1.0, 1.0, -1.0, 1.0, 1.0, 0.0,
76 1.0, 1.0, -1.0, 1.0, 1.0, 0.0,
77 1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
78
79 -1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
80 -1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
81 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
82 1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
83
84 -1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
85 1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
86 1.0, -1.0, 1.0, 0.0, 1.0, 0.0,
87 -1.0, -1.0, 1.0, 0.0, 1.0, 0.0,
88
89 1.0, -1.0, -1.0, 0.0, 1.0, 1.0,
90 1.0, 1.0, -1.0, 0.0, 1.0, 1.0,
91 1.0, 1.0, 1.0, 0.0, 1.0, 1.0,
92 1.0, -1.0, 1.0, 0.0, 1.0, 1.0,
93
94 -1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
95 -1.0, -1.0, 1.0, 1.0, 1.0, 1.0,
96 -1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
97 -1.0, 1.0, -1.0, 1.0, 1.0, 1.0
98 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000099 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
100 dawn::BufferUsage::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -0400101
102 static const float planeData[6 * 4] = {
103 -2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
104 2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
105 2.0, -1.0, 2.0, 0.5, 0.5, 0.5,
106 -2.0, -1.0, 2.0, 0.5, 0.5, 0.5,
107 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000108 planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData),
109 dawn::BufferUsage::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -0400110}
111
Austin Eng58c76b32017-06-02 14:31:53 -0400112struct CameraData {
Austin Eng376f1c62017-05-30 20:03:44 -0400113 glm::mat4 view;
114 glm::mat4 proj;
115} cameraData;
116
117void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +0200118 device = CreateCppDawnDevice();
Austin Eng376f1c62017-05-30 20:03:44 -0400119
Corentin Wallezb703def2018-06-14 20:26:27 -0400120 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700121 swapchain = GetSwapChain(device);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000122 swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
123 640, 480);
Austin Eng376f1c62017-05-30 20:03:44 -0400124
125 initBuffers();
126
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000127 dawn::ShaderModule vsModule =
128 utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400129 #version 450
130 layout(set = 0, binding = 0) uniform cameraData {
131 mat4 view;
132 mat4 proj;
133 } camera;
134 layout(set = 0, binding = 1) uniform modelData {
135 mat4 modelMatrix;
136 };
137 layout(location = 0) in vec3 pos;
138 layout(location = 1) in vec3 col;
139 layout(location = 2) out vec3 f_col;
140 void main() {
141 f_col = col;
142 gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000143 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400144
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000145 dawn::ShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000146 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400147 #version 450
148 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400149 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400150 void main() {
151 fragColor = vec4(f_col, 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400152 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400153
Corentin Wallez4828d922018-07-18 13:45:46 +0200154 dawn::ShaderModule fsReflectionModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000155 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400156 #version 450
157 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400158 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400159 void main() {
160 fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400161 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400162
Yunchao Heeea20912019-05-22 22:46:32 +0000163 utils::ComboVertexInputDescriptor vertexInput;
Yunchao He2d4b5292019-06-06 17:54:30 +0000164 vertexInput.cBuffers[0].attributeCount = 2;
Yunchao Heeea20912019-05-22 22:46:32 +0000165 vertexInput.cAttributes[0].format = dawn::VertexFormat::Float3;
166 vertexInput.cAttributes[1].shaderLocation = 1;
167 vertexInput.cAttributes[1].offset = 3 * sizeof(float);
168 vertexInput.cAttributes[1].format = dawn::VertexFormat::Float3;
Yunchao He1ba2cb82019-03-28 05:09:01 +0000169
Yunchao He2d4b5292019-06-06 17:54:30 +0000170 vertexInput.bufferCount = 1;
Yunchao Heeea20912019-05-22 22:46:32 +0000171 vertexInput.cBuffers[0].stride = 6 * sizeof(float);
Austin Eng376f1c62017-05-30 20:03:44 -0400172
Kai Ninomiya234becf2018-07-10 12:23:50 -0700173 auto bgl = utils::MakeBindGroupLayout(
174 device, {
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000175 {0, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer},
176 {1, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700177 });
Austin Eng376f1c62017-05-30 20:03:44 -0400178
Corentin Wallez4828d922018-07-18 13:45:46 +0200179 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Austin Eng376f1c62017-05-30 20:03:44 -0400180
Corentin Wallez82b65732018-08-22 15:37:29 +0200181 dawn::BufferDescriptor cameraBufDesc;
182 cameraBufDesc.size = sizeof(CameraData);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000183 cameraBufDesc.usage = dawn::BufferUsage::CopyDst | dawn::BufferUsage::Uniform;
Corentin Wallez82b65732018-08-22 15:37:29 +0200184 cameraBuffer = device.CreateBuffer(&cameraBufDesc);
Austin Eng376f1c62017-05-30 20:03:44 -0400185
186 glm::mat4 transform(1.0);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000187 transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
188 dawn::BufferUsage::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400189
190 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000191 transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
192 dawn::BufferUsage::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400193
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000194 bindGroup[0] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000195 {0, cameraBuffer, 0, sizeof(CameraData)},
196 {1, transformBuffer[0], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000197 });
Austin Eng376f1c62017-05-30 20:03:44 -0400198
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000199 bindGroup[1] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000200 {0, cameraBuffer, 0, sizeof(CameraData)},
201 {1, transformBuffer[1], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000202 });
Austin Eng376f1c62017-05-30 20:03:44 -0400203
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700204 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400205
Yan, Shaoboa4924272018-12-10 19:47:22 +0000206 utils::ComboRenderPipelineDescriptor descriptor(device);
207 descriptor.layout = pl;
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000208 descriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000209 descriptor.cFragmentStage.module = fsModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000210 descriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000211 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000212 descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000213 descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yunchao Heea563332019-01-04 04:28:37 +0000214 descriptor.cDepthStencilState.depthWriteEnabled = true;
215 descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000216
217 pipeline = device.CreateRenderPipeline(&descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400218
Yan, Shaoboa4924272018-12-10 19:47:22 +0000219 utils::ComboRenderPipelineDescriptor pDescriptor(device);
220 pDescriptor.layout = pl;
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000221 pDescriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000222 pDescriptor.cFragmentStage.module = fsModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000223 pDescriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000224 pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000225 pDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000226 pDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yunchao Hebaa37412019-01-30 21:11:43 +0000227 pDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
228 pDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
Yunchao Heea563332019-01-04 04:28:37 +0000229 pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000230
231 planePipeline = device.CreateRenderPipeline(&pDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400232
Yan, Shaoboa4924272018-12-10 19:47:22 +0000233 utils::ComboRenderPipelineDescriptor rfDescriptor(device);
234 rfDescriptor.layout = pl;
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000235 rfDescriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000236 rfDescriptor.cFragmentStage.module = fsReflectionModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000237 rfDescriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000238 rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000239 rfDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000240 rfDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yunchao He6fe9b982019-02-14 08:05:37 +0000241 rfDescriptor.cDepthStencilState.stencilFront.compare = dawn::CompareFunction::Equal;
242 rfDescriptor.cDepthStencilState.stencilBack.compare = dawn::CompareFunction::Equal;
243 rfDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
244 rfDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
Yunchao Heea563332019-01-04 04:28:37 +0000245 rfDescriptor.cDepthStencilState.depthWriteEnabled = true;
246 rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000247
248 reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400249
250 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
251}
252
253struct {uint32_t a; float b;} s;
254void frame() {
255 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400256 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400257 if (s.b >= 1.0f) {s.b = 0.0f;}
Austin Eng376f1c62017-05-30 20:03:44 -0400258
259 cameraData.view = glm::lookAt(
260 glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
261 glm::vec3(0.0f, 0.0f, 0.0f),
Corentin Wallezd46cabd2019-09-20 23:53:27 +0000262 glm::vec3(0.0f, 1.0f, 0.0f)
Austin Eng376f1c62017-05-30 20:03:44 -0400263 );
264
Corentin Wallez67ab1ea2019-06-06 16:19:15 +0000265 cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData);
Austin Eng376f1c62017-05-30 20:03:44 -0400266
Jiawei Shaob2c50232019-02-27 09:21:56 +0000267 dawn::Texture backbuffer = swapchain.GetNextTexture();
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000268 utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700269
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000270 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000271 {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000272 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000273 pass.SetPipeline(pipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000274 pass.SetBindGroup(0, bindGroup[0]);
François Beaufort91b21422019-10-10 07:29:58 +0000275 pass.SetVertexBuffer(0, vertexBuffer);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000276 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000277 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400278
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000279 pass.SetStencilReference(0x1);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000280 pass.SetPipeline(planePipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000281 pass.SetBindGroup(0, bindGroup[0]);
François Beaufort91b21422019-10-10 07:29:58 +0000282 pass.SetVertexBuffer(0, planeBuffer);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000283 pass.DrawIndexed(6, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400284
Yan, Shaobo300eec02018-12-21 10:40:26 +0000285 pass.SetPipeline(reflectionPipeline);
François Beaufort91b21422019-10-10 07:29:58 +0000286 pass.SetVertexBuffer(0, vertexBuffer);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000287 pass.SetBindGroup(0, bindGroup[1]);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000288 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400289
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000290 pass.EndPass();
291 }
292
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000293 dawn::CommandBuffer commands = encoder.Finish();
Austin Eng376f1c62017-05-30 20:03:44 -0400294 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700295 swapchain.Present(backbuffer);
296 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400297}
298
299int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400300 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400301 return 1;
302 }
303 init();
304
305 while (!ShouldQuit()) {
306 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400307 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400308 }
309
310 // TODO release stuff
311}