blob: 3423a9a71586577a81d963e93c9149bf7c673bb4 [file] [log] [blame]
Austin Eng376f1c62017-05-30 20:03:44 -04001// Copyright 2017 The NXT Authors
2//
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 Wallez5ee7afd2017-06-19 13:09:41 -040017#include "utils/NXTHelpers.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>
21#include <glm/glm/glm.hpp>
22#include <glm/glm/gtc/matrix_transform.hpp>
23#include <glm/glm/gtc/type_ptr.hpp>
24
25nxt::Device device;
26
27nxt::Buffer indexBuffer;
28nxt::Buffer vertexBuffer;
29nxt::Buffer planeBuffer;
30nxt::Buffer cameraBuffer;
31nxt::Buffer transformBuffer[2];
32
33nxt::BindGroup cameraBindGroup;
34nxt::BindGroup bindGroup[2];
35nxt::BindGroup cubeTransformBindGroup[2];
36
37nxt::Queue queue;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070038nxt::SwapChain swapchain;
39nxt::TextureView depthStencilView;
Corentin Wallez66ff4472017-07-14 11:32:57 -040040nxt::RenderPipeline pipeline;
41nxt::RenderPipeline planePipeline;
42nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -040064 indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -040097 vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -0400105 planeBuffer = utils::CreateFrozenBufferFromData(device, planeData, sizeof(planeData), nxt::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() {
114 device = CreateCppNXTDevice();
115
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(),
119 nxt::TextureUsageBit::OutputAttachment, 640, 480);
Austin Eng376f1c62017-05-30 20:03:44 -0400120
121 initBuffers();
122
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400123 nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -0400141 nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::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 Wallezb6fb5f32017-08-29 13:37:45 -0400149 nxt::ShaderModule fsReflectionModule =
150 utils::CreateShaderModule(device, nxt::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()
159 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32, 0)
160 .SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32B32, 3 * sizeof(float))
161 .SetInput(0, 6 * sizeof(float), nxt::InputStepMode::Vertex)
Austin Eng376f1c62017-05-30 20:03:44 -0400162 .GetResult();
163
164 nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
165 .SetBindingsType(nxt::ShaderStageBit::Vertex, nxt::BindingType::UniformBuffer, 0, 2)
166 .GetResult();
167
Kai Ninomiyaf53f98b2018-06-27 16:21:39 -0700168 nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Austin Eng376f1c62017-05-30 20:03:44 -0400169
170 cameraBuffer = device.CreateBufferBuilder()
Austin Eng39c901d2017-06-12 17:33:44 -0400171 .SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform)
172 .SetInitialUsage(nxt::BufferUsageBit::TransferDst)
Austin Eng58c76b32017-06-02 14:31:53 -0400173 .SetSize(sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400174 .GetResult();
175
176 glm::mat4 transform(1.0);
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400177 transformBuffer[0] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400178
179 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400180 transformBuffer[1] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400181
182 nxt::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder()
Austin Eng58c76b32017-06-02 14:31:53 -0400183 .SetExtent(0, sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400184 .GetResult();
185
186 nxt::BufferView transformBufferView[2] = {
187 transformBuffer[0].CreateBufferViewBuilder()
188 .SetExtent(0, sizeof(glm::mat4))
189 .GetResult(),
190 transformBuffer[1].CreateBufferViewBuilder()
191 .SetExtent(0, sizeof(glm::mat4))
192 .GetResult(),
193 };
194
195 bindGroup[0] = device.CreateBindGroupBuilder()
196 .SetLayout(bgl)
197 .SetUsage(nxt::BindGroupUsage::Frozen)
198 .SetBufferViews(0, 1, &cameraBufferView)
199 .SetBufferViews(1, 1, &transformBufferView[0])
200 .GetResult();
201
202 bindGroup[1] = device.CreateBindGroupBuilder()
203 .SetLayout(bgl)
204 .SetUsage(nxt::BindGroupUsage::Frozen)
205 .SetBufferViews(0, 1, &cameraBufferView)
206 .SetBufferViews(1, 1, &transformBufferView[1])
207 .GetResult();
208
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700209 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400210
211 auto depthStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400212 .SetDepthCompareFunction(nxt::CompareFunction::Less)
213 .SetDepthWriteEnabled(true)
Austin Eng376f1c62017-05-30 20:03:44 -0400214 .GetResult();
215
Corentin Wallez66ff4472017-07-14 11:32:57 -0400216 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400217 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
218 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400219 .SetLayout(pl)
220 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
221 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
Corentin Wallez405dcd62017-09-19 13:38:05 -0400222 .SetIndexFormat(nxt::IndexFormat::Uint32)
Austin Eng376f1c62017-05-30 20:03:44 -0400223 .SetInputState(inputState)
224 .SetDepthStencilState(depthStencilState)
225 .GetResult();
226
227 auto planeStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400228 .SetDepthCompareFunction(nxt::CompareFunction::Less)
229 .SetDepthWriteEnabled(false)
230 .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Always, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400231 .GetResult();
232
Corentin Wallez66ff4472017-07-14 11:32:57 -0400233 planePipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400234 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
235 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400236 .SetLayout(pl)
237 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
238 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
239 .SetInputState(inputState)
240 .SetDepthStencilState(planeStencilState)
241 .GetResult();
242
243 auto reflectionStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400244 .SetDepthCompareFunction(nxt::CompareFunction::Less)
245 .SetDepthWriteEnabled(true)
246 .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Equal, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400247 .GetResult();
248
Corentin Wallez66ff4472017-07-14 11:32:57 -0400249 reflectionPipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400250 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
251 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400252 .SetLayout(pl)
253 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
254 .SetStage(nxt::ShaderStage::Fragment, fsReflectionModule, "main")
255 .SetInputState(inputState)
256 .SetDepthStencilState(reflectionStencilState)
257 .GetResult();
258
259 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
260}
261
262struct {uint32_t a; float b;} s;
263void frame() {
264 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400265 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400266 if (s.b >= 1.0f) {s.b = 0.0f;}
267 static const uint32_t vertexBufferOffsets[1] = {0};
268
269 cameraData.view = glm::lookAt(
270 glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
271 glm::vec3(0.0f, 0.0f, 0.0f),
272 glm::vec3(0.0f, 1.0f, 0.0f)
273 );
274
Austin Eng39c901d2017-06-12 17:33:44 -0400275 cameraBuffer.TransitionUsage(nxt::BufferUsageBit::TransferDst);
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
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700278 nxt::Texture backbuffer;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400279 nxt::RenderPassDescriptor renderPass;
280 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700281
Austin Eng376f1c62017-05-30 20:03:44 -0400282 nxt::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 .TransitionBufferUsage(cameraBuffer, nxt::BufferUsageBit::Uniform)
286 .SetBindGroup(0, bindGroup[0])
287 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
Corentin Wallez405dcd62017-09-19 13:38:05 -0400288 .SetIndexBuffer(indexBuffer, 0)
Austin Eng376f1c62017-05-30 20:03:44 -0400289 .DrawElements(36, 1, 0, 0)
290
Austin Eng10634392017-06-01 11:30:03 -0400291 .SetStencilReference(0x1)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400292 .SetRenderPipeline(planePipeline)
Austin Eng0c508892017-07-21 18:36:13 -0400293 .SetBindGroup(0, bindGroup[0])
Austin Eng376f1c62017-05-30 20:03:44 -0400294 .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
295 .DrawElements(6, 1, 0, 0)
296
Corentin Wallez66ff4472017-07-14 11:32:57 -0400297 .SetRenderPipeline(reflectionPipeline)
Austin Eng376f1c62017-05-30 20:03:44 -0400298 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
299 .SetBindGroup(0, bindGroup[1])
300 .DrawElements(36, 1, 0, 0)
301 .EndRenderPass()
302 .GetResult();
303
304 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700305 backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
306 swapchain.Present(backbuffer);
307 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400308}
309
310int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400311 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400312 return 1;
313 }
314 init();
315
316 while (!ShouldQuit()) {
317 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400318 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400319 }
320
321 // TODO release stuff
322}