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