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