blob: 3a48806bd48c41ab63181da8ddf297feef38f571 [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 Wallez2a1d8c22019-07-12 17:52:22 +0000127 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, utils::ShaderStage::Vertex, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400128 #version 450
129 layout(set = 0, binding = 0) uniform cameraData {
130 mat4 view;
131 mat4 proj;
132 } camera;
133 layout(set = 0, binding = 1) uniform modelData {
134 mat4 modelMatrix;
135 };
136 layout(location = 0) in vec3 pos;
137 layout(location = 1) in vec3 col;
138 layout(location = 2) out vec3 f_col;
139 void main() {
140 f_col = col;
141 gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000142 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400143
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000144 dawn::ShaderModule fsModule =
145 utils::CreateShaderModule(device, utils::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400146 #version 450
147 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400148 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400149 void main() {
150 fragColor = vec4(f_col, 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400151 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400152
Corentin Wallez4828d922018-07-18 13:45:46 +0200153 dawn::ShaderModule fsReflectionModule =
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000154 utils::CreateShaderModule(device, utils::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400155 #version 450
156 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400157 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400158 void main() {
159 fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400160 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400161
Yunchao Heeea20912019-05-22 22:46:32 +0000162 utils::ComboVertexInputDescriptor vertexInput;
Yunchao He2d4b5292019-06-06 17:54:30 +0000163 vertexInput.cBuffers[0].attributeCount = 2;
Yunchao Heeea20912019-05-22 22:46:32 +0000164 vertexInput.cAttributes[0].format = dawn::VertexFormat::Float3;
165 vertexInput.cAttributes[1].shaderLocation = 1;
166 vertexInput.cAttributes[1].offset = 3 * sizeof(float);
167 vertexInput.cAttributes[1].format = dawn::VertexFormat::Float3;
Yunchao He1ba2cb82019-03-28 05:09:01 +0000168
Yunchao He2d4b5292019-06-06 17:54:30 +0000169 vertexInput.bufferCount = 1;
Yunchao Heeea20912019-05-22 22:46:32 +0000170 vertexInput.cBuffers[0].stride = 6 * sizeof(float);
Austin Eng376f1c62017-05-30 20:03:44 -0400171
Kai Ninomiya234becf2018-07-10 12:23:50 -0700172 auto bgl = utils::MakeBindGroupLayout(
173 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200174 {0, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
175 {1, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700176 });
Austin Eng376f1c62017-05-30 20:03:44 -0400177
Corentin Wallez4828d922018-07-18 13:45:46 +0200178 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Austin Eng376f1c62017-05-30 20:03:44 -0400179
Corentin Wallez82b65732018-08-22 15:37:29 +0200180 dawn::BufferDescriptor cameraBufDesc;
181 cameraBufDesc.size = sizeof(CameraData);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000182 cameraBufDesc.usage = dawn::BufferUsage::CopyDst | dawn::BufferUsage::Uniform;
Corentin Wallez82b65732018-08-22 15:37:29 +0200183 cameraBuffer = device.CreateBuffer(&cameraBufDesc);
Austin Eng376f1c62017-05-30 20:03:44 -0400184
185 glm::mat4 transform(1.0);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000186 transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
187 dawn::BufferUsage::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400188
189 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000190 transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
191 dawn::BufferUsage::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400192
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000193 bindGroup[0] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000194 {0, cameraBuffer, 0, sizeof(CameraData)},
195 {1, transformBuffer[0], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000196 });
Austin Eng376f1c62017-05-30 20:03:44 -0400197
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000198 bindGroup[1] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000199 {0, cameraBuffer, 0, sizeof(CameraData)},
200 {1, transformBuffer[1], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000201 });
Austin Eng376f1c62017-05-30 20:03:44 -0400202
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700203 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400204
Yan, Shaoboa4924272018-12-10 19:47:22 +0000205 utils::ComboRenderPipelineDescriptor descriptor(device);
206 descriptor.layout = pl;
207 descriptor.cVertexStage.module = vsModule;
208 descriptor.cFragmentStage.module = fsModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000209 descriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000210 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000211 descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Yunchao He938811e2019-02-20 13:00:36 +0000212 descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
Yunchao Heea563332019-01-04 04:28:37 +0000213 descriptor.cDepthStencilState.depthWriteEnabled = true;
214 descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000215
216 pipeline = device.CreateRenderPipeline(&descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400217
Yan, Shaoboa4924272018-12-10 19:47:22 +0000218 utils::ComboRenderPipelineDescriptor pDescriptor(device);
219 pDescriptor.layout = pl;
220 pDescriptor.cVertexStage.module = vsModule;
221 pDescriptor.cFragmentStage.module = fsModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000222 pDescriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000223 pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000224 pDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Yunchao He938811e2019-02-20 13:00:36 +0000225 pDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
Yunchao Hebaa37412019-01-30 21:11:43 +0000226 pDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
227 pDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
Yunchao Heea563332019-01-04 04:28:37 +0000228 pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000229
230 planePipeline = device.CreateRenderPipeline(&pDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400231
Yan, Shaoboa4924272018-12-10 19:47:22 +0000232 utils::ComboRenderPipelineDescriptor rfDescriptor(device);
233 rfDescriptor.layout = pl;
234 rfDescriptor.cVertexStage.module = vsModule;
235 rfDescriptor.cFragmentStage.module = fsReflectionModule;
Yunchao Heeea20912019-05-22 22:46:32 +0000236 rfDescriptor.vertexInput = &vertexInput;
Yunchao He108bcbd2019-02-15 02:20:57 +0000237 rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000238 rfDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Yunchao He938811e2019-02-20 13:00:36 +0000239 rfDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
Yunchao He6fe9b982019-02-14 08:05:37 +0000240 rfDescriptor.cDepthStencilState.stencilFront.compare = dawn::CompareFunction::Equal;
241 rfDescriptor.cDepthStencilState.stencilBack.compare = dawn::CompareFunction::Equal;
242 rfDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
243 rfDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
Yunchao Heea563332019-01-04 04:28:37 +0000244 rfDescriptor.cDepthStencilState.depthWriteEnabled = true;
245 rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000246
247 reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400248
249 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
250}
251
252struct {uint32_t a; float b;} s;
253void frame() {
254 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400255 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400256 if (s.b >= 1.0f) {s.b = 0.0f;}
Austin Engcf52d712019-04-05 20:51:29 +0000257 static const uint64_t vertexBufferOffsets[1] = {0};
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 Wallez804fc742018-07-03 14:43:46 +0200262 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();
Austin Engb4b3ea02019-04-09 16:57:00 +0000268 utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultView()},
Jiawei Shaob2c50232019-02-27 09:21:56 +0000269 depthStencilView);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700270
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000271 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000272 {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000273 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000274 pass.SetPipeline(pipeline);
Yan, Shaobo991ab982019-03-18 06:01:37 +0000275 pass.SetBindGroup(0, bindGroup[0], 0, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000276 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
277 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000278 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400279
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000280 pass.SetStencilReference(0x1);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000281 pass.SetPipeline(planePipeline);
Yan, Shaobo991ab982019-03-18 06:01:37 +0000282 pass.SetBindGroup(0, bindGroup[0], 0, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000283 pass.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000284 pass.DrawIndexed(6, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400285
Yan, Shaobo300eec02018-12-21 10:40:26 +0000286 pass.SetPipeline(reflectionPipeline);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000287 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
Yan, Shaobo991ab982019-03-18 06:01:37 +0000288 pass.SetBindGroup(0, bindGroup[1], 0, nullptr);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000289 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400290
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000291 pass.EndPass();
292 }
293
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000294 dawn::CommandBuffer commands = encoder.Finish();
Austin Eng376f1c62017-05-30 20:03:44 -0400295 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700296 swapchain.Present(backbuffer);
297 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400298}
299
300int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400301 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400302 return 1;
303 }
304 init();
305
306 while (!ShouldQuit()) {
307 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400308 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400309 }
310
311 // TODO release stuff
312}