blob: 66ba8b07ed80ab73cb9568e24504c496c647d14a [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 Wallez4828d922018-07-18 13:45:46 +020065 indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
Austin Eng376f1c62017-05-30 20:03:44 -040066
67 static const float vertexData[6 * 4 * 6] = {
68 -1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
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
73 -1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
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
78 -1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
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
83 -1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
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
88 1.0, -1.0, -1.0, 0.0, 1.0, 1.0,
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
93 -1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
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 };
Corentin Wallez4828d922018-07-18 13:45:46 +020098 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -040099
100 static const float planeData[6 * 4] = {
101 -2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
102 2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
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 };
Corentin Wallez4828d922018-07-18 13:45:46 +0200106 planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), dawn::BufferUsageBit::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -0400107}
108
Austin Eng58c76b32017-06-02 14:31:53 -0400109struct CameraData {
Austin Eng376f1c62017-05-30 20:03:44 -0400110 glm::mat4 view;
111 glm::mat4 proj;
112} cameraData;
113
114void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +0200115 device = CreateCppDawnDevice();
Austin Eng376f1c62017-05-30 20:03:44 -0400116
Corentin Wallezb703def2018-06-14 20:26:27 -0400117 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700118 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400119 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +0200120 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Austin Eng376f1c62017-05-30 20:03:44 -0400121
122 initBuffers();
123
Corentin Wallez4828d922018-07-18 13:45:46 +0200124 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400125 #version 450
126 layout(set = 0, binding = 0) uniform cameraData {
127 mat4 view;
128 mat4 proj;
129 } camera;
130 layout(set = 0, binding = 1) uniform modelData {
131 mat4 modelMatrix;
132 };
133 layout(location = 0) in vec3 pos;
134 layout(location = 1) in vec3 col;
135 layout(location = 2) out vec3 f_col;
136 void main() {
137 f_col = col;
138 gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
139 })"
140 );
141
Corentin Wallez4828d922018-07-18 13:45:46 +0200142 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400143 #version 450
144 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400145 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400146 void main() {
147 fragColor = vec4(f_col, 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400148 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400149
Corentin Wallez4828d922018-07-18 13:45:46 +0200150 dawn::ShaderModule fsReflectionModule =
151 utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400152 #version 450
153 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400154 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400155 void main() {
156 fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400157 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400158
159 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200160 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32, 0)
161 .SetAttribute(1, 0, dawn::VertexFormat::FloatR32G32B32, 3 * sizeof(float))
162 .SetInput(0, 6 * sizeof(float), dawn::InputStepMode::Vertex)
Austin Eng376f1c62017-05-30 20:03:44 -0400163 .GetResult();
164
Kai Ninomiya234becf2018-07-10 12:23:50 -0700165 auto bgl = utils::MakeBindGroupLayout(
166 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200167 {0, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
168 {1, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700169 });
Austin Eng376f1c62017-05-30 20:03:44 -0400170
Corentin Wallez4828d922018-07-18 13:45:46 +0200171 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Austin Eng376f1c62017-05-30 20:03:44 -0400172
Corentin Wallez82b65732018-08-22 15:37:29 +0200173 dawn::BufferDescriptor cameraBufDesc;
174 cameraBufDesc.size = sizeof(CameraData);
175 cameraBufDesc.usage = dawn::BufferUsageBit::TransferDst | dawn::BufferUsageBit::Uniform;
176 cameraBuffer = device.CreateBuffer(&cameraBufDesc);
Austin Eng376f1c62017-05-30 20:03:44 -0400177
178 glm::mat4 transform(1.0);
Corentin Wallez4828d922018-07-18 13:45:46 +0200179 transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400180
181 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez4828d922018-07-18 13:45:46 +0200182 transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400183
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000184 bindGroup[0] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000185 {0, cameraBuffer, 0, sizeof(CameraData)},
186 {1, transformBuffer[0], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000187 });
Austin Eng376f1c62017-05-30 20:03:44 -0400188
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000189 bindGroup[1] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000190 {0, cameraBuffer, 0, sizeof(CameraData)},
191 {1, transformBuffer[1], 0, sizeof(glm::mat4)}
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000192 });
Austin Eng376f1c62017-05-30 20:03:44 -0400193
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700194 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400195
196 auto depthStencilState = device.CreateDepthStencilStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200197 .SetDepthCompareFunction(dawn::CompareFunction::Less)
Austin Eng10634392017-06-01 11:30:03 -0400198 .SetDepthWriteEnabled(true)
Austin Eng376f1c62017-05-30 20:03:44 -0400199 .GetResult();
200
Yan, Shaoboa4924272018-12-10 19:47:22 +0000201 utils::ComboRenderPipelineDescriptor descriptor(device);
202 descriptor.layout = pl;
203 descriptor.cVertexStage.module = vsModule;
204 descriptor.cFragmentStage.module = fsModule;
205 descriptor.inputState = inputState;
206 descriptor.cAttachmentsState.hasDepthStencilAttachment = true;
207 descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint;
208 descriptor.cColorAttachments[0].format =
209 GetPreferredSwapChainTextureFormat();
210 descriptor.depthStencilState = depthStencilState;
211
212 pipeline = device.CreateRenderPipeline(&descriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400213
Yunchao He48485e32018-12-15 02:32:34 +0000214 dawn::StencilStateFaceDescriptor planeStencilDescriptor;
215 planeStencilDescriptor.compare = dawn::CompareFunction::Always;
216 planeStencilDescriptor.stencilFailOp = dawn::StencilOperation::Keep;
217 planeStencilDescriptor.depthFailOp = dawn::StencilOperation::Keep;
218 planeStencilDescriptor.passOp = dawn::StencilOperation::Replace;
Austin Eng376f1c62017-05-30 20:03:44 -0400219 auto planeStencilState = device.CreateDepthStencilStateBuilder()
Yunchao He48485e32018-12-15 02:32:34 +0000220 .SetDepthCompareFunction(dawn::CompareFunction::Less)
221 .SetDepthWriteEnabled(false)
222 .SetStencilFunction(dawn::Face::Both, &planeStencilDescriptor)
223 .GetResult();
Austin Eng376f1c62017-05-30 20:03:44 -0400224
Yan, Shaoboa4924272018-12-10 19:47:22 +0000225 utils::ComboRenderPipelineDescriptor pDescriptor(device);
226 pDescriptor.layout = pl;
227 pDescriptor.cVertexStage.module = vsModule;
228 pDescriptor.cFragmentStage.module = fsModule;
229 pDescriptor.inputState = inputState;
230 pDescriptor.cAttachmentsState.hasDepthStencilAttachment = true;
231 pDescriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint;
232 pDescriptor.cColorAttachments[0].format =
233 GetPreferredSwapChainTextureFormat();
234 pDescriptor.depthStencilState = planeStencilState;
235
236 planePipeline = device.CreateRenderPipeline(&pDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400237
Yunchao He48485e32018-12-15 02:32:34 +0000238 dawn::StencilStateFaceDescriptor reflectionStencilDescriptor;
239 reflectionStencilDescriptor.compare = dawn::CompareFunction::Equal;
240 reflectionStencilDescriptor.stencilFailOp = dawn::StencilOperation::Keep;
241 reflectionStencilDescriptor.depthFailOp = dawn::StencilOperation::Keep;
242 reflectionStencilDescriptor.passOp = dawn::StencilOperation::Replace;
243 auto reflectionStencilState =
244 device.CreateDepthStencilStateBuilder()
245 .SetDepthCompareFunction(dawn::CompareFunction::Less)
246 .SetDepthWriteEnabled(true)
247 .SetStencilFunction(dawn::Face::Both, &reflectionStencilDescriptor)
248 .GetResult();
Austin Eng376f1c62017-05-30 20:03:44 -0400249
Yan, Shaoboa4924272018-12-10 19:47:22 +0000250 utils::ComboRenderPipelineDescriptor rfDescriptor(device);
251 rfDescriptor.layout = pl;
252 rfDescriptor.cVertexStage.module = vsModule;
253 rfDescriptor.cFragmentStage.module = fsReflectionModule;
254 rfDescriptor.inputState = inputState;
255 rfDescriptor.cAttachmentsState.hasDepthStencilAttachment = true;
256 rfDescriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint;
257 rfDescriptor.cColorAttachments[0].format =
258 GetPreferredSwapChainTextureFormat();
259 rfDescriptor.depthStencilState = reflectionStencilState;
260
261 reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor);
Austin Eng376f1c62017-05-30 20:03:44 -0400262
263 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
264}
265
266struct {uint32_t a; float b;} s;
267void frame() {
268 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400269 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400270 if (s.b >= 1.0f) {s.b = 0.0f;}
271 static const uint32_t vertexBufferOffsets[1] = {0};
272
273 cameraData.view = glm::lookAt(
274 glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
275 glm::vec3(0.0f, 0.0f, 0.0f),
Corentin Wallez804fc742018-07-03 14:43:46 +0200276 glm::vec3(0.0f, -1.0f, 0.0f)
Austin Eng376f1c62017-05-30 20:03:44 -0400277 );
278
Stephen Whitee5ae3272018-02-04 11:07:02 -0500279 cameraBuffer.SetSubData(0, sizeof(CameraData), reinterpret_cast<uint8_t*>(&cameraData));
Austin Eng376f1c62017-05-30 20:03:44 -0400280
Corentin Wallez4828d922018-07-18 13:45:46 +0200281 dawn::Texture backbuffer;
282 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400283 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700284
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000285 dawn::CommandBufferBuilder builder = device.CreateCommandBufferBuilder();
286 {
287 dawn::RenderPassEncoder pass = builder.BeginRenderPass(renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000288 pass.SetPipeline(pipeline);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000289 pass.SetBindGroup(0, bindGroup[0]);
290 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
291 pass.SetIndexBuffer(indexBuffer, 0);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000292 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400293
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000294 pass.SetStencilReference(0x1);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000295 pass.SetPipeline(planePipeline);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000296 pass.SetBindGroup(0, bindGroup[0]);
297 pass.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000298 pass.DrawIndexed(6, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400299
Yan, Shaobo300eec02018-12-21 10:40:26 +0000300 pass.SetPipeline(reflectionPipeline);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000301 pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
302 pass.SetBindGroup(0, bindGroup[1]);
Jiawei Shaoff9562f2018-12-13 01:05:26 +0000303 pass.DrawIndexed(36, 1, 0, 0, 0);
Austin Eng376f1c62017-05-30 20:03:44 -0400304
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000305 pass.EndPass();
306 }
307
308 dawn::CommandBuffer commands = builder.GetResult();
Austin Eng376f1c62017-05-30 20:03:44 -0400309 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700310 swapchain.Present(backbuffer);
311 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400312}
313
314int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400315 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400316 return 1;
317 }
318 init();
319
320 while (!ShouldQuit()) {
321 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400322 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400323 }
324
325 // TODO release stuff
326}