Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 1 | // 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 Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 17 | #include "utils/NXTHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame^] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 19 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 20 | #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 | |
| 25 | nxt::Device device; |
| 26 | |
| 27 | nxt::Buffer indexBuffer; |
| 28 | nxt::Buffer vertexBuffer; |
| 29 | nxt::Buffer planeBuffer; |
| 30 | nxt::Buffer cameraBuffer; |
| 31 | nxt::Buffer transformBuffer[2]; |
| 32 | |
| 33 | nxt::BindGroup cameraBindGroup; |
| 34 | nxt::BindGroup bindGroup[2]; |
| 35 | nxt::BindGroup cubeTransformBindGroup[2]; |
| 36 | |
| 37 | nxt::Queue queue; |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 38 | nxt::RenderPipeline pipeline; |
| 39 | nxt::RenderPipeline planePipeline; |
| 40 | nxt::RenderPipeline reflectionPipeline; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 41 | nxt::RenderPass renderpass; |
| 42 | nxt::Framebuffer framebuffer; |
| 43 | |
| 44 | void 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 Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 64 | indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 65 | |
| 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 Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 97 | vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 98 | |
| 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 Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 105 | planeBuffer = utils::CreateFrozenBufferFromData(device, planeData, sizeof(planeData), nxt::BufferUsageBit::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 108 | struct CameraData { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 109 | glm::mat4 view; |
| 110 | glm::mat4 proj; |
| 111 | } cameraData; |
| 112 | |
| 113 | void init() { |
| 114 | device = CreateCppNXTDevice(); |
| 115 | |
| 116 | queue = device.CreateQueueBuilder().GetResult(); |
| 117 | |
| 118 | initBuffers(); |
| 119 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 120 | nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 121 | #version 450 |
| 122 | layout(set = 0, binding = 0) uniform cameraData { |
| 123 | mat4 view; |
| 124 | mat4 proj; |
| 125 | } camera; |
| 126 | layout(set = 0, binding = 1) uniform modelData { |
| 127 | mat4 modelMatrix; |
| 128 | }; |
| 129 | layout(location = 0) in vec3 pos; |
| 130 | layout(location = 1) in vec3 col; |
| 131 | layout(location = 2) out vec3 f_col; |
| 132 | void main() { |
| 133 | f_col = col; |
| 134 | gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0); |
| 135 | })" |
| 136 | ); |
| 137 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 138 | nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 139 | #version 450 |
| 140 | layout(location = 2) in vec3 f_col; |
| 141 | out vec4 fragColor; |
| 142 | void main() { |
| 143 | fragColor = vec4(f_col, 1.0); |
| 144 | })" |
| 145 | ); |
| 146 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 147 | nxt::ShaderModule fsReflectionModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 148 | #version 450 |
| 149 | layout(location = 2) in vec3 f_col; |
| 150 | out vec4 fragColor; |
| 151 | void main() { |
| 152 | fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0); |
| 153 | })" |
| 154 | ); |
| 155 | |
| 156 | auto inputState = device.CreateInputStateBuilder() |
| 157 | .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32, 0) |
| 158 | .SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32B32, 3 * sizeof(float)) |
| 159 | .SetInput(0, 6 * sizeof(float), nxt::InputStepMode::Vertex) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 160 | .GetResult(); |
| 161 | |
| 162 | nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder() |
| 163 | .SetBindingsType(nxt::ShaderStageBit::Vertex, nxt::BindingType::UniformBuffer, 0, 2) |
| 164 | .GetResult(); |
| 165 | |
| 166 | nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder() |
| 167 | .SetBindGroupLayout(0, bgl) |
| 168 | .GetResult(); |
| 169 | |
| 170 | cameraBuffer = device.CreateBufferBuilder() |
Austin Eng | 39c901d | 2017-06-12 17:33:44 -0400 | [diff] [blame] | 171 | .SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform) |
| 172 | .SetInitialUsage(nxt::BufferUsageBit::TransferDst) |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 173 | .SetSize(sizeof(CameraData)) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 174 | .GetResult(); |
| 175 | |
| 176 | glm::mat4 transform(1.0); |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 177 | transformBuffer[0] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 178 | |
| 179 | transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f)); |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 180 | transformBuffer[1] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 181 | |
| 182 | nxt::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder() |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 183 | .SetExtent(0, sizeof(CameraData)) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 184 | .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 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 209 | utils::CreateDefaultRenderPass(device, &renderpass, &framebuffer); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 210 | |
| 211 | auto depthStencilState = device.CreateDepthStencilStateBuilder() |
Austin Eng | 1063439 | 2017-06-01 11:30:03 -0400 | [diff] [blame] | 212 | .SetDepthCompareFunction(nxt::CompareFunction::Less) |
| 213 | .SetDepthWriteEnabled(true) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 214 | .GetResult(); |
| 215 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 216 | pipeline = device.CreateRenderPipelineBuilder() |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 217 | .SetSubpass(renderpass, 0) |
| 218 | .SetLayout(pl) |
| 219 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 220 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 221 | .SetInputState(inputState) |
| 222 | .SetDepthStencilState(depthStencilState) |
| 223 | .GetResult(); |
| 224 | |
| 225 | auto planeStencilState = device.CreateDepthStencilStateBuilder() |
Austin Eng | 1063439 | 2017-06-01 11:30:03 -0400 | [diff] [blame] | 226 | .SetDepthCompareFunction(nxt::CompareFunction::Less) |
| 227 | .SetDepthWriteEnabled(false) |
| 228 | .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Always, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 229 | .GetResult(); |
| 230 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 231 | planePipeline = device.CreateRenderPipelineBuilder() |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 232 | .SetSubpass(renderpass, 0) |
| 233 | .SetLayout(pl) |
| 234 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 235 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 236 | .SetInputState(inputState) |
| 237 | .SetDepthStencilState(planeStencilState) |
| 238 | .GetResult(); |
| 239 | |
| 240 | auto reflectionStencilState = device.CreateDepthStencilStateBuilder() |
Austin Eng | 1063439 | 2017-06-01 11:30:03 -0400 | [diff] [blame] | 241 | .SetDepthCompareFunction(nxt::CompareFunction::Less) |
| 242 | .SetDepthWriteEnabled(true) |
| 243 | .SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Equal, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 244 | .GetResult(); |
| 245 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 246 | reflectionPipeline = device.CreateRenderPipelineBuilder() |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 247 | .SetSubpass(renderpass, 0) |
| 248 | .SetLayout(pl) |
| 249 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 250 | .SetStage(nxt::ShaderStage::Fragment, fsReflectionModule, "main") |
| 251 | .SetInputState(inputState) |
| 252 | .SetDepthStencilState(reflectionStencilState) |
| 253 | .GetResult(); |
| 254 | |
| 255 | cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f); |
| 256 | } |
| 257 | |
| 258 | struct {uint32_t a; float b;} s; |
| 259 | void frame() { |
| 260 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 261 | s.b += 0.01f; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 262 | if (s.b >= 1.0f) {s.b = 0.0f;} |
| 263 | static const uint32_t vertexBufferOffsets[1] = {0}; |
| 264 | |
| 265 | cameraData.view = glm::lookAt( |
| 266 | glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))), |
| 267 | glm::vec3(0.0f, 0.0f, 0.0f), |
| 268 | glm::vec3(0.0f, 1.0f, 0.0f) |
| 269 | ); |
| 270 | |
Austin Eng | 39c901d | 2017-06-12 17:33:44 -0400 | [diff] [blame] | 271 | cameraBuffer.TransitionUsage(nxt::BufferUsageBit::TransferDst); |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 272 | cameraBuffer.SetSubData(0, sizeof(CameraData) / sizeof(uint32_t), reinterpret_cast<uint32_t*>(&cameraData)); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 273 | |
| 274 | nxt::CommandBuffer commands = device.CreateCommandBufferBuilder() |
| 275 | .BeginRenderPass(renderpass, framebuffer) |
Kai Ninomiya | fa37f22 | 2017-06-29 23:53:08 -0700 | [diff] [blame] | 276 | .BeginRenderSubpass() |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 277 | .SetRenderPipeline(pipeline) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 278 | .TransitionBufferUsage(cameraBuffer, nxt::BufferUsageBit::Uniform) |
| 279 | .SetBindGroup(0, bindGroup[0]) |
| 280 | .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets) |
| 281 | .SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32) |
| 282 | .DrawElements(36, 1, 0, 0) |
| 283 | |
Austin Eng | 1063439 | 2017-06-01 11:30:03 -0400 | [diff] [blame] | 284 | .SetStencilReference(0x1) |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 285 | .SetRenderPipeline(planePipeline) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 286 | .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets) |
| 287 | .DrawElements(6, 1, 0, 0) |
| 288 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 289 | .SetRenderPipeline(reflectionPipeline) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 290 | .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets) |
| 291 | .SetBindGroup(0, bindGroup[1]) |
| 292 | .DrawElements(36, 1, 0, 0) |
Kai Ninomiya | fa37f22 | 2017-06-29 23:53:08 -0700 | [diff] [blame] | 293 | .EndRenderSubpass() |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 294 | .EndRenderPass() |
| 295 | .GetResult(); |
| 296 | |
| 297 | queue.Submit(1, &commands); |
| 298 | DoSwapBuffers(); |
| 299 | } |
| 300 | |
| 301 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 302 | if (!InitSample(argc, argv)) { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 303 | return 1; |
| 304 | } |
| 305 | init(); |
| 306 | |
| 307 | while (!ShouldQuit()) { |
| 308 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame^] | 309 | utils::USleep(16000); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // TODO release stuff |
| 313 | } |