blob: f4a003c0a1c03bb5397667a2a533f88a79211aba [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
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
Austin Eng376f1c62017-05-30 20:03:44 -040020#include <vector>
Corentin Wallez4d7d1692018-08-13 08:23:27 +020021#include <glm/glm.hpp>
22#include <glm/gtc/matrix_transform.hpp>
23#include <glm/gtc/type_ptr.hpp>
Austin Eng376f1c62017-05-30 20:03:44 -040024
Corentin Wallez4828d922018-07-18 13:45:46 +020025dawn::Device device;
Austin Eng376f1c62017-05-30 20:03:44 -040026
Corentin Wallez4828d922018-07-18 13:45:46 +020027dawn::Buffer indexBuffer;
28dawn::Buffer vertexBuffer;
29dawn::Buffer planeBuffer;
30dawn::Buffer cameraBuffer;
31dawn::Buffer transformBuffer[2];
Austin Eng376f1c62017-05-30 20:03:44 -040032
Corentin Wallez4828d922018-07-18 13:45:46 +020033dawn::BindGroup cameraBindGroup;
34dawn::BindGroup bindGroup[2];
35dawn::BindGroup cubeTransformBindGroup[2];
Austin Eng376f1c62017-05-30 20:03:44 -040036
Corentin Wallez4828d922018-07-18 13:45:46 +020037dawn::Queue queue;
38dawn::SwapChain swapchain;
39dawn::TextureView depthStencilView;
40dawn::RenderPipeline pipeline;
41dawn::RenderPipeline planePipeline;
42dawn::RenderPipeline reflectionPipeline;
Austin Eng376f1c62017-05-30 20:03:44 -040043
44void initBuffers() {
45 static const uint32_t indexData[6*6] = {
46 0, 1, 2,
47 0, 2, 3,
48
49 4, 5, 6,
50 4, 6, 7,
51
52 8, 9, 10,
53 8, 10, 11,
54
55 12, 13, 14,
56 12, 14, 15,
57
58 16, 17, 18,
59 16, 18, 19,
60
61 20, 21, 22,
62 20, 22, 23
63 };
Corentin Wallez4828d922018-07-18 13:45:46 +020064 indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
Austin Eng376f1c62017-05-30 20:03:44 -040065
66 static const float vertexData[6 * 4 * 6] = {
67 -1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
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
72 -1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
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
77 -1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
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
82 -1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
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
87 1.0, -1.0, -1.0, 0.0, 1.0, 1.0,
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
92 -1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
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 };
Corentin Wallez4828d922018-07-18 13:45:46 +020097 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -040098
99 static const float planeData[6 * 4] = {
100 -2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
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 };
Corentin Wallez4828d922018-07-18 13:45:46 +0200105 planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), dawn::BufferUsageBit::Vertex);
Austin Eng376f1c62017-05-30 20:03:44 -0400106}
107
Austin Eng58c76b32017-06-02 14:31:53 -0400108struct CameraData {
Austin Eng376f1c62017-05-30 20:03:44 -0400109 glm::mat4 view;
110 glm::mat4 proj;
111} cameraData;
112
113void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +0200114 device = CreateCppDawnDevice();
Austin Eng376f1c62017-05-30 20:03:44 -0400115
Corentin Wallezb703def2018-06-14 20:26:27 -0400116 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700117 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400118 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +0200119 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Austin Eng376f1c62017-05-30 20:03:44 -0400120
121 initBuffers();
122
Corentin Wallez4828d922018-07-18 13:45:46 +0200123 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400124 #version 450
125 layout(set = 0, binding = 0) uniform cameraData {
126 mat4 view;
127 mat4 proj;
128 } camera;
129 layout(set = 0, binding = 1) uniform modelData {
130 mat4 modelMatrix;
131 };
132 layout(location = 0) in vec3 pos;
133 layout(location = 1) in vec3 col;
134 layout(location = 2) out vec3 f_col;
135 void main() {
136 f_col = col;
137 gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
138 })"
139 );
140
Corentin Wallez4828d922018-07-18 13:45:46 +0200141 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400142 #version 450
143 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400144 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400145 void main() {
146 fragColor = vec4(f_col, 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400147 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400148
Corentin Wallez4828d922018-07-18 13:45:46 +0200149 dawn::ShaderModule fsReflectionModule =
150 utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Austin Eng376f1c62017-05-30 20:03:44 -0400151 #version 450
152 layout(location = 2) in vec3 f_col;
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400153 layout(location = 0) out vec4 fragColor;
Austin Eng376f1c62017-05-30 20:03:44 -0400154 void main() {
155 fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400156 })");
Austin Eng376f1c62017-05-30 20:03:44 -0400157
158 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200159 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32, 0)
160 .SetAttribute(1, 0, dawn::VertexFormat::FloatR32G32B32, 3 * sizeof(float))
161 .SetInput(0, 6 * sizeof(float), dawn::InputStepMode::Vertex)
Austin Eng376f1c62017-05-30 20:03:44 -0400162 .GetResult();
163
Kai Ninomiya234becf2018-07-10 12:23:50 -0700164 auto bgl = utils::MakeBindGroupLayout(
165 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200166 {0, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
167 {1, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700168 });
Austin Eng376f1c62017-05-30 20:03:44 -0400169
Corentin Wallez4828d922018-07-18 13:45:46 +0200170 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Austin Eng376f1c62017-05-30 20:03:44 -0400171
172 cameraBuffer = device.CreateBufferBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200173 .SetAllowedUsage(dawn::BufferUsageBit::TransferDst | dawn::BufferUsageBit::Uniform)
Austin Eng58c76b32017-06-02 14:31:53 -0400174 .SetSize(sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400175 .GetResult();
176
177 glm::mat4 transform(1.0);
Corentin Wallez4828d922018-07-18 13:45:46 +0200178 transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400179
180 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez4828d922018-07-18 13:45:46 +0200181 transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400182
Corentin Wallez4828d922018-07-18 13:45:46 +0200183 dawn::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder()
Austin Eng58c76b32017-06-02 14:31:53 -0400184 .SetExtent(0, sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400185 .GetResult();
186
Corentin Wallez4828d922018-07-18 13:45:46 +0200187 dawn::BufferView transformBufferView[2] = {
Austin Eng376f1c62017-05-30 20:03:44 -0400188 transformBuffer[0].CreateBufferViewBuilder()
189 .SetExtent(0, sizeof(glm::mat4))
190 .GetResult(),
191 transformBuffer[1].CreateBufferViewBuilder()
192 .SetExtent(0, sizeof(glm::mat4))
193 .GetResult(),
194 };
195
196 bindGroup[0] = device.CreateBindGroupBuilder()
197 .SetLayout(bgl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200198 .SetUsage(dawn::BindGroupUsage::Frozen)
Austin Eng376f1c62017-05-30 20:03:44 -0400199 .SetBufferViews(0, 1, &cameraBufferView)
200 .SetBufferViews(1, 1, &transformBufferView[0])
201 .GetResult();
202
203 bindGroup[1] = device.CreateBindGroupBuilder()
204 .SetLayout(bgl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200205 .SetUsage(dawn::BindGroupUsage::Frozen)
Austin Eng376f1c62017-05-30 20:03:44 -0400206 .SetBufferViews(0, 1, &cameraBufferView)
207 .SetBufferViews(1, 1, &transformBufferView[1])
208 .GetResult();
209
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700210 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400211
212 auto depthStencilState = device.CreateDepthStencilStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200213 .SetDepthCompareFunction(dawn::CompareFunction::Less)
Austin Eng10634392017-06-01 11:30:03 -0400214 .SetDepthWriteEnabled(true)
Austin Eng376f1c62017-05-30 20:03:44 -0400215 .GetResult();
216
Corentin Wallez66ff4472017-07-14 11:32:57 -0400217 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400218 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
Corentin Wallez4828d922018-07-18 13:45:46 +0200219 .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400220 .SetLayout(pl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200221 .SetStage(dawn::ShaderStage::Vertex, vsModule, "main")
222 .SetStage(dawn::ShaderStage::Fragment, fsModule, "main")
223 .SetIndexFormat(dawn::IndexFormat::Uint32)
Austin Eng376f1c62017-05-30 20:03:44 -0400224 .SetInputState(inputState)
225 .SetDepthStencilState(depthStencilState)
226 .GetResult();
227
228 auto planeStencilState = device.CreateDepthStencilStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200229 .SetDepthCompareFunction(dawn::CompareFunction::Less)
Austin Eng10634392017-06-01 11:30:03 -0400230 .SetDepthWriteEnabled(false)
Corentin Wallez4828d922018-07-18 13:45:46 +0200231 .SetStencilFunction(dawn::Face::Both, dawn::CompareFunction::Always, dawn::StencilOperation::Keep, dawn::StencilOperation::Keep, dawn::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400232 .GetResult();
233
Corentin Wallez66ff4472017-07-14 11:32:57 -0400234 planePipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400235 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
Corentin Wallez4828d922018-07-18 13:45:46 +0200236 .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400237 .SetLayout(pl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200238 .SetStage(dawn::ShaderStage::Vertex, vsModule, "main")
239 .SetStage(dawn::ShaderStage::Fragment, fsModule, "main")
Austin Eng376f1c62017-05-30 20:03:44 -0400240 .SetInputState(inputState)
241 .SetDepthStencilState(planeStencilState)
242 .GetResult();
243
244 auto reflectionStencilState = device.CreateDepthStencilStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200245 .SetDepthCompareFunction(dawn::CompareFunction::Less)
Austin Eng10634392017-06-01 11:30:03 -0400246 .SetDepthWriteEnabled(true)
Corentin Wallez4828d922018-07-18 13:45:46 +0200247 .SetStencilFunction(dawn::Face::Both, dawn::CompareFunction::Equal, dawn::StencilOperation::Keep, dawn::StencilOperation::Keep, dawn::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400248 .GetResult();
249
Corentin Wallez66ff4472017-07-14 11:32:57 -0400250 reflectionPipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400251 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
Corentin Wallez4828d922018-07-18 13:45:46 +0200252 .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400253 .SetLayout(pl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200254 .SetStage(dawn::ShaderStage::Vertex, vsModule, "main")
255 .SetStage(dawn::ShaderStage::Fragment, fsReflectionModule, "main")
Austin Eng376f1c62017-05-30 20:03:44 -0400256 .SetInputState(inputState)
257 .SetDepthStencilState(reflectionStencilState)
258 .GetResult();
259
260 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
261}
262
263struct {uint32_t a; float b;} s;
264void frame() {
265 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400266 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400267 if (s.b >= 1.0f) {s.b = 0.0f;}
268 static const uint32_t vertexBufferOffsets[1] = {0};
269
270 cameraData.view = glm::lookAt(
271 glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
272 glm::vec3(0.0f, 0.0f, 0.0f),
Corentin Wallez804fc742018-07-03 14:43:46 +0200273 glm::vec3(0.0f, -1.0f, 0.0f)
Austin Eng376f1c62017-05-30 20:03:44 -0400274 );
275
Stephen Whitee5ae3272018-02-04 11:07:02 -0500276 cameraBuffer.SetSubData(0, sizeof(CameraData), reinterpret_cast<uint8_t*>(&cameraData));
Austin Eng376f1c62017-05-30 20:03:44 -0400277
Corentin Wallez4828d922018-07-18 13:45:46 +0200278 dawn::Texture backbuffer;
279 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400280 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700281
Corentin Wallez4828d922018-07-18 13:45:46 +0200282 dawn::CommandBuffer commands = device.CreateCommandBufferBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400283 .BeginRenderPass(renderPass)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400284 .SetRenderPipeline(pipeline)
Austin Eng376f1c62017-05-30 20:03:44 -0400285 .SetBindGroup(0, bindGroup[0])
286 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
Corentin Wallez405dcd62017-09-19 13:38:05 -0400287 .SetIndexBuffer(indexBuffer, 0)
Austin Eng376f1c62017-05-30 20:03:44 -0400288 .DrawElements(36, 1, 0, 0)
289
Austin Eng10634392017-06-01 11:30:03 -0400290 .SetStencilReference(0x1)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400291 .SetRenderPipeline(planePipeline)
Austin Eng0c508892017-07-21 18:36:13 -0400292 .SetBindGroup(0, bindGroup[0])
Austin Eng376f1c62017-05-30 20:03:44 -0400293 .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
294 .DrawElements(6, 1, 0, 0)
295
Corentin Wallez66ff4472017-07-14 11:32:57 -0400296 .SetRenderPipeline(reflectionPipeline)
Austin Eng376f1c62017-05-30 20:03:44 -0400297 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
298 .SetBindGroup(0, bindGroup[1])
299 .DrawElements(36, 1, 0, 0)
300 .EndRenderPass()
301 .GetResult();
302
303 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700304 swapchain.Present(backbuffer);
305 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400306}
307
308int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400309 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400310 return 1;
311 }
312 init();
313
314 while (!ShouldQuit()) {
315 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400316 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400317 }
318
319 // TODO release stuff
320}