blob: 3d9b9ff53cc663a7e0748e502aa2d33d8c8d6e84 [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
116 queue = device.CreateQueueBuilder().GetResult();
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
168 nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
169 .SetBindGroupLayout(0, bgl)
170 .GetResult();
171
172 cameraBuffer = device.CreateBufferBuilder()
Austin Eng39c901d2017-06-12 17:33:44 -0400173 .SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform)
174 .SetInitialUsage(nxt::BufferUsageBit::TransferDst)
Austin Eng58c76b32017-06-02 14:31:53 -0400175 .SetSize(sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400176 .GetResult();
177
178 glm::mat4 transform(1.0);
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400179 transformBuffer[0] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400180
181 transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400182 transformBuffer[1] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
Austin Eng376f1c62017-05-30 20:03:44 -0400183
184 nxt::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder()
Austin Eng58c76b32017-06-02 14:31:53 -0400185 .SetExtent(0, sizeof(CameraData))
Austin Eng376f1c62017-05-30 20:03:44 -0400186 .GetResult();
187
188 nxt::BufferView transformBufferView[2] = {
189 transformBuffer[0].CreateBufferViewBuilder()
190 .SetExtent(0, sizeof(glm::mat4))
191 .GetResult(),
192 transformBuffer[1].CreateBufferViewBuilder()
193 .SetExtent(0, sizeof(glm::mat4))
194 .GetResult(),
195 };
196
197 bindGroup[0] = device.CreateBindGroupBuilder()
198 .SetLayout(bgl)
199 .SetUsage(nxt::BindGroupUsage::Frozen)
200 .SetBufferViews(0, 1, &cameraBufferView)
201 .SetBufferViews(1, 1, &transformBufferView[0])
202 .GetResult();
203
204 bindGroup[1] = device.CreateBindGroupBuilder()
205 .SetLayout(bgl)
206 .SetUsage(nxt::BindGroupUsage::Frozen)
207 .SetBufferViews(0, 1, &cameraBufferView)
208 .SetBufferViews(1, 1, &transformBufferView[1])
209 .GetResult();
210
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700211 depthStencilView = CreateDefaultDepthStencilView(device);
Austin Eng376f1c62017-05-30 20:03:44 -0400212
213 auto depthStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400214 .SetDepthCompareFunction(nxt::CompareFunction::Less)
215 .SetDepthWriteEnabled(true)
Austin Eng376f1c62017-05-30 20:03:44 -0400216 .GetResult();
217
Corentin Wallez66ff4472017-07-14 11:32:57 -0400218 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400219 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
220 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400221 .SetLayout(pl)
222 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
223 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
Corentin Wallez405dcd62017-09-19 13:38:05 -0400224 .SetIndexFormat(nxt::IndexFormat::Uint32)
Austin Eng376f1c62017-05-30 20:03:44 -0400225 .SetInputState(inputState)
226 .SetDepthStencilState(depthStencilState)
227 .GetResult();
228
229 auto planeStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400230 .SetDepthCompareFunction(nxt::CompareFunction::Less)
231 .SetDepthWriteEnabled(false)
232 .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Always, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400233 .GetResult();
234
Corentin Wallez66ff4472017-07-14 11:32:57 -0400235 planePipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400236 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
237 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400238 .SetLayout(pl)
239 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
240 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
241 .SetInputState(inputState)
242 .SetDepthStencilState(planeStencilState)
243 .GetResult();
244
245 auto reflectionStencilState = device.CreateDepthStencilStateBuilder()
Austin Eng10634392017-06-01 11:30:03 -0400246 .SetDepthCompareFunction(nxt::CompareFunction::Less)
247 .SetDepthWriteEnabled(true)
248 .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Equal, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
Austin Eng376f1c62017-05-30 20:03:44 -0400249 .GetResult();
250
Corentin Wallez66ff4472017-07-14 11:32:57 -0400251 reflectionPipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400252 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
253 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Austin Eng376f1c62017-05-30 20:03:44 -0400254 .SetLayout(pl)
255 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
256 .SetStage(nxt::ShaderStage::Fragment, fsReflectionModule, "main")
257 .SetInputState(inputState)
258 .SetDepthStencilState(reflectionStencilState)
259 .GetResult();
260
261 cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
262}
263
264struct {uint32_t a; float b;} s;
265void frame() {
266 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400267 s.b += 0.01f;
Austin Eng376f1c62017-05-30 20:03:44 -0400268 if (s.b >= 1.0f) {s.b = 0.0f;}
269 static const uint32_t vertexBufferOffsets[1] = {0};
270
271 cameraData.view = glm::lookAt(
272 glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
273 glm::vec3(0.0f, 0.0f, 0.0f),
274 glm::vec3(0.0f, 1.0f, 0.0f)
275 );
276
Austin Eng39c901d2017-06-12 17:33:44 -0400277 cameraBuffer.TransitionUsage(nxt::BufferUsageBit::TransferDst);
Stephen Whitee5ae3272018-02-04 11:07:02 -0500278 cameraBuffer.SetSubData(0, sizeof(CameraData), reinterpret_cast<uint8_t*>(&cameraData));
Austin Eng376f1c62017-05-30 20:03:44 -0400279
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700280 nxt::Texture backbuffer;
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400281 nxt::RenderPassInfo renderPass;
282 GetNextRenderPassInfo(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700283
Austin Eng376f1c62017-05-30 20:03:44 -0400284 nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400285 .BeginRenderPass(renderPass)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400286 .SetRenderPipeline(pipeline)
Austin Eng376f1c62017-05-30 20:03:44 -0400287 .TransitionBufferUsage(cameraBuffer, nxt::BufferUsageBit::Uniform)
288 .SetBindGroup(0, bindGroup[0])
289 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
Corentin Wallez405dcd62017-09-19 13:38:05 -0400290 .SetIndexBuffer(indexBuffer, 0)
Austin Eng376f1c62017-05-30 20:03:44 -0400291 .DrawElements(36, 1, 0, 0)
292
Austin Eng10634392017-06-01 11:30:03 -0400293 .SetStencilReference(0x1)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400294 .SetRenderPipeline(planePipeline)
Austin Eng0c508892017-07-21 18:36:13 -0400295 .SetBindGroup(0, bindGroup[0])
Austin Eng376f1c62017-05-30 20:03:44 -0400296 .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
297 .DrawElements(6, 1, 0, 0)
298
Corentin Wallez66ff4472017-07-14 11:32:57 -0400299 .SetRenderPipeline(reflectionPipeline)
Austin Eng376f1c62017-05-30 20:03:44 -0400300 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
301 .SetBindGroup(0, bindGroup[1])
302 .DrawElements(36, 1, 0, 0)
303 .EndRenderPass()
304 .GetResult();
305
306 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700307 backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
308 swapchain.Present(backbuffer);
309 DoFlush();
Austin Eng376f1c62017-05-30 20:03:44 -0400310}
311
312int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400313 if (!InitSample(argc, argv)) {
Austin Eng376f1c62017-05-30 20:03:44 -0400314 return 1;
315 }
316 init();
317
318 while (!ShouldQuit()) {
319 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400320 utils::USleep(16000);
Austin Eng376f1c62017-05-30 20:03:44 -0400321 }
322
323 // TODO release stuff
324}